generated from thothom/base-project-plugin
-
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.
- Loading branch information
1 parent
24a06a3
commit ba3fbee
Showing
24 changed files
with
519 additions
and
92 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
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,117 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
|
||
import { existsSync, writeFileSync } from "fs"; | ||
|
||
import { getConfigFile } from "../utils/get-config-file"; | ||
import { getRootPath } from "../utils/get-root-path"; | ||
import { Logger } from "../../utils/logger"; | ||
|
||
import { getMigrationsPath } from "../utils/get-migrations-path"; | ||
import { MigrationGenerator } from "../../lib/migration-generator"; | ||
import { Plugin } from "./types/plugin"; | ||
import { getTemplate } from "../utils/get-template"; | ||
import { getSymbVersion } from "../utils/get-symb-version"; | ||
|
||
export const genMigrations = () => { | ||
const migrationsPath = getMigrationsPath(); | ||
|
||
const { connectionConfig, plugin } = getConfigFile(); | ||
|
||
if (!existsSync(getRootPath(`node_modules/${plugin}`))) { | ||
Logger.error(`Plugin not found: ${plugin}`); | ||
|
||
process.exit(1); | ||
} | ||
|
||
const { Connection } = require(plugin) as Plugin; | ||
|
||
const connection = new Connection(connectionConfig); | ||
|
||
const template = getTemplate("migration:generate"); | ||
const symbVersion = getSymbVersion(); | ||
|
||
connectionConfig.entities.forEach(entity => { | ||
const entityMetadata = connection.entityManager.getEntityMetadata(entity); | ||
|
||
const migration = new MigrationGenerator(); | ||
|
||
/** | ||
* Entity | ||
*/ | ||
|
||
migration.createEntity({ | ||
databaseName: entityMetadata.databaseName, | ||
extras: entityMetadata.extras, | ||
}); | ||
|
||
/** | ||
* Enums | ||
*/ | ||
|
||
entityMetadata.columns | ||
.filter(col => Boolean(col.enumValues)) | ||
.forEach(col => { | ||
migration.createEnum({ | ||
entityDatabaseName: entityMetadata.databaseName, | ||
columnDatabaseName: col.databaseName, | ||
enumName: col.enumName!, | ||
enumValues: col.enumValues!, | ||
extras: col.extras, | ||
}); | ||
}); | ||
|
||
/** | ||
* Columns | ||
*/ | ||
|
||
entityMetadata.columns.forEach(col => { | ||
migration.createColumn({ | ||
entityDatabaseName: entityMetadata.databaseName, | ||
columnDatabaseName: col.databaseName, | ||
enumName: col.enumName, | ||
primary: col.primary, | ||
comment: col.comment, | ||
type: col.databaseType, | ||
extras: col.extras, | ||
}); | ||
}); | ||
|
||
/** | ||
* Indexes | ||
*/ | ||
|
||
entityMetadata.indexes?.forEach(idx => { | ||
const columnsDatabaseNames = connection.entityManager.convertColumnsNames( | ||
{ | ||
entity, | ||
columnsNames: idx.columns.map(col => col.name), | ||
}, | ||
); | ||
|
||
const extras = Object.fromEntries( | ||
idx.columns.map(col => [col.name, col.extras]), | ||
); | ||
|
||
migration.createIndex({ | ||
entityDatabaseName: entityMetadata.databaseName, | ||
indexDatabaseName: idx.databaseName, | ||
columnsDatabaseNames, | ||
extras, | ||
}); | ||
}); | ||
|
||
const code = `${Date.now()}-${entityMetadata.name}`; | ||
|
||
const { up, down } = migration.format(); | ||
|
||
const formattedTemplate = template | ||
.replace(/\$\{CODE\}/g, code) | ||
.replace(/\$\{DESCRIPTION\}/g, `${entityMetadata.name} Migration`) | ||
.replace(/\$\{PLUGIN\}/g, plugin) | ||
.replace(/\$\{SYMB_VERSION\}/g, symbVersion) | ||
.replace(/\$\{MIGRATION_UP_LOGIC\}/g, up) | ||
.replace(/\$\{MIGRATION_DOWN_LOGIC\}/g, down); | ||
|
||
writeFileSync(`${migrationsPath}/${code}.ts`, formattedTemplate); | ||
}); | ||
}; |
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
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
|
||
import { BaseConnectionOptions, BaseConnection } from "@techmmunity/symbiosis"; | ||
import { BaseQueryRunner, BaseSyncManager } from "../../.."; | ||
|
||
export interface Plugin { | ||
Connection: { | ||
new (connectionConfig: BaseConnectionOptions): BaseConnection; | ||
}; | ||
QueryRunner: { | ||
new (connection: any): BaseQueryRunner; | ||
}; | ||
SyncManager: { | ||
new (connection: any): BaseSyncManager; | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Migration ${CODE} | ||
* | ||
* ${DESCRIPTION} | ||
* | ||
* @plugin ${PLUGIN} | ||
* @symbVersion ${SYMB_VERSION} | ||
*/ | ||
import { BaseMigration, QueryRunner } from "@techmmunity/symbiosis-cli"; | ||
|
||
export class Migration implements BaseMigration { | ||
public code = "${CODE}"; | ||
public description = "${DESCRIPTION}"; | ||
|
||
public async up(queryRunner: QueryRunner) { | ||
${MIGRATION_UP_LOGIC} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner) { | ||
${MIGRATION_DOWN_LOGIC} | ||
} | ||
} |
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
Oops, something went wrong.