-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathtypeorm-seeding.ts
73 lines (62 loc) · 2.91 KB
/
typeorm-seeding.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import 'reflect-metadata'
import { ObjectType, getConnection, Connection } from 'typeorm'
import { EntityFactory } from './entity-factory'
import { EntityFactoryDefinition, Factory, FactoryFunction, SeederConstructor, Seeder } from './types'
import { getNameOfEntity } from './utils/factory.util'
import { loadFiles, importFiles } from './utils/file.util'
import { ConfigureOption, configureConnection, getConnectionOptions, createConnection } from './connection'
// -------------------------------------------------------------------------
// Handy Exports
// -------------------------------------------------------------------------
export * from './importer'
export * from './connection'
export { Factory, Seeder } from './types'
export { times } from './helpers'
// -------------------------------------------------------------------------
// Types & Variables
// -------------------------------------------------------------------------
;(global as any).seeder = {
entityFactories: new Map<string, EntityFactoryDefinition<any, any>>(),
}
// -------------------------------------------------------------------------
// Facade functions
// -------------------------------------------------------------------------
export const define = <Entity, Context>(entity: ObjectType<Entity>, factoryFn: FactoryFunction<Entity, Context>) => {
;(global as any).seeder.entityFactories.set(getNameOfEntity(entity), {
entity,
factory: factoryFn,
})
}
export const factory: Factory = <Entity, Context>(entity: ObjectType<Entity>) => (context?: Context) => {
const name = getNameOfEntity(entity)
const entityFactoryObject = (global as any).seeder.entityFactories.get(name)
return new EntityFactory<Entity, Context>(name, entity, entityFactoryObject.factory, context)
}
export const runSeeder = async (clazz: SeederConstructor): Promise<any> => {
const seeder: Seeder = new clazz()
const connection = await createConnection()
return seeder.run(factory, connection)
}
// -------------------------------------------------------------------------
// Facade functions for testing
// -------------------------------------------------------------------------
export const useRefreshDatabase = async (options: ConfigureOption = {}): Promise<Connection> => {
configureConnection(options)
const option = await getConnectionOptions()
const connection = await createConnection(option)
if (connection && connection.isConnected) {
await connection.dropDatabase()
await connection.synchronize()
}
return connection
}
export const tearDownDatabase = async (): Promise<void> => {
const connection = await createConnection()
return connection && connection.isConnected ? connection.close() : undefined
}
export const useSeeding = async (options: ConfigureOption = {}): Promise<void> => {
configureConnection(options)
const option = await getConnectionOptions()
const factoryFiles = loadFiles(option.factories)
await importFiles(factoryFiles)
}