-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: delete docker folders. add DatabaseFactory.ts template.
- Loading branch information
1 parent
0e243ff
commit 29f9117
Showing
4 changed files
with
50 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
templates/indexFiles/Main/Infrastructure/Factories/DatabaseFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |