Skip to content

Commit cce10e6

Browse files
author
Gery Hirschfeld
authoredApr 17, 2020
Merge pull request #58 from w3tecch/feat/testing-connection
feat: add setConnectionOptions & uses open connections
2 parents d097cd0 + 866f766 commit cce10e6

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed
 

‎ormconfig.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = [
22
{
3-
name: 'sample',
3+
name: 'default',
44
type: 'sqlite',
55
database: 'test.db',
66
entities: ['sample/entities/**/*{.ts,.js}'],

‎sample/test/sample.integration.test.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
import { useSeeding, useRefreshDatabase, tearDownDatabase, factory } from '../../src/typeorm-seeding'
1+
import {
2+
useSeeding,
3+
useRefreshDatabase,
4+
tearDownDatabase,
5+
factory,
6+
setConnectionOptions,
7+
} from '../../src/typeorm-seeding'
28
import { User } from '../entities/User.entity'
39
import { Connection } from 'typeorm'
410

511
describe('Sample Integration Test', () => {
612
let connection: Connection
713
beforeAll(async (done) => {
8-
connection = await useRefreshDatabase({ connection: 'memory' })
14+
setConnectionOptions({
15+
type: 'sqlite',
16+
database: ':memory:',
17+
entities: ['sample/entities/**/*{.ts,.js}'],
18+
})
19+
connection = await useRefreshDatabase()
920
await useSeeding()
1021
done()
1122
})

‎src/connection.ts

+25-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
Connection as TypeORMConnection,
44
ConnectionOptions as TypeORMConnectionOptions,
55
createConnection as TypeORMCreateConnection,
6+
getConnection as TypeORMGetConnection,
67
} from 'typeorm'
78
import { printError } from './utils/log.util'
89

@@ -32,6 +33,7 @@ if ((global as any)[KEY] === undefined) {
3233
configureOption: defaultConfigureOption,
3334
ormConfig: undefined,
3435
connection: undefined,
36+
overrideConnectionOptions: {},
3537
}
3638
}
3739

@@ -42,8 +44,13 @@ export const configureConnection = (option: ConfigureOption = {}) => {
4244
}
4345
}
4446

47+
export const setConnectionOptions = (options: TypeORMConnectionOptions): void => {
48+
;(global as any)[KEY].overrideConnectionOptions = options
49+
}
50+
4551
export const getConnectionOptions = async (): Promise<ConnectionOptions> => {
4652
const ormConfig = (global as any)[KEY].ormConfig
53+
const overrideConnectionOptions = (global as any)[KEY].overrideConnectionOptions
4754
if (ormConfig === undefined) {
4855
const configureOption = (global as any)[KEY].configureOption
4956
const connection = configureOption.connection
@@ -58,6 +65,12 @@ export const getConnectionOptions = async (): Promise<ConnectionOptions> => {
5865
options = filteredOptions
5966
}
6067
}
68+
if (options.length > 1) {
69+
const filteredOptions = options.filter((o) => o.name === 'default')
70+
if (filteredOptions.length === 1) {
71+
options = filteredOptions
72+
}
73+
}
6174
if (options.length === 1) {
6275
const option = options[0]
6376
if (!option.factories) {
@@ -66,15 +79,19 @@ export const getConnectionOptions = async (): Promise<ConnectionOptions> => {
6679
if (!option.seeds) {
6780
option.seeds = [process.env.TYPEORM_SEEDING_SEEDS || 'src/database/seeds/**/*{.ts,.js}']
6881
}
69-
;(global as any)[KEY].ormConfig = option
70-
return option
82+
;(global as any)[KEY].ormConfig = {
83+
...option,
84+
...overrideConnectionOptions,
85+
}
86+
return (global as any)[KEY].ormConfig
7187
}
7288
printError('There are multiple connections please provide a connection name')
7389
}
7490
return ormConfig
7591
}
7692

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

@@ -83,7 +100,12 @@ export const createConnection = async (option?: TypeORMConnectionOptions): Promi
83100
}
84101

85102
if (connection === undefined) {
86-
connection = await TypeORMCreateConnection(ormConfig)
103+
try {
104+
connection = await TypeORMGetConnection(configureOption.name)
105+
} catch (_) {}
106+
if (connection === undefined) {
107+
connection = await TypeORMCreateConnection(ormConfig)
108+
}
87109
;(global as any)[KEY].connection = connection
88110
}
89111
return connection

‎src/typeorm-seeding.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const useRefreshDatabase = async (options: ConfigureOption = {}): Promise
5353
configureConnection(options)
5454
const option = await getConnectionOptions()
5555
const connection = await createConnection(option)
56-
if (connection.isConnected) {
56+
if (connection && connection.isConnected) {
5757
await connection.dropDatabase()
5858
await connection.synchronize()
5959
}

0 commit comments

Comments
 (0)
Please sign in to comment.