Skip to content

Commit

Permalink
Update ormConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Mar 6, 2023
1 parent 6d10b45 commit e3df2e2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
21 changes: 15 additions & 6 deletions src/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -32,6 +41,6 @@ const config: ConnectionOptions = {
rejectUnauthorized: false,
},
},
};
} as DataSourceOptions);

export = config;
export default connectionSource;

0 comments on commit e3df2e2

Please sign in to comment.