From 866f7663908639804be512ca1e3874977d298ce4 Mon Sep 17 00:00:00 2001 From: hirsch Date: Fri, 17 Apr 2020 19:17:27 +0200 Subject: [PATCH] feat: add setConnectionOptions & uses open connections --- ormconfig.js | 2 +- sample/test/sample.integration.test.ts | 15 ++++++++++++-- src/connection.ts | 28 +++++++++++++++++++++++--- src/typeorm-seeding.ts | 2 +- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/ormconfig.js b/ormconfig.js index 185bb896..423c3d1a 100644 --- a/ormconfig.js +++ b/ormconfig.js @@ -1,6 +1,6 @@ module.exports = [ { - name: 'sample', + name: 'default', type: 'sqlite', database: 'test.db', entities: ['sample/entities/**/*{.ts,.js}'], diff --git a/sample/test/sample.integration.test.ts b/sample/test/sample.integration.test.ts index 77927f5c..2f4eaa1e 100644 --- a/sample/test/sample.integration.test.ts +++ b/sample/test/sample.integration.test.ts @@ -1,11 +1,22 @@ -import { useSeeding, useRefreshDatabase, tearDownDatabase, factory } from '../../src/typeorm-seeding' +import { + useSeeding, + useRefreshDatabase, + tearDownDatabase, + factory, + setConnectionOptions, +} from '../../src/typeorm-seeding' import { User } from '../entities/User.entity' import { Connection } from 'typeorm' describe('Sample Integration Test', () => { let connection: Connection beforeAll(async (done) => { - connection = await useRefreshDatabase({ connection: 'memory' }) + setConnectionOptions({ + type: 'sqlite', + database: ':memory:', + entities: ['sample/entities/**/*{.ts,.js}'], + }) + connection = await useRefreshDatabase() await useSeeding() done() }) diff --git a/src/connection.ts b/src/connection.ts index 85a68c7c..073bec12 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -3,6 +3,7 @@ import { Connection as TypeORMConnection, ConnectionOptions as TypeORMConnectionOptions, createConnection as TypeORMCreateConnection, + getConnection as TypeORMGetConnection, } from 'typeorm' import { printError } from './utils/log.util' @@ -32,6 +33,7 @@ if ((global as any)[KEY] === undefined) { configureOption: defaultConfigureOption, ormConfig: undefined, connection: undefined, + overrideConnectionOptions: {}, } } @@ -42,8 +44,13 @@ export const configureConnection = (option: ConfigureOption = {}) => { } } +export const setConnectionOptions = (options: TypeORMConnectionOptions): void => { + ;(global as any)[KEY].overrideConnectionOptions = options +} + export const getConnectionOptions = async (): Promise => { const ormConfig = (global as any)[KEY].ormConfig + const overrideConnectionOptions = (global as any)[KEY].overrideConnectionOptions if (ormConfig === undefined) { const configureOption = (global as any)[KEY].configureOption const connection = configureOption.connection @@ -58,6 +65,12 @@ export const getConnectionOptions = async (): Promise => { options = filteredOptions } } + if (options.length > 1) { + const filteredOptions = options.filter((o) => o.name === 'default') + if (filteredOptions.length === 1) { + options = filteredOptions + } + } if (options.length === 1) { const option = options[0] if (!option.factories) { @@ -66,8 +79,11 @@ export const getConnectionOptions = async (): Promise => { if (!option.seeds) { option.seeds = [process.env.TYPEORM_SEEDING_SEEDS || 'src/database/seeds/**/*{.ts,.js}'] } - ;(global as any)[KEY].ormConfig = option - return option + ;(global as any)[KEY].ormConfig = { + ...option, + ...overrideConnectionOptions, + } + return (global as any)[KEY].ormConfig } printError('There are multiple connections please provide a connection name') } @@ -75,6 +91,7 @@ export const getConnectionOptions = async (): Promise => { } export const createConnection = async (option?: TypeORMConnectionOptions): Promise => { + const configureOption = (global as any)[KEY].configureOption let connection = (global as any)[KEY].connection let ormConfig = (global as any)[KEY].ormConfig @@ -83,7 +100,12 @@ export const createConnection = async (option?: TypeORMConnectionOptions): Promi } if (connection === undefined) { - connection = await TypeORMCreateConnection(ormConfig) + try { + connection = await TypeORMGetConnection(configureOption.name) + } catch (_) {} + if (connection === undefined) { + connection = await TypeORMCreateConnection(ormConfig) + } ;(global as any)[KEY].connection = connection } return connection diff --git a/src/typeorm-seeding.ts b/src/typeorm-seeding.ts index b7d3f65c..7c847784 100644 --- a/src/typeorm-seeding.ts +++ b/src/typeorm-seeding.ts @@ -53,7 +53,7 @@ export const useRefreshDatabase = async (options: ConfigureOption = {}): Promise configureConnection(options) const option = await getConnectionOptions() const connection = await createConnection(option) - if (connection.isConnected) { + if (connection && connection.isConnected) { await connection.dropDatabase() await connection.synchronize() }