diff --git a/package.json b/package.json index 4ef6048..c002755 100644 --- a/package.json +++ b/package.json @@ -15,13 +15,13 @@ "lint": "eslint", "lint:fix": "eslint --fix", "prettify": "prettier --write '**/*.ts*'", - "db:migrate:run:local": "NODE_ENV=development ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:run -d ./src/dataSource.ts", - "db:migrate:run:staging": "NODE_ENV=staging ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:run -d ./src/dataSource.ts", - "db:migrate:revert:local": "NODE_ENV=development ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:revert -d ./src/dataSource.ts", - "db:migrate:run:test": "NODE_ENV=test ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:run -d ./src/dataSource.ts", - "db:migrate:revert:test": "NODE_ENV=test ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:revert -d ./src/dataSource.ts", + "db:migrate:run:local": "NODE_ENV=development ts-node -r tsconfig-paths/register ./src/scripts/runMigrations.ts", + "db:migrate:run:staging": "NODE_ENV=staging ts-node -r tsconfig-paths/register ./src/scripts/runMigrations.ts", + "db:migrate:revert:local": "NODE_ENV=development ts-node -r tsconfig-paths/register ./src/scripts/revertMigrations.ts", + "db:migrate:run:test": "NODE_ENV=test ts-node -r tsconfig-paths/register ./src/scripts/runMigrations.ts", + "db:migrate:revert:test": "NODE_ENV=test ts-node -r tsconfig-paths/register ./src/scripts/revertMigrations.ts", + "db:migrate:run:production": "NODE_ENV=production ts-node -r tsconfig-paths/register ./src/scripts/runMigrations.ts", "start:server:staging": "npm run db:migrate:run:staging && cd dist && pm2-runtime src/index.js", - "db:migrate:run:production": "NODE_ENV=production ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:run -d ./src/dataSource.ts", "start:server:production": "npm run db:migrate:run:production && cd dist && pm2-runtime src/index.js" }, "keywords": [], diff --git a/src/dataSource.ts b/src/dataSource.ts index 03d9126..1ab393e 100644 --- a/src/dataSource.ts +++ b/src/dataSource.ts @@ -20,3 +20,9 @@ export const AppDataSource = new DataSource({ migrations: ['./migrations/*.ts'], subscribers: [], }); + +export async function initializeDataSource() { + if (!AppDataSource.isInitialized) { + await AppDataSource.initialize(); + } +} diff --git a/src/scripts/revertMigrations.ts b/src/scripts/revertMigrations.ts new file mode 100644 index 0000000..93e0304 --- /dev/null +++ b/src/scripts/revertMigrations.ts @@ -0,0 +1,13 @@ +import { initializeDataSource, AppDataSource } from '../dataSource'; + +(async () => { + try { + await initializeDataSource(); + await AppDataSource.undoLastMigration(); + console.log('Last migration has been reverted successfully'); + process.exit(0); + } catch (error) { + console.error('Error reverting migrations:', error); + process.exit(1); + } +})(); diff --git a/src/scripts/runMigrations.ts b/src/scripts/runMigrations.ts new file mode 100644 index 0000000..fd9d1f4 --- /dev/null +++ b/src/scripts/runMigrations.ts @@ -0,0 +1,13 @@ +import { initializeDataSource, AppDataSource } from '../dataSource'; + +(async () => { + try { + await initializeDataSource(); + await AppDataSource.runMigrations(); + console.log('Migrations have been executed successfully'); + process.exit(0); + } catch (error) { + console.error('Error running migrations:', error); + process.exit(1); + } +})();