Skip to content

Commit 2b25741

Browse files
author
hirsch88
committed
Refactor seeding
1 parent 7c95460 commit 2b25741

File tree

3 files changed

+102
-85
lines changed

3 files changed

+102
-85
lines changed

package-scripts.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module.exports = {
5353
seed: series(
5454
'nps banner.seed',
5555
'nps db.config',
56-
runFast('./src/lib/seeds/')
56+
runFast('./src/lib/seeds/cli.ts')
5757
),
5858
config: runFast('./src/lib/ormconfig.ts'),
5959
drop: runFast('./node_modules/typeorm/cli.js schema:drop')

src/lib/seeds/cli.ts

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
import { Connection } from 'typeorm';
7+
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+
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+
});

src/lib/seeds/index.ts

+14-84
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,23 @@
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';
61
import { Connection } from 'typeorm';
2+
import 'reflect-metadata';
73
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;
394

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+
// -------------------------------------------------------------------------
888

899
export * from './FactoryInterface';
9010
export * from './EntityFactoryInterface';
9111
export * from './SeedsInterface';
9212
export * from './Factory';
9313
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

Comments
 (0)