Skip to content

Commit

Permalink
fix: delete docker folders. add DatabaseFactory.ts template.
Browse files Browse the repository at this point in the history
  • Loading branch information
jooherrera committed Jan 25, 2024
1 parent 0e243ff commit 29f9117
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 56 deletions.
14 changes: 7 additions & 7 deletions bin/createProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,6 @@ const createProject = async() =>
{
await copyDomainFiles(answers, tmpDir);
}
},
{
title: 'Clean Domain Files',
task: async() =>
{
await cleanDomains(answers, tmpDir);
}
},
{
title: 'Copy Index Files',
Expand All @@ -113,6 +106,13 @@ const createProject = async() =>
await copyRootFiles(answers, tmpDir);
}
},
{
title: 'Clean Domain Files',
task: async() =>
{
await cleanDomains(answers, tmpDir);
}
},
{
title: 'Set .env file',
task: async() =>
Expand Down
55 changes: 8 additions & 47 deletions lib/createProject/cleanDomains/cleanOrm.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,31 @@
import { join, resolve } from 'path';
import { rm } from 'node:fs/promises';
import pkg from 'lodash';
import { writeFile } from 'fs/promises';
const { last } = pkg;
import g from 'glob';
import { promisify } from 'util';

const cleanFilesInfo = async(vars, httpMapper) =>
{
const mapperFilter = httpMapper.filter(item => vars.orm === item);
const ormChosen = last(mapperFilter);
const rootDestinyPath = join(resolve(), vars.projectName);

const ormFactoryFile = `import MainConfig from '../../../Config/MainConfig';
import Create${ormChosen}Connection from '../Database/Create${ormChosen}Connection';
import ICreateConnection from '../Database/ICreateConnection';
type DbValueProp = typeof Create${ormChosen}Connection;
class DatabaseFactory
{
private readonly dbDefault: string;
private config = MainConfig.getInstance().getConfig();
constructor(dbDefault?: string)
{
this.dbDefault = dbDefault ?? this.config.dbConfig.default;
}
create(_db?: string): ICreateConnection
{
const db = _db ?? this.dbDefault;
const { dbConfig } = this.config;
const config = dbConfig[db];
const strategy = new Map<string, DbValueProp>();
strategy.set('${ormChosen}', Create${ormChosen}Connection);
return new (strategy.get(db))(config);
}
}
export default DatabaseFactory;
`;

await writeFile(`${rootDestinyPath}/src/Main/Infrastructure/Factories/DatabaseFactory.ts`, ormFactoryFile, { flag: 'w' });
};
import {remove} from 'fs-extra'

const cleanOrm = async(vars, ormMapper) =>
{
const DockerORMFolder = {
Mongoose: "mongo",
MikroORM: "postgres"
}

const glob = promisify(g);
const mapperFilter = ormMapper.filter(item => vars.orm !== item);
const rootDestinyPath = join(resolve(), vars.projectName);

for (const orm of mapperFilter)
{
remove(`${rootDestinyPath}/docker/${DockerORMFolder[orm]}`)

const ormFiles = await glob(`${rootDestinyPath}/src/**/*${orm}*.ts`);

for (const file of ormFiles)
{
await rm(file, { force: true });
}
}

await cleanFilesInfo(vars, ormMapper);
};

export default cleanOrm;
4 changes: 2 additions & 2 deletions lib/createProject/copyIndexFiles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import copyTemplate from '../utils/cp.js';
import { resolve, join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { resolve, join } from 'path';

const copyIndexFiles = async(vars, rootPath) =>
{
Expand All @@ -15,6 +14,7 @@ const copyIndexFiles = async(vars, rootPath) =>
await copyTemplate(vars, join(rootPath, '/node-experience/src/seed.ts'), rootDestinyPath);
await copyTemplate(vars, join(rootPath, 'templates/indexFiles/Shared/DI/container.ts'), `${rootDestinyPath}/Shared/DI/`);
await copyTemplate(vars, join(rootPath, 'templates/indexFiles/Config/validateEnv.ts'), `${rootDestinyPath}/Config/`);
await copyTemplate(vars, join(rootPath, 'templates/indexFiles/Main/Infrastructure/Factories/DatabaseFactory.ts'), `${rootDestinyPath}/Main/Infrastructure/Factories/`);
};

export default copyIndexFiles;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import MainConfig from '../../../Config/MainConfig';
import ICreateConnection from '../Database/ICreateConnection';
{{#ifEquals orm "Mongoose" }}
import CreateMongooseConnection from '../Database/CreateMongooseConnection';
type DbValueProp = typeof CreateMongooseConnection;
{{/ifEquals}}
{{#ifEquals orm "MikroORM" }}
import CreateMikroORMConnection from '../Database/CreateMikroORMConnection';
type DbValueProp = typeof CreateMikroORMConnection;
{{/ifEquals}}

class DatabaseFactory
{
#config = MainConfig.getInstance().getConfig().dbConfig;

create(_db?: string): ICreateConnection
{
const dbDefault = this.#config.default;
const db = _db ?? dbDefault;

const strategy = new Map<string, DbValueProp>();

{{#ifEquals orm "Mongoose" }}
strategy.set('Mongoose', CreateMongooseConnection);
{{/ifEquals}}
{{#ifEquals orm "MikroORM" }}
strategy.set('MikroORM', CreateMikroORMConnection);
{{/ifEquals}}
return new (strategy.get(db))(this.#config[db]);
}
}

export default DatabaseFactory;

0 comments on commit 29f9117

Please sign in to comment.