From e3df2e26e958bb9cf78b38f79cefbe36dac066f3 Mon Sep 17 00:00:00 2001 From: danielailie Date: Mon, 13 Feb 2023 11:24:18 +0200 Subject: [PATCH] Update ormConfig --- package.json | 4 ++-- src/ormconfig.ts | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 0a642fd4f..a1a7bf6ea 100644 --- a/package.json +++ b/package.json @@ -21,8 +21,8 @@ "clear_jest": "jest --clearCache", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", "test:e2e": "NODE_ENV=test-e2e jest --config ./test/jest-e2e.json --forceExit --maxWorkers=4", - "typeorm": "ts-node --require ts-node/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts", - "generate-migration": "npm run typeorm -- migration:generate -n", + "typeorm": "ts-node --require ts-node/register ./node_modules/typeorm/cli.js -d src/ormconfig.ts", + "generate-migration": "npm run typeorm -- migration:generate src/db/migrations/$npm_config_name", "run-migrations": "npm run typeorm migration:run", "revert-migrations": "npm run typeorm migration:revert" }, diff --git a/src/ormconfig.ts b/src/ormconfig.ts index c7cb7d719..ce15728a1 100644 --- a/src/ormconfig.ts +++ b/src/ormconfig.ts @@ -1,5 +1,9 @@ -import { ConnectionOptions } from 'typeorm'; +import { DataSource, DataSourceOptions } from 'typeorm'; import { MysqlConnectionCredentialsOptions } from 'typeorm/driver/mysql/MysqlConnectionCredentialsOptions'; +import * as dotenv from 'dotenv'; +import * as dotenvExpand from 'dotenv-expand'; + +dotenvExpand.expand(dotenv.config()); const db: MysqlConnectionCredentialsOptions = { host: process.env.DB_HOST, @@ -12,11 +16,16 @@ const db: MysqlConnectionCredentialsOptions = { const dbSlaves = process.env.DB_SLAVES ? JSON.parse(process.env.DB_SLAVES) : [db]; - -const config: ConnectionOptions = { +const connectionSource = new DataSource({ + migrationsTableName: 'migrations', type: 'mysql', + host: process.env.DB_HOST, + port: parseInt(process.env.DB_PORT), + username: process.env.DB_USERNAME, + password: process.env.DB_PASSWORD, + database: process.env.DB_NAME, + logging: process.env.DB_LOGGING, synchronize: false, - dropSchema: false, replication: { master: db, slaves: dbSlaves, @@ -32,6 +41,6 @@ const config: ConnectionOptions = { rejectUnauthorized: false, }, }, -}; +} as DataSourceOptions); -export = config; +export default connectionSource;