@sdflc/backend-helpers 1.0.79 → 1.0.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/BaseCore.js +496 -600
- package/build/BaseGateway.js +331 -377
- package/build/DataCache.js +16 -19
- package/build/Database.js +55 -74
- package/build/DbMigratorAndSeeder.js +88 -114
- package/build/RedisDataLoader.js +153 -184
- package/build/apiContext.js +35 -33
- package/build/baseController.js +9 -16
- package/build/commonValidators.js +22 -31
- package/build/configBulder.js +5 -1
- package/build/dbHelpers.js +19 -29
- package/build/dbSeeder.js +11 -20
- package/build/dbTableHelpers.js +10 -10
- package/build/downloadHelpers.js +15 -25
- package/build/esm/BaseCore.js +496 -600
- package/build/esm/BaseGateway.js +331 -377
- package/build/esm/DataCache.js +16 -19
- package/build/esm/Database.js +55 -74
- package/build/esm/DbMigratorAndSeeder.js +88 -114
- package/build/esm/RedisDataLoader.js +153 -184
- package/build/esm/apiContext.js +35 -33
- package/build/esm/baseController.js +9 -16
- package/build/esm/commonValidators.js +22 -31
- package/build/esm/configBulder.js +5 -1
- package/build/esm/dbHelpers.js +19 -29
- package/build/esm/dbSeeder.js +11 -20
- package/build/esm/dbTableHelpers.js +10 -10
- package/build/esm/downloadHelpers.js +15 -25
- package/build/esm/knexConfigBuilder.js +17 -7
- package/build/esm/passwordHelpers.js +7 -16
- package/build/esm/resolversHelpers.js +11 -4
- package/build/esm/userToken.js +2 -2
- package/build/knexConfigBuilder.js +17 -7
- package/build/passwordHelpers.js +7 -16
- package/build/resolversHelpers.js +11 -4
- package/build/userToken.js +2 -2
- package/package.json +2 -3
package/build/esm/DataCache.js
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import DataLoader from 'dataloader';
|
|
11
2
|
import { RedisDataLoader } from './RedisDataLoader';
|
|
12
3
|
import { buildKey as defaultBuildKey } from '@sdflc/utils';
|
|
13
4
|
import { FIELDS } from '.';
|
|
14
5
|
import { castArray } from 'lodash';
|
|
15
6
|
class DataCache {
|
|
7
|
+
buildKey;
|
|
8
|
+
loader;
|
|
9
|
+
config;
|
|
10
|
+
redisClient;
|
|
16
11
|
constructor(props) {
|
|
17
12
|
const { loaderFn, keyPrefix, context, buildKey, redisClient, table } = props || {};
|
|
18
|
-
if (!
|
|
13
|
+
if (!props?.idField) {
|
|
19
14
|
props.idField = FIELDS.ID;
|
|
20
15
|
}
|
|
21
|
-
if (!
|
|
16
|
+
if (!props?.idFieldAs) {
|
|
22
17
|
props.idFieldAs = FIELDS.ID;
|
|
23
18
|
}
|
|
24
19
|
this.config = props;
|
|
@@ -26,7 +21,7 @@ class DataCache {
|
|
|
26
21
|
this.buildKey = typeof buildKey === 'function' ? buildKey : defaultBuildKey;
|
|
27
22
|
this.batchLoaderDefault = this.batchLoaderDefault.bind(this);
|
|
28
23
|
const fn = typeof loaderFn === 'function' ? loaderFn : this.batchLoaderDefault(props);
|
|
29
|
-
const prefixToUse =
|
|
24
|
+
const prefixToUse = context?.keyPrefix || keyPrefix || table;
|
|
30
25
|
this.init(fn, prefixToUse, context);
|
|
31
26
|
}
|
|
32
27
|
batchLoaderDefault(props) {
|
|
@@ -40,9 +35,12 @@ class DataCache {
|
|
|
40
35
|
else if (!db) {
|
|
41
36
|
throw Error(`DataCache requires 'db' property to be setup`);
|
|
42
37
|
}
|
|
43
|
-
return (ids) =>
|
|
38
|
+
return async (ids) => {
|
|
44
39
|
const resultItems = [];
|
|
45
|
-
const readCfg =
|
|
40
|
+
const readCfg = {
|
|
41
|
+
...props,
|
|
42
|
+
buildKey: this.buildKey,
|
|
43
|
+
};
|
|
46
44
|
const idsToUse = castArray(ids);
|
|
47
45
|
if (!readAll) {
|
|
48
46
|
readCfg.mapBy = idFieldAs || idField;
|
|
@@ -51,7 +49,7 @@ class DataCache {
|
|
|
51
49
|
values: idsToUse.filter((id) => id),
|
|
52
50
|
};
|
|
53
51
|
}
|
|
54
|
-
const dbItems =
|
|
52
|
+
const dbItems = await db.read(readCfg);
|
|
55
53
|
if (readAll) {
|
|
56
54
|
resultItems.push(...dbItems);
|
|
57
55
|
}
|
|
@@ -59,7 +57,7 @@ class DataCache {
|
|
|
59
57
|
resultItems.push(...idsToUse.map((id) => dbItems[this.buildKey(id)] || null));
|
|
60
58
|
}
|
|
61
59
|
return resultItems;
|
|
62
|
-
}
|
|
60
|
+
};
|
|
63
61
|
}
|
|
64
62
|
init(batchFunction, keyPrefix, context) {
|
|
65
63
|
if (this.redisClient) {
|
|
@@ -86,12 +84,11 @@ class DataCache {
|
|
|
86
84
|
return this.loader.loadMany(keys.map((key) => this.buildKey(key)));
|
|
87
85
|
}
|
|
88
86
|
clear(key) {
|
|
89
|
-
var _a;
|
|
90
87
|
if (Array.isArray(key)) {
|
|
91
|
-
key.forEach((item) =>
|
|
88
|
+
key.forEach((item) => this.loader?.clear(this.buildKey(item)));
|
|
92
89
|
}
|
|
93
90
|
else {
|
|
94
|
-
|
|
91
|
+
this.loader?.clear(this.buildKey(key));
|
|
95
92
|
}
|
|
96
93
|
}
|
|
97
94
|
}
|
package/build/esm/Database.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { camelCase } from 'lodash';
|
|
11
2
|
import knex from 'knex';
|
|
12
3
|
import { buildKey as defaultBuildKey, Logger, LoggerLevels } from '@sdflc/utils';
|
|
@@ -18,11 +9,11 @@ const buildKey = (args) => {
|
|
|
18
9
|
return parts.join('|');
|
|
19
10
|
};
|
|
20
11
|
class Database {
|
|
12
|
+
trx = null;
|
|
13
|
+
knexHandle = null;
|
|
14
|
+
dbSchema = '';
|
|
15
|
+
logger = new Logger({ level: LoggerLevels.ERROR });
|
|
21
16
|
constructor(props) {
|
|
22
|
-
this.trx = null;
|
|
23
|
-
this.knexHandle = null;
|
|
24
|
-
this.dbSchema = '';
|
|
25
|
-
this.logger = new Logger({ level: LoggerLevels.ERROR });
|
|
26
17
|
if (!props.knexConfig) {
|
|
27
18
|
throw new Error('The Database class expects to have `knexConfig` in its constructor props');
|
|
28
19
|
}
|
|
@@ -73,43 +64,35 @@ class Database {
|
|
|
73
64
|
raw(query, bindings) {
|
|
74
65
|
return this.knexHandle.raw(query, bindings);
|
|
75
66
|
}
|
|
76
|
-
runRawQuery(query, bindings) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return rows;
|
|
80
|
-
});
|
|
67
|
+
async runRawQuery(query, bindings) {
|
|
68
|
+
const rows = await (this.trx ? this.trx : this.knexHandle).raw(query, bindings);
|
|
69
|
+
return rows;
|
|
81
70
|
}
|
|
82
|
-
startTransaction() {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return trx;
|
|
87
|
-
});
|
|
71
|
+
async startTransaction() {
|
|
72
|
+
const trx = await this.createTransaction(this.trx);
|
|
73
|
+
this.trx = trx;
|
|
74
|
+
return trx;
|
|
88
75
|
}
|
|
89
76
|
setTransaction(trx) {
|
|
90
77
|
this.trx = trx;
|
|
91
78
|
}
|
|
92
|
-
commitTransaction() {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
});
|
|
79
|
+
async commitTransaction() {
|
|
80
|
+
if (this.trx) {
|
|
81
|
+
await this.trx.commit();
|
|
82
|
+
this.trx = undefined;
|
|
83
|
+
}
|
|
99
84
|
}
|
|
100
|
-
rollbackTransaction() {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
yield this.trx.rollback();
|
|
105
|
-
}
|
|
106
|
-
catch (ex) {
|
|
107
|
-
// Do nothing here
|
|
108
|
-
this.logger.error(`Failed to rollback a transaction:`, ex.message);
|
|
109
|
-
}
|
|
85
|
+
async rollbackTransaction() {
|
|
86
|
+
if (this.trx) {
|
|
87
|
+
try {
|
|
88
|
+
await this.trx.rollback();
|
|
110
89
|
}
|
|
111
|
-
|
|
112
|
-
|
|
90
|
+
catch (ex) {
|
|
91
|
+
// Do nothing here
|
|
92
|
+
this.logger.error(`Failed to rollback a transaction:`, ex.message);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
this.trx = undefined;
|
|
113
96
|
}
|
|
114
97
|
doCalculated(item, calculatedFields) {
|
|
115
98
|
calculatedFields.forEach((calcField) => {
|
|
@@ -118,40 +101,38 @@ class Database {
|
|
|
118
101
|
}
|
|
119
102
|
});
|
|
120
103
|
}
|
|
121
|
-
read(args) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const key = fnBuildKey(item[camelCase(mapBy)]);
|
|
139
|
-
if (Array.isArray(calculated) && calculated.length > 0) {
|
|
140
|
-
this.doCalculated(item, calculated);
|
|
141
|
-
}
|
|
142
|
-
acc[key] = item;
|
|
143
|
-
return acc;
|
|
144
|
-
}, {});
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
104
|
+
async read(args) {
|
|
105
|
+
const { where, whereIn, extendQuery, table, selectFields, mapBy, calculated, buildKey } = args || {};
|
|
106
|
+
const query = this.getBuilder(table).select(Array.isArray(selectFields) && selectFields.length > 0 ? selectFields : '*');
|
|
107
|
+
if (where) {
|
|
108
|
+
query.where(where);
|
|
109
|
+
}
|
|
110
|
+
if (whereIn) {
|
|
111
|
+
query.whereIn(whereIn.field, whereIn.values);
|
|
112
|
+
}
|
|
113
|
+
if (typeof extendQuery === 'function') {
|
|
114
|
+
await extendQuery(query);
|
|
115
|
+
}
|
|
116
|
+
let dbResults = await query;
|
|
117
|
+
if (mapBy) {
|
|
118
|
+
const fnBuildKey = typeof buildKey === 'function' ? buildKey : defaultBuildKey;
|
|
119
|
+
dbResults = dbResults.reduce((acc, item) => {
|
|
120
|
+
const key = fnBuildKey(item[camelCase(mapBy)]);
|
|
147
121
|
if (Array.isArray(calculated) && calculated.length > 0) {
|
|
148
|
-
|
|
149
|
-
this.doCalculated(row, calculated);
|
|
150
|
-
});
|
|
122
|
+
this.doCalculated(item, calculated);
|
|
151
123
|
}
|
|
124
|
+
acc[key] = item;
|
|
125
|
+
return acc;
|
|
126
|
+
}, {});
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
if (Array.isArray(calculated) && calculated.length > 0) {
|
|
130
|
+
dbResults.forEach((row) => {
|
|
131
|
+
this.doCalculated(row, calculated);
|
|
132
|
+
});
|
|
152
133
|
}
|
|
153
|
-
|
|
154
|
-
|
|
134
|
+
}
|
|
135
|
+
return dbResults;
|
|
155
136
|
}
|
|
156
137
|
createSortBy(sortBy, defaultSortBy) {
|
|
157
138
|
if (!sortBy) {
|
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import path from 'path';
|
|
11
2
|
import BPromise from 'bluebird';
|
|
12
3
|
import { reduce, camelCase, flatten, map } from 'lodash';
|
|
@@ -16,16 +7,16 @@ const SQLS = {
|
|
|
16
7
|
listTablesSchema: (appSettings) => `
|
|
17
8
|
SELECT TABLE_NAME as table_name
|
|
18
9
|
FROM INFORMATION_SCHEMA.TABLES
|
|
19
|
-
WHERE TABLE_SCHEMA = '${appSettings
|
|
10
|
+
WHERE TABLE_SCHEMA = '${appSettings?.dbSchema}';
|
|
20
11
|
`,
|
|
21
12
|
listTables: () => `
|
|
22
13
|
SELECT TABLE_NAME as table_name
|
|
23
14
|
FROM INFORMATION_SCHEMA.TABLES
|
|
24
15
|
`,
|
|
25
16
|
dropTablesSchema: (appSettings) => `
|
|
26
|
-
SELECT 'DROP TABLE IF EXISTS ${appSettings
|
|
17
|
+
SELECT 'DROP TABLE IF EXISTS ${appSettings?.dbSchema}.' || TABLE_NAME || ';' as cmd
|
|
27
18
|
FROM INFORMATION_SCHEMA.TABLES
|
|
28
|
-
WHERE TABLE_SCHEMA = '${appSettings
|
|
19
|
+
WHERE TABLE_SCHEMA = '${appSettings?.dbSchema}';
|
|
29
20
|
`,
|
|
30
21
|
dropTables: () => `
|
|
31
22
|
SELECT 'DROP TABLE IF EXISTS ' || TABLE_NAME || ';' as cmd
|
|
@@ -34,21 +25,22 @@ const SQLS = {
|
|
|
34
25
|
},
|
|
35
26
|
};
|
|
36
27
|
class DbMigratorAndSeeder {
|
|
28
|
+
knexHandle;
|
|
29
|
+
config;
|
|
30
|
+
logger;
|
|
37
31
|
constructor(knexHandle, config) {
|
|
38
32
|
this.knexHandle = knexHandle;
|
|
39
33
|
this.config = config;
|
|
40
|
-
this.logger =
|
|
34
|
+
this.logger = config?.logger instanceof Logger ? config.logger : new Logger({ level: LoggerLevels.LOG });
|
|
41
35
|
}
|
|
42
|
-
runMigrations() {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
this.logger.log(`Current version of database: ${version}`);
|
|
51
|
-
});
|
|
36
|
+
async runMigrations() {
|
|
37
|
+
const result = await this.knexHandle.migrate.latest();
|
|
38
|
+
if (this.config && this.config.runDbSeed && result[1].length > 0) {
|
|
39
|
+
await this.knexHandle.seed.run();
|
|
40
|
+
}
|
|
41
|
+
this.logger.log(`Ran ${result[1].length} migration(s)`);
|
|
42
|
+
const version = this.knexHandle.migrate.currentVersion();
|
|
43
|
+
this.logger.log(`Current version of database: ${version}`);
|
|
52
44
|
}
|
|
53
45
|
/**
|
|
54
46
|
* Go from array of objects which contain keys of object names which have an array value of seeded objects
|
|
@@ -67,31 +59,27 @@ class DbMigratorAndSeeder {
|
|
|
67
59
|
/**
|
|
68
60
|
* Drop all tables, run migrations, then seed the tables
|
|
69
61
|
*/
|
|
70
|
-
resetDb() {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
});
|
|
62
|
+
async resetDb() {
|
|
63
|
+
try {
|
|
64
|
+
await this.wipeDb();
|
|
65
|
+
await this.runMigrations();
|
|
66
|
+
}
|
|
67
|
+
catch (ex) {
|
|
68
|
+
this.logger.error('An exception has occurred on resetting database: ', ex);
|
|
69
|
+
}
|
|
80
70
|
}
|
|
81
71
|
/**
|
|
82
72
|
* Selects all the tables in the schema and drops them.
|
|
83
73
|
* This approach does not work in case we have constraints.
|
|
84
74
|
* TODO: Remove all constraints and then drop tables.
|
|
85
75
|
*/
|
|
86
|
-
wipeDb() {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
BPromise.each(commands, (cmd) => this.knexHandle.raw(cmd));
|
|
94
|
-
});
|
|
76
|
+
async wipeDb() {
|
|
77
|
+
const query = this.config.dbSchema
|
|
78
|
+
? SQLS[this.config.dbType].dropTablesSchema(this.config)
|
|
79
|
+
: SQLS[this.config.dbType].dropTables(this.config);
|
|
80
|
+
const result = await this.knexHandle.raw(query);
|
|
81
|
+
const commands = result.rows.map((r) => r.cmd);
|
|
82
|
+
BPromise.each(commands, (cmd) => this.knexHandle.raw(cmd));
|
|
95
83
|
}
|
|
96
84
|
/**
|
|
97
85
|
* Removes all the records from provided list of tables
|
|
@@ -100,14 +88,12 @@ class DbMigratorAndSeeder {
|
|
|
100
88
|
* @param appSettings settings with db related information
|
|
101
89
|
* @returns promise
|
|
102
90
|
*/
|
|
103
|
-
cleanDb(tablesToClean) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
resolve(tablesToClean);
|
|
110
|
-
});
|
|
91
|
+
async cleanDb(tablesToClean) {
|
|
92
|
+
for (let idx = 0, len = tablesToClean.length; idx < len; ++idx) {
|
|
93
|
+
await this.cleanDbTable(tablesToClean[idx]);
|
|
94
|
+
}
|
|
95
|
+
return new BPromise((resolve) => {
|
|
96
|
+
resolve(tablesToClean);
|
|
111
97
|
});
|
|
112
98
|
}
|
|
113
99
|
/**
|
|
@@ -116,84 +102,72 @@ class DbMigratorAndSeeder {
|
|
|
116
102
|
* it is used to add extra filters to clear the table to avoid for example users data
|
|
117
103
|
* @param table Name of table from which to remove all the records
|
|
118
104
|
*/
|
|
119
|
-
cleanDbTable(table) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
}
|
|
105
|
+
async cleanDbTable(table) {
|
|
106
|
+
const query = this.knexHandle(table).withSchema(this.config.dbSchema).delete();
|
|
107
|
+
const cleanTableFilters = this.config?.cleanTableFilters?.[table];
|
|
108
|
+
if (cleanTableFilters != undefined) {
|
|
109
|
+
if (Array.isArray(cleanTableFilters.where)) {
|
|
110
|
+
cleanTableFilters.where.forEach((whereItem) => {
|
|
111
|
+
if (whereItem.fieldName) {
|
|
112
|
+
query.where(whereItem.fieldName, whereItem.operand, whereItem.value);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
132
115
|
}
|
|
133
|
-
|
|
134
|
-
|
|
116
|
+
}
|
|
117
|
+
await query;
|
|
135
118
|
}
|
|
136
119
|
/**
|
|
137
120
|
* Returns a list of tables in db schedm specified in settings
|
|
138
121
|
* @returns arrays of table names in the provided db schema
|
|
139
122
|
*/
|
|
140
|
-
listDbTables() {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
.map((row) => row.table_name)
|
|
150
|
-
.filter((table) => skipTables.indexOf(table) === -1);
|
|
151
|
-
});
|
|
123
|
+
async listDbTables() {
|
|
124
|
+
const skipTables = this.config?.skipTables ?? [];
|
|
125
|
+
const query = this.config.dbSchema
|
|
126
|
+
? SQLS[this.config.dbType].listTablesSchema(this.config)
|
|
127
|
+
: SQLS[this.config.dbType].listTables(this.config);
|
|
128
|
+
const tables = await this.knexHandle.raw(query);
|
|
129
|
+
return (tables.rows || [])
|
|
130
|
+
.map((row) => row.table_name)
|
|
131
|
+
.filter((table) => skipTables.indexOf(table) === -1);
|
|
152
132
|
}
|
|
153
|
-
generateSeeds(dirname, seedFiles) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
return seedGroups;
|
|
175
|
-
});
|
|
133
|
+
async generateSeeds(dirname, seedFiles) {
|
|
134
|
+
let seedGroups = [];
|
|
135
|
+
const seedFileNames = flatten(seedFiles);
|
|
136
|
+
const seedFnExports = await Promise.all(map(seedFileNames, (filename) => import(path.join(dirname, `./objects/${filename}.js`))));
|
|
137
|
+
const seedFns = map(seedFnExports, 'default');
|
|
138
|
+
const seedFnsMap = {};
|
|
139
|
+
for (let i = 0; i < seedFileNames.length; i++) {
|
|
140
|
+
const seedFileName = seedFileNames[i];
|
|
141
|
+
seedFnsMap[seedFileName] = seedFns[i];
|
|
142
|
+
}
|
|
143
|
+
seedFiles.reduce((seeds, fileGroup) => {
|
|
144
|
+
seedGroups = seedGroups.concat(fileGroup.reduce((group, filename) => {
|
|
145
|
+
const seedFn = seedFnsMap[filename];
|
|
146
|
+
const seed = seedFn(seeds);
|
|
147
|
+
const filenameCamel = camelCase(filename);
|
|
148
|
+
seeds[filenameCamel] = seed;
|
|
149
|
+
return Object.assign({ [filenameCamel]: seed }, group);
|
|
150
|
+
}, {}));
|
|
151
|
+
return seeds;
|
|
152
|
+
}, {});
|
|
153
|
+
return seedGroups;
|
|
176
154
|
}
|
|
177
155
|
removeSeedsFromDb(seedFiles) {
|
|
178
156
|
const tablesToReset = flatten(seedFiles).reverse();
|
|
179
157
|
this.logger.log('Deleting records from tables: ', tablesToReset);
|
|
180
158
|
return this.cleanDb(tablesToReset);
|
|
181
159
|
}
|
|
182
|
-
insertRecords(seedGroups) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}), Promise.resolve());
|
|
189
|
-
});
|
|
160
|
+
async insertRecords(seedGroups) {
|
|
161
|
+
this.logger.log('Inserting seeds records into tables...');
|
|
162
|
+
return seedGroups.reduce(async (previousGroup, group) => {
|
|
163
|
+
await previousGroup;
|
|
164
|
+
return Promise.all(Object.keys(group).map((table) => this.knexHandle(table).withSchema(this.config.dbSchema).insert(group[table])));
|
|
165
|
+
}, Promise.resolve());
|
|
190
166
|
}
|
|
191
|
-
runSeeds(dirname, seedFiles) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
return yield this.insertRecords(groups);
|
|
196
|
-
});
|
|
167
|
+
async runSeeds(dirname, seedFiles) {
|
|
168
|
+
const groups = await this.generateSeeds(dirname, seedFiles);
|
|
169
|
+
await this.removeSeedsFromDb(seedFiles);
|
|
170
|
+
return await this.insertRecords(groups);
|
|
197
171
|
}
|
|
198
172
|
}
|
|
199
173
|
export { DbMigratorAndSeeder };
|