Skip to content

Commit

Permalink
update ormConfig and add datasourceConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Mar 6, 2023
1 parent e3df2e2 commit 0911505
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"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 -d src/ormconfig.ts",
"typeorm": "ts-node --require ts-node/register ./node_modules/typeorm/cli.js -d src/datasource.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
46 changes: 46 additions & 0 deletions src/datasource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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,
port: parseInt(process.env.DB_PORT),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
};

const dbSlaves = process.env.DB_SLAVES
? JSON.parse(process.env.DB_SLAVES)
: [db];
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,
replication: {
master: db,
slaves: dbSlaves,
},
entities:
process.env.NODE_ENV === 'test-e2e'
? ['src/db/**/*.entity.js']
: ['dist/db/**/*.entity.js'],
migrations: ['dist/db/migrations/*.js'],
extra: {
connectionLimit: process.env.DB_CONNECTION_LIMIT,
ssl: {
rejectUnauthorized: false,
},
},
} as DataSourceOptions);

export default connectionSource;
21 changes: 6 additions & 15 deletions src/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { DataSource, DataSourceOptions } from 'typeorm';
import { ConnectionOptions } 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 @@ -16,16 +12,11 @@ const db: MysqlConnectionCredentialsOptions = {
const dbSlaves = process.env.DB_SLAVES
? JSON.parse(process.env.DB_SLAVES)
: [db];
const connectionSource = new DataSource({
migrationsTableName: 'migrations',

const config: ConnectionOptions = {
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 @@ -41,6 +32,6 @@ const connectionSource = new DataSource({
rejectUnauthorized: false,
},
},
} as DataSourceOptions);
};

export default connectionSource;
export = config;

0 comments on commit 0911505

Please sign in to comment.