@sdflc/backend-helpers 1.0.77 → 1.0.79
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/README.md +178 -178
- package/build/BaseCore.d.ts +88 -88
- package/build/BaseCore.js +816 -819
- package/build/BaseGateway.d.ts +49 -49
- package/build/BaseGateway.js +482 -484
- package/build/DataCache.d.ts +14 -14
- package/build/DataCache.js +104 -104
- package/build/Database.d.ts +32 -32
- package/build/Database.js +198 -198
- package/build/DbMigratorAndSeeder.d.ts +49 -49
- package/build/DbMigratorAndSeeder.js +238 -228
- package/build/RedisDataLoader.d.ts +41 -41
- package/build/RedisDataLoader.js +243 -248
- package/build/apiContext.d.ts +11 -11
- package/build/apiContext.js +143 -143
- package/build/{BaseController.d.ts → baseController.d.ts} +17 -17
- package/build/{BaseController.js → baseController.js} +66 -66
- package/build/checkItHelpers.d.ts +3 -3
- package/build/checkItHelpers.js +28 -28
- package/build/commonValidators.d.ts +17 -17
- package/build/commonValidators.js +292 -298
- package/build/configBulder.d.ts +2 -2
- package/build/configBulder.js +32 -32
- package/build/constants.d.ts +30 -30
- package/build/constants.js +80 -81
- package/build/dbHelpers.d.ts +43 -43
- package/build/dbHelpers.js +166 -166
- package/build/dbSeeder.d.ts +2 -2
- package/build/dbSeeder.js +92 -82
- package/build/dbTableHelpers.d.ts +11 -11
- package/build/dbTableHelpers.js +110 -110
- package/build/downloadHelpers.d.ts +2 -2
- package/build/downloadHelpers.js +83 -83
- package/build/esm/BaseCore.d.ts +88 -0
- package/build/esm/BaseCore.js +810 -0
- package/build/esm/BaseGateway.d.ts +49 -0
- package/build/esm/BaseGateway.js +476 -0
- package/build/esm/DataCache.d.ts +14 -0
- package/build/esm/DataCache.js +98 -0
- package/build/esm/Database.d.ts +32 -0
- package/build/esm/Database.js +192 -0
- package/build/esm/DbMigratorAndSeeder.d.ts +49 -0
- package/build/esm/DbMigratorAndSeeder.js +199 -0
- package/build/esm/RedisDataLoader.d.ts +41 -0
- package/build/esm/RedisDataLoader.js +237 -0
- package/build/esm/apiContext.d.ts +11 -0
- package/build/esm/apiContext.js +135 -0
- package/build/esm/baseController.d.ts +17 -0
- package/build/esm/baseController.js +60 -0
- package/build/esm/checkItHelpers.d.ts +3 -0
- package/build/esm/checkItHelpers.js +23 -0
- package/build/esm/commonValidators.d.ts +17 -0
- package/build/esm/commonValidators.js +273 -0
- package/build/esm/configBulder.d.ts +2 -0
- package/build/esm/configBulder.js +28 -0
- package/build/esm/constants.d.ts +30 -0
- package/build/esm/constants.js +69 -0
- package/build/esm/dbHelpers.d.ts +43 -0
- package/build/esm/dbHelpers.js +154 -0
- package/build/esm/dbSeeder.d.ts +2 -0
- package/build/esm/dbSeeder.js +53 -0
- package/build/esm/dbTableHelpers.d.ts +11 -0
- package/build/esm/dbTableHelpers.js +99 -0
- package/build/esm/downloadHelpers.d.ts +2 -0
- package/build/esm/downloadHelpers.js +76 -0
- package/build/esm/index.d.ts +21 -0
- package/build/esm/index.js +21 -0
- package/build/esm/interfaces/ApiContext.d.ts +24 -0
- package/build/esm/interfaces/ApiContext.js +1 -0
- package/build/esm/interfaces/ApiContextInterface.d.ts +12 -0
- package/build/esm/interfaces/ApiContextInterface.js +1 -0
- package/build/esm/interfaces/BaseCoreActionsInterface.d.ts +38 -0
- package/build/esm/interfaces/BaseCoreActionsInterface.js +1 -0
- package/build/esm/interfaces/BaseCoreDoAuthOnActionInterface.d.ts +12 -0
- package/build/esm/interfaces/BaseCoreDoAuthOnActionInterface.js +1 -0
- package/build/esm/interfaces/BaseCoreDoingWhatInterface.d.ts +13 -0
- package/build/esm/interfaces/BaseCoreDoingWhatInterface.js +1 -0
- package/build/esm/interfaces/BaseCorePropsInterface.d.ts +36 -0
- package/build/esm/interfaces/BaseCorePropsInterface.js +1 -0
- package/build/esm/interfaces/BaseCoreValidatorsInterface.d.ts +13 -0
- package/build/esm/interfaces/BaseCoreValidatorsInterface.js +1 -0
- package/build/esm/interfaces/BaseGatewayPropsInterface.d.ts +39 -0
- package/build/esm/interfaces/BaseGatewayPropsInterface.js +1 -0
- package/build/esm/interfaces/BuildConfigOptions.d.ts +6 -0
- package/build/esm/interfaces/BuildConfigOptions.js +1 -0
- package/build/esm/interfaces/BuildDefaultResolversOptions.d.ts +17 -0
- package/build/esm/interfaces/BuildDefaultResolversOptions.js +1 -0
- package/build/esm/interfaces/BuildListQueryArgsInterface.d.ts +6 -0
- package/build/esm/interfaces/BuildListQueryArgsInterface.js +1 -0
- package/build/esm/interfaces/CacheInterface.d.ts +15 -0
- package/build/esm/interfaces/CacheInterface.js +1 -0
- package/build/esm/interfaces/CommonValidatorArgs.d.ts +3 -0
- package/build/esm/interfaces/CommonValidatorArgs.js +1 -0
- package/build/esm/interfaces/CoreActionInterface.d.ts +14 -0
- package/build/esm/interfaces/CoreActionInterface.js +1 -0
- package/build/esm/interfaces/CreateUserTokenArgs.d.ts +11 -0
- package/build/esm/interfaces/CreateUserTokenArgs.js +1 -0
- package/build/esm/interfaces/CreateUserTokenResult.d.ts +4 -0
- package/build/esm/interfaces/CreateUserTokenResult.js +1 -0
- package/build/esm/interfaces/DatabaseOptions.d.ts +22 -0
- package/build/esm/interfaces/DatabaseOptions.js +1 -0
- package/build/esm/interfaces/DatabaseReadInterface.d.ts +14 -0
- package/build/esm/interfaces/DatabaseReadInterface.js +1 -0
- package/build/esm/interfaces/DbFieldAddDefaultInterface.d.ts +9 -0
- package/build/esm/interfaces/DbFieldAddDefaultInterface.js +1 -0
- package/build/esm/interfaces/DownloadFileArgs.d.ts +6 -0
- package/build/esm/interfaces/DownloadFileArgs.js +1 -0
- package/build/esm/interfaces/ExtractCredentialsArgs.d.ts +19 -0
- package/build/esm/interfaces/ExtractCredentialsArgs.js +1 -0
- package/build/esm/interfaces/JwtTokenSecretAndSettings.d.ts +5 -0
- package/build/esm/interfaces/JwtTokenSecretAndSettings.js +1 -0
- package/build/esm/interfaces/ListArgsInterface.d.ts +5 -0
- package/build/esm/interfaces/ListArgsInterface.js +1 -0
- package/build/esm/interfaces/LoaderInterface.d.ts +4 -0
- package/build/esm/interfaces/LoaderInterface.js +1 -0
- package/build/esm/interfaces/PaginationInterface.d.ts +7 -0
- package/build/esm/interfaces/PaginationInterface.js +1 -0
- package/build/esm/interfaces/SortItemInterface.d.ts +4 -0
- package/build/esm/interfaces/SortItemInterface.js +1 -0
- package/build/esm/interfaces/UserTokenParams.d.ts +8 -0
- package/build/esm/interfaces/UserTokenParams.js +1 -0
- package/build/esm/interfaces/VerifyUserTokenArgs.d.ts +13 -0
- package/build/esm/interfaces/VerifyUserTokenArgs.js +1 -0
- package/build/esm/interfaces/index.d.ts +28 -0
- package/build/esm/interfaces/index.js +27 -0
- package/build/esm/knexConfigBuilder.d.ts +1 -0
- package/build/esm/knexConfigBuilder.js +73 -0
- package/build/esm/passwordHelpers.d.ts +5 -0
- package/build/esm/passwordHelpers.js +22 -0
- package/build/esm/redisClient.d.ts +17 -0
- package/build/esm/redisClient.js +94 -0
- package/build/esm/resolversHelpers.d.ts +3 -0
- package/build/esm/resolversHelpers.js +59 -0
- package/build/esm/userToken.d.ts +8 -0
- package/build/esm/userToken.js +74 -0
- package/build/esm/utils.d.ts +2 -0
- package/build/esm/utils.js +13 -0
- package/build/index.d.ts +21 -21
- package/build/index.js +37 -37
- package/build/interfaces/ApiContext.d.ts +24 -24
- package/build/interfaces/ApiContext.js +2 -2
- package/build/interfaces/ApiContextInterface.d.ts +12 -12
- package/build/interfaces/ApiContextInterface.js +2 -2
- package/build/interfaces/BaseCoreActionsInterface.d.ts +38 -38
- package/build/interfaces/BaseCoreActionsInterface.js +2 -2
- package/build/interfaces/BaseCoreDoAuthOnActionInterface.d.ts +12 -12
- package/build/interfaces/BaseCoreDoAuthOnActionInterface.js +2 -2
- package/build/interfaces/BaseCoreDoingWhatInterface.d.ts +13 -13
- package/build/interfaces/BaseCoreDoingWhatInterface.js +2 -2
- package/build/interfaces/BaseCorePropsInterface.d.ts +36 -36
- package/build/interfaces/BaseCorePropsInterface.js +2 -2
- package/build/interfaces/BaseCoreValidatorsInterface.d.ts +13 -13
- package/build/interfaces/BaseCoreValidatorsInterface.js +2 -2
- package/build/interfaces/BaseGatewayPropsInterface.d.ts +39 -39
- package/build/interfaces/BaseGatewayPropsInterface.js +2 -2
- package/build/interfaces/BuildConfigOptions.d.ts +6 -6
- package/build/interfaces/BuildConfigOptions.js +2 -2
- package/build/interfaces/BuildDefaultResolversOptions.d.ts +17 -17
- package/build/interfaces/BuildDefaultResolversOptions.js +2 -2
- package/build/interfaces/BuildListQueryArgsInterface.d.ts +6 -6
- package/build/interfaces/BuildListQueryArgsInterface.js +2 -2
- package/build/interfaces/CacheInterface.d.ts +15 -15
- package/build/interfaces/CacheInterface.js +2 -2
- package/build/interfaces/CommonValidatorArgs.d.ts +3 -3
- package/build/interfaces/CommonValidatorArgs.js +2 -2
- package/build/interfaces/CoreActionInterface.d.ts +14 -14
- package/build/interfaces/CoreActionInterface.js +2 -2
- package/build/interfaces/CreateUserTokenArgs.d.ts +11 -11
- package/build/interfaces/CreateUserTokenArgs.js +2 -2
- package/build/interfaces/CreateUserTokenResult.d.ts +4 -4
- package/build/interfaces/CreateUserTokenResult.js +2 -2
- package/build/interfaces/DatabaseOptions.d.ts +22 -22
- package/build/interfaces/DatabaseOptions.js +2 -2
- package/build/interfaces/DatabaseReadInterface.d.ts +14 -14
- package/build/interfaces/DatabaseReadInterface.js +2 -2
- package/build/interfaces/DbFieldAddDefaultInterface.d.ts +9 -9
- package/build/interfaces/DbFieldAddDefaultInterface.js +2 -2
- package/build/interfaces/DownloadFileArgs.d.ts +6 -6
- package/build/interfaces/DownloadFileArgs.js +2 -2
- package/build/interfaces/ExtractCredentialsArgs.d.ts +19 -19
- package/build/interfaces/ExtractCredentialsArgs.js +2 -2
- package/build/interfaces/JwtTokenSecretAndSettings.d.ts +5 -5
- package/build/interfaces/JwtTokenSecretAndSettings.js +2 -2
- package/build/interfaces/ListArgsInterface.d.ts +5 -5
- package/build/interfaces/ListArgsInterface.js +2 -2
- package/build/interfaces/LoaderInterface.d.ts +4 -4
- package/build/interfaces/LoaderInterface.js +2 -2
- package/build/interfaces/PaginationInterface.d.ts +7 -7
- package/build/interfaces/PaginationInterface.js +2 -2
- package/build/interfaces/SortItemInterface.d.ts +4 -4
- package/build/interfaces/SortItemInterface.js +2 -2
- package/build/interfaces/UserTokenParams.d.ts +8 -8
- package/build/interfaces/UserTokenParams.js +2 -2
- package/build/interfaces/VerifyUserTokenArgs.d.ts +13 -13
- package/build/interfaces/VerifyUserTokenArgs.js +2 -2
- package/build/interfaces/index.d.ts +28 -28
- package/build/interfaces/index.js +43 -43
- package/build/knexConfigBuilder.d.ts +1 -1
- package/build/knexConfigBuilder.js +80 -80
- package/build/passwordHelpers.d.ts +5 -5
- package/build/passwordHelpers.js +30 -30
- package/build/redisClient.d.ts +17 -17
- package/build/redisClient.js +97 -97
- package/build/resolversHelpers.d.ts +3 -3
- package/build/resolversHelpers.js +62 -62
- package/build/userToken.d.ts +8 -8
- package/build/userToken.js +86 -86
- package/build/utils.d.ts +2 -2
- package/build/utils.js +21 -21
- package/package.json +79 -71
package/build/dbHelpers.js
CHANGED
|
@@ -1,166 +1,166 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.dbRunMigrations = exports.dbTransformSeedsToObject = exports.dbReset = exports.dbWipe = exports.dbClean = exports.dbListTables = exports.dbCleanTable = void 0;
|
|
16
|
-
const bluebird_1 = __importDefault(require("bluebird"));
|
|
17
|
-
const lodash_1 = require("lodash");
|
|
18
|
-
const SQLS = {
|
|
19
|
-
pg: {
|
|
20
|
-
listTablesSchema: (appSettings) => `
|
|
21
|
-
SELECT TABLE_NAME as table_name
|
|
22
|
-
FROM INFORMATION_SCHEMA.TABLES
|
|
23
|
-
WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
|
|
24
|
-
`,
|
|
25
|
-
listTables: () => `
|
|
26
|
-
SELECT TABLE_NAME as table_name
|
|
27
|
-
FROM INFORMATION_SCHEMA.TABLES
|
|
28
|
-
`,
|
|
29
|
-
dropTablesSchema: (appSettings) => `
|
|
30
|
-
SELECT 'DROP TABLE IF EXISTS ${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}.' || TABLE_NAME || ';' as cmd
|
|
31
|
-
FROM INFORMATION_SCHEMA.TABLES
|
|
32
|
-
WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
|
|
33
|
-
`,
|
|
34
|
-
dropTables: () => `
|
|
35
|
-
SELECT 'DROP TABLE IF EXISTS ' || TABLE_NAME || ';' as cmd
|
|
36
|
-
FROM INFORMATION_SCHEMA.TABLES';
|
|
37
|
-
`,
|
|
38
|
-
},
|
|
39
|
-
mssql: {
|
|
40
|
-
listTablesSchema: (appSettings) => `
|
|
41
|
-
SELECT TABLE_NAME as table_name
|
|
42
|
-
FROM INFORMATION_SCHEMA.TABLES
|
|
43
|
-
WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
|
|
44
|
-
`,
|
|
45
|
-
listTables: () => `
|
|
46
|
-
SELECT TABLE_NAME as table_name
|
|
47
|
-
FROM INFORMATION_SCHEMA.TABLES
|
|
48
|
-
`,
|
|
49
|
-
dropTablesSchema: (appSettings) => `
|
|
50
|
-
SELECT 'DROP TABLE IF EXISTS [${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}].[' + TABLE_NAME + '];' as cmd
|
|
51
|
-
FROM INFORMATION_SCHEMA.TABLES
|
|
52
|
-
WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
|
|
53
|
-
`,
|
|
54
|
-
dropTables: () => `
|
|
55
|
-
SELECT 'DROP TABLE IF EXISTS ' + TABLE_NAME + ';' as cmd
|
|
56
|
-
FROM INFORMATION_SCHEMA.TABLES';
|
|
57
|
-
`,
|
|
58
|
-
},
|
|
59
|
-
};
|
|
60
|
-
/**
|
|
61
|
-
* Removes all the records from provided table.
|
|
62
|
-
* If appSettings has cleanTable object with table names then
|
|
63
|
-
* it is used to add extra filters to clear the table to avoid for example users data
|
|
64
|
-
* @param table Name of table from which to remove all the records
|
|
65
|
-
* @param knexHandle Knex handle object with configured connection to DB
|
|
66
|
-
* @param appSettings settings with db related information
|
|
67
|
-
*/
|
|
68
|
-
const dbCleanTable = (table, knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
|
|
69
|
-
var _a;
|
|
70
|
-
const query = knexHandle(table).withSchema(appSettings.dbSchema).delete();
|
|
71
|
-
const cleanTableFilters = (_a = appSettings === null || appSettings === void 0 ? void 0 : appSettings.cleanTableFilters) === null || _a === void 0 ? void 0 : _a[table];
|
|
72
|
-
if (cleanTableFilters != undefined) {
|
|
73
|
-
if (Array.isArray(cleanTableFilters.where)) {
|
|
74
|
-
cleanTableFilters.where.forEach((whereItem) => {
|
|
75
|
-
query.where(whereItem.fieldName, whereItem.operand, whereItem.value);
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
yield query;
|
|
80
|
-
});
|
|
81
|
-
exports.dbCleanTable = dbCleanTable;
|
|
82
|
-
/**
|
|
83
|
-
* Returns a list of tables in db schedm specified in settings
|
|
84
|
-
* @param knexHandle Knex handle object with configured connection to DB
|
|
85
|
-
* @param appSettings settings with db related information
|
|
86
|
-
* @returns arrays of table names in the provided db schema
|
|
87
|
-
*/
|
|
88
|
-
const dbListTables = (knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
|
|
89
|
-
const skipTables = (appSettings || {}).skipTables || [];
|
|
90
|
-
let query = SQLS[appSettings.dbType].listTablesSchema(appSettings);
|
|
91
|
-
if (!appSettings.dbSchema) {
|
|
92
|
-
query = SQLS[appSettings.dbType].listTables(appSettings);
|
|
93
|
-
}
|
|
94
|
-
const tables = yield knexHandle.raw(query);
|
|
95
|
-
return (tables.rows || [])
|
|
96
|
-
.map((row) => row.table_name)
|
|
97
|
-
.filter((table) => skipTables.indexOf(table) === -1);
|
|
98
|
-
});
|
|
99
|
-
exports.dbListTables = dbListTables;
|
|
100
|
-
/**
|
|
101
|
-
* Removes all the records from provided list of tables
|
|
102
|
-
* @param tablesToClean array of table names
|
|
103
|
-
* @param knexHandle Knex handle object with configured connection to DB
|
|
104
|
-
* @param appSettings settings with db related information
|
|
105
|
-
* @returns promise
|
|
106
|
-
*/
|
|
107
|
-
const dbClean = (tablesToClean, knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
|
|
108
|
-
for (let idx = 0, len = tablesToClean.length; idx < len; ++idx) {
|
|
109
|
-
yield dbCleanTable(tablesToClean[idx], knexHandle, appSettings);
|
|
110
|
-
}
|
|
111
|
-
return new bluebird_1.default((resolve) => {
|
|
112
|
-
resolve(tablesToClean);
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
exports.dbClean = dbClean;
|
|
116
|
-
/**
|
|
117
|
-
* Selects all the tables in the schema and drops them.
|
|
118
|
-
* This approach does not work in case we have constraints.
|
|
119
|
-
* TODO: Remove all constraints and then drop tables.
|
|
120
|
-
*/
|
|
121
|
-
const dbWipe = (knexHandle, appSettings) => {
|
|
122
|
-
let query = SQLS[appSettings.dbType].dropTablesSchema(appSettings);
|
|
123
|
-
if (!appSettings.dbSchema) {
|
|
124
|
-
query = SQLS[appSettings.dbType].dropTables(appSettings);
|
|
125
|
-
}
|
|
126
|
-
return knexHandle
|
|
127
|
-
.raw(query)
|
|
128
|
-
.then((result) => result.rows.map((r) => r.cmd))
|
|
129
|
-
.then((commands) => bluebird_1.default.each(commands, (cmd) => knexHandle.raw(cmd)));
|
|
130
|
-
};
|
|
131
|
-
exports.dbWipe = dbWipe;
|
|
132
|
-
/**
|
|
133
|
-
* Drop all tables, run migrations, then seed
|
|
134
|
-
*/
|
|
135
|
-
const dbReset = (knexHandle, appSettings) => {
|
|
136
|
-
return dbWipe(knexHandle, appSettings)
|
|
137
|
-
.then(() => dbRunMigrations(knexHandle, appSettings))
|
|
138
|
-
.catch((error) => console.log('An exception has occurred on resetting database: ', error));
|
|
139
|
-
};
|
|
140
|
-
exports.dbReset = dbReset;
|
|
141
|
-
/**
|
|
142
|
-
* Go from array of objects which contain keys of object names which have an array value of seeded objects
|
|
143
|
-
* to a single object with keys of object names and arrays of seeded objects
|
|
144
|
-
* ie from [{table1: [], table2: [] }, { table3: [] }]
|
|
145
|
-
* to { table1: [], table2: [], table3: []}
|
|
146
|
-
*/
|
|
147
|
-
const dbTransformSeedsToObject = (generatedSeedArrays) => (0, lodash_1.reduce)(generatedSeedArrays, (acc, cur) => {
|
|
148
|
-
for (const key in cur) {
|
|
149
|
-
acc[key] = cur[key];
|
|
150
|
-
}
|
|
151
|
-
return acc;
|
|
152
|
-
}, {});
|
|
153
|
-
exports.dbTransformSeedsToObject = dbTransformSeedsToObject;
|
|
154
|
-
const dbRunMigrations = (knexHandle, appSettings) => {
|
|
155
|
-
return knexHandle.migrate
|
|
156
|
-
.latest()
|
|
157
|
-
.then((result) => __awaiter(void 0, void 0, void 0, function* () {
|
|
158
|
-
if (appSettings && appSettings.runDbSeed && result[1].length > 0) {
|
|
159
|
-
yield knexHandle.seed.run();
|
|
160
|
-
}
|
|
161
|
-
console.log('Ran', result[1].length, 'migration(s)');
|
|
162
|
-
return knexHandle.migrate.currentVersion();
|
|
163
|
-
}))
|
|
164
|
-
.then((version) => console.log('Current version of database: ', version));
|
|
165
|
-
};
|
|
166
|
-
exports.dbRunMigrations = dbRunMigrations;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.dbRunMigrations = exports.dbTransformSeedsToObject = exports.dbReset = exports.dbWipe = exports.dbClean = exports.dbListTables = exports.dbCleanTable = void 0;
|
|
16
|
+
const bluebird_1 = __importDefault(require("bluebird"));
|
|
17
|
+
const lodash_1 = require("lodash");
|
|
18
|
+
const SQLS = {
|
|
19
|
+
pg: {
|
|
20
|
+
listTablesSchema: (appSettings) => `
|
|
21
|
+
SELECT TABLE_NAME as table_name
|
|
22
|
+
FROM INFORMATION_SCHEMA.TABLES
|
|
23
|
+
WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
|
|
24
|
+
`,
|
|
25
|
+
listTables: () => `
|
|
26
|
+
SELECT TABLE_NAME as table_name
|
|
27
|
+
FROM INFORMATION_SCHEMA.TABLES
|
|
28
|
+
`,
|
|
29
|
+
dropTablesSchema: (appSettings) => `
|
|
30
|
+
SELECT 'DROP TABLE IF EXISTS ${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}.' || TABLE_NAME || ';' as cmd
|
|
31
|
+
FROM INFORMATION_SCHEMA.TABLES
|
|
32
|
+
WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
|
|
33
|
+
`,
|
|
34
|
+
dropTables: () => `
|
|
35
|
+
SELECT 'DROP TABLE IF EXISTS ' || TABLE_NAME || ';' as cmd
|
|
36
|
+
FROM INFORMATION_SCHEMA.TABLES';
|
|
37
|
+
`,
|
|
38
|
+
},
|
|
39
|
+
mssql: {
|
|
40
|
+
listTablesSchema: (appSettings) => `
|
|
41
|
+
SELECT TABLE_NAME as table_name
|
|
42
|
+
FROM INFORMATION_SCHEMA.TABLES
|
|
43
|
+
WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
|
|
44
|
+
`,
|
|
45
|
+
listTables: () => `
|
|
46
|
+
SELECT TABLE_NAME as table_name
|
|
47
|
+
FROM INFORMATION_SCHEMA.TABLES
|
|
48
|
+
`,
|
|
49
|
+
dropTablesSchema: (appSettings) => `
|
|
50
|
+
SELECT 'DROP TABLE IF EXISTS [${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}].[' + TABLE_NAME + '];' as cmd
|
|
51
|
+
FROM INFORMATION_SCHEMA.TABLES
|
|
52
|
+
WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
|
|
53
|
+
`,
|
|
54
|
+
dropTables: () => `
|
|
55
|
+
SELECT 'DROP TABLE IF EXISTS ' + TABLE_NAME + ';' as cmd
|
|
56
|
+
FROM INFORMATION_SCHEMA.TABLES';
|
|
57
|
+
`,
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Removes all the records from provided table.
|
|
62
|
+
* If appSettings has cleanTable object with table names then
|
|
63
|
+
* it is used to add extra filters to clear the table to avoid for example users data
|
|
64
|
+
* @param table Name of table from which to remove all the records
|
|
65
|
+
* @param knexHandle Knex handle object with configured connection to DB
|
|
66
|
+
* @param appSettings settings with db related information
|
|
67
|
+
*/
|
|
68
|
+
const dbCleanTable = (table, knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
|
|
69
|
+
var _a;
|
|
70
|
+
const query = knexHandle(table).withSchema(appSettings.dbSchema).delete();
|
|
71
|
+
const cleanTableFilters = (_a = appSettings === null || appSettings === void 0 ? void 0 : appSettings.cleanTableFilters) === null || _a === void 0 ? void 0 : _a[table];
|
|
72
|
+
if (cleanTableFilters != undefined) {
|
|
73
|
+
if (Array.isArray(cleanTableFilters.where)) {
|
|
74
|
+
cleanTableFilters.where.forEach((whereItem) => {
|
|
75
|
+
query.where(whereItem.fieldName, whereItem.operand, whereItem.value);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
yield query;
|
|
80
|
+
});
|
|
81
|
+
exports.dbCleanTable = dbCleanTable;
|
|
82
|
+
/**
|
|
83
|
+
* Returns a list of tables in db schedm specified in settings
|
|
84
|
+
* @param knexHandle Knex handle object with configured connection to DB
|
|
85
|
+
* @param appSettings settings with db related information
|
|
86
|
+
* @returns arrays of table names in the provided db schema
|
|
87
|
+
*/
|
|
88
|
+
const dbListTables = (knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
|
|
89
|
+
const skipTables = (appSettings || {}).skipTables || [];
|
|
90
|
+
let query = SQLS[appSettings.dbType].listTablesSchema(appSettings);
|
|
91
|
+
if (!appSettings.dbSchema) {
|
|
92
|
+
query = SQLS[appSettings.dbType].listTables(appSettings);
|
|
93
|
+
}
|
|
94
|
+
const tables = yield knexHandle.raw(query);
|
|
95
|
+
return (tables.rows || [])
|
|
96
|
+
.map((row) => row.table_name)
|
|
97
|
+
.filter((table) => skipTables.indexOf(table) === -1);
|
|
98
|
+
});
|
|
99
|
+
exports.dbListTables = dbListTables;
|
|
100
|
+
/**
|
|
101
|
+
* Removes all the records from provided list of tables
|
|
102
|
+
* @param tablesToClean array of table names
|
|
103
|
+
* @param knexHandle Knex handle object with configured connection to DB
|
|
104
|
+
* @param appSettings settings with db related information
|
|
105
|
+
* @returns promise
|
|
106
|
+
*/
|
|
107
|
+
const dbClean = (tablesToClean, knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
|
|
108
|
+
for (let idx = 0, len = tablesToClean.length; idx < len; ++idx) {
|
|
109
|
+
yield dbCleanTable(tablesToClean[idx], knexHandle, appSettings);
|
|
110
|
+
}
|
|
111
|
+
return new bluebird_1.default((resolve) => {
|
|
112
|
+
resolve(tablesToClean);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
exports.dbClean = dbClean;
|
|
116
|
+
/**
|
|
117
|
+
* Selects all the tables in the schema and drops them.
|
|
118
|
+
* This approach does not work in case we have constraints.
|
|
119
|
+
* TODO: Remove all constraints and then drop tables.
|
|
120
|
+
*/
|
|
121
|
+
const dbWipe = (knexHandle, appSettings) => {
|
|
122
|
+
let query = SQLS[appSettings.dbType].dropTablesSchema(appSettings);
|
|
123
|
+
if (!appSettings.dbSchema) {
|
|
124
|
+
query = SQLS[appSettings.dbType].dropTables(appSettings);
|
|
125
|
+
}
|
|
126
|
+
return knexHandle
|
|
127
|
+
.raw(query)
|
|
128
|
+
.then((result) => result.rows.map((r) => r.cmd))
|
|
129
|
+
.then((commands) => bluebird_1.default.each(commands, (cmd) => knexHandle.raw(cmd)));
|
|
130
|
+
};
|
|
131
|
+
exports.dbWipe = dbWipe;
|
|
132
|
+
/**
|
|
133
|
+
* Drop all tables, run migrations, then seed
|
|
134
|
+
*/
|
|
135
|
+
const dbReset = (knexHandle, appSettings) => {
|
|
136
|
+
return dbWipe(knexHandle, appSettings)
|
|
137
|
+
.then(() => dbRunMigrations(knexHandle, appSettings))
|
|
138
|
+
.catch((error) => console.log('An exception has occurred on resetting database: ', error));
|
|
139
|
+
};
|
|
140
|
+
exports.dbReset = dbReset;
|
|
141
|
+
/**
|
|
142
|
+
* Go from array of objects which contain keys of object names which have an array value of seeded objects
|
|
143
|
+
* to a single object with keys of object names and arrays of seeded objects
|
|
144
|
+
* ie from [{table1: [], table2: [] }, { table3: [] }]
|
|
145
|
+
* to { table1: [], table2: [], table3: []}
|
|
146
|
+
*/
|
|
147
|
+
const dbTransformSeedsToObject = (generatedSeedArrays) => (0, lodash_1.reduce)(generatedSeedArrays, (acc, cur) => {
|
|
148
|
+
for (const key in cur) {
|
|
149
|
+
acc[key] = cur[key];
|
|
150
|
+
}
|
|
151
|
+
return acc;
|
|
152
|
+
}, {});
|
|
153
|
+
exports.dbTransformSeedsToObject = dbTransformSeedsToObject;
|
|
154
|
+
const dbRunMigrations = (knexHandle, appSettings) => {
|
|
155
|
+
return knexHandle.migrate
|
|
156
|
+
.latest()
|
|
157
|
+
.then((result) => __awaiter(void 0, void 0, void 0, function* () {
|
|
158
|
+
if (appSettings && appSettings.runDbSeed && result[1].length > 0) {
|
|
159
|
+
yield knexHandle.seed.run();
|
|
160
|
+
}
|
|
161
|
+
console.log('Ran', result[1].length, 'migration(s)');
|
|
162
|
+
return knexHandle.migrate.currentVersion();
|
|
163
|
+
}))
|
|
164
|
+
.then((version) => console.log('Current version of database: ', version));
|
|
165
|
+
};
|
|
166
|
+
exports.dbRunMigrations = dbRunMigrations;
|
package/build/dbSeeder.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const dbSeed: (dirname: string, seedFiles: any, knexHandle: any, config: any) => Promise<any>;
|
|
2
|
-
export { dbSeed };
|
|
1
|
+
declare const dbSeed: (dirname: string, seedFiles: any, knexHandle: any, config: any) => Promise<any>;
|
|
2
|
+
export { dbSeed };
|
package/build/dbSeeder.js
CHANGED
|
@@ -1,82 +1,92 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return
|
|
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
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return seedGroups
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
45
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.dbSeed = void 0;
|
|
49
|
+
const path_1 = __importDefault(require("path"));
|
|
50
|
+
const lodash_1 = require("lodash");
|
|
51
|
+
const dbHelpers_1 = require("./dbHelpers");
|
|
52
|
+
const generateSeeds = (dirname, seedFiles) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
let seedGroups = [];
|
|
54
|
+
const seedFileNames = (0, lodash_1.flatten)(seedFiles);
|
|
55
|
+
const seedFnExports = yield Promise.all((0, lodash_1.map)(seedFileNames, (filename) => Promise.resolve(`${path_1.default.join(dirname, `./objects/${filename}.js`)}`).then(s => __importStar(require(s)))));
|
|
56
|
+
const seedFns = (0, lodash_1.map)(seedFnExports, 'default');
|
|
57
|
+
const seedFnsMap = {};
|
|
58
|
+
for (let i = 0; i < seedFileNames.length; i++) {
|
|
59
|
+
const seedFileName = seedFileNames[i];
|
|
60
|
+
seedFnsMap[seedFileName] = seedFns[i];
|
|
61
|
+
}
|
|
62
|
+
seedFiles.reduce((seeds, fileGroup) => {
|
|
63
|
+
seedGroups = seedGroups.concat(fileGroup.reduce((group, filename) => {
|
|
64
|
+
const seedFn = seedFnsMap[filename];
|
|
65
|
+
const seed = seedFn(seeds);
|
|
66
|
+
const filenameCamel = (0, lodash_1.camelCase)(filename);
|
|
67
|
+
seeds[filenameCamel] = seed;
|
|
68
|
+
return Object.assign({ [filenameCamel]: seed }, group);
|
|
69
|
+
}, {}));
|
|
70
|
+
return seeds;
|
|
71
|
+
}, {});
|
|
72
|
+
return seedGroups;
|
|
73
|
+
});
|
|
74
|
+
const removeSeedsFromDb = (seedFiles, knexHandle, config) => {
|
|
75
|
+
const tablesToReset = (0, lodash_1.flatten)(seedFiles).reverse();
|
|
76
|
+
console.log('Deleting records from tables: ', tablesToReset);
|
|
77
|
+
return (0, dbHelpers_1.dbClean)(tablesToReset, knexHandle, config);
|
|
78
|
+
};
|
|
79
|
+
const insertRecords = (seedGroups, knexHandle, config) => {
|
|
80
|
+
const { dbSchema } = config || {};
|
|
81
|
+
console.log('Inserting seeds records tables tables...');
|
|
82
|
+
return seedGroups.reduce((previousGroup, group) => __awaiter(void 0, void 0, void 0, function* () {
|
|
83
|
+
yield previousGroup;
|
|
84
|
+
return Promise.all(Object.keys(group).map((table) => knexHandle(table).withSchema(dbSchema).insert(group[table])));
|
|
85
|
+
}), Promise.resolve());
|
|
86
|
+
};
|
|
87
|
+
const dbSeed = (dirname, seedFiles, knexHandle, config) => __awaiter(void 0, void 0, void 0, function* () {
|
|
88
|
+
const groups = yield generateSeeds(dirname, seedFiles);
|
|
89
|
+
yield removeSeedsFromDb(seedFiles, knexHandle, config);
|
|
90
|
+
return yield insertRecords(groups, knexHandle, config);
|
|
91
|
+
});
|
|
92
|
+
exports.dbSeed = dbSeed;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { DbFieldAddDefaultInterface } from './interfaces';
|
|
2
|
-
export declare const dbFieldAddDefaults: (table: any, opt?: DbFieldAddDefaultInterface) => void;
|
|
3
|
-
export declare const addOrderNo: (table: any, opt?: any) => any;
|
|
4
|
-
export declare const addAmount: (table: any, fieldName: string) => any;
|
|
5
|
-
export declare const addSpaceId: (table: any, opt?: any) => any;
|
|
6
|
-
export declare const addUserId: (table: any, opt?: any) => any;
|
|
7
|
-
export declare const addAccountId: (table: any, opt?: any) => any;
|
|
8
|
-
export declare const addLang: (table: any, opt?: any) => any;
|
|
9
|
-
export declare const defaultFields: (opt?: any) => {
|
|
10
|
-
[x: number]: string;
|
|
11
|
-
};
|
|
1
|
+
import { DbFieldAddDefaultInterface } from './interfaces';
|
|
2
|
+
export declare const dbFieldAddDefaults: (table: any, opt?: DbFieldAddDefaultInterface) => void;
|
|
3
|
+
export declare const addOrderNo: (table: any, opt?: any) => any;
|
|
4
|
+
export declare const addAmount: (table: any, fieldName: string) => any;
|
|
5
|
+
export declare const addSpaceId: (table: any, opt?: any) => any;
|
|
6
|
+
export declare const addUserId: (table: any, opt?: any) => any;
|
|
7
|
+
export declare const addAccountId: (table: any, opt?: any) => any;
|
|
8
|
+
export declare const addLang: (table: any, opt?: any) => any;
|
|
9
|
+
export declare const defaultFields: (opt?: any) => {
|
|
10
|
+
[x: number]: string;
|
|
11
|
+
};
|