|
1 | | -import { |
2 | | - ConnectionOptionsReader, |
3 | | - Connection as TypeORMConnection, |
4 | | - ConnectionOptions as TypeORMConnectionOptions, |
5 | | - createConnection as TypeORMCreateConnection, |
6 | | - getConnection as TypeORMGetConnection, |
7 | | -} from 'typeorm' |
8 | | -import { printError } from './utils/log.util' |
| 1 | +import { resolve } from 'path' |
| 2 | +import { DataSource, DataSourceOptions } from 'typeorm' |
9 | 3 |
|
10 | 4 | interface SeedingOptions { |
11 | 5 | factories: string[] |
12 | 6 | seeds: string[] |
13 | 7 | } |
14 | 8 |
|
15 | | -export declare type ConnectionOptions = TypeORMConnectionOptions & SeedingOptions |
| 9 | +export declare type ConnectionOptions = DataSourceOptions & SeedingOptions |
16 | 10 |
|
17 | 11 | export interface ConfigureOption { |
18 | | - root?: string |
19 | | - configName?: string |
20 | | - connection?: string |
| 12 | + dataSourcePath?: string |
21 | 13 | } |
22 | 14 |
|
23 | 15 | const KEY = 'TypeORM_Seeding_Connection' |
24 | 16 |
|
25 | | -const defaultConfigureOption: ConfigureOption = { |
26 | | - root: process.cwd(), |
27 | | - configName: '', |
28 | | - connection: '', |
29 | | -} |
30 | | - |
31 | 17 | if ((global as any)[KEY] === undefined) { |
32 | 18 | ;(global as any)[KEY] = { |
33 | | - configureOption: defaultConfigureOption, |
| 19 | + dataSource: undefined as DataSource | undefined, |
| 20 | + dataSourcePath: undefined as string | undefined, |
34 | 21 | ormConfig: undefined, |
35 | 22 | connection: undefined, |
36 | | - overrideConnectionOptions: {}, |
| 23 | + overrideConnectionOptions: { |
| 24 | + factories: [process.env.TYPEORM_SEEDING_FACTORIES || 'src/database/factories/**/*{.ts,.js}'], |
| 25 | + seeds: [process.env.TYPEORM_SEEDING_SEEDS || 'src/database/seeds/**/*{.ts,.js}'], |
| 26 | + } as ConnectionOptions, |
37 | 27 | } |
38 | 28 | } |
39 | 29 |
|
40 | 30 | export const configureConnection = (option: ConfigureOption = {}) => { |
41 | | - ;(global as any)[KEY].configureOption = { |
42 | | - ...defaultConfigureOption, |
| 31 | + ;(global as any)[KEY] = { |
| 32 | + ...(global as any)[KEY], |
43 | 33 | ...option, |
44 | 34 | } |
45 | 35 | } |
46 | 36 |
|
47 | | -export const setConnectionOptions = (options: Partial<TypeORMConnectionOptions>): void => { |
48 | | - ;(global as any)[KEY].overrideConnectionOptions = options |
| 37 | +export const setConnectionOptions = (options: Partial<ConnectionOptions>): void => { |
| 38 | + ;(global as any)[KEY].overrideConnectionOptions = { |
| 39 | + ...(global as any)[KEY].overrideConnectionOptions, |
| 40 | + ...options, |
| 41 | + } |
49 | 42 | } |
50 | 43 |
|
51 | 44 | export const getConnectionOptions = async (): Promise<ConnectionOptions> => { |
52 | | - const ormConfig = (global as any)[KEY].ormConfig |
53 | | - const overrideConnectionOptions = (global as any)[KEY].overrideConnectionOptions |
54 | | - if (ormConfig === undefined) { |
55 | | - const configureOption = (global as any)[KEY].configureOption |
56 | | - const connection = configureOption.connection |
57 | | - const reader = new ConnectionOptionsReader({ |
58 | | - root: configureOption.root, |
59 | | - configName: configureOption.configName, |
60 | | - }) |
61 | | - let options = (await reader.all()) as any[] |
62 | | - if (connection !== undefined && connection !== '') { |
63 | | - const filteredOptions = options.filter((o) => o.name === connection) |
64 | | - if (filteredOptions.length === 1) { |
65 | | - options = filteredOptions |
66 | | - } |
67 | | - } |
68 | | - if (options.length > 1) { |
69 | | - const filteredOptions = options.filter((o) => o.name === 'default') |
70 | | - if (filteredOptions.length === 1) { |
71 | | - options = filteredOptions |
72 | | - } |
73 | | - } |
74 | | - if (options.length === 1) { |
75 | | - const option = options[0] |
76 | | - if (!option.factories) { |
77 | | - option.factories = [process.env.TYPEORM_SEEDING_FACTORIES || 'src/database/factories/**/*{.ts,.js}'] |
78 | | - } |
79 | | - if (!option.seeds) { |
80 | | - option.seeds = [process.env.TYPEORM_SEEDING_SEEDS || 'src/database/seeds/**/*{.ts,.js}'] |
81 | | - } |
82 | | - ;(global as any)[KEY].ormConfig = { |
83 | | - ...option, |
84 | | - ...overrideConnectionOptions, |
85 | | - } |
86 | | - return (global as any)[KEY].ormConfig |
87 | | - } |
88 | | - printError('There are multiple connections please provide a connection name') |
89 | | - } |
90 | | - return ormConfig |
| 45 | + return (global as any)[KEY].overrideConnectionOptions |
91 | 46 | } |
92 | 47 |
|
93 | | -export const createConnection = async (option?: TypeORMConnectionOptions): Promise<TypeORMConnection> => { |
94 | | - const configureOption = (global as any)[KEY].configureOption |
95 | | - let connection = (global as any)[KEY].connection |
96 | | - let ormConfig = (global as any)[KEY].ormConfig |
97 | | - |
98 | | - if (option !== undefined) { |
99 | | - ormConfig = option |
| 48 | +export const resetDataSource = async () => { |
| 49 | + ;(global as any)[KEY].overrideConnectionOptions = {} |
| 50 | + const ds = (global as any)[KEY].dataSource as DataSource |
| 51 | + if (ds) { |
| 52 | + ;(global as any)[KEY].dataSource = undefined |
| 53 | + return ds && ds.isInitialized ? ds.destroy() : undefined |
100 | 54 | } |
| 55 | +} |
| 56 | + |
| 57 | +export const loadDataSource = async (options?: DataSourceOptions): Promise<DataSource> => { |
| 58 | + const { dataSourcePath, dataSource, overrideConnectionOptions } = (global as any)[KEY] |
101 | 59 |
|
102 | | - if (connection === undefined) { |
103 | | - try { |
104 | | - connection = await TypeORMGetConnection(configureOption.name) |
105 | | - } catch (_) {} |
106 | | - if (connection === undefined) { |
107 | | - connection = await TypeORMCreateConnection(ormConfig) |
| 60 | + let ds: DataSource | undefined |
| 61 | + if (!dataSource) { |
| 62 | + if (dataSourcePath) { |
| 63 | + ds = (await require(resolve(process.cwd(), dataSourcePath))).default as DataSource |
| 64 | + ds.setOptions({ ...overrideConnectionOptions, ...options }) |
| 65 | + } else { |
| 66 | + ds = new DataSource({ ...overrideConnectionOptions, ...options }) |
108 | 67 | } |
109 | | - ;(global as any)[KEY].connection = connection |
| 68 | + await ds.initialize() |
| 69 | + ;(global as any)[KEY].dataSource = ds |
110 | 70 | } |
111 | | - return connection |
| 71 | + |
| 72 | + return (global as any)[KEY].dataSource |
112 | 73 | } |
0 commit comments