@sdflc/backend-helpers 1.0.17 → 1.0.21

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.
@@ -4,3 +4,8 @@ import { ApiContextInterface } from './interfaces';
4
4
  * @returns {object} A context object with all available cores
5
5
  */
6
6
  export declare const createApiContext: (args: ApiContextInterface) => any;
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) => any;
@@ -11,9 +11,11 @@ var __assign = (this && this.__assign) || function () {
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.createApiContext = void 0;
14
+ exports.createContextDefault = exports.createApiContext = void 0;
15
15
  var lodash_1 = require("lodash");
16
16
  var uuid_1 = require("uuid");
17
+ var utils_1 = require("@sdflc/utils");
18
+ var database_1 = require("./database");
17
19
  /**
18
20
  * Creates a GraphQL context object with all available cores.
19
21
  * @returns {object} A context object with all available cores
@@ -34,3 +36,59 @@ var createApiContext = function (args) {
34
36
  return context;
35
37
  };
36
38
  exports.createApiContext = createApiContext;
39
+ /**
40
+ * Creates default a GraphQL context object with all avilable cores.
41
+ * @returns {object} A context object with all available cores
42
+ */
43
+ var createContextDefault = function (args) {
44
+ var _a = args || {}, knexConfig = _a.knexConfig, dbSchema = _a.dbSchema, cores = _a.cores, req = _a.req;
45
+ var authorization = req.headers['authorization'];
46
+ var spaceId = req.headers['x-space-id'];
47
+ var apiKey = req.headers['x-api-key'];
48
+ var managementApiKey = req.headers['x-management-api-key'];
49
+ var accountId = req.headers['x-account-id'];
50
+ var roleId = req.headers['x-role-id'];
51
+ var userId = req.headers['x-user-id'];
52
+ var userAgent = req.headers['user-agent'];
53
+ var referer = req.headers['referer'];
54
+ var ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
55
+ var acceptLanguage = req.headers['accept-language'];
56
+ var languages = utils_1.extractLanguages(acceptLanguage);
57
+ var requestedLang = req.query['lang'];
58
+ var requestId = uuid_1.v4();
59
+ var db = new database_1.Database({
60
+ knexConfig: knexConfig,
61
+ dbSchema: dbSchema,
62
+ });
63
+ return exports.createApiContext({
64
+ cores: cores,
65
+ req: req,
66
+ requestId: requestId,
67
+ extendContext: function (_a) {
68
+ var context = _a.context /*, req */;
69
+ context.db = db;
70
+ },
71
+ getCoreContext: function ( /*args: any*/) {
72
+ // TODO: const { req } = args;
73
+ return {
74
+ db: db,
75
+ context: {
76
+ spaceId: spaceId,
77
+ apiKey: apiKey,
78
+ managementApiKey: managementApiKey,
79
+ userAgent: userAgent,
80
+ referer: referer,
81
+ ipAddress: ipAddress,
82
+ acceptLanguage: acceptLanguage,
83
+ languages: languages,
84
+ requestedLang: requestedLang,
85
+ authorization: authorization,
86
+ accountId: accountId,
87
+ roleId: roleId,
88
+ userId: userId,
89
+ },
90
+ };
91
+ },
92
+ });
93
+ };
94
+ exports.createContextDefault = createContextDefault;
@@ -1,35 +1,18 @@
1
- import { BaseCoreValidatorsInterface } from './interfaces/BaseCoreValidatorsInterface';
2
1
  import { Logger } from '@sdflc/utils';
3
- import { BaseCoreInterface, DoingWhatInterface, CoreActionInterface } from './interfaces';
2
+ import { BaseCoreInterface, BaseCoreDoingWhatInterface, BaseCoreActionsInterface, CoreActionInterface, BaseCoreValidatorsInterface } from './interfaces';
4
3
  import { BaseGateway } from './baseGateway';
5
4
  import { Database } from './database';
6
5
  declare class BaseCore {
6
+ protected props: BaseCoreInterface;
7
7
  protected db: Database;
8
8
  protected context: any;
9
9
  protected gateway?: BaseGateway;
10
- protected doingWhat?: DoingWhatInterface;
10
+ protected doingWhat?: BaseCoreDoingWhatInterface;
11
11
  protected logger: Logger;
12
12
  protected name?: string;
13
13
  protected gateways?: any;
14
- protected beforeList: any;
15
- protected beforeCreate: any;
16
- protected beforeCreateMany: any;
17
- protected beforeUpdate: any;
18
- protected beforeUpdateMany: any;
19
- protected beforeSet: any;
20
- protected beforeRemove: any;
21
- protected beforeRemoveMany: any;
22
- protected afterList: any;
23
- protected afterCreate: any;
24
- protected afterCreateMany: any;
25
- protected afterUpdate: any;
26
- protected afterUpdateMany: any;
27
- protected afterSet: any;
28
- protected afterRemove: any;
29
- protected afterRemoveMany: any;
30
14
  protected requestId: string;
31
15
  protected validators: BaseCoreValidatorsInterface;
32
- protected authenticate: any;
33
16
  protected doAuth: boolean;
34
17
  constructor(props: BaseCoreInterface);
35
18
  getDb(): Database;
@@ -37,7 +20,7 @@ declare class BaseCore {
37
20
  getContext(key?: string): any;
38
21
  getGateways(): any;
39
22
  success(data: any, code?: number): any;
40
- failture(code: number, errorMsg: string, data?: any): any;
23
+ failure(code: number, errorMsg: string, data?: any): any;
41
24
  validationFailure(ex: any): any;
42
25
  exceptionFailure(ex: any, doingWhat: string | any, data?: any): any;
43
26
  checkResult(args: any): Promise<void>;
@@ -52,5 +35,23 @@ declare class BaseCore {
52
35
  set(args: any): Promise<any>;
53
36
  remove(args: any): Promise<any>;
54
37
  removeMany(args: any): Promise<any>;
38
+ beforeList(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
39
+ afterList(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
40
+ beforeCreate(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
41
+ afterCreate(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
42
+ beforeCreateMany(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
43
+ afterCreateMany(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
44
+ beforeUpdate(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
45
+ afterUpdate(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
46
+ beforeUpdateMany(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
47
+ afterUpdateMany(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
48
+ beforeSet(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
49
+ afterSet(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
50
+ beforeRemove(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
51
+ afterRemove(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
52
+ beforeRemoveMany(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
53
+ afterRemoveMany(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
54
+ onException(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
55
+ authenticate(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
55
56
  }
56
57
  export { BaseCore };
package/build/baseCore.js CHANGED
@@ -56,10 +56,10 @@ var utils_1 = require("@sdflc/utils");
56
56
  var utils_2 = require("./utils");
57
57
  var constants_1 = __importDefault(require("./constants"));
58
58
  var ACCESS_RIGHTS = constants_1.default.ACCESS_RIGHTS;
59
- var defaultAfterAndBefore = function (params) {
59
+ var defaultAfterAndBefore = function (params, opt) {
60
60
  return params;
61
61
  };
62
- var defaultValidate = function (params, gateways, opt) {
62
+ var defaultValidate = function (params, opt) {
63
63
  return [true, {}];
64
64
  };
65
65
  var defaultValidators = {
@@ -80,34 +80,15 @@ var defaultAuth = function (params) {
80
80
  var BaseCore = /** @class */ (function () {
81
81
  function BaseCore(props) {
82
82
  this.validators = defaultValidators;
83
- this.db = props.db || {};
83
+ this.props = props || {};
84
+ this.db = props.db;
84
85
  this.name = props.name || '';
85
86
  this.context = props.context || {};
86
87
  this.gateway = props.gateway;
87
88
  this.doingWhat = props.doingWhat;
88
89
  this.logger = props.logger || new utils_1.Logger({ level: props.logLevel });
89
90
  this.gateways = __assign({ gateway: this.gateway }, (props.gateways || {}));
90
- this.beforeList = typeof props.beforeList === 'function' ? props.beforeList : defaultAfterAndBefore;
91
- this.beforeCreate = typeof props.beforeCreate === 'function' ? props.beforeCreate : defaultAfterAndBefore;
92
- this.beforeCreateMany =
93
- typeof props.beforeCreateMany === 'function' ? props.beforeCreateMany : defaultAfterAndBefore;
94
- this.beforeUpdate = typeof props.beforeUpdate === 'function' ? props.beforeUpdate : defaultAfterAndBefore;
95
- this.beforeUpdateMany =
96
- typeof props.beforeUpdateMany === 'function' ? props.beforeUpdateMany : defaultAfterAndBefore;
97
- this.beforeSet = typeof props.beforeSet === 'function' ? props.beforeSet : defaultAfterAndBefore;
98
- this.beforeRemove = typeof props.beforeRemove === 'function' ? props.beforeRemove : defaultAfterAndBefore;
99
- this.beforeRemoveMany =
100
- typeof props.beforeRemoveMany === 'function' ? props.beforeRemoveMany : defaultAfterAndBefore;
101
- this.afterList = typeof props.afterList === 'function' ? props.afterList : defaultAfterAndBefore;
102
- this.afterCreate = typeof props.afterCreate === 'function' ? props.afterCreate : defaultAfterAndBefore;
103
- this.afterCreateMany = typeof props.afterCreateMany === 'function' ? props.afterCreateMany : defaultAfterAndBefore;
104
- this.afterUpdate = typeof props.afterUpdate === 'function' ? props.afterUpdate : defaultAfterAndBefore;
105
- this.afterUpdateMany = typeof props.afterUpdateMany === 'function' ? props.afterUpdateMany : defaultAfterAndBefore;
106
- this.afterSet = typeof props.afterSet === 'function' ? props.afterSet : defaultAfterAndBefore;
107
- this.afterRemove = typeof props.afterRemove === 'function' ? props.afterRemove : defaultAfterAndBefore;
108
- this.afterRemoveMany = typeof props.afterRemoveMany === 'function' ? props.afterRemoveMany : defaultAfterAndBefore;
109
91
  this.validators = utils_2.setFunctions((props === null || props === void 0 ? void 0 : props.validators) || {}, this.validators);
110
- this.authenticate = typeof props.authenticate === 'function' ? props.authenticate : defaultAuth;
111
92
  this.doAuth = props.doAuth === true;
112
93
  this.requestId = props.requestId || '';
113
94
  }
@@ -140,7 +121,7 @@ var BaseCore = /** @class */ (function () {
140
121
  errors: [],
141
122
  }).toJS();
142
123
  };
143
- BaseCore.prototype.failture = function (code, errorMsg, data) {
124
+ BaseCore.prototype.failure = function (code, errorMsg, data) {
144
125
  var result = new api_helpers_1.OpResult({ data: data });
145
126
  result.addError('', errorMsg, code);
146
127
  return result.toJS();
@@ -201,19 +182,25 @@ var BaseCore = /** @class */ (function () {
201
182
  };
202
183
  BaseCore.prototype.runAction = function (opt) {
203
184
  return __awaiter(this, void 0, void 0, function () {
204
- var _a, args, validate, doAuth, action, hasTransaction, doingWhat, sameRequest, step, setStep, _b, authResult, errMsg, validatedItems, valideResult, result_1, result, ex_1, what;
185
+ var _a, args, validate, doAuth, action, hasTransaction, doingWhat, sameRequest, step, setStep, actionOpt, _b, authResult, errMsg, validatedItems, valideResult, result_1, result, ex_1, what;
205
186
  return __generator(this, function (_c) {
206
187
  switch (_c.label) {
207
188
  case 0:
208
189
  _a = opt || {}, args = _a.args, validate = _a.validate, doAuth = _a.doAuth, action = _a.action, hasTransaction = _a.hasTransaction, doingWhat = _a.doingWhat;
209
- sameRequest = this.requestId && (args === null || args === void 0 ? void 0 : args.requestId) === this.requestId;
190
+ sameRequest = !!this.requestId && (args === null || args === void 0 ? void 0 : args.requestId) === this.requestId;
210
191
  step = doingWhat || '';
211
192
  setStep = function (stepName) {
212
193
  step = stepName;
213
194
  };
195
+ actionOpt = {
196
+ core: this,
197
+ setStep: setStep,
198
+ sameRequest: sameRequest,
199
+ hasTransaction: hasTransaction,
200
+ };
214
201
  _c.label = 1;
215
202
  case 1:
216
- _c.trys.push([1, 11, , 14]);
203
+ _c.trys.push([1, 11, , 15]);
217
204
  if (!!sameRequest) return [3 /*break*/, 5];
218
205
  if (!hasTransaction) return [3 /*break*/, 3];
219
206
  return [4 /*yield*/, this.db.startTransaction()];
@@ -222,7 +209,7 @@ var BaseCore = /** @class */ (function () {
222
209
  _c.label = 3;
223
210
  case 3:
224
211
  if (!(doAuth === true || (doAuth == null && this.doAuth))) return [3 /*break*/, 5];
225
- return [4 /*yield*/, this.authenticate(opt)];
212
+ return [4 /*yield*/, this.authenticate(opt, actionOpt)];
226
213
  case 4:
227
214
  _b = _c.sent(), authResult = _b[0], errMsg = _b[1];
228
215
  if (!authResult) {
@@ -232,12 +219,7 @@ var BaseCore = /** @class */ (function () {
232
219
  case 5:
233
220
  validatedItems = {};
234
221
  if (!(typeof validate === 'function')) return [3 /*break*/, 8];
235
- return [4 /*yield*/, validate(args, this.getGateways(), {
236
- core: this,
237
- setStep: setStep,
238
- sameRequest: sameRequest,
239
- hasTransaction: hasTransaction,
240
- })];
222
+ return [4 /*yield*/, validate(args, actionOpt)];
241
223
  case 6:
242
224
  valideResult = _c.sent();
243
225
  result_1 = valideResult;
@@ -254,7 +236,7 @@ var BaseCore = /** @class */ (function () {
254
236
  // if there was an error then `result` is expected to be an object with the same structure as the checkit library does
255
237
  _c.sent();
256
238
  return [2 /*return*/, result_1.code != null ? result_1 : new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED })];
257
- case 8: return [4 /*yield*/, action(args, { setStep: setStep, validatedItems: validatedItems, sameRequest: sameRequest })];
239
+ case 8: return [4 /*yield*/, action(args, __assign(__assign({}, actionOpt), { validatedItems: validatedItems }))];
258
240
  case 9:
259
241
  result = _c.sent();
260
242
  return [4 /*yield*/, this.checkResult({ hasTransaction: hasTransaction, result: result, sameRequest: sameRequest })];
@@ -268,7 +250,9 @@ var BaseCore = /** @class */ (function () {
268
250
  case 12:
269
251
  _c.sent();
270
252
  _c.label = 13;
271
- case 13:
253
+ case 13: return [4 /*yield*/, this.onException(args, __assign(__assign({}, actionOpt), { doingWhat: doingWhat, exception: ex_1 }))];
254
+ case 14:
255
+ _c.sent();
272
256
  what = doingWhat;
273
257
  if (doingWhat !== step) {
274
258
  what = {
@@ -276,7 +260,7 @@ var BaseCore = /** @class */ (function () {
276
260
  };
277
261
  }
278
262
  return [2 /*return*/, this.exceptionFailure(ex_1, what)];
279
- case 14: return [2 /*return*/];
263
+ case 15: return [2 /*return*/];
280
264
  }
281
265
  });
282
266
  });
@@ -297,17 +281,17 @@ var BaseCore = /** @class */ (function () {
297
281
  switch (_b.label) {
298
282
  case 0:
299
283
  this.logger.log("Requesting a list for " + this.name + ":", args);
300
- return [4 /*yield*/, this.beforeList(args, this.getGateways(), __assign({ core: this }, opt))];
284
+ return [4 /*yield*/, this.beforeList(args, opt)];
301
285
  case 1:
302
286
  object = _b.sent();
303
287
  return [4 /*yield*/, ((_a = this.gateway) === null || _a === void 0 ? void 0 : _a.list(object))];
304
288
  case 2:
305
289
  items = _b.sent();
306
- return [4 /*yield*/, this.afterList(items, this.getGateways(), __assign({ core: this }, opt))];
290
+ return [4 /*yield*/, this.afterList(items, opt)];
307
291
  case 3:
308
292
  result = _b.sent();
309
293
  // ON NO DATA RETURN EMPTY CODE
310
- return [2 /*return*/, this.success(result)];
294
+ return [2 /*return*/, this.success(result /*, result.length > 0 OP_RESULT_CODES.OK : OP_RESULT_CODES.*/)];
311
295
  }
312
296
  });
313
297
  }); },
@@ -394,20 +378,20 @@ var BaseCore = /** @class */ (function () {
394
378
  case 0:
395
379
  params = (args || {}).params;
396
380
  this.logger.log("Requesting creation of an item for " + this.name + ":", args);
397
- return [4 /*yield*/, this.beforeCreate(params, this.getGateways(), __assign({ core: this }, opt))];
381
+ return [4 /*yield*/, this.beforeCreate(params, opt)];
398
382
  case 1:
399
383
  object = _b.sent();
400
384
  return [4 /*yield*/, ((_a = this.gateway) === null || _a === void 0 ? void 0 : _a.create(object))];
401
385
  case 2:
402
386
  item = _b.sent();
403
- return [4 /*yield*/, this.afterCreate(item, this.getGateways(), __assign({ core: this }, opt))];
387
+ return [4 /*yield*/, this.afterCreate(item, opt)];
404
388
  case 3:
405
389
  result = _b.sent();
406
390
  return [2 /*return*/, this.success(result)];
407
391
  }
408
392
  });
409
393
  }); },
410
- hasTransaction: false,
394
+ hasTransaction: true,
411
395
  doingWhat: (_c = (_b = (_a = this.doingWhat) === null || _a === void 0 ? void 0 : _a.create) !== null && _b !== void 0 ? _b : this.name) !== null && _c !== void 0 ? _c : 'creating an item',
412
396
  })];
413
397
  });
@@ -430,20 +414,20 @@ var BaseCore = /** @class */ (function () {
430
414
  case 0:
431
415
  params = (args || {}).params;
432
416
  this.logger.log("Requesting creation of multiple items for " + this.name + ":", args);
433
- return [4 /*yield*/, this.beforeCreateMany(params, this.getGateways(), __assign({ core: this }, opt))];
417
+ return [4 /*yield*/, this.beforeCreateMany(params, opt)];
434
418
  case 1:
435
419
  objects = _b.sent();
436
420
  return [4 /*yield*/, ((_a = this.gateway) === null || _a === void 0 ? void 0 : _a.create(objects))];
437
421
  case 2:
438
422
  items = _b.sent();
439
- return [4 /*yield*/, this.afterCreateMany(items, this.getGateways(), __assign({ core: this }, opt))];
423
+ return [4 /*yield*/, this.afterCreateMany(items, opt)];
440
424
  case 3:
441
425
  result = _b.sent();
442
426
  return [2 /*return*/, this.success(result)];
443
427
  }
444
428
  });
445
429
  }); },
446
- hasTransaction: false,
430
+ hasTransaction: true,
447
431
  doingWhat: (_c = (_b = (_a = this.doingWhat) === null || _a === void 0 ? void 0 : _a.createMany) !== null && _b !== void 0 ? _b : this.name) !== null && _c !== void 0 ? _c : 'creating multiple items',
448
432
  })];
449
433
  });
@@ -466,20 +450,20 @@ var BaseCore = /** @class */ (function () {
466
450
  case 0:
467
451
  _a = args || {}, where = _a.where, params = _a.params;
468
452
  this.logger.log("Requesting update of an item for " + this.name + ":", args);
469
- return [4 /*yield*/, this.beforeUpdate(params, this.getGateways(), __assign({ core: this }, opt))];
453
+ return [4 /*yield*/, this.beforeUpdate(params, opt)];
470
454
  case 1:
471
455
  object = _c.sent();
472
456
  return [4 /*yield*/, ((_b = this.gateway) === null || _b === void 0 ? void 0 : _b.update(where, object))];
473
457
  case 2:
474
458
  item = _c.sent();
475
- return [4 /*yield*/, this.afterUpdate(item, this.getGateways(), __assign({ core: this }, opt))];
459
+ return [4 /*yield*/, this.afterUpdate(item, opt)];
476
460
  case 3:
477
461
  result = _c.sent();
478
462
  return [2 /*return*/, this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND)];
479
463
  }
480
464
  });
481
465
  }); },
482
- hasTransaction: false,
466
+ hasTransaction: true,
483
467
  doingWhat: (_c = (_b = (_a = this.doingWhat) === null || _a === void 0 ? void 0 : _a.update) !== null && _b !== void 0 ? _b : this.name) !== null && _c !== void 0 ? _c : 'updating an item',
484
468
  })];
485
469
  });
@@ -502,20 +486,20 @@ var BaseCore = /** @class */ (function () {
502
486
  case 0:
503
487
  params = (args || {}).params;
504
488
  this.logger.log("Requesting update of an item for " + this.name + ":", args);
505
- return [4 /*yield*/, this.beforeUpdateMany(params, this.getGateways(), __assign({ core: this }, opt))];
489
+ return [4 /*yield*/, this.beforeUpdateMany(params, opt)];
506
490
  case 1:
507
491
  object = _b.sent();
508
492
  return [4 /*yield*/, ((_a = this.gateway) === null || _a === void 0 ? void 0 : _a.updateMany(object))];
509
493
  case 2:
510
494
  items = _b.sent();
511
- return [4 /*yield*/, this.afterUpdateMany(items, this.getGateways(), __assign({ core: this }, opt))];
495
+ return [4 /*yield*/, this.afterUpdateMany(items, opt)];
512
496
  case 3:
513
497
  result = _b.sent();
514
498
  return [2 /*return*/, this.success(result)];
515
499
  }
516
500
  });
517
501
  }); },
518
- hasTransaction: false,
502
+ hasTransaction: true,
519
503
  doingWhat: (_c = (_b = (_a = this.doingWhat) === null || _a === void 0 ? void 0 : _a.updateMany) !== null && _b !== void 0 ? _b : this.name) !== null && _c !== void 0 ? _c : 'updating multiple items',
520
504
  })];
521
505
  });
@@ -538,20 +522,20 @@ var BaseCore = /** @class */ (function () {
538
522
  case 0:
539
523
  params = (args || {}).params;
540
524
  this.logger.log("Requesting setting of item(s) for " + this.name + ":", args);
541
- return [4 /*yield*/, this.beforeSet(params, __assign({ core: this }, opt))];
525
+ return [4 /*yield*/, this.beforeSet(params, opt)];
542
526
  case 1:
543
527
  object = _b.sent();
544
528
  return [4 /*yield*/, ((_a = this.gateway) === null || _a === void 0 ? void 0 : _a.set(object))];
545
529
  case 2:
546
530
  items = _b.sent();
547
- return [4 /*yield*/, this.afterSet(items, __assign({ core: this }, opt))];
531
+ return [4 /*yield*/, this.afterSet(items, opt)];
548
532
  case 3:
549
533
  result = _b.sent();
550
534
  return [2 /*return*/, this.success(result)];
551
535
  }
552
536
  });
553
537
  }); },
554
- hasTransaction: false,
538
+ hasTransaction: true,
555
539
  doingWhat: (_c = (_b = (_a = this.doingWhat) === null || _a === void 0 ? void 0 : _a.updateMany) !== null && _b !== void 0 ? _b : this.name) !== null && _c !== void 0 ? _c : 'updating multiple items',
556
540
  })];
557
541
  });
@@ -574,13 +558,13 @@ var BaseCore = /** @class */ (function () {
574
558
  case 0:
575
559
  where = (args || {}).where;
576
560
  this.logger.log("Requesting removal of an item for " + this.name + ":", args);
577
- return [4 /*yield*/, this.beforeRemove(where, this.getGateways(), __assign({ core: this }, opt))];
561
+ return [4 /*yield*/, this.beforeRemove(where, opt)];
578
562
  case 1:
579
563
  object = _b.sent();
580
564
  return [4 /*yield*/, ((_a = this.gateway) === null || _a === void 0 ? void 0 : _a.remove(object))];
581
565
  case 2:
582
566
  items = _b.sent();
583
- return [4 /*yield*/, this.afterRemove(items, this.getGateways(), __assign({ core: this }, opt))];
567
+ return [4 /*yield*/, this.afterRemove(items, opt)];
584
568
  case 3:
585
569
  result = _b.sent();
586
570
  return [2 /*return*/, this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND)];
@@ -610,13 +594,13 @@ var BaseCore = /** @class */ (function () {
610
594
  case 0:
611
595
  where = (args || {}).where;
612
596
  this.logger.log("Requesting removal of multiple items for " + this.name + ":", args);
613
- return [4 /*yield*/, this.beforeRemoveMany(where, this.getGateways(), __assign({ core: this }, opt))];
597
+ return [4 /*yield*/, this.beforeRemoveMany(where, opt)];
614
598
  case 1:
615
599
  object = _b.sent();
616
600
  return [4 /*yield*/, ((_a = this.gateway) === null || _a === void 0 ? void 0 : _a.remove(object))];
617
601
  case 2:
618
602
  items = _b.sent();
619
- return [4 /*yield*/, this.afterRemoveMany(items, this.getGateways(), __assign({ core: this }, opt))];
603
+ return [4 /*yield*/, this.afterRemoveMany(items, opt)];
620
604
  case 3:
621
605
  result = _b.sent();
622
606
  return [2 /*return*/, this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND)];
@@ -629,6 +613,168 @@ var BaseCore = /** @class */ (function () {
629
613
  });
630
614
  });
631
615
  };
616
+ BaseCore.prototype.beforeList = function (params, opt) {
617
+ return __awaiter(this, void 0, void 0, function () {
618
+ var fn;
619
+ return __generator(this, function (_a) {
620
+ fn = typeof this.props.beforeList === 'function' ? this.props.beforeList : defaultAfterAndBefore;
621
+ return [2 /*return*/, fn(params, opt)];
622
+ });
623
+ });
624
+ };
625
+ BaseCore.prototype.afterList = function (params, opt) {
626
+ return __awaiter(this, void 0, void 0, function () {
627
+ var fn;
628
+ return __generator(this, function (_a) {
629
+ fn = typeof this.props.afterList === 'function' ? this.props.afterList : defaultAfterAndBefore;
630
+ return [2 /*return*/, fn(params, opt)];
631
+ });
632
+ });
633
+ };
634
+ BaseCore.prototype.beforeCreate = function (params, opt) {
635
+ return __awaiter(this, void 0, void 0, function () {
636
+ var fn;
637
+ return __generator(this, function (_a) {
638
+ fn = typeof this.props.beforeCreate === 'function' ? this.props.beforeCreate : defaultAfterAndBefore;
639
+ return [2 /*return*/, fn(params, opt)];
640
+ });
641
+ });
642
+ };
643
+ BaseCore.prototype.afterCreate = function (params, opt) {
644
+ return __awaiter(this, void 0, void 0, function () {
645
+ var fn;
646
+ return __generator(this, function (_a) {
647
+ fn = typeof this.props.afterCreate === 'function' ? this.props.afterCreate : defaultAfterAndBefore;
648
+ return [2 /*return*/, fn(params, opt)];
649
+ });
650
+ });
651
+ };
652
+ BaseCore.prototype.beforeCreateMany = function (params, opt) {
653
+ return __awaiter(this, void 0, void 0, function () {
654
+ var fn;
655
+ return __generator(this, function (_a) {
656
+ fn = typeof this.props.beforeCreateMany === 'function' ? this.props.beforeCreateMany : defaultAfterAndBefore;
657
+ return [2 /*return*/, fn(params, opt)];
658
+ });
659
+ });
660
+ };
661
+ BaseCore.prototype.afterCreateMany = function (params, opt) {
662
+ return __awaiter(this, void 0, void 0, function () {
663
+ var fn;
664
+ return __generator(this, function (_a) {
665
+ fn = typeof this.props.afterCreateMany === 'function' ? this.props.afterCreateMany : defaultAfterAndBefore;
666
+ return [2 /*return*/, fn(params, opt)];
667
+ });
668
+ });
669
+ };
670
+ BaseCore.prototype.beforeUpdate = function (params, opt) {
671
+ return __awaiter(this, void 0, void 0, function () {
672
+ var fn;
673
+ return __generator(this, function (_a) {
674
+ fn = typeof this.props.beforeUpdate === 'function' ? this.props.beforeUpdate : defaultAfterAndBefore;
675
+ return [2 /*return*/, fn(params, opt)];
676
+ });
677
+ });
678
+ };
679
+ BaseCore.prototype.afterUpdate = function (params, opt) {
680
+ return __awaiter(this, void 0, void 0, function () {
681
+ var fn;
682
+ return __generator(this, function (_a) {
683
+ fn = typeof this.props.afterUpdate === 'function' ? this.props.afterUpdate : defaultAfterAndBefore;
684
+ return [2 /*return*/, fn(params, opt)];
685
+ });
686
+ });
687
+ };
688
+ BaseCore.prototype.beforeUpdateMany = function (params, opt) {
689
+ return __awaiter(this, void 0, void 0, function () {
690
+ var fn;
691
+ return __generator(this, function (_a) {
692
+ fn = typeof this.props.beforeUpdateMany === 'function' ? this.props.beforeUpdateMany : defaultAfterAndBefore;
693
+ return [2 /*return*/, fn(params, opt)];
694
+ });
695
+ });
696
+ };
697
+ BaseCore.prototype.afterUpdateMany = function (params, opt) {
698
+ return __awaiter(this, void 0, void 0, function () {
699
+ var fn;
700
+ return __generator(this, function (_a) {
701
+ fn = typeof this.props.afterUpdateMany === 'function' ? this.props.afterUpdateMany : defaultAfterAndBefore;
702
+ return [2 /*return*/, fn(params, opt)];
703
+ });
704
+ });
705
+ };
706
+ BaseCore.prototype.beforeSet = function (params, opt) {
707
+ return __awaiter(this, void 0, void 0, function () {
708
+ var fn;
709
+ return __generator(this, function (_a) {
710
+ fn = typeof this.props.beforeSet === 'function' ? this.props.beforeSet : defaultAfterAndBefore;
711
+ return [2 /*return*/, fn(params, opt)];
712
+ });
713
+ });
714
+ };
715
+ BaseCore.prototype.afterSet = function (params, opt) {
716
+ return __awaiter(this, void 0, void 0, function () {
717
+ var fn;
718
+ return __generator(this, function (_a) {
719
+ fn = typeof this.props.afterSet === 'function' ? this.props.afterSet : defaultAfterAndBefore;
720
+ return [2 /*return*/, fn(params, opt)];
721
+ });
722
+ });
723
+ };
724
+ BaseCore.prototype.beforeRemove = function (params, opt) {
725
+ return __awaiter(this, void 0, void 0, function () {
726
+ var fn;
727
+ return __generator(this, function (_a) {
728
+ fn = typeof this.props.beforeRemove === 'function' ? this.props.beforeRemove : defaultAfterAndBefore;
729
+ return [2 /*return*/, fn(params, opt)];
730
+ });
731
+ });
732
+ };
733
+ BaseCore.prototype.afterRemove = function (params, opt) {
734
+ return __awaiter(this, void 0, void 0, function () {
735
+ var fn;
736
+ return __generator(this, function (_a) {
737
+ fn = typeof this.props.afterRemove === 'function' ? this.props.afterRemove : defaultAfterAndBefore;
738
+ return [2 /*return*/, fn(params, opt)];
739
+ });
740
+ });
741
+ };
742
+ BaseCore.prototype.beforeRemoveMany = function (params, opt) {
743
+ return __awaiter(this, void 0, void 0, function () {
744
+ var fn;
745
+ return __generator(this, function (_a) {
746
+ fn = typeof this.props.beforeRemoveMany === 'function' ? this.props.beforeRemoveMany : defaultAfterAndBefore;
747
+ return [2 /*return*/, fn(params, opt)];
748
+ });
749
+ });
750
+ };
751
+ BaseCore.prototype.afterRemoveMany = function (params, opt) {
752
+ return __awaiter(this, void 0, void 0, function () {
753
+ var fn;
754
+ return __generator(this, function (_a) {
755
+ fn = typeof this.props.afterRemoveMany === 'function' ? this.props.afterRemoveMany : defaultAfterAndBefore;
756
+ return [2 /*return*/, fn(params, opt)];
757
+ });
758
+ });
759
+ };
760
+ BaseCore.prototype.onException = function (params, opt) {
761
+ return __awaiter(this, void 0, void 0, function () {
762
+ var fn;
763
+ return __generator(this, function (_a) {
764
+ fn = typeof this.props.onException === 'function' ? this.props.onException : defaultAfterAndBefore;
765
+ return [2 /*return*/, fn(params, opt)];
766
+ });
767
+ });
768
+ };
769
+ BaseCore.prototype.authenticate = function (params, opt) {
770
+ return __awaiter(this, void 0, void 0, function () {
771
+ var fn;
772
+ return __generator(this, function (_a) {
773
+ fn = typeof this.props.authenticate === 'function' ? this.props.authenticate : defaultAuth;
774
+ return [2 /*return*/, fn(params, opt)];
775
+ });
776
+ });
777
+ };
632
778
  return BaseCore;
633
779
  }());
634
780
  exports.BaseCore = BaseCore;
@@ -13,7 +13,7 @@ declare class BaseGateway {
13
13
  constructor(props: BaseGatewayInterface);
14
14
  getDb(): import("./database").Database;
15
15
  getBuilder(): any;
16
- getIdField(): string;
16
+ getIdField(): any;
17
17
  getCore(): BaseCore;
18
18
  setCore(core: BaseCore): void;
19
19
  get(id: any): Promise<any>;
@@ -224,6 +224,7 @@ var BaseGateway = /** @class */ (function () {
224
224
  BaseGateway.prototype.updateMany = function (params) {
225
225
  return __awaiter(this, void 0, void 0, function () {
226
226
  var results, idx, len, item, items;
227
+ var _this = this;
227
228
  return __generator(this, function (_a) {
228
229
  switch (_a.label) {
229
230
  case 0:
@@ -244,7 +245,11 @@ var BaseGateway = /** @class */ (function () {
244
245
  case 3:
245
246
  ++idx;
246
247
  return [3 /*break*/, 1];
247
- case 4: return [2 /*return*/, results];
248
+ case 4:
249
+ if (results.length > 0 && this.config.noCache !== true) {
250
+ results.forEach(function (item) { return _this.loader.clear(item[_this.getIdField()]); });
251
+ }
252
+ return [2 /*return*/, results];
248
253
  }
249
254
  });
250
255
  });
@@ -0,0 +1,20 @@
1
+ export declare const buildDefaultGatewayProps: (args: any) => {
2
+ filter: (query: any, params: any) => any;
3
+ keyPrefix: any;
4
+ context: any;
5
+ buildKey: any;
6
+ table: any;
7
+ idField: any;
8
+ setFields: any;
9
+ readAll: boolean;
10
+ redisClient: any;
11
+ cacheTtl: any;
12
+ noCache: boolean;
13
+ idGenerator: () => string;
14
+ insertFormatter: (args?: any) => any;
15
+ updateFormatter: (args?: any) => any;
16
+ updateFilter: (query: any, params: any) => void;
17
+ removeFormatter: () => any;
18
+ removeFilter: (query: any, params: any) => void;
19
+ returnFields: any;
20
+ };
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.buildDefaultGatewayProps = void 0;
7
+ var uuid_1 = require("uuid");
8
+ var dayjs_1 = __importDefault(require("dayjs"));
9
+ var lodash_1 = require("lodash");
10
+ var constants_1 = __importDefault(require("./constants"));
11
+ var FIELDS = constants_1.default.FIELDS, STATUSES = constants_1.default.STATUSES;
12
+ var buildDefaultGatewayProps = function (args) {
13
+ var config = args.config, tableName = args.tableName, filter = args.filter, updateFilter = args.updateFilter, hasStatus = args.hasStatus, hasRemovedAt = args.hasRemovedAt, hasRemovedAtStr = args.hasRemovedAtStr, setFields = args.setFields, idField = args.idField, activeStatuses = args.activeStatuses, redisClient = args.redisClient, context = args.context, buildKey = args.buildKey, returnFields = args.returnFields;
14
+ return {
15
+ filter: function (query, params) {
16
+ if (hasStatus === undefined || hasStatus === true) {
17
+ query.whereIn(FIELDS.STATUS, activeStatuses);
18
+ }
19
+ else if (hasRemovedAt === undefined || hasRemovedAt === true) {
20
+ query.whereNull(FIELDS.REMOVED_AT);
21
+ }
22
+ if (typeof filter === 'function') {
23
+ filter(query, params);
24
+ }
25
+ return query;
26
+ },
27
+ keyPrefix: tableName,
28
+ context: context,
29
+ buildKey: buildKey,
30
+ table: tableName,
31
+ idField: idField || FIELDS.ID,
32
+ setFields: setFields,
33
+ readAll: false,
34
+ redisClient: redisClient,
35
+ cacheTtl: config.cacheTtl,
36
+ noCache: false,
37
+ idGenerator: function () {
38
+ var id = uuid_1.v4();
39
+ return id;
40
+ },
41
+ insertFormatter: function (args) {
42
+ return args;
43
+ },
44
+ updateFormatter: function (args) {
45
+ return args;
46
+ },
47
+ updateFilter: function (query, params) {
48
+ var id = (params || {}).id;
49
+ if (id) {
50
+ query.whereIn(FIELDS.ID, lodash_1.castArray(id));
51
+ }
52
+ if (typeof updateFilter === 'function') {
53
+ updateFilter(query, params);
54
+ }
55
+ },
56
+ removeFormatter: function () {
57
+ var removalTime = dayjs_1.default();
58
+ var removedAt = removalTime.toDate();
59
+ var obj = {
60
+ removedAt: removedAt,
61
+ };
62
+ if (hasRemovedAtStr === undefined || hasRemovedAtStr === true) {
63
+ obj[FIELDS.REMOVED_AT_STR] = removalTime.format('YYYYMMDDTHHmmssZ');
64
+ }
65
+ if (hasStatus === undefined || hasStatus === true) {
66
+ obj[FIELDS.STATUS] = STATUSES.REMOVED;
67
+ }
68
+ return obj;
69
+ },
70
+ removeFilter: function (query, params) {
71
+ var arr = [];
72
+ if (Array.isArray(params)) {
73
+ arr.push.apply(arr, params);
74
+ }
75
+ else {
76
+ arr.push(params);
77
+ }
78
+ query = query.whereIn(FIELDS.ID, arr.map(function (item) { return item[FIELDS.ID]; }));
79
+ if (hasStatus === undefined || hasStatus === true) {
80
+ query.where(FIELDS.STATUS, '!=', STATUSES.REMOVED);
81
+ }
82
+ },
83
+ returnFields: returnFields || [FIELDS.ID],
84
+ };
85
+ };
86
+ exports.buildDefaultGatewayProps = buildDefaultGatewayProps;
@@ -1,29 +1,2 @@
1
- declare const constants: {
2
- ID_FIELD: string;
3
- FIELDS: {
4
- STATUS: string;
5
- VERSION: string;
6
- CREATED_BY: string;
7
- UPDATED_BY: string;
8
- REMOVED_BY: string;
9
- CREATED_AT: string;
10
- UPDATED_AT: string;
11
- REMOVED_AT: string;
12
- REMOVED_AT_STR: string;
13
- };
14
- STATUSES: {
15
- TEST: number;
16
- ACTIVE: number;
17
- DRAFT: number;
18
- DISABLED: number;
19
- REMOVED: number;
20
- };
21
- ACCESS_RIGHTS: {
22
- LIST: number;
23
- GET: number;
24
- CREATE: number;
25
- UPDATE: number;
26
- REMOVE: number;
27
- };
28
- };
1
+ declare const constants: any;
29
2
  export default constants;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var constants = {
4
4
  ID_FIELD: 'id',
5
5
  FIELDS: {
6
+ ID: 'id',
6
7
  STATUS: 'status',
7
8
  VERSION: 'version',
8
9
  CREATED_BY: 'created_by',
@@ -12,13 +13,20 @@ var constants = {
12
13
  UPDATED_AT: 'updated_at',
13
14
  REMOVED_AT: 'removed_at',
14
15
  REMOVED_AT_STR: 'removed_at_str',
16
+ LANG: 'LANG',
17
+ ACCOUNT_ID: 'account_id',
18
+ ROLE_ID: 'role_id',
19
+ USER_ID: 'user_id',
20
+ ORDER_NO: 'order_no',
15
21
  },
16
22
  STATUSES: {
17
23
  TEST: 50,
18
24
  ACTIVE: 100,
19
25
  DRAFT: 200,
20
- DISABLED: 300,
21
- REMOVED: 1000,
26
+ PENDING: 300,
27
+ BLOCKED: 1000,
28
+ DISABLED: 5000,
29
+ REMOVED: 10000,
22
30
  },
23
31
  ACCESS_RIGHTS: {
24
32
  LIST: 1,
@@ -1,3 +1,10 @@
1
1
  import { DbFieldAddDefaultInterface } from './interfaces';
2
2
  declare const dbFieldAddDefaults: (table: any, opt?: DbFieldAddDefaultInterface | undefined) => 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 defaultFields: (opt?: any) => {
8
+ [x: number]: string;
9
+ };
3
10
  export { dbFieldAddDefaults };
@@ -3,38 +3,82 @@ 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.dbFieldAddDefaults = void 0;
6
+ exports.dbFieldAddDefaults = exports.defaultFields = exports.addUserId = exports.addSpaceId = exports.addAmount = exports.addOrderNo = void 0;
7
7
  var constants_1 = __importDefault(require("./constants"));
8
+ var utils_1 = require("@sdflc/utils");
9
+ var FIELDS = constants_1.default.FIELDS, STATUSES = constants_1.default.STATUSES;
8
10
  var dbFieldAddDefaults = function (table, opt) {
9
11
  var _a = opt || {}, addUserInfo = _a.addUserInfo, addVersion = _a.addVersion, addStatus = _a.addStatus, skipTimestamps = _a.skipTimestamps, skipRemovedAtStr = _a.skipRemovedAtStr;
10
12
  if (addStatus) {
11
13
  table
12
- .integer(constants_1.default.FIELDS.STATUS)
13
- .defaultTo(constants_1.default.STATUSES.ACTIVE)
14
- .comment('The field is used to specify record status: 50 - Test, 100 - Active, 200 - Draft, 300 - Disabled, 1000 - Removed');
14
+ .integer(FIELDS.STATUS)
15
+ .defaultTo(STATUSES.ACTIVE)
16
+ .comment('The field is used to specify record status: 50 - Test, 100 - Active, 200 - Draft, 300 - Pending, 1000 - Blocked, 5000 - Disabled, 10000 - Removed');
15
17
  }
16
18
  if (addVersion) {
17
- table.integer(constants_1.default.FIELDS.VERSION).defaultTo(0).comment('The field is used to control versioning');
19
+ table.integer(FIELDS.VERSION).defaultTo(0).comment('The field is used to control versioning');
18
20
  }
19
21
  if (addUserInfo) {
20
- table.uuid(constants_1.default.FIELDS.CREATED_BY).comment('ID of user who created the record');
21
- table.uuid(constants_1.default.FIELDS.UPDATED_BY).comment('ID of the last user who update the record');
22
- table.uuid(constants_1.default.FIELDS.REMOVED_BY).comment('ID of user who removed the record');
22
+ table.uuid(FIELDS.CREATED_BY).comment('ID of user who created the record');
23
+ table.uuid(FIELDS.UPDATED_BY).comment('ID of the last user who update the record');
24
+ table.uuid(FIELDS.REMOVED_BY).comment('ID of user who removed the record');
23
25
  }
24
26
  if (skipTimestamps !== true) {
25
27
  table.timestamps(true, true);
26
28
  table
27
- .dateTime(constants_1.default.FIELDS.REMOVED_AT)
29
+ .dateTime(FIELDS.REMOVED_AT)
28
30
  .comment('Date and time of removal. As it is nullable it cannot be used in any unique index');
29
31
  if (skipRemovedAtStr !== true) {
30
- table.text(constants_1.default.FIELDS.REMOVED_AT_STR).defaultTo('').comment('Used to create unique indexes.');
32
+ table.text(FIELDS.REMOVED_AT_STR).defaultTo('').comment('Used to create unique indexes.');
31
33
  }
32
34
  }
33
35
  if (addStatus) {
34
- table.index([constants_1.default.FIELDS.STATUS]);
36
+ table.index([FIELDS.STATUS]);
35
37
  }
36
38
  if (skipRemovedAtStr !== true) {
37
- table.index([constants_1.default.FIELDS.REMOVED_AT_STR]);
39
+ table.index([FIELDS.REMOVED_AT_STR]);
38
40
  }
39
41
  };
40
42
  exports.dbFieldAddDefaults = dbFieldAddDefaults;
43
+ var addOrderNo = function (table, opt) {
44
+ var chain = table.text(FIELDS.ORDER_NO).notNullable().defaultTo('10000000');
45
+ if ((opt === null || opt === void 0 ? void 0 : opt.addIndex) === true) {
46
+ table.index([FIELDS.ORDER_NO]);
47
+ }
48
+ return chain;
49
+ };
50
+ exports.addOrderNo = addOrderNo;
51
+ var addAmount = function (table, fieldName) {
52
+ return table.decimal(fieldName, 19, 4);
53
+ };
54
+ exports.addAmount = addAmount;
55
+ var addSpaceId = function (table, opt) {
56
+ var chain = table.text(FIELDS.spaceId).notNullable().comment('Reference to the space table');
57
+ if ((opt === null || opt === void 0 ? void 0 : opt.addForeignKey) === true) {
58
+ table.foreign(FIELDS.spaceId).references("space." + FIELDS.ID).onUpdate('CASCADE').onDelete('CASCADE');
59
+ }
60
+ return chain;
61
+ };
62
+ exports.addSpaceId = addSpaceId;
63
+ var addUserId = function (table, opt) {
64
+ var chain = table.uuid(FIELDS.USER_ID).notNullable().comment('Reference to the user table');
65
+ if ((opt === null || opt === void 0 ? void 0 : opt.addForeignKey) === true) {
66
+ table.foreign(FIELDS.userId).references("user." + FIELDS.ID).onUpdate('CASCADE').onDelete('CASCADE');
67
+ }
68
+ return chain;
69
+ };
70
+ exports.addUserId = addUserId;
71
+ var defaultFields = function (opt) {
72
+ var _a;
73
+ var addUserInfo = (opt || {}).addUserInfo;
74
+ var obj = (_a = {},
75
+ _a[FIELDS.CREATED_AT] = '2021-01-01T00:00:00Z',
76
+ _a[FIELDS.UPDATED_AT] = '2021-01-01T00:00:00Z',
77
+ _a);
78
+ if (addUserInfo === true) {
79
+ obj[FIELDS.CREATED_BY] = utils_1.UUID_EMPTY;
80
+ obj[FIELDS.UPDATED_BY] = utils_1.UUID_EMPTY;
81
+ }
82
+ return obj;
83
+ };
84
+ exports.defaultFields = defaultFields;
package/build/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import constants from './constants';
2
- export { CacheInterface, LoaderInterface, BaseGatewayInterface, ListArgsInterface, SortItemInterface, PaginationInterface, BaseCoreInterface, DoingWhatInterface, CoreActionInterface, ApiContextInterface, DbFieldAddDefaultInterface, BuildConfigOptions, DatabaseOptions, DatabaseReadInterface, BuildDefaultResolvers, BaseCoreValidatorsInterface, } from './interfaces';
2
+ export { CacheInterface, LoaderInterface, BaseGatewayInterface, ListArgsInterface, SortItemInterface, PaginationInterface, BaseCoreInterface, BaseCoreDoingWhatInterface, CoreActionInterface, ApiContextInterface, DbFieldAddDefaultInterface, BuildConfigOptions, DatabaseOptions, DatabaseReadInterface, BuildDefaultResolvers, BaseCoreValidatorsInterface, UserTokenParams, CreateUserTokenArgs, VerifyUserTokenArgs, BaseCoreActionsInterface, } from './interfaces';
3
3
  export { buildConfig } from './configBulder';
4
4
  export { Database } from './database';
5
5
  export { dbCleanTable, dbListTables, dbClean, dbWipe, dbReset, dbTransformSeedsToObject, dbRunMigrations, } from './dbHelpers';
@@ -11,9 +11,10 @@ export { checkArray, createErrorOnCheckError } from './checkItHelpers';
11
11
  export { DataCache } from './dataCache';
12
12
  export { BaseGateway } from './baseGateway';
13
13
  export { BaseCore } from './baseCore';
14
- export { createApiContext } from './apiContext';
14
+ export { createApiContext, createContextDefault } from './apiContext';
15
15
  export { BaseController } from './baseController';
16
16
  export { dbFieldAddDefaults } from './dbTableHelpers';
17
17
  export { buildDefaultResolvers } from './resolversHelpers';
18
18
  export { createUserToken, verifyUserToken } from './userToken';
19
19
  export { constants };
20
+ export { buildDefaultGatewayProps } from './baseGatewayHelpers';
package/build/index.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.constants = exports.verifyUserToken = exports.createUserToken = exports.buildDefaultResolvers = exports.dbFieldAddDefaults = exports.BaseController = exports.createApiContext = exports.BaseCore = exports.BaseGateway = exports.DataCache = exports.createErrorOnCheckError = exports.checkArray = exports.createRedisClient = exports.buildKnexConfig = exports.dbSeed = exports.DbMigratorAndSeeder = exports.dbRunMigrations = exports.dbTransformSeedsToObject = exports.dbReset = exports.dbWipe = exports.dbClean = exports.dbListTables = exports.dbCleanTable = exports.Database = exports.buildConfig = void 0;
6
+ exports.buildDefaultGatewayProps = exports.constants = exports.verifyUserToken = exports.createUserToken = exports.buildDefaultResolvers = exports.dbFieldAddDefaults = exports.BaseController = exports.createContextDefault = exports.createApiContext = exports.BaseCore = exports.BaseGateway = exports.DataCache = exports.createErrorOnCheckError = exports.checkArray = exports.createRedisClient = exports.buildKnexConfig = exports.dbSeed = exports.DbMigratorAndSeeder = exports.dbRunMigrations = exports.dbTransformSeedsToObject = exports.dbReset = exports.dbWipe = exports.dbClean = exports.dbListTables = exports.dbCleanTable = exports.Database = exports.buildConfig = void 0;
7
7
  var constants_1 = __importDefault(require("./constants"));
8
8
  exports.constants = constants_1.default;
9
9
  var configBulder_1 = require("./configBulder");
@@ -37,6 +37,7 @@ var baseCore_1 = require("./baseCore");
37
37
  Object.defineProperty(exports, "BaseCore", { enumerable: true, get: function () { return baseCore_1.BaseCore; } });
38
38
  var apiContext_1 = require("./apiContext");
39
39
  Object.defineProperty(exports, "createApiContext", { enumerable: true, get: function () { return apiContext_1.createApiContext; } });
40
+ Object.defineProperty(exports, "createContextDefault", { enumerable: true, get: function () { return apiContext_1.createContextDefault; } });
40
41
  var baseController_1 = require("./baseController");
41
42
  Object.defineProperty(exports, "BaseController", { enumerable: true, get: function () { return baseController_1.BaseController; } });
42
43
  var dbTableHelpers_1 = require("./dbTableHelpers");
@@ -46,3 +47,5 @@ Object.defineProperty(exports, "buildDefaultResolvers", { enumerable: true, get:
46
47
  var userToken_1 = require("./userToken");
47
48
  Object.defineProperty(exports, "createUserToken", { enumerable: true, get: function () { return userToken_1.createUserToken; } });
48
49
  Object.defineProperty(exports, "verifyUserToken", { enumerable: true, get: function () { return userToken_1.verifyUserToken; } });
50
+ var baseGatewayHelpers_1 = require("./baseGatewayHelpers");
51
+ Object.defineProperty(exports, "buildDefaultGatewayProps", { enumerable: true, get: function () { return baseGatewayHelpers_1.buildDefaultGatewayProps; } });
@@ -0,0 +1,34 @@
1
+ import { BaseCore } from '..';
2
+ export interface BaseCoreActionsInterface {
3
+ /**
4
+ * Reference to the core object that processes the request
5
+ */
6
+ core: BaseCore;
7
+ /**
8
+ * Sets current step when processing an action
9
+ * @param str Name of step
10
+ */
11
+ setStep?(str: string): void;
12
+ /**
13
+ * The sameRequest is true when we call a core method when handling the same request (actual for GraphQL resolvers)
14
+ */
15
+ sameRequest?: boolean;
16
+ /**
17
+ * The transaction is active when processing some of actions that configured to open transaction
18
+ */
19
+ hasTransaction?: boolean;
20
+ /**
21
+ * The validatedItems exists after validator execution.
22
+ * It is expected that validator return an array in which second element is some
23
+ * items that were read from data or created otherwise during validation
24
+ */
25
+ validatedItems?: any;
26
+ /**
27
+ * The exception object exists only when exception happened when processing an action
28
+ */
29
+ exception?: any;
30
+ /**
31
+ * Name of action for exception error messages
32
+ */
33
+ doingWhat?: string;
34
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,13 @@
1
+ export interface BaseCoreDoingWhatInterface {
2
+ list: string;
3
+ get: string;
4
+ getMany: string;
5
+ create: string;
6
+ createMany: string;
7
+ update: string;
8
+ updateMany: string;
9
+ set: string;
10
+ remove: string;
11
+ removeMany: string;
12
+ [key: string]: string;
13
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,20 +1,9 @@
1
+ import { BaseCoreActionsInterface } from './BaseCoreActionsInterface';
1
2
  import { LoggerLevels, Logger } from '@sdflc/utils';
2
3
  import { Database } from '../database';
3
4
  import { BaseGateway } from '../baseGateway';
4
5
  import { BaseCoreValidatorsInterface } from './BaseCoreValidatorsInterface';
5
- export interface DoingWhatInterface {
6
- list: string;
7
- get: string;
8
- getMany: string;
9
- create: string;
10
- createMany: string;
11
- update: string;
12
- updateMany: string;
13
- set: string;
14
- remove: string;
15
- removeMany: string;
16
- [key: string]: string;
17
- }
6
+ import { BaseCoreDoingWhatInterface } from './BaseCoreDoingWhatInterface';
18
7
  export interface BaseCoreInterface {
19
8
  db: Database;
20
9
  context?: any;
@@ -24,35 +13,26 @@ export interface BaseCoreInterface {
24
13
  logLevel: LoggerLevels;
25
14
  logger?: Logger;
26
15
  name?: string;
27
- doingWhat?: DoingWhatInterface;
28
- beforeList?(params: any, gateways?: any, opt?: any): any;
29
- beforeCreate?(params: any, gateways?: any, opt?: any): any;
30
- beforeCreateMany?(params: any, gateways?: any, opt?: any): any;
31
- beforeUpdate?(params: any, gateways?: any, opt?: any): any;
32
- beforeUpdateMany?(params: any, gateways?: any, opt?: any): any;
33
- beforeSet?(params: any, gateways?: any, opt?: any): any;
34
- beforeRemove?(params: any, gateways?: any, opt?: any): any;
35
- beforeRemoveMany?(params: any, gateways?: any, opt?: any): any;
36
- afterList?(params: any, gateways?: any, opt?: any): any;
37
- afterCreate?(params: any, gateways?: any, opt?: any): any;
38
- afterCreateMany?(params: any, gateways?: any, opt?: any): any;
39
- afterUpdate?(params: any, gateways?: any, opt?: any): any;
40
- afterUpdateMany?(params: any, gateways?: any, opt?: any): any;
41
- afterSet?(params: any, gateways?: any, opt?: any): any;
42
- afterRemove?(params: any, gateways?: any, opt?: any): any;
43
- afterRemoveMany?(params: any, gateways?: any, opt?: any): any;
44
- validateList?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
45
- validateGet?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
46
- validateGetMany?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
47
- validateCreate?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
48
- validateCreateMany?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
49
- validateUpdate?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
50
- validateUpdateMany?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
51
- validateSet?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
52
- validateRemove?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
53
- validateRemoveMany?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
54
- validators?: BaseCoreValidatorsInterface;
55
- authenticate?(params: any): Promise<any[]> | any[];
56
- doAuth: boolean;
57
16
  requestId: string;
17
+ doAuth: boolean;
18
+ doingWhat?: BaseCoreDoingWhatInterface;
19
+ beforeList?(params: any, opt?: BaseCoreActionsInterface): any;
20
+ beforeCreate?(params: any, opt?: BaseCoreActionsInterface): any;
21
+ beforeCreateMany?(params: any, opt?: BaseCoreActionsInterface): any;
22
+ beforeUpdate?(params: any, opt?: BaseCoreActionsInterface): any;
23
+ beforeUpdateMany?(params: any, opt?: BaseCoreActionsInterface): any;
24
+ beforeSet?(params: any, opt?: any): BaseCoreActionsInterface;
25
+ beforeRemove?(params: any, opt?: BaseCoreActionsInterface): any;
26
+ beforeRemoveMany?(params: any, opt?: BaseCoreActionsInterface): any;
27
+ afterList?(params: any, opt?: BaseCoreActionsInterface): any;
28
+ afterCreate?(params: any, opt?: BaseCoreActionsInterface): any;
29
+ afterCreateMany?(params: any, opt?: BaseCoreActionsInterface): any;
30
+ afterUpdate?(params: any, opt?: BaseCoreActionsInterface): any;
31
+ afterUpdateMany?(params: any, opt?: BaseCoreActionsInterface): any;
32
+ afterSet?(params: any, opt?: BaseCoreActionsInterface): any;
33
+ afterRemove?(params: any, opt?: BaseCoreActionsInterface): any;
34
+ afterRemoveMany?(params: any, opt?: BaseCoreActionsInterface): any;
35
+ onException?(params: any, opt?: BaseCoreActionsInterface): any;
36
+ validators?: BaseCoreValidatorsInterface;
37
+ authenticate?(params: any, opt?: BaseCoreActionsInterface): Promise<any[]> | any[];
58
38
  }
@@ -1,12 +1,13 @@
1
+ import { BaseCoreActionsInterface } from './BaseCoreActionsInterface';
1
2
  export interface BaseCoreValidatorsInterface {
2
- list?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
3
- get?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
4
- getMany?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
5
- create?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
6
- createMany?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
7
- update?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
8
- updateMany?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
9
- set?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
10
- remove?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
11
- removeMany?(params: any, gateways?: any, opt?: any): Promise<any[]> | any[];
3
+ list?(params: any, opt?: BaseCoreActionsInterface): Promise<any[]> | any[];
4
+ get?(params: any, opt?: BaseCoreActionsInterface): Promise<any[]> | any[];
5
+ getMany?(params: any, opt?: BaseCoreActionsInterface): Promise<any[]> | any[];
6
+ create?(params: any, opt?: BaseCoreActionsInterface): Promise<any[]> | any[];
7
+ createMany?(params: any, opt?: BaseCoreActionsInterface): Promise<any[]> | any[];
8
+ update?(params: any, opt?: BaseCoreActionsInterface): Promise<any[]> | any[];
9
+ updateMany?(params: any, opt?: BaseCoreActionsInterface): Promise<any[]> | any[];
10
+ set?(params: any, opt?: BaseCoreActionsInterface): Promise<any[]> | any[];
11
+ remove?(params: any, opt?: BaseCoreActionsInterface): Promise<any[]> | any[];
12
+ removeMany?(params: any, opt?: BaseCoreActionsInterface): Promise<any[]> | any[];
12
13
  }
@@ -1,4 +1,5 @@
1
1
  export interface UserTokenParams {
2
+ spaceId: string;
2
3
  accountId: string;
3
4
  roleId: string;
4
5
  userId: string;
@@ -4,7 +4,7 @@ export { BaseGatewayInterface } from './BaseGatewayInterface';
4
4
  export { ListArgsInterface } from './ListArgsInterface';
5
5
  export { SortItemInterface } from './SortItemInterface';
6
6
  export { PaginationInterface } from './PaginationInterface';
7
- export { BaseCoreInterface, DoingWhatInterface } from './BaseCoreInterface';
7
+ export { BaseCoreInterface } from './BaseCoreInterface';
8
8
  export { CoreActionInterface } from './CoreActionInterface';
9
9
  export { ApiContextInterface } from './ApiContextInterface';
10
10
  export { DbFieldAddDefaultInterface } from './DbFieldAddDefaultInterface';
@@ -16,3 +16,5 @@ export { BaseCoreValidatorsInterface } from './BaseCoreValidatorsInterface';
16
16
  export { UserTokenParams } from './UserTokenParams';
17
17
  export { CreateUserTokenArgs } from './CreateUserTokenArgs';
18
18
  export { VerifyUserTokenArgs } from './VerifyUserTokenArgs';
19
+ export { BaseCoreActionsInterface } from './BaseCoreActionsInterface';
20
+ export { BaseCoreDoingWhatInterface } from './BaseCoreDoingWhatInterface';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.17",
3
+ "version": "1.0.21",
4
4
  "description": "Set of helper functions and classes to simplify building backend apps within @sdflc ecosystem",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",