|
1 | | -import 'reflect-metadata'; |
2 | | -import * as path from 'path'; |
3 | | -import * as glob from 'glob'; |
4 | | -import * as commander from 'commander'; |
5 | | -import * as Chalk from 'chalk'; |
6 | 1 | import { Connection } from 'typeorm'; |
| 2 | +import 'reflect-metadata'; |
7 | 3 | import { Factory } from './Factory'; |
8 | | -import { getConnection } from './connection'; |
9 | | - |
10 | | - |
11 | | -// Get executiuon path to look from there for seeds and factories |
12 | | -const runDir = process.cwd(); |
13 | | - |
14 | | -// Cli helper |
15 | | -commander |
16 | | - .version('0.0.0') |
17 | | - .description('Run database seeds of your project') |
18 | | - .option('-L, --logging', 'enable sql query logging') |
19 | | - .option('--factories <path>', 'add filepath for your factories') |
20 | | - .option('--seeds <path>', 'add filepath for your seeds') |
21 | | - .option('--config <filepath>', 'add filepath to your database config (must be a json)') |
22 | | - .parse(process.argv); |
23 | | - |
24 | | -// Get cli parameter for a different factory path |
25 | | -const factoryPath = (commander.factories) |
26 | | - ? commander.factories |
27 | | - : 'src/database/'; |
28 | | - |
29 | | -// Get cli parameter for a different seeds path |
30 | | -const seedsPath = (commander.seeds) |
31 | | - ? commander.seeds |
32 | | - : 'src/database/seeds/'; |
33 | | - |
34 | | -// Search for seeds and factories |
35 | | -glob(path.join(runDir, factoryPath, '**/*Factory{.js,.ts}'), (errFactories: any, factories: string[]) => { |
36 | | - glob(path.join(runDir, seedsPath, '*{.js,.ts}'), (errSeeds: any, seeds: string[]) => { |
37 | | - const log = console.log; |
38 | | - const chalk = Chalk.default; |
39 | 4 |
|
40 | | - // Status logging to print out the amount of factories and seeds. |
41 | | - log(chalk.bold('seeds')); |
42 | | - log('🔎 ', chalk.gray.underline(`found:`), |
43 | | - chalk.blue.bold(`${factories.length} factories`, chalk.gray('&'), chalk.blue.bold(`${seeds.length} seeds`))); |
44 | | - |
45 | | - // Initialize all factories |
46 | | - for (const factory of factories) { |
47 | | - require(factory); |
48 | | - } |
49 | | - |
50 | | - // Get typeorm database connection and pass them to the factory instance |
51 | | - getConnection().then((connection: Connection) => { |
52 | | - const factory = Factory.getInstance(); |
53 | | - factory.setConnection(connection); |
54 | | - |
55 | | - // Initialize and seed all seeds. |
56 | | - const queue: Array<Promise<void>> = []; |
57 | | - for (const seed of seeds) { |
58 | | - try { |
59 | | - const seedFile: any = require(seed); |
60 | | - let className = seed.split('/')[seed.split('/').length - 1]; |
61 | | - className = className.replace('.ts', '').replace('.js', ''); |
62 | | - className = className.split('-')[className.split('-').length - 1]; |
63 | | - log('\n' + chalk.gray.underline(`executing seed: `), chalk.green.bold(`${className}`)); |
64 | | - queue.push((new seedFile[className]()).seed(factory)); |
65 | | - } catch (error) { |
66 | | - console.error('Could not run seed ' + seed, error); |
67 | | - } |
68 | | - } |
69 | | - |
70 | | - // Promise to catch the end for termination and logging |
71 | | - Promise |
72 | | - .all(queue) |
73 | | - .then(() => { |
74 | | - log('\n👍 ', chalk.gray.underline(`finished seeding`)); |
75 | | - process.exit(0); |
76 | | - }) |
77 | | - .catch((error) => { |
78 | | - console.error('Could not run seed ' + error); |
79 | | - process.exit(1); |
80 | | - }); |
81 | | - |
82 | | - }).catch((error) => { |
83 | | - console.error('Could not connection to database ' + error); |
84 | | - process.exit(1); |
85 | | - }); |
86 | | - }); |
87 | | -}); |
| 5 | +// ------------------------------------------------------------------------- |
| 6 | +// Handy Exports |
| 7 | +// ------------------------------------------------------------------------- |
88 | 8 |
|
89 | 9 | export * from './FactoryInterface'; |
90 | 10 | export * from './EntityFactoryInterface'; |
91 | 11 | export * from './SeedsInterface'; |
92 | 12 | export * from './Factory'; |
93 | 13 | export * from './utils'; |
| 14 | + |
| 15 | +// ------------------------------------------------------------------------- |
| 16 | +// Facade functions |
| 17 | +// ------------------------------------------------------------------------- |
| 18 | + |
| 19 | +export const getFactory = (connection: Connection) => { |
| 20 | + const factory = Factory.getInstance(); |
| 21 | + factory.setConnection(connection); |
| 22 | + return factory; |
| 23 | +}; |
0 commit comments