Skip to content

Commit

Permalink
feat: add setConnectionOptions & uses open connections
Browse files Browse the repository at this point in the history
  • Loading branch information
hirsch committed Apr 17, 2020
1 parent d097cd0 commit 866f766
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ormconfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = [
{
name: 'sample',
name: 'default',
type: 'sqlite',
database: 'test.db',
entities: ['sample/entities/**/*{.ts,.js}'],
Expand Down
15 changes: 13 additions & 2 deletions sample/test/sample.integration.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
Expand Down
28 changes: 25 additions & 3 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -32,6 +33,7 @@ if ((global as any)[KEY] === undefined) {
configureOption: defaultConfigureOption,
ormConfig: undefined,
connection: undefined,
overrideConnectionOptions: {},
}
}

Expand All @@ -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<ConnectionOptions> => {
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
Expand All @@ -58,6 +65,12 @@ export const getConnectionOptions = async (): Promise<ConnectionOptions> => {
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) {
Expand All @@ -66,15 +79,19 @@ export const getConnectionOptions = async (): Promise<ConnectionOptions> => {
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')
}
return ormConfig
}

export const createConnection = async (option?: TypeORMConnectionOptions): Promise<TypeORMConnection> => {
const configureOption = (global as any)[KEY].configureOption
let connection = (global as any)[KEY].connection
let ormConfig = (global as any)[KEY].ormConfig

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/typeorm-seeding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 866f766

Please sign in to comment.