@sdflc/backend-helpers 1.0.110 → 1.0.112
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 +20 -13
- package/build/BaseGateway.js +4 -3
- package/build/baseController.js +2 -1
- package/build/commonValidators.d.ts +1 -0
- package/build/commonValidators.js +14 -1
- package/build/esm/BaseCore.js +20 -13
- package/build/esm/BaseGateway.js +4 -3
- package/build/esm/baseController.js +2 -1
- package/build/esm/commonValidators.d.ts +1 -0
- package/build/esm/commonValidators.js +12 -0
- package/build/esm/utils.d.ts +2 -0
- package/build/esm/utils.js +12 -0
- package/build/utils.d.ts +2 -0
- package/build/utils.js +15 -1
- package/package.json +3 -3
package/build/BaseCore.js
CHANGED
|
@@ -11,6 +11,7 @@ const lodash_1 = require("lodash");
|
|
|
11
11
|
const commonValidators_1 = require("./commonValidators");
|
|
12
12
|
const constants_1 = require("./constants");
|
|
13
13
|
const userToken_1 = require("./userToken");
|
|
14
|
+
const utils_2 = require("./utils");
|
|
14
15
|
const defaultValidate = ( /*args: any, opt?: BaseCoreActionsInterface*/) => {
|
|
15
16
|
return [true, {}];
|
|
16
17
|
};
|
|
@@ -29,13 +30,17 @@ const defaultValidators = {
|
|
|
29
30
|
remove: commonValidators_1.validateRemove,
|
|
30
31
|
removeMany: commonValidators_1.validateRemoveMany,
|
|
31
32
|
};
|
|
33
|
+
const localLogger = new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
|
|
32
34
|
class BaseCore {
|
|
33
35
|
measureTool = new utils_1.MeasureTool();
|
|
34
36
|
config;
|
|
35
37
|
logger;
|
|
36
38
|
constructor(props) {
|
|
37
39
|
this.config = props || {};
|
|
38
|
-
this.logger =
|
|
40
|
+
this.logger = localLogger;
|
|
41
|
+
if ((0, utils_2.isLogger)(this.config.logger)) {
|
|
42
|
+
this.logger = this.config.logger;
|
|
43
|
+
}
|
|
39
44
|
this.config.doAuthOnAction = {
|
|
40
45
|
list: props?.doAuthOnAction?.list != null ? props?.doAuthOnAction?.list : props?.doAuth,
|
|
41
46
|
get: props?.doAuthOnAction?.get != null ? props?.doAuthOnAction?.get : props?.doAuth,
|
|
@@ -94,7 +99,7 @@ class BaseCore {
|
|
|
94
99
|
}
|
|
95
100
|
validationFailure(ex) {
|
|
96
101
|
const result = new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
|
|
97
|
-
if (
|
|
102
|
+
if ((0, utils_2.isOpResult)(ex)) {
|
|
98
103
|
return result.toJS();
|
|
99
104
|
}
|
|
100
105
|
Object.keys(ex?.errors || {}).forEach((key) => {
|
|
@@ -131,6 +136,7 @@ class BaseCore {
|
|
|
131
136
|
async checkResult(args) {
|
|
132
137
|
const { hasTransaction, result, sameRequest, doingWhat, skipCheckResult } = args;
|
|
133
138
|
if (sameRequest) {
|
|
139
|
+
this.logger.debug(`Skip result check due to same request flag on ${doingWhat}`);
|
|
134
140
|
return;
|
|
135
141
|
}
|
|
136
142
|
if (hasTransaction) {
|
|
@@ -290,7 +296,7 @@ class BaseCore {
|
|
|
290
296
|
accessRights: [utils_1.ACCESS_RIGHTS.LIST],
|
|
291
297
|
action: async (args, opt) => {
|
|
292
298
|
const object = await this.beforeList(args, opt);
|
|
293
|
-
if (
|
|
299
|
+
if ((0, utils_2.isOpResult)(object)) {
|
|
294
300
|
return object;
|
|
295
301
|
}
|
|
296
302
|
const items = await this.getContext().gateways[this.config.gatewayName]?.list(object);
|
|
@@ -311,7 +317,7 @@ class BaseCore {
|
|
|
311
317
|
accessRights: [utils_1.ACCESS_RIGHTS.GET],
|
|
312
318
|
action: async (args, opt) => {
|
|
313
319
|
const args2use = await this.beforeGet(args, opt);
|
|
314
|
-
if (
|
|
320
|
+
if ((0, utils_2.isOpResult)(args2use)) {
|
|
315
321
|
return args2use;
|
|
316
322
|
}
|
|
317
323
|
if (args2use?.id == null) {
|
|
@@ -337,7 +343,7 @@ class BaseCore {
|
|
|
337
343
|
accessRights: [utils_1.ACCESS_RIGHTS.GET],
|
|
338
344
|
action: async (args, opt) => {
|
|
339
345
|
const args2use = await this.beforeGetMany(args, opt);
|
|
340
|
-
if (
|
|
346
|
+
if ((0, utils_2.isOpResult)(args2use)) {
|
|
341
347
|
return args2use;
|
|
342
348
|
}
|
|
343
349
|
if (!Array.isArray(args2use?.ids)) {
|
|
@@ -374,7 +380,7 @@ class BaseCore {
|
|
|
374
380
|
});
|
|
375
381
|
}
|
|
376
382
|
const object = await this.beforeCreate(params, opt);
|
|
377
|
-
if (
|
|
383
|
+
if ((0, utils_2.isOpResult)(object)) {
|
|
378
384
|
return object;
|
|
379
385
|
}
|
|
380
386
|
const items = await this.getContext().gateways[this.config.gatewayName]?.create(object);
|
|
@@ -395,7 +401,7 @@ class BaseCore {
|
|
|
395
401
|
action: async (args, opt) => {
|
|
396
402
|
const { params } = args || {};
|
|
397
403
|
const objects = await this.beforeCreateMany(params, opt);
|
|
398
|
-
if (
|
|
404
|
+
if ((0, utils_2.isOpResult)(objects)) {
|
|
399
405
|
return objects;
|
|
400
406
|
}
|
|
401
407
|
const items = await this.getContext().gateways[this.config.gatewayName]?.create(objects);
|
|
@@ -416,7 +422,7 @@ class BaseCore {
|
|
|
416
422
|
action: async (args, opt) => {
|
|
417
423
|
const { where, params } = args || {};
|
|
418
424
|
const object = await this.beforeUpdate(params, opt);
|
|
419
|
-
if (
|
|
425
|
+
if ((0, utils_2.isOpResult)(object)) {
|
|
420
426
|
return object;
|
|
421
427
|
}
|
|
422
428
|
const items = await this.getContext().gateways[this.config.gatewayName]?.update(where, object);
|
|
@@ -437,7 +443,7 @@ class BaseCore {
|
|
|
437
443
|
action: async (args, opt) => {
|
|
438
444
|
const { params } = args || {};
|
|
439
445
|
const object = await this.beforeUpdateMany(params, opt);
|
|
440
|
-
if (
|
|
446
|
+
if ((0, utils_2.isOpResult)(object)) {
|
|
441
447
|
return object;
|
|
442
448
|
}
|
|
443
449
|
const items = await this.getContext().gateways[this.config.gatewayName]?.updateMany(object);
|
|
@@ -458,7 +464,7 @@ class BaseCore {
|
|
|
458
464
|
action: async (args, opt /* { setStep, validatedItems, sameRequest } */) => {
|
|
459
465
|
const { params } = args || {};
|
|
460
466
|
const object = await this.beforeSet(params, opt);
|
|
461
|
-
if (
|
|
467
|
+
if ((0, utils_2.isOpResult)(object)) {
|
|
462
468
|
return object;
|
|
463
469
|
}
|
|
464
470
|
const items = await this.getContext().gateways[this.config.gatewayName]?.set(object);
|
|
@@ -479,7 +485,7 @@ class BaseCore {
|
|
|
479
485
|
action: async (args, opt) => {
|
|
480
486
|
const { where } = args || {};
|
|
481
487
|
const object = await this.beforeRemove(where, opt);
|
|
482
|
-
if (
|
|
488
|
+
if ((0, utils_2.isOpResult)(object)) {
|
|
483
489
|
return object;
|
|
484
490
|
}
|
|
485
491
|
const items = await this.getContext().gateways[this.config.gatewayName]?.remove(object);
|
|
@@ -500,7 +506,7 @@ class BaseCore {
|
|
|
500
506
|
action: async (args, opt) => {
|
|
501
507
|
const { where } = args || {};
|
|
502
508
|
const object = await this.beforeRemoveMany(where, opt);
|
|
503
|
-
if (
|
|
509
|
+
if ((0, utils_2.isOpResult)(object)) {
|
|
504
510
|
return object;
|
|
505
511
|
}
|
|
506
512
|
const items = await this.getContext().gateways[this.config.gatewayName]?.remove(object);
|
|
@@ -684,6 +690,7 @@ class BaseCore {
|
|
|
684
690
|
else if (space?.apiKey && space?.apiKey !== context.apiKey) {
|
|
685
691
|
return [false, 'API Key is invalid'];
|
|
686
692
|
}
|
|
693
|
+
return [false, 'No authentication provided'];
|
|
687
694
|
}
|
|
688
695
|
return [true, ''];
|
|
689
696
|
}
|
|
@@ -696,7 +703,7 @@ class BaseCore {
|
|
|
696
703
|
addRemovedModifierToFields(fields) {
|
|
697
704
|
const obj = {};
|
|
698
705
|
if (!fields) {
|
|
699
|
-
this.logger.
|
|
706
|
+
this.logger.warn(`The 'addRemovedModifierToFields' method expected its fields parameter to be a field name or an array of table field names but got:`, fields);
|
|
700
707
|
return obj;
|
|
701
708
|
}
|
|
702
709
|
(0, lodash_1.castArray)(fields).forEach((field) => {
|
package/build/BaseGateway.js
CHANGED
|
@@ -428,11 +428,12 @@ class BaseGateway {
|
|
|
428
428
|
if (hasSpace) {
|
|
429
429
|
skipFields.push(constants_1.FIELDS.SPACE_ID);
|
|
430
430
|
}
|
|
431
|
+
const idField = this.getIdFieldUpdateRemove();
|
|
431
432
|
for (let idx = 0, len = objects.length; idx < len; ++idx) {
|
|
432
433
|
const object = objects[idx];
|
|
433
434
|
objects[idx] = await this.transformOnSet(object);
|
|
434
|
-
if (objects[idx][
|
|
435
|
-
objects[idx][
|
|
435
|
+
if (objects[idx][idField] == null) {
|
|
436
|
+
objects[idx][idField] = await this.generateId(object);
|
|
436
437
|
}
|
|
437
438
|
}
|
|
438
439
|
const fields = Array.isArray(mergeFields) ? mergeFields : Object.keys(objects[0]);
|
|
@@ -442,7 +443,7 @@ class BaseGateway {
|
|
|
442
443
|
const items = await this.getBuilder()
|
|
443
444
|
.insert(chunks[idx])
|
|
444
445
|
.returning(this.config.returnFields)
|
|
445
|
-
.onConflict(this.config.setFields ||
|
|
446
|
+
.onConflict(this.config.setFields || idField)
|
|
446
447
|
.merge((0, lodash_1.pullAll)(fields, skipFields));
|
|
447
448
|
result.push(...items);
|
|
448
449
|
}
|
package/build/baseController.js
CHANGED
|
@@ -7,6 +7,7 @@ exports.BaseController = void 0;
|
|
|
7
7
|
const check_types_1 = __importDefault(require("check-types"));
|
|
8
8
|
const api_helpers_1 = require("@sdflc/api-helpers");
|
|
9
9
|
const utils_1 = require("@sdflc/utils");
|
|
10
|
+
const utils_2 = require("./utils");
|
|
10
11
|
/**
|
|
11
12
|
* Base class for REST API controllers.
|
|
12
13
|
*/
|
|
@@ -38,7 +39,7 @@ class BaseController {
|
|
|
38
39
|
return async (req, res, next) => {
|
|
39
40
|
try {
|
|
40
41
|
const result = await method(req, res, next);
|
|
41
|
-
if (
|
|
42
|
+
if ((0, utils_2.isOpResult)(result)) {
|
|
42
43
|
res.status(result.getHttpStatus()).json(result.toJS());
|
|
43
44
|
}
|
|
44
45
|
else if (result?.redirectUrl) {
|
|
@@ -15,3 +15,4 @@ export declare const validateGetMany: (args: any, opt: BaseCoreActionsInterface)
|
|
|
15
15
|
export declare const validateRemoveMany: (args: any, opt: BaseCoreActionsInterface) => Promise<OpResult[] | {}[]>;
|
|
16
16
|
export declare const validateCreate: ({ rules }: CommonValidatorArgs) => (args: any, opt: BaseCoreActionsInterface) => Promise<{}[] | {}[]>;
|
|
17
17
|
export declare const validateUpdate: ({ rules }: CommonValidatorArgs) => (args: any, opt: BaseCoreActionsInterface) => Promise<{}[] | {}[]>;
|
|
18
|
+
export declare const validateSet: ({ rules }: CommonValidatorArgs) => (args: any, opt: BaseCoreActionsInterface) => Promise<{}[] | {}[]>;
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.validateUpdate = exports.validateCreate = exports.validateRemoveMany = exports.validateGetMany = exports.validateRemove = exports.validateGet = exports.validateList = exports.validateMultipleTextIds = exports.validateMultipleUuidIds = exports.validateMultipleIntIds = exports.validateSingleTextId = exports.validateSingleUuidId = exports.validateSingleIntId = exports.shouldHaveId = exports.shouldHaveParams = void 0;
|
|
6
|
+
exports.validateSet = exports.validateUpdate = exports.validateCreate = exports.validateRemoveMany = exports.validateGetMany = exports.validateRemove = exports.validateGet = exports.validateList = exports.validateMultipleTextIds = exports.validateMultipleUuidIds = exports.validateMultipleIntIds = exports.validateSingleTextId = exports.validateSingleUuidId = exports.validateSingleIntId = exports.shouldHaveId = exports.shouldHaveParams = void 0;
|
|
7
7
|
const lodash_1 = require("lodash");
|
|
8
8
|
const checkit_1 = __importDefault(require("checkit"));
|
|
9
9
|
const check_types_1 = __importDefault(require("check-types"));
|
|
@@ -309,3 +309,16 @@ const validateUpdate = ({ rules }) => async (args, opt) => {
|
|
|
309
309
|
return [true, {}];
|
|
310
310
|
};
|
|
311
311
|
exports.validateUpdate = validateUpdate;
|
|
312
|
+
const validateSet = ({ rules }) => async (args, opt) => {
|
|
313
|
+
const { params } = args || {};
|
|
314
|
+
const [paramsCheck] = (0, exports.shouldHaveParams)(params);
|
|
315
|
+
if (paramsCheck !== true) {
|
|
316
|
+
return [paramsCheck, {}];
|
|
317
|
+
}
|
|
318
|
+
const [result] = rules.validateSync(params);
|
|
319
|
+
if (result) {
|
|
320
|
+
return [(0, checkItHelpers_1.createErrorOnCheckError)(result), {}];
|
|
321
|
+
}
|
|
322
|
+
return [true, {}];
|
|
323
|
+
};
|
|
324
|
+
exports.validateSet = validateSet;
|
package/build/esm/BaseCore.js
CHANGED
|
@@ -5,6 +5,7 @@ import { castArray } from 'lodash';
|
|
|
5
5
|
import { validateGet, validateGetMany, validateRemove, validateRemoveMany } from './commonValidators';
|
|
6
6
|
import { ACTIONS, DEFAULT_ORDER_NO, DEFAULT_ORDER_NO_STEP, FIELDS, HEADERS, ID_TYPES, ORDER_BYS } from './constants';
|
|
7
7
|
import { getDefaultJwtAlgorithm, getDefaultJwtIssuer, verifyUserToken } from './userToken';
|
|
8
|
+
import { isOpResult, isLogger } from './utils';
|
|
8
9
|
const defaultValidate = ( /*args: any, opt?: BaseCoreActionsInterface*/) => {
|
|
9
10
|
return [true, {}];
|
|
10
11
|
};
|
|
@@ -23,13 +24,17 @@ const defaultValidators = {
|
|
|
23
24
|
remove: validateRemove,
|
|
24
25
|
removeMany: validateRemoveMany,
|
|
25
26
|
};
|
|
27
|
+
const localLogger = new Logger({ level: LoggerLevels.ERROR });
|
|
26
28
|
class BaseCore {
|
|
27
29
|
measureTool = new MeasureTool();
|
|
28
30
|
config;
|
|
29
31
|
logger;
|
|
30
32
|
constructor(props) {
|
|
31
33
|
this.config = props || {};
|
|
32
|
-
this.logger =
|
|
34
|
+
this.logger = localLogger;
|
|
35
|
+
if (isLogger(this.config.logger)) {
|
|
36
|
+
this.logger = this.config.logger;
|
|
37
|
+
}
|
|
33
38
|
this.config.doAuthOnAction = {
|
|
34
39
|
list: props?.doAuthOnAction?.list != null ? props?.doAuthOnAction?.list : props?.doAuth,
|
|
35
40
|
get: props?.doAuthOnAction?.get != null ? props?.doAuthOnAction?.get : props?.doAuth,
|
|
@@ -88,7 +93,7 @@ class BaseCore {
|
|
|
88
93
|
}
|
|
89
94
|
validationFailure(ex) {
|
|
90
95
|
const result = new OpResult({ code: OP_RESULT_CODES.VALIDATION_FAILED });
|
|
91
|
-
if (ex
|
|
96
|
+
if (isOpResult(ex)) {
|
|
92
97
|
return result.toJS();
|
|
93
98
|
}
|
|
94
99
|
Object.keys(ex?.errors || {}).forEach((key) => {
|
|
@@ -125,6 +130,7 @@ class BaseCore {
|
|
|
125
130
|
async checkResult(args) {
|
|
126
131
|
const { hasTransaction, result, sameRequest, doingWhat, skipCheckResult } = args;
|
|
127
132
|
if (sameRequest) {
|
|
133
|
+
this.logger.debug(`Skip result check due to same request flag on ${doingWhat}`);
|
|
128
134
|
return;
|
|
129
135
|
}
|
|
130
136
|
if (hasTransaction) {
|
|
@@ -284,7 +290,7 @@ class BaseCore {
|
|
|
284
290
|
accessRights: [ACCESS_RIGHTS.LIST],
|
|
285
291
|
action: async (args, opt) => {
|
|
286
292
|
const object = await this.beforeList(args, opt);
|
|
287
|
-
if (object
|
|
293
|
+
if (isOpResult(object)) {
|
|
288
294
|
return object;
|
|
289
295
|
}
|
|
290
296
|
const items = await this.getContext().gateways[this.config.gatewayName]?.list(object);
|
|
@@ -305,7 +311,7 @@ class BaseCore {
|
|
|
305
311
|
accessRights: [ACCESS_RIGHTS.GET],
|
|
306
312
|
action: async (args, opt) => {
|
|
307
313
|
const args2use = await this.beforeGet(args, opt);
|
|
308
|
-
if (args2use
|
|
314
|
+
if (isOpResult(args2use)) {
|
|
309
315
|
return args2use;
|
|
310
316
|
}
|
|
311
317
|
if (args2use?.id == null) {
|
|
@@ -331,7 +337,7 @@ class BaseCore {
|
|
|
331
337
|
accessRights: [ACCESS_RIGHTS.GET],
|
|
332
338
|
action: async (args, opt) => {
|
|
333
339
|
const args2use = await this.beforeGetMany(args, opt);
|
|
334
|
-
if (args2use
|
|
340
|
+
if (isOpResult(args2use)) {
|
|
335
341
|
return args2use;
|
|
336
342
|
}
|
|
337
343
|
if (!Array.isArray(args2use?.ids)) {
|
|
@@ -368,7 +374,7 @@ class BaseCore {
|
|
|
368
374
|
});
|
|
369
375
|
}
|
|
370
376
|
const object = await this.beforeCreate(params, opt);
|
|
371
|
-
if (object
|
|
377
|
+
if (isOpResult(object)) {
|
|
372
378
|
return object;
|
|
373
379
|
}
|
|
374
380
|
const items = await this.getContext().gateways[this.config.gatewayName]?.create(object);
|
|
@@ -389,7 +395,7 @@ class BaseCore {
|
|
|
389
395
|
action: async (args, opt) => {
|
|
390
396
|
const { params } = args || {};
|
|
391
397
|
const objects = await this.beforeCreateMany(params, opt);
|
|
392
|
-
if (objects
|
|
398
|
+
if (isOpResult(objects)) {
|
|
393
399
|
return objects;
|
|
394
400
|
}
|
|
395
401
|
const items = await this.getContext().gateways[this.config.gatewayName]?.create(objects);
|
|
@@ -410,7 +416,7 @@ class BaseCore {
|
|
|
410
416
|
action: async (args, opt) => {
|
|
411
417
|
const { where, params } = args || {};
|
|
412
418
|
const object = await this.beforeUpdate(params, opt);
|
|
413
|
-
if (object
|
|
419
|
+
if (isOpResult(object)) {
|
|
414
420
|
return object;
|
|
415
421
|
}
|
|
416
422
|
const items = await this.getContext().gateways[this.config.gatewayName]?.update(where, object);
|
|
@@ -431,7 +437,7 @@ class BaseCore {
|
|
|
431
437
|
action: async (args, opt) => {
|
|
432
438
|
const { params } = args || {};
|
|
433
439
|
const object = await this.beforeUpdateMany(params, opt);
|
|
434
|
-
if (object
|
|
440
|
+
if (isOpResult(object)) {
|
|
435
441
|
return object;
|
|
436
442
|
}
|
|
437
443
|
const items = await this.getContext().gateways[this.config.gatewayName]?.updateMany(object);
|
|
@@ -452,7 +458,7 @@ class BaseCore {
|
|
|
452
458
|
action: async (args, opt /* { setStep, validatedItems, sameRequest } */) => {
|
|
453
459
|
const { params } = args || {};
|
|
454
460
|
const object = await this.beforeSet(params, opt);
|
|
455
|
-
if (object
|
|
461
|
+
if (isOpResult(object)) {
|
|
456
462
|
return object;
|
|
457
463
|
}
|
|
458
464
|
const items = await this.getContext().gateways[this.config.gatewayName]?.set(object);
|
|
@@ -473,7 +479,7 @@ class BaseCore {
|
|
|
473
479
|
action: async (args, opt) => {
|
|
474
480
|
const { where } = args || {};
|
|
475
481
|
const object = await this.beforeRemove(where, opt);
|
|
476
|
-
if (object
|
|
482
|
+
if (isOpResult(object)) {
|
|
477
483
|
return object;
|
|
478
484
|
}
|
|
479
485
|
const items = await this.getContext().gateways[this.config.gatewayName]?.remove(object);
|
|
@@ -494,7 +500,7 @@ class BaseCore {
|
|
|
494
500
|
action: async (args, opt) => {
|
|
495
501
|
const { where } = args || {};
|
|
496
502
|
const object = await this.beforeRemoveMany(where, opt);
|
|
497
|
-
if (object
|
|
503
|
+
if (isOpResult(object)) {
|
|
498
504
|
return object;
|
|
499
505
|
}
|
|
500
506
|
const items = await this.getContext().gateways[this.config.gatewayName]?.remove(object);
|
|
@@ -678,6 +684,7 @@ class BaseCore {
|
|
|
678
684
|
else if (space?.apiKey && space?.apiKey !== context.apiKey) {
|
|
679
685
|
return [false, 'API Key is invalid'];
|
|
680
686
|
}
|
|
687
|
+
return [false, 'No authentication provided'];
|
|
681
688
|
}
|
|
682
689
|
return [true, ''];
|
|
683
690
|
}
|
|
@@ -690,7 +697,7 @@ class BaseCore {
|
|
|
690
697
|
addRemovedModifierToFields(fields) {
|
|
691
698
|
const obj = {};
|
|
692
699
|
if (!fields) {
|
|
693
|
-
this.logger.
|
|
700
|
+
this.logger.warn(`The 'addRemovedModifierToFields' method expected its fields parameter to be a field name or an array of table field names but got:`, fields);
|
|
694
701
|
return obj;
|
|
695
702
|
}
|
|
696
703
|
castArray(fields).forEach((field) => {
|
package/build/esm/BaseGateway.js
CHANGED
|
@@ -422,11 +422,12 @@ class BaseGateway {
|
|
|
422
422
|
if (hasSpace) {
|
|
423
423
|
skipFields.push(FIELDS.SPACE_ID);
|
|
424
424
|
}
|
|
425
|
+
const idField = this.getIdFieldUpdateRemove();
|
|
425
426
|
for (let idx = 0, len = objects.length; idx < len; ++idx) {
|
|
426
427
|
const object = objects[idx];
|
|
427
428
|
objects[idx] = await this.transformOnSet(object);
|
|
428
|
-
if (objects[idx][
|
|
429
|
-
objects[idx][
|
|
429
|
+
if (objects[idx][idField] == null) {
|
|
430
|
+
objects[idx][idField] = await this.generateId(object);
|
|
430
431
|
}
|
|
431
432
|
}
|
|
432
433
|
const fields = Array.isArray(mergeFields) ? mergeFields : Object.keys(objects[0]);
|
|
@@ -436,7 +437,7 @@ class BaseGateway {
|
|
|
436
437
|
const items = await this.getBuilder()
|
|
437
438
|
.insert(chunks[idx])
|
|
438
439
|
.returning(this.config.returnFields)
|
|
439
|
-
.onConflict(this.config.setFields ||
|
|
440
|
+
.onConflict(this.config.setFields || idField)
|
|
440
441
|
.merge(pullAll(fields, skipFields));
|
|
441
442
|
result.push(...items);
|
|
442
443
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import check from 'check-types';
|
|
2
2
|
import { OpResult } from '@sdflc/api-helpers';
|
|
3
3
|
import { Logger, LoggerLevels } from '@sdflc/utils';
|
|
4
|
+
import { isOpResult } from './utils';
|
|
4
5
|
/**
|
|
5
6
|
* Base class for REST API controllers.
|
|
6
7
|
*/
|
|
@@ -32,7 +33,7 @@ class BaseController {
|
|
|
32
33
|
return async (req, res, next) => {
|
|
33
34
|
try {
|
|
34
35
|
const result = await method(req, res, next);
|
|
35
|
-
if (result
|
|
36
|
+
if (isOpResult(result)) {
|
|
36
37
|
res.status(result.getHttpStatus()).json(result.toJS());
|
|
37
38
|
}
|
|
38
39
|
else if (result?.redirectUrl) {
|
|
@@ -15,3 +15,4 @@ export declare const validateGetMany: (args: any, opt: BaseCoreActionsInterface)
|
|
|
15
15
|
export declare const validateRemoveMany: (args: any, opt: BaseCoreActionsInterface) => Promise<OpResult[] | {}[]>;
|
|
16
16
|
export declare const validateCreate: ({ rules }: CommonValidatorArgs) => (args: any, opt: BaseCoreActionsInterface) => Promise<{}[] | {}[]>;
|
|
17
17
|
export declare const validateUpdate: ({ rules }: CommonValidatorArgs) => (args: any, opt: BaseCoreActionsInterface) => Promise<{}[] | {}[]>;
|
|
18
|
+
export declare const validateSet: ({ rules }: CommonValidatorArgs) => (args: any, opt: BaseCoreActionsInterface) => Promise<{}[] | {}[]>;
|
|
@@ -289,3 +289,15 @@ export const validateUpdate = ({ rules }) => async (args, opt) => {
|
|
|
289
289
|
}
|
|
290
290
|
return [true, {}];
|
|
291
291
|
};
|
|
292
|
+
export const validateSet = ({ rules }) => async (args, opt) => {
|
|
293
|
+
const { params } = args || {};
|
|
294
|
+
const [paramsCheck] = shouldHaveParams(params);
|
|
295
|
+
if (paramsCheck !== true) {
|
|
296
|
+
return [paramsCheck, {}];
|
|
297
|
+
}
|
|
298
|
+
const [result] = rules.validateSync(params);
|
|
299
|
+
if (result) {
|
|
300
|
+
return [createErrorOnCheckError(result), {}];
|
|
301
|
+
}
|
|
302
|
+
return [true, {}];
|
|
303
|
+
};
|
package/build/esm/utils.d.ts
CHANGED
package/build/esm/utils.js
CHANGED
|
@@ -11,3 +11,15 @@ export const setFunctions = (obj, defaultFuncs = {}) => {
|
|
|
11
11
|
return acc;
|
|
12
12
|
}, {});
|
|
13
13
|
};
|
|
14
|
+
export const isOpResult = (result) => {
|
|
15
|
+
return result &&
|
|
16
|
+
typeof result.code === 'number' &&
|
|
17
|
+
typeof result.didFail === 'function';
|
|
18
|
+
};
|
|
19
|
+
export const isLogger = (obj) => {
|
|
20
|
+
return obj &&
|
|
21
|
+
typeof obj.warn === 'function' &&
|
|
22
|
+
typeof obj.error === 'function' &&
|
|
23
|
+
typeof obj.debug === 'function' &&
|
|
24
|
+
typeof obj.log === 'function';
|
|
25
|
+
};
|
package/build/utils.d.ts
CHANGED
package/build/utils.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.setFunctions = exports.setFunction = void 0;
|
|
6
|
+
exports.isLogger = exports.isOpResult = exports.setFunctions = exports.setFunction = void 0;
|
|
7
7
|
const check_types_1 = __importDefault(require("check-types"));
|
|
8
8
|
const setFunction = (func, defaultFunc) => {
|
|
9
9
|
return check_types_1.default.function(func) ? func : defaultFunc;
|
|
@@ -19,3 +19,17 @@ const setFunctions = (obj, defaultFuncs = {}) => {
|
|
|
19
19
|
}, {});
|
|
20
20
|
};
|
|
21
21
|
exports.setFunctions = setFunctions;
|
|
22
|
+
const isOpResult = (result) => {
|
|
23
|
+
return result &&
|
|
24
|
+
typeof result.code === 'number' &&
|
|
25
|
+
typeof result.didFail === 'function';
|
|
26
|
+
};
|
|
27
|
+
exports.isOpResult = isOpResult;
|
|
28
|
+
const isLogger = (obj) => {
|
|
29
|
+
return obj &&
|
|
30
|
+
typeof obj.warn === 'function' &&
|
|
31
|
+
typeof obj.error === 'function' &&
|
|
32
|
+
typeof obj.debug === 'function' &&
|
|
33
|
+
typeof obj.log === 'function';
|
|
34
|
+
};
|
|
35
|
+
exports.isLogger = isLogger;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdflc/backend-helpers",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.112",
|
|
4
4
|
"description": "Set of helper functions and classes to simplify building backend apps within @sdflc ecosystem",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"exports": {
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"@sdflc/api-helpers": "^1.1.13",
|
|
61
|
-
"@sdflc/utils": "^1.0.
|
|
61
|
+
"@sdflc/utils": "^1.0.25",
|
|
62
62
|
"bcrypt": "^6.0.0",
|
|
63
63
|
"bluebird": "^3.7.2",
|
|
64
64
|
"check-types": "^11.2.3",
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"jsonwebtoken": "^9.0.3",
|
|
72
72
|
"knex": "^3.1.0",
|
|
73
73
|
"lodash": "^4.17.21",
|
|
74
|
-
"pg": "^8.
|
|
74
|
+
"pg": "^8.17.1",
|
|
75
75
|
"redis": "^5.10.0",
|
|
76
76
|
"uuid": "^13.0.0"
|
|
77
77
|
}
|