@sdflc/backend-helpers 1.0.78 → 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 -603
- package/build/BaseGateway.js +331 -379
- package/build/DataCache.js +16 -19
- package/build/Database.js +55 -74
- package/build/DbMigratorAndSeeder.js +88 -114
- package/build/RedisDataLoader.js +153 -189
- package/build/apiContext.js +35 -33
- package/build/baseController.js +9 -16
- package/build/commonValidators.js +22 -37
- 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.d.ts +88 -0
- package/build/esm/BaseCore.js +706 -0
- package/build/esm/BaseGateway.d.ts +49 -0
- package/build/esm/BaseGateway.js +430 -0
- package/build/esm/DataCache.d.ts +14 -0
- package/build/esm/DataCache.js +95 -0
- package/build/esm/Database.d.ts +32 -0
- package/build/esm/Database.js +173 -0
- package/build/esm/DbMigratorAndSeeder.d.ts +49 -0
- package/build/esm/DbMigratorAndSeeder.js +173 -0
- package/build/esm/RedisDataLoader.d.ts +41 -0
- package/build/esm/RedisDataLoader.js +206 -0
- package/build/esm/apiContext.d.ts +11 -0
- package/build/esm/apiContext.js +137 -0
- package/build/esm/baseController.d.ts +17 -0
- package/build/esm/baseController.js +53 -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 +264 -0
- package/build/esm/configBulder.d.ts +2 -0
- package/build/esm/configBulder.js +32 -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 +144 -0
- package/build/esm/dbSeeder.d.ts +2 -0
- package/build/esm/dbSeeder.js +44 -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 +66 -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 +83 -0
- package/build/esm/passwordHelpers.d.ts +5 -0
- package/build/esm/passwordHelpers.js +13 -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 +66 -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/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 +13 -6
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { camelCase } from 'lodash';
|
|
2
|
+
import knex from 'knex';
|
|
3
|
+
import { buildKey as defaultBuildKey, Logger, LoggerLevels } from '@sdflc/utils';
|
|
4
|
+
const knexHandles = {};
|
|
5
|
+
const buildKey = (args) => {
|
|
6
|
+
const { connection, dbSchema } = args || {};
|
|
7
|
+
const { host, port, user, password, database } = connection || {};
|
|
8
|
+
const parts = [host, port, user, password, database, dbSchema];
|
|
9
|
+
return parts.join('|');
|
|
10
|
+
};
|
|
11
|
+
class Database {
|
|
12
|
+
trx = null;
|
|
13
|
+
knexHandle = null;
|
|
14
|
+
dbSchema = '';
|
|
15
|
+
logger = new Logger({ level: LoggerLevels.ERROR });
|
|
16
|
+
constructor(props) {
|
|
17
|
+
if (!props.knexConfig) {
|
|
18
|
+
throw new Error('The Database class expects to have `knexConfig` in its constructor props');
|
|
19
|
+
}
|
|
20
|
+
if (props.logger instanceof Logger) {
|
|
21
|
+
this.logger = props.logger;
|
|
22
|
+
}
|
|
23
|
+
const cfgKey = buildKey({ connection: props.knexConfig.connection, dbSchema: props.dbSchema });
|
|
24
|
+
if (!knexHandles[cfgKey]) {
|
|
25
|
+
this.logger.debug(`Create a new KnexJs configuration`);
|
|
26
|
+
knexHandles[cfgKey] = knex(props.knexConfig);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
this.logger.debug(`Reusing existing KnexJs configuration`);
|
|
30
|
+
}
|
|
31
|
+
this.knexHandle = knexHandles[cfgKey];
|
|
32
|
+
this.dbSchema = props.dbSchema || '';
|
|
33
|
+
this.trx = props.trx;
|
|
34
|
+
}
|
|
35
|
+
destroy() {
|
|
36
|
+
if (this.knexHandle) {
|
|
37
|
+
this.knexHandle.destroy();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
isDbError(ex) {
|
|
41
|
+
const { code } = ex || {};
|
|
42
|
+
switch (code) {
|
|
43
|
+
default:
|
|
44
|
+
return [false, ''];
|
|
45
|
+
case '23503':
|
|
46
|
+
// ex.detail: 'Key (message_id)=(78403d9e-4e95-4091-8136-ae99ddbfa3ca) is not present in table "message".',
|
|
47
|
+
// ex.constraint: 'message_recipient_status_message_id_foreign',
|
|
48
|
+
return [true, `Reference does not exist`];
|
|
49
|
+
case '23505':
|
|
50
|
+
return [true, `Such item already exists`];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
createTransaction(trx) {
|
|
54
|
+
return new Promise((resolve) => {
|
|
55
|
+
return (trx ? trx : this.knexHandle).transaction(resolve);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
getSchema() {
|
|
59
|
+
return this.dbSchema;
|
|
60
|
+
}
|
|
61
|
+
getBuilder(arg) {
|
|
62
|
+
return this.trx ? this.trx(arg).withSchema(this.getSchema()) : this.knexHandle(arg).withSchema(this.getSchema());
|
|
63
|
+
}
|
|
64
|
+
raw(query, bindings) {
|
|
65
|
+
return this.knexHandle.raw(query, bindings);
|
|
66
|
+
}
|
|
67
|
+
async runRawQuery(query, bindings) {
|
|
68
|
+
const rows = await (this.trx ? this.trx : this.knexHandle).raw(query, bindings);
|
|
69
|
+
return rows;
|
|
70
|
+
}
|
|
71
|
+
async startTransaction() {
|
|
72
|
+
const trx = await this.createTransaction(this.trx);
|
|
73
|
+
this.trx = trx;
|
|
74
|
+
return trx;
|
|
75
|
+
}
|
|
76
|
+
setTransaction(trx) {
|
|
77
|
+
this.trx = trx;
|
|
78
|
+
}
|
|
79
|
+
async commitTransaction() {
|
|
80
|
+
if (this.trx) {
|
|
81
|
+
await this.trx.commit();
|
|
82
|
+
this.trx = undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async rollbackTransaction() {
|
|
86
|
+
if (this.trx) {
|
|
87
|
+
try {
|
|
88
|
+
await this.trx.rollback();
|
|
89
|
+
}
|
|
90
|
+
catch (ex) {
|
|
91
|
+
// Do nothing here
|
|
92
|
+
this.logger.error(`Failed to rollback a transaction:`, ex.message);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
this.trx = undefined;
|
|
96
|
+
}
|
|
97
|
+
doCalculated(item, calculatedFields) {
|
|
98
|
+
calculatedFields.forEach((calcField) => {
|
|
99
|
+
if (calcField.name) {
|
|
100
|
+
item[calcField.name] = typeof calcField.value === 'function' ? calcField.value(item) : calcField.value;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
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)]);
|
|
121
|
+
if (Array.isArray(calculated) && calculated.length > 0) {
|
|
122
|
+
this.doCalculated(item, calculated);
|
|
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
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return dbResults;
|
|
136
|
+
}
|
|
137
|
+
createSortBy(sortBy, defaultSortBy) {
|
|
138
|
+
if (!sortBy) {
|
|
139
|
+
if (Array.isArray(defaultSortBy)) {
|
|
140
|
+
return this.createSortBy(defaultSortBy);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return sortBy.reduce((acc, item) => {
|
|
147
|
+
acc.push({
|
|
148
|
+
column: item.name,
|
|
149
|
+
order: item.order,
|
|
150
|
+
});
|
|
151
|
+
return acc;
|
|
152
|
+
}, []);
|
|
153
|
+
}
|
|
154
|
+
createLimitOffset(page, pageSize) {
|
|
155
|
+
const limit = isNaN(pageSize) || pageSize == null ? undefined : Number(pageSize);
|
|
156
|
+
const pageToUse = isNaN(page) || page < 1 ? 1 : Number(page);
|
|
157
|
+
return {
|
|
158
|
+
limit,
|
|
159
|
+
offset: limit ? (pageToUse - 1) * limit : 0,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
createLimitOffsetSortBy(pagination, defaultSortBy) {
|
|
163
|
+
const { page, pageSize, sortBy } = pagination || {};
|
|
164
|
+
const { limit, offset } = this.createLimitOffset(page, pageSize);
|
|
165
|
+
const sortByToUse = this.createSortBy(sortBy, defaultSortBy);
|
|
166
|
+
return {
|
|
167
|
+
limit,
|
|
168
|
+
offset,
|
|
169
|
+
sortBy: sortByToUse,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
export { Database };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
declare class DbMigratorAndSeeder {
|
|
2
|
+
private knexHandle;
|
|
3
|
+
private config;
|
|
4
|
+
private logger;
|
|
5
|
+
constructor(knexHandle: any, config?: any);
|
|
6
|
+
runMigrations(): Promise<void>;
|
|
7
|
+
/**
|
|
8
|
+
* Go from array of objects which contain keys of object names which have an array value of seeded objects
|
|
9
|
+
* to a single object with keys of object names and arrays of seeded objects
|
|
10
|
+
* ie from [{table1: [], table2: [] }, { table3: [] }]
|
|
11
|
+
* to { table1: [], table2: [], table3: []}
|
|
12
|
+
*/
|
|
13
|
+
transformSeedsToObject(generatedSeedArrays: any[]): any;
|
|
14
|
+
/**
|
|
15
|
+
* Drop all tables, run migrations, then seed the tables
|
|
16
|
+
*/
|
|
17
|
+
resetDb(): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Selects all the tables in the schema and drops them.
|
|
20
|
+
* This approach does not work in case we have constraints.
|
|
21
|
+
* TODO: Remove all constraints and then drop tables.
|
|
22
|
+
*/
|
|
23
|
+
wipeDb(): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Removes all the records from provided list of tables
|
|
26
|
+
* @param tablesToClean array of table names
|
|
27
|
+
* @param knexHandle Knex handle object with configured connection to DB
|
|
28
|
+
* @param appSettings settings with db related information
|
|
29
|
+
* @returns promise
|
|
30
|
+
*/
|
|
31
|
+
cleanDb(tablesToClean: any[]): Promise<unknown>;
|
|
32
|
+
/**
|
|
33
|
+
* Removes all the records from provided table.
|
|
34
|
+
* If appSettings has cleanTable object with table names then
|
|
35
|
+
* it is used to add extra filters to clear the table to avoid for example users data
|
|
36
|
+
* @param table Name of table from which to remove all the records
|
|
37
|
+
*/
|
|
38
|
+
cleanDbTable(table: string): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Returns a list of tables in db schedm specified in settings
|
|
41
|
+
* @returns arrays of table names in the provided db schema
|
|
42
|
+
*/
|
|
43
|
+
listDbTables(): Promise<any>;
|
|
44
|
+
generateSeeds(dirname: string, seedFiles: any): Promise<any>;
|
|
45
|
+
removeSeedsFromDb(seedFiles: any): Promise<unknown>;
|
|
46
|
+
insertRecords(seedGroups: any): Promise<any>;
|
|
47
|
+
runSeeds(dirname: string, seedFiles: any): Promise<any>;
|
|
48
|
+
}
|
|
49
|
+
export { DbMigratorAndSeeder };
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import BPromise from 'bluebird';
|
|
3
|
+
import { reduce, camelCase, flatten, map } from 'lodash';
|
|
4
|
+
import { Logger, LoggerLevels } from '@sdflc/utils';
|
|
5
|
+
const SQLS = {
|
|
6
|
+
pg: {
|
|
7
|
+
listTablesSchema: (appSettings) => `
|
|
8
|
+
SELECT TABLE_NAME as table_name
|
|
9
|
+
FROM INFORMATION_SCHEMA.TABLES
|
|
10
|
+
WHERE TABLE_SCHEMA = '${appSettings?.dbSchema}';
|
|
11
|
+
`,
|
|
12
|
+
listTables: () => `
|
|
13
|
+
SELECT TABLE_NAME as table_name
|
|
14
|
+
FROM INFORMATION_SCHEMA.TABLES
|
|
15
|
+
`,
|
|
16
|
+
dropTablesSchema: (appSettings) => `
|
|
17
|
+
SELECT 'DROP TABLE IF EXISTS ${appSettings?.dbSchema}.' || TABLE_NAME || ';' as cmd
|
|
18
|
+
FROM INFORMATION_SCHEMA.TABLES
|
|
19
|
+
WHERE TABLE_SCHEMA = '${appSettings?.dbSchema}';
|
|
20
|
+
`,
|
|
21
|
+
dropTables: () => `
|
|
22
|
+
SELECT 'DROP TABLE IF EXISTS ' || TABLE_NAME || ';' as cmd
|
|
23
|
+
FROM INFORMATION_SCHEMA.TABLES';
|
|
24
|
+
`,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
class DbMigratorAndSeeder {
|
|
28
|
+
knexHandle;
|
|
29
|
+
config;
|
|
30
|
+
logger;
|
|
31
|
+
constructor(knexHandle, config) {
|
|
32
|
+
this.knexHandle = knexHandle;
|
|
33
|
+
this.config = config;
|
|
34
|
+
this.logger = config?.logger instanceof Logger ? config.logger : new Logger({ level: LoggerLevels.LOG });
|
|
35
|
+
}
|
|
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}`);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Go from array of objects which contain keys of object names which have an array value of seeded objects
|
|
47
|
+
* to a single object with keys of object names and arrays of seeded objects
|
|
48
|
+
* ie from [{table1: [], table2: [] }, { table3: [] }]
|
|
49
|
+
* to { table1: [], table2: [], table3: []}
|
|
50
|
+
*/
|
|
51
|
+
transformSeedsToObject(generatedSeedArrays) {
|
|
52
|
+
return reduce(generatedSeedArrays, (acc, cur) => {
|
|
53
|
+
for (const key in cur) {
|
|
54
|
+
acc[key] = cur[key];
|
|
55
|
+
}
|
|
56
|
+
return acc;
|
|
57
|
+
}, {});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Drop all tables, run migrations, then seed the tables
|
|
61
|
+
*/
|
|
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
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Selects all the tables in the schema and drops them.
|
|
73
|
+
* This approach does not work in case we have constraints.
|
|
74
|
+
* TODO: Remove all constraints and then drop tables.
|
|
75
|
+
*/
|
|
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));
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Removes all the records from provided list of tables
|
|
86
|
+
* @param tablesToClean array of table names
|
|
87
|
+
* @param knexHandle Knex handle object with configured connection to DB
|
|
88
|
+
* @param appSettings settings with db related information
|
|
89
|
+
* @returns promise
|
|
90
|
+
*/
|
|
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);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Removes all the records from provided table.
|
|
101
|
+
* If appSettings has cleanTable object with table names then
|
|
102
|
+
* it is used to add extra filters to clear the table to avoid for example users data
|
|
103
|
+
* @param table Name of table from which to remove all the records
|
|
104
|
+
*/
|
|
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
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
await query;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Returns a list of tables in db schedm specified in settings
|
|
121
|
+
* @returns arrays of table names in the provided db schema
|
|
122
|
+
*/
|
|
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);
|
|
132
|
+
}
|
|
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;
|
|
154
|
+
}
|
|
155
|
+
removeSeedsFromDb(seedFiles) {
|
|
156
|
+
const tablesToReset = flatten(seedFiles).reverse();
|
|
157
|
+
this.logger.log('Deleting records from tables: ', tablesToReset);
|
|
158
|
+
return this.cleanDb(tablesToReset);
|
|
159
|
+
}
|
|
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());
|
|
166
|
+
}
|
|
167
|
+
async runSeeds(dirname, seedFiles) {
|
|
168
|
+
const groups = await this.generateSeeds(dirname, seedFiles);
|
|
169
|
+
await this.removeSeedsFromDb(seedFiles);
|
|
170
|
+
return await this.insertRecords(groups);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
export { DbMigratorAndSeeder };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
declare class RedisDataLoader {
|
|
2
|
+
private opt?;
|
|
3
|
+
private dataLoaderOpt?;
|
|
4
|
+
private keySpace;
|
|
5
|
+
private loader?;
|
|
6
|
+
private redisClient;
|
|
7
|
+
private static messages;
|
|
8
|
+
constructor(redisClient: any, keySpace?: string, userLoader?: any, opt?: any);
|
|
9
|
+
/**
|
|
10
|
+
* Adds a prefix to the passed key
|
|
11
|
+
* @param key Key value
|
|
12
|
+
* @returns Trasnformed key value that includes key space
|
|
13
|
+
*/
|
|
14
|
+
makeKey(key: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* Deserializes response from Readis
|
|
17
|
+
* @param str String to deserialize
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
parse(str: any): Promise<any>;
|
|
21
|
+
/**
|
|
22
|
+
* Serializes passed value to a string
|
|
23
|
+
* @param value A value to be serialized
|
|
24
|
+
* @returns serialized value in the form of string
|
|
25
|
+
*/
|
|
26
|
+
toString(value: any): Promise<string>;
|
|
27
|
+
rSetAndGet(key: string, rawValue: any): Promise<any>;
|
|
28
|
+
rMSetAndGet(keys: string[], rawValues: any[]): Promise<any[]>;
|
|
29
|
+
rGet(key: string): Promise<any>;
|
|
30
|
+
rMGet(keys: string[]): Promise<any[]>;
|
|
31
|
+
rSet(key: string, rawValue: any): Promise<any>;
|
|
32
|
+
rMSet(keys: string[], values: any[]): Promise<any>;
|
|
33
|
+
rDel(key: string): Promise<any>;
|
|
34
|
+
load(key: string): Promise<any>;
|
|
35
|
+
loadMany(keys: string[]): Promise<any>;
|
|
36
|
+
prime(key: string, value: any): Promise<any>;
|
|
37
|
+
clear(key: string): Promise<any>;
|
|
38
|
+
clearAllLocal(): Promise<any>;
|
|
39
|
+
clearLocal(key: string): Promise<any>;
|
|
40
|
+
}
|
|
41
|
+
export { RedisDataLoader };
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { isObject, last, map, omit, pick } from 'lodash';
|
|
2
|
+
import DataLoader from 'dataloader';
|
|
3
|
+
import stringify from 'json-stable-stringify';
|
|
4
|
+
const customOptions = ['expire', 'serialize', 'deserialize', 'cacheKeyFn'];
|
|
5
|
+
const mapPromise = (promise, fn) => Promise.all(promise.map(fn));
|
|
6
|
+
class RedisDataLoader {
|
|
7
|
+
opt;
|
|
8
|
+
dataLoaderOpt;
|
|
9
|
+
keySpace;
|
|
10
|
+
loader;
|
|
11
|
+
redisClient;
|
|
12
|
+
static messages = {
|
|
13
|
+
keyRequired: 'The "key" parameter is required',
|
|
14
|
+
keysArrayRequired: 'The "keys" parameter is required and should be an array',
|
|
15
|
+
valueRequired: 'The "value" parameter is required',
|
|
16
|
+
valueObjectOrNull: 'The "value" parameter must be an object or null',
|
|
17
|
+
keysValuesLengths: 'The "keys" and "values" are expected to be the same',
|
|
18
|
+
};
|
|
19
|
+
constructor(redisClient, keySpace, userLoader, opt) {
|
|
20
|
+
this.redisClient = redisClient;
|
|
21
|
+
this.opt = pick(opt, customOptions) || {};
|
|
22
|
+
this.dataLoaderOpt = {
|
|
23
|
+
...omit(opt, customOptions),
|
|
24
|
+
cacheKeyFn: this.opt.cacheKeyFn,
|
|
25
|
+
};
|
|
26
|
+
this.opt.cacheKeyFn =
|
|
27
|
+
typeof this.opt.cacheKeyFn === 'function'
|
|
28
|
+
? this.opt.cacheKeyFn
|
|
29
|
+
: (key) => (isObject(key) ? stringify(key) : key);
|
|
30
|
+
this.keySpace = keySpace ? keySpace + ':' : '';
|
|
31
|
+
this.loader = new DataLoader(async (keys) => {
|
|
32
|
+
const results = await this.rMGet(keys);
|
|
33
|
+
const keysToRead = [];
|
|
34
|
+
const keysIdxsToRead = [];
|
|
35
|
+
keys.forEach((key, idx) => {
|
|
36
|
+
if (results[idx] == null) {
|
|
37
|
+
keysToRead.push(key);
|
|
38
|
+
keysIdxsToRead.push(idx);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
if (keysToRead.length > 0) {
|
|
42
|
+
const loadedData = await userLoader.load(keysToRead);
|
|
43
|
+
const data = await this.rMSetAndGet(keysToRead, loadedData);
|
|
44
|
+
keysIdxsToRead.forEach((keyIdx, idx) => {
|
|
45
|
+
results[keyIdx] = data[idx];
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return results;
|
|
49
|
+
}, this.dataLoaderOpt);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Adds a prefix to the passed key
|
|
53
|
+
* @param key Key value
|
|
54
|
+
* @returns Trasnformed key value that includes key space
|
|
55
|
+
*/
|
|
56
|
+
makeKey(key) {
|
|
57
|
+
return `${this.keySpace}${this.opt.cacheKeyFn(key)}`;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Deserializes response from Readis
|
|
61
|
+
* @param str String to deserialize
|
|
62
|
+
* @returns
|
|
63
|
+
*/
|
|
64
|
+
async parse(str) {
|
|
65
|
+
if (str === '' || str === null) {
|
|
66
|
+
return str;
|
|
67
|
+
}
|
|
68
|
+
else if (typeof this.opt.deserialize === 'function') {
|
|
69
|
+
return await this.opt.deserialize(str);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
return JSON.parse(str);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Serializes passed value to a string
|
|
77
|
+
* @param value A value to be serialized
|
|
78
|
+
* @returns serialized value in the form of string
|
|
79
|
+
*/
|
|
80
|
+
async toString(value) {
|
|
81
|
+
if (value === null) {
|
|
82
|
+
return '';
|
|
83
|
+
}
|
|
84
|
+
else if (typeof this.opt.serialize === 'function') {
|
|
85
|
+
return await this.opt.serialize(value);
|
|
86
|
+
}
|
|
87
|
+
else if (isObject(value)) {
|
|
88
|
+
return JSON.stringify(value);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
throw new Error(RedisDataLoader.messages.valueObjectOrNull);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async rSetAndGet(key, rawValue) {
|
|
95
|
+
const value = await this.toString(rawValue);
|
|
96
|
+
const fullKey = this.makeKey(key);
|
|
97
|
+
const multi = this.redisClient.multi();
|
|
98
|
+
multi.set(fullKey, value);
|
|
99
|
+
if (this.opt.expire) {
|
|
100
|
+
multi.expire(fullKey, this.opt.expire);
|
|
101
|
+
}
|
|
102
|
+
multi.get(fullKey);
|
|
103
|
+
const replies = await multi.exec();
|
|
104
|
+
const lastReply = last(replies);
|
|
105
|
+
const result = await this.parse(lastReply);
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
async rMSetAndGet(keys, rawValues) {
|
|
109
|
+
const multi = this.redisClient.multi();
|
|
110
|
+
const fullKeys = await mapPromise(keys, (key) => this.makeKey(key));
|
|
111
|
+
const values = await mapPromise(rawValues, (rawValue) => this.toString(rawValue));
|
|
112
|
+
multi.mSet(fullKeys.reduce((acc, fullKey, idx) => {
|
|
113
|
+
acc.push(fullKey);
|
|
114
|
+
acc.push(values[idx]);
|
|
115
|
+
return acc;
|
|
116
|
+
}, []));
|
|
117
|
+
if (this.opt.expire) {
|
|
118
|
+
for (const fullKey of fullKeys) {
|
|
119
|
+
multi.expire(fullKey, this.opt.expire);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
multi.mGet(fullKeys);
|
|
123
|
+
const replies = await multi.exec();
|
|
124
|
+
const lastReplies = last(replies);
|
|
125
|
+
const result = await mapPromise(lastReplies, (lastReply) => this.parse(lastReply));
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
async rGet(key) {
|
|
129
|
+
const reply = await this.redisClient.get(this.makeKey(key));
|
|
130
|
+
const result = await this.parse(reply);
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
async rMGet(keys) {
|
|
134
|
+
const replies = await this.redisClient.mGet(map(keys, (key) => this.makeKey(key)));
|
|
135
|
+
const results = await mapPromise(replies, (reply) => this.parse(reply));
|
|
136
|
+
return results;
|
|
137
|
+
}
|
|
138
|
+
async rSet(key, rawValue) {
|
|
139
|
+
const value = await this.toString(rawValue);
|
|
140
|
+
const reply = await this.redisClient.set(this.makeKey(key), value);
|
|
141
|
+
const result = await this.parse(reply);
|
|
142
|
+
// result is expected to be OK
|
|
143
|
+
return result;
|
|
144
|
+
}
|
|
145
|
+
async rMSet(keys, values) {
|
|
146
|
+
if (keys.length != values.length) {
|
|
147
|
+
throw new Error(RedisDataLoader.messages.keysValuesLengths);
|
|
148
|
+
}
|
|
149
|
+
const multi = this.redisClient.multi();
|
|
150
|
+
multi.mSet(keys.reduce((acc, key, idx) => {
|
|
151
|
+
acc.push(this.makeKey(key));
|
|
152
|
+
acc.push(values[idx]);
|
|
153
|
+
return acc;
|
|
154
|
+
}, []));
|
|
155
|
+
if (this.opt.expire) {
|
|
156
|
+
for (const key of keys) {
|
|
157
|
+
multi.expire(key, this.opt.expire);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// The reply is expected to be "OK"
|
|
161
|
+
const reply = await multi.exec();
|
|
162
|
+
return reply;
|
|
163
|
+
}
|
|
164
|
+
async rDel(key) {
|
|
165
|
+
return await this.redisClient.del(this.makeKey(key));
|
|
166
|
+
}
|
|
167
|
+
async load(key) {
|
|
168
|
+
if (!key) {
|
|
169
|
+
throw new TypeError(RedisDataLoader.messages.keyRequired);
|
|
170
|
+
}
|
|
171
|
+
return await this.loader.load(key);
|
|
172
|
+
}
|
|
173
|
+
async loadMany(keys) {
|
|
174
|
+
if (!Array.isArray(keys)) {
|
|
175
|
+
throw new TypeError(RedisDataLoader.messages.keysArrayRequired);
|
|
176
|
+
}
|
|
177
|
+
return await this.loader.loadMany(keys);
|
|
178
|
+
}
|
|
179
|
+
async prime(key, value) {
|
|
180
|
+
if (!key) {
|
|
181
|
+
throw new TypeError(RedisDataLoader.messages.keyRequired);
|
|
182
|
+
}
|
|
183
|
+
else if (value === undefined) {
|
|
184
|
+
throw new TypeError(RedisDataLoader.messages.valueRequired);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
const result = await this.rSetAndGet(key, value);
|
|
188
|
+
return await this.loader.clear(key).prime(key, result === '' ? null : result);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
async clear(key) {
|
|
192
|
+
if (!key) {
|
|
193
|
+
throw new TypeError(RedisDataLoader.messages.keyRequired);
|
|
194
|
+
}
|
|
195
|
+
const resp = await this.rDel(key);
|
|
196
|
+
await this.loader.clear(key);
|
|
197
|
+
return resp;
|
|
198
|
+
}
|
|
199
|
+
async clearAllLocal() {
|
|
200
|
+
return await this.loader.clearAll();
|
|
201
|
+
}
|
|
202
|
+
async clearLocal(key) {
|
|
203
|
+
return await this.loader.clear(key);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
export { RedisDataLoader };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ApiContext, ApiContextInterface } from './interfaces';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a GraphQL context object with all available cores.
|
|
4
|
+
* @returns {object} A context object with all available cores
|
|
5
|
+
*/
|
|
6
|
+
export declare const createApiContext: (args: ApiContextInterface) => Promise<ApiContext>;
|
|
7
|
+
/**
|
|
8
|
+
* Creates default a GraphQL context object with all avilable cores.
|
|
9
|
+
* @returns {object} A context object with all available cores
|
|
10
|
+
*/
|
|
11
|
+
export declare const createContextDefault: (args: any) => Promise<ApiContext>;
|