@sdflc/backend-helpers 1.0.65 → 1.0.67
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/BaseController.js +0 -5
- package/build/BaseCore.js +18 -7
- package/build/BaseGateway.d.ts +4 -3
- package/build/BaseGateway.js +30 -6
- package/build/Database.d.ts +1 -0
- package/build/Database.js +25 -1
- package/build/apiContext.js +1 -0
- package/build/configBulder.js +9 -1
- package/build/interfaces/CoreActionInterface.d.ts +1 -0
- package/build/interfaces/DatabaseOptions.d.ts +5 -0
- package/build/knexConfigBuilder.js +3 -3
- package/package.json +1 -1
package/build/BaseController.js
CHANGED
|
@@ -43,7 +43,6 @@ class BaseController {
|
|
|
43
43
|
*/
|
|
44
44
|
json(method) {
|
|
45
45
|
return (req, res, next) => __awaiter(this, void 0, void 0, function* () {
|
|
46
|
-
var _a, _b;
|
|
47
46
|
try {
|
|
48
47
|
const result = yield method(req, res, next);
|
|
49
48
|
if (result instanceof api_helpers_1.OpResult) {
|
|
@@ -55,10 +54,6 @@ class BaseController {
|
|
|
55
54
|
else {
|
|
56
55
|
res.status(204).json(new api_helpers_1.OpResult());
|
|
57
56
|
}
|
|
58
|
-
if (((_a = req.context) === null || _a === void 0 ? void 0 : _a.isExternalDb) === false && ((_b = req.context) === null || _b === void 0 ? void 0 : _b.db)) {
|
|
59
|
-
this.logger.log('Release DB connection');
|
|
60
|
-
yield req.context.db.destroy();
|
|
61
|
-
}
|
|
62
57
|
yield this.onResponse(req, res, next);
|
|
63
58
|
}
|
|
64
59
|
catch (ex) {
|
package/build/BaseCore.js
CHANGED
|
@@ -127,15 +127,17 @@ class BaseCore {
|
|
|
127
127
|
}
|
|
128
128
|
checkResult(args) {
|
|
129
129
|
return __awaiter(this, void 0, void 0, function* () {
|
|
130
|
-
const { hasTransaction, result, sameRequest } = args;
|
|
130
|
+
const { hasTransaction, result, sameRequest, doingWhat } = args;
|
|
131
131
|
if (sameRequest) {
|
|
132
132
|
return;
|
|
133
133
|
}
|
|
134
134
|
if (hasTransaction && result) {
|
|
135
135
|
if (result.code >= api_helpers_1.OP_RESULT_CODES.OK) {
|
|
136
|
+
this.logger.debug(`Commit DB transaction on ${doingWhat}`);
|
|
136
137
|
yield this.getDb().commitTransaction();
|
|
137
138
|
}
|
|
138
139
|
else {
|
|
140
|
+
this.logger.debug(`Rollback DB transaction on ${doingWhat} as action result code is '${result.code}'`);
|
|
139
141
|
yield this.getDb().rollbackTransaction();
|
|
140
142
|
}
|
|
141
143
|
}
|
|
@@ -143,7 +145,7 @@ class BaseCore {
|
|
|
143
145
|
}
|
|
144
146
|
runAction(opt) {
|
|
145
147
|
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
-
const { args, validate, doAuth, action, hasTransaction, doingWhat, onException, shouldLogArgs } = opt || {};
|
|
148
|
+
const { args, validate, doAuth, action, hasTransaction, doingWhat, onException, shouldLogArgs, requireSpace } = opt || {};
|
|
147
149
|
// If we process action with the same requestId as requestId passed to the constructor it means
|
|
148
150
|
// we are processing the same request from FE and can skip some of the operations like transactions and etc
|
|
149
151
|
const sameRequest = !!this.config.context.requestId && (args === null || args === void 0 ? void 0 : args.requestId) === this.config.context.requestId;
|
|
@@ -175,6 +177,7 @@ class BaseCore {
|
|
|
175
177
|
try {
|
|
176
178
|
if (!sameRequest) {
|
|
177
179
|
if (hasTransaction) {
|
|
180
|
+
this.logger.debug(`Start DB transaction on ${step}`);
|
|
178
181
|
yield this.getDb().startTransaction();
|
|
179
182
|
}
|
|
180
183
|
if (doAuth === true || (doAuth == null && this.config.doAuth)) {
|
|
@@ -182,15 +185,21 @@ class BaseCore {
|
|
|
182
185
|
const [authResult, errMsg] = yield this.authenticate(opt, actionOpt);
|
|
183
186
|
this.measureTool.addCheckpoint('Authenticate request: end');
|
|
184
187
|
if (!authResult) {
|
|
185
|
-
|
|
188
|
+
const result = new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg);
|
|
189
|
+
this.logger.error(`The ${step} requires authentication and it has failed:`, errMsg);
|
|
190
|
+
yield this.checkResult({ hasTransaction, result, sameRequest, doingWhat });
|
|
191
|
+
return result;
|
|
186
192
|
}
|
|
187
193
|
}
|
|
188
|
-
else {
|
|
194
|
+
else if (requireSpace != null && requireSpace !== false) {
|
|
189
195
|
this.measureTool.addCheckpoint('Find space: start');
|
|
190
196
|
const [spaceResult, errMsg] = yield this.findSpace();
|
|
191
197
|
this.measureTool.addCheckpoint('Find space: end');
|
|
192
198
|
if (spaceResult !== true) {
|
|
193
|
-
|
|
199
|
+
const result = new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg);
|
|
200
|
+
this.logger.error(`The ${step} requires a space and it has failed:`, errMsg);
|
|
201
|
+
yield this.checkResult({ hasTransaction, result, sameRequest, doingWhat });
|
|
202
|
+
return result;
|
|
194
203
|
}
|
|
195
204
|
}
|
|
196
205
|
}
|
|
@@ -219,13 +228,15 @@ class BaseCore {
|
|
|
219
228
|
let time = this.measureTool.stop();
|
|
220
229
|
this.logger.debug(`Authorization and validation time for the ${doingWhat} took ${time.durationMs} ms`);
|
|
221
230
|
const result = yield action(args, Object.assign(Object.assign({}, actionOpt), { validatedItems }));
|
|
222
|
-
yield this.checkResult({ hasTransaction, result, sameRequest });
|
|
231
|
+
yield this.checkResult({ hasTransaction, result, sameRequest, doingWhat });
|
|
223
232
|
time = this.measureTool.stop(doingWhat);
|
|
224
233
|
this.logger.debug(`The ${time.name} process time is ${time.durationMs} ms`);
|
|
225
234
|
return result;
|
|
226
235
|
}
|
|
227
236
|
catch (ex) {
|
|
237
|
+
this.logger.error(`An exception has occurred on ${step}:`, ex);
|
|
228
238
|
if (!sameRequest && hasTransaction) {
|
|
239
|
+
this.logger.debug(`Rolling back DB transaction on ${step} due to an excaption`);
|
|
229
240
|
yield this.getDb().rollbackTransaction();
|
|
230
241
|
}
|
|
231
242
|
let errMessage = '';
|
|
@@ -236,7 +247,7 @@ class BaseCore {
|
|
|
236
247
|
}
|
|
237
248
|
}
|
|
238
249
|
catch (ex2) {
|
|
239
|
-
this.logger.
|
|
250
|
+
this.logger.error(`An exception has occured when handling exception:`, ex2);
|
|
240
251
|
}
|
|
241
252
|
if (!errMessage) {
|
|
242
253
|
errMessage = doingWhat;
|
package/build/BaseGateway.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { DateTimeHelpers } from '@sdflc/utils';
|
|
1
|
+
import { DateTimeHelpers, Logger } from '@sdflc/utils';
|
|
2
2
|
import { ApiContext, BaseGatewayPropsInterface, BuildListQueryArgsInterface, ListArgsInterface } from './interfaces';
|
|
3
3
|
import { Database } from './Database';
|
|
4
4
|
declare class BaseGateway {
|
|
5
5
|
protected config: BaseGatewayPropsInterface;
|
|
6
|
+
protected logger: Logger;
|
|
6
7
|
protected loader: any;
|
|
7
8
|
protected dateTime: DateTimeHelpers;
|
|
8
9
|
protected dateTimeNow: Date;
|
|
@@ -10,8 +11,8 @@ declare class BaseGateway {
|
|
|
10
11
|
setConfigPart(props: BaseGatewayPropsInterface): this;
|
|
11
12
|
generateId(args?: any): Promise<string>;
|
|
12
13
|
onListFilter(query: any, filterParams: any): Promise<void>;
|
|
13
|
-
onUpdateFilter(query: any, filterParams: any): Promise<
|
|
14
|
-
onRemoveFilter(query: any, filterParams: any): Promise<
|
|
14
|
+
onUpdateFilter(query: any, filterParams: any): Promise<number>;
|
|
15
|
+
onRemoveFilter(query: any, filterParams: any): Promise<number>;
|
|
15
16
|
transformOnCreate(params: any): Promise<any>;
|
|
16
17
|
transformOnUpdate(params: any, where?: any): Promise<any>;
|
|
17
18
|
transformOnSet(params: any): Promise<any>;
|
package/build/BaseGateway.js
CHANGED
|
@@ -38,6 +38,7 @@ class BaseGateway {
|
|
|
38
38
|
}
|
|
39
39
|
this.config.context.usedKeyPrefixes[this.config.keyPrefix] = true;
|
|
40
40
|
}
|
|
41
|
+
this.logger = props.logger instanceof utils_1.Logger ? props.logger : new utils_1.Logger({ level: utils_1.LoggerLevels.WARNING });
|
|
41
42
|
this.loader = typeof this.config.loader === 'function' ? this.config.loader : new DataCache_1.DataCache(this.config);
|
|
42
43
|
}
|
|
43
44
|
setConfigPart(props) {
|
|
@@ -85,15 +86,19 @@ class BaseGateway {
|
|
|
85
86
|
// This function should be overriden
|
|
86
87
|
const { hasStatus, hasVersion } = this.config;
|
|
87
88
|
const idName = this.getIdField();
|
|
89
|
+
let filtersAppliedQty = 0;
|
|
88
90
|
if (filterParams[idName]) {
|
|
91
|
+
filtersAppliedQty++;
|
|
89
92
|
query.whereIn(idName, (0, lodash_1.castArray)(filterParams[idName]));
|
|
90
93
|
}
|
|
91
94
|
if (hasVersion) {
|
|
92
95
|
if (filterParams[constants_1.FIELDS.VERSION] != null && !isNaN(filterParams[constants_1.FIELDS.VERSION])) {
|
|
96
|
+
filtersAppliedQty++;
|
|
93
97
|
query.where({ [constants_1.FIELDS.VERSION]: filterParams[constants_1.FIELDS.VERSION] });
|
|
94
98
|
}
|
|
95
99
|
}
|
|
96
100
|
if (hasStatus) {
|
|
101
|
+
filtersAppliedQty++;
|
|
97
102
|
if (Array.isArray(filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS])) {
|
|
98
103
|
query.whereIn(constants_1.FIELDS.STATUS, filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS]);
|
|
99
104
|
}
|
|
@@ -101,17 +106,24 @@ class BaseGateway {
|
|
|
101
106
|
query.where(constants_1.FIELDS.STATUS, '!=', utils_1.STATUSES.REMOVED);
|
|
102
107
|
}
|
|
103
108
|
}
|
|
109
|
+
return filtersAppliedQty;
|
|
104
110
|
});
|
|
105
111
|
}
|
|
106
112
|
onRemoveFilter(query, filterParams) {
|
|
107
113
|
return __awaiter(this, void 0, void 0, function* () {
|
|
108
114
|
// This function should be overriden
|
|
109
115
|
const { hasStatus } = this.config;
|
|
110
|
-
const arr = (0, lodash_1.castArray)(filterParams);
|
|
111
|
-
|
|
116
|
+
const arr = filterParams ? (0, lodash_1.castArray)(filterParams) : [];
|
|
117
|
+
let filtersAppliedQty = 1;
|
|
118
|
+
if (arr.length > 0) {
|
|
119
|
+
filtersAppliedQty++;
|
|
120
|
+
query = query.whereIn(this.getIdField(), arr.map((item) => item[this.getIdField()]));
|
|
121
|
+
}
|
|
112
122
|
if (hasStatus === true) {
|
|
123
|
+
filtersAppliedQty++;
|
|
113
124
|
query.where(constants_1.FIELDS.STATUS, '!=', utils_1.STATUSES.REMOVED);
|
|
114
125
|
}
|
|
126
|
+
return filtersAppliedQty;
|
|
115
127
|
});
|
|
116
128
|
}
|
|
117
129
|
transformOnCreate(params) {
|
|
@@ -322,7 +334,10 @@ class BaseGateway {
|
|
|
322
334
|
return __awaiter(this, void 0, void 0, function* () {
|
|
323
335
|
const object = yield this.transformOnUpdate(params, where);
|
|
324
336
|
const query = this.getBuilder().update(object).returning(this.config.returnFields);
|
|
325
|
-
yield this.onUpdateFilter(query, where);
|
|
337
|
+
const filtersAppliedQty = yield this.onUpdateFilter(query, where);
|
|
338
|
+
if (filtersAppliedQty == null || filtersAppliedQty <= 0) {
|
|
339
|
+
throw new Error(`Failed to update the table '${this.config.table}' as none of filters applied: ${JSON.stringify(where, null, 2)}`);
|
|
340
|
+
}
|
|
326
341
|
const items = yield query;
|
|
327
342
|
if (items.length > 0 && this.config.noCache !== true) {
|
|
328
343
|
items.forEach((item) => this.loader.clear(item[this.getIdField()]));
|
|
@@ -351,7 +366,10 @@ class BaseGateway {
|
|
|
351
366
|
return __awaiter(this, void 0, void 0, function* () {
|
|
352
367
|
const object = yield this.transformOnUpdate(params, where);
|
|
353
368
|
const query = this.getBuilder().increment(object).returning(this.config.returnFields);
|
|
354
|
-
yield this.onUpdateFilter(query, where);
|
|
369
|
+
const filtersAppliedQty = yield this.onUpdateFilter(query, where);
|
|
370
|
+
if (filtersAppliedQty == null || filtersAppliedQty <= 0) {
|
|
371
|
+
throw new Error(`Failed to update the table '${this.config.table}' as none of filters applied: ${JSON.stringify(where, null, 2)}`);
|
|
372
|
+
}
|
|
355
373
|
const items = yield query;
|
|
356
374
|
if (items.length > 0 && this.config.noCache !== true) {
|
|
357
375
|
items.forEach((item) => this.loader.clear(item[this.getIdField()]));
|
|
@@ -363,7 +381,10 @@ class BaseGateway {
|
|
|
363
381
|
return __awaiter(this, void 0, void 0, function* () {
|
|
364
382
|
const object = yield this.transformOnUpdate(params, where);
|
|
365
383
|
const query = this.getBuilder().decrement(object).returning(this.config.returnFields);
|
|
366
|
-
yield this.onUpdateFilter(query, where);
|
|
384
|
+
const filtersAppliedQty = yield this.onUpdateFilter(query, where);
|
|
385
|
+
if (filtersAppliedQty == null || filtersAppliedQty <= 0) {
|
|
386
|
+
throw new Error(`Failed to update the table '${this.config.table}' as none of filters applied: ${JSON.stringify(where, null, 2)}`);
|
|
387
|
+
}
|
|
367
388
|
const items = yield query;
|
|
368
389
|
if (items.length > 0 && this.config.noCache !== true) {
|
|
369
390
|
items.forEach((item) => this.loader.clear(item[this.getIdField()]));
|
|
@@ -428,7 +449,10 @@ class BaseGateway {
|
|
|
428
449
|
const object = yield this.transformOnRemove();
|
|
429
450
|
query.update(object).returning(this.config.returnFields);
|
|
430
451
|
}
|
|
431
|
-
yield this.onRemoveFilter(query, where);
|
|
452
|
+
const filtersAppliedQty = yield this.onRemoveFilter(query, where);
|
|
453
|
+
if (filtersAppliedQty == null || filtersAppliedQty <= 0) {
|
|
454
|
+
throw new Error(`Failed to remove records in the table '${this.config.table}' as none of filters applied: ${JSON.stringify(where, null, 2)}`);
|
|
455
|
+
}
|
|
432
456
|
const items = yield query;
|
|
433
457
|
if (items.length > 0 && this.config.noCache !== true) {
|
|
434
458
|
items.forEach((item) => this.loader.clear(item[this.getIdField()]));
|
package/build/Database.d.ts
CHANGED
package/build/Database.js
CHANGED
|
@@ -16,15 +16,34 @@ exports.Database = void 0;
|
|
|
16
16
|
const lodash_1 = require("lodash");
|
|
17
17
|
const knex_1 = __importDefault(require("knex"));
|
|
18
18
|
const utils_1 = require("@sdflc/utils");
|
|
19
|
+
const knexHandles = {};
|
|
20
|
+
const buildKey = (args) => {
|
|
21
|
+
const { connection, dbSchema } = args || {};
|
|
22
|
+
const { host, port, user, password, database } = connection || {};
|
|
23
|
+
const parts = [host, port, user, password, database, dbSchema];
|
|
24
|
+
return parts.join('|');
|
|
25
|
+
};
|
|
19
26
|
class Database {
|
|
20
27
|
constructor(props) {
|
|
21
28
|
this.trx = null;
|
|
22
29
|
this.knexHandle = null;
|
|
23
30
|
this.dbSchema = '';
|
|
31
|
+
this.logger = new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
|
|
24
32
|
if (!props.knexConfig) {
|
|
25
33
|
throw new Error('The Database class expects to have `knexConfig` in its constructor props');
|
|
26
34
|
}
|
|
27
|
-
|
|
35
|
+
if (props.logger instanceof utils_1.Logger) {
|
|
36
|
+
this.logger = props.logger;
|
|
37
|
+
}
|
|
38
|
+
const cfgKey = buildKey({ connection: props.knexConfig.connection, dbSchema: props.dbSchema });
|
|
39
|
+
if (!knexHandles[cfgKey]) {
|
|
40
|
+
this.logger.debug(`Create a new KnexJs configuration`);
|
|
41
|
+
knexHandles[cfgKey] = (0, knex_1.default)(props.knexConfig);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this.logger.debug(`Reusing existing KnexJs configuration`);
|
|
45
|
+
}
|
|
46
|
+
this.knexHandle = knexHandles[cfgKey];
|
|
28
47
|
this.dbSchema = props.dbSchema || '';
|
|
29
48
|
this.trx = props.trx;
|
|
30
49
|
}
|
|
@@ -38,6 +57,10 @@ class Database {
|
|
|
38
57
|
switch (code) {
|
|
39
58
|
default:
|
|
40
59
|
return [false, ''];
|
|
60
|
+
case '23503':
|
|
61
|
+
// ex.detail: 'Key (message_id)=(78403d9e-4e95-4091-8136-ae99ddbfa3ca) is not present in table "message".',
|
|
62
|
+
// ex.constraint: 'message_recipient_status_message_id_foreign',
|
|
63
|
+
return [true, `Reference does not exist`];
|
|
41
64
|
case '23505':
|
|
42
65
|
return [true, `Such item already exists`];
|
|
43
66
|
}
|
|
@@ -88,6 +111,7 @@ class Database {
|
|
|
88
111
|
}
|
|
89
112
|
catch (ex) {
|
|
90
113
|
// Do nothing here
|
|
114
|
+
this.logger.error(`Failed to rollback a transaction:`, ex.message);
|
|
91
115
|
}
|
|
92
116
|
}
|
|
93
117
|
this.trx = undefined;
|
package/build/apiContext.js
CHANGED
package/build/configBulder.js
CHANGED
|
@@ -16,7 +16,15 @@ const buildConfig = (dirName, opt) => {
|
|
|
16
16
|
* Creates an application settings consisting of environment variables
|
|
17
17
|
*/
|
|
18
18
|
const envVars = Object.keys(env).reduce((acc, key) => {
|
|
19
|
-
|
|
19
|
+
let value = env[key];
|
|
20
|
+
const tryValue = (value || '').toLowerCase();
|
|
21
|
+
if (tryValue === 'false') {
|
|
22
|
+
value = false;
|
|
23
|
+
}
|
|
24
|
+
else if (tryValue === 'true') {
|
|
25
|
+
value = true;
|
|
26
|
+
}
|
|
27
|
+
acc[(0, lodash_1.camelCase)(key)] = value;
|
|
20
28
|
return acc;
|
|
21
29
|
}, {});
|
|
22
30
|
return Object.assign(Object.assign({}, envVars), { nodeEnv: env.NODE_ENV, isProduction: env.NODE_ENV === 'production' });
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Logger } from '@sdflc/utils';
|
|
1
2
|
/**
|
|
2
3
|
* Interface to initialize Database object
|
|
3
4
|
*/
|
|
@@ -14,4 +15,8 @@ export interface DatabaseOptions {
|
|
|
14
15
|
* Knex transaction object
|
|
15
16
|
*/
|
|
16
17
|
trx?: any;
|
|
18
|
+
/**
|
|
19
|
+
* Logger for debugging/logging
|
|
20
|
+
*/
|
|
21
|
+
logger?: Logger;
|
|
17
22
|
}
|
|
@@ -18,7 +18,7 @@ const camelResponse = (result) => {
|
|
|
18
18
|
return (0, utils_1.camelKeys)(result);
|
|
19
19
|
};
|
|
20
20
|
const buildKnexConfig = (dirName, appConfig) => {
|
|
21
|
-
const { dbType, dbHost, dbPort, dbName, dbSchema, dbUser, dbPassword, debug, nodeEnv, migrationsTemplateFileName, migrationsFolder, migrationsTable, useSchemaForMigrationsTable, seedsFolder, seedsFolderDev, seedsFolderProd, wrapIdentifierOverrideMap, dbPool, } = appConfig;
|
|
21
|
+
const { dbType, dbHost, dbPort, dbName, dbSchema, dbUser, dbPassword, dbPoolMin, dbPoolMax, debug, nodeEnv, migrationsTemplateFileName, migrationsFolder, migrationsTable, useSchemaForMigrationsTable, seedsFolder, seedsFolderDev, seedsFolderProd, wrapIdentifierOverrideMap, dbPool, } = appConfig;
|
|
22
22
|
const sharedConfig = {
|
|
23
23
|
client: dbType || 'pg',
|
|
24
24
|
postProcessResponse: camelResponse,
|
|
@@ -43,8 +43,8 @@ const buildKnexConfig = (dirName, appConfig) => {
|
|
|
43
43
|
}
|
|
44
44
|
else {
|
|
45
45
|
sharedConfig.pool = {
|
|
46
|
-
min: 0,
|
|
47
|
-
max: 50,
|
|
46
|
+
min: dbPoolMin != null && !isNaN(dbPoolMin) ? dbPoolMin : 0,
|
|
47
|
+
max: dbPoolMax != null && !isNaN(dbPoolMax) ? dbPoolMax : 50,
|
|
48
48
|
acquireTimeoutMillis: 30000,
|
|
49
49
|
createTimeoutMillis: 30000,
|
|
50
50
|
destroyTimeoutMillis: 5000,
|
package/package.json
CHANGED