@sdflc/backend-helpers 1.0.20 → 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.
@@ -1,8 +1,9 @@
1
1
  import { Logger } from '@sdflc/utils';
2
- import { BaseCoreInterface, BaseCoreDoingWhatInterface, CoreActionInterface, BaseCoreValidatorsInterface } from './interfaces';
2
+ import { BaseCoreInterface, BaseCoreDoingWhatInterface, BaseCoreActionsInterface, CoreActionInterface, BaseCoreValidatorsInterface } from './interfaces';
3
3
  import { BaseGateway } from './baseGateway';
4
4
  import { Database } from './database';
5
5
  declare class BaseCore {
6
+ protected props: BaseCoreInterface;
6
7
  protected db: Database;
7
8
  protected context: any;
8
9
  protected gateway?: BaseGateway;
@@ -10,26 +11,8 @@ declare class BaseCore {
10
11
  protected logger: Logger;
11
12
  protected name?: string;
12
13
  protected gateways?: any;
13
- protected beforeList: any;
14
- protected beforeCreate: any;
15
- protected beforeCreateMany: any;
16
- protected beforeUpdate: any;
17
- protected beforeUpdateMany: any;
18
- protected beforeSet: any;
19
- protected beforeRemove: any;
20
- protected beforeRemoveMany: any;
21
- protected afterList: any;
22
- protected afterCreate: any;
23
- protected afterCreateMany: any;
24
- protected afterUpdate: any;
25
- protected afterUpdateMany: any;
26
- protected afterSet: any;
27
- protected afterRemove: any;
28
- protected afterRemoveMany: any;
29
- protected onException: 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,7 +56,7 @@ 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
62
  var defaultValidate = function (params, opt) {
@@ -80,35 +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
- this.onException = typeof props.onException === 'function' ? props.onException : defaultAfterAndBefore;
110
91
  this.validators = utils_2.setFunctions((props === null || props === void 0 ? void 0 : props.validators) || {}, this.validators);
111
- this.authenticate = typeof props.authenticate === 'function' ? props.authenticate : defaultAuth;
112
92
  this.doAuth = props.doAuth === true;
113
93
  this.requestId = props.requestId || '';
114
94
  }
@@ -141,7 +121,7 @@ var BaseCore = /** @class */ (function () {
141
121
  errors: [],
142
122
  }).toJS();
143
123
  };
144
- BaseCore.prototype.failture = function (code, errorMsg, data) {
124
+ BaseCore.prototype.failure = function (code, errorMsg, data) {
145
125
  var result = new api_helpers_1.OpResult({ data: data });
146
126
  result.addError('', errorMsg, code);
147
127
  return result.toJS();
@@ -633,6 +613,168 @@ var BaseCore = /** @class */ (function () {
633
613
  });
634
614
  });
635
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
+ };
636
778
  return BaseCore;
637
779
  }());
638
780
  exports.BaseCore = BaseCore;
@@ -17,6 +17,7 @@ var constants = {
17
17
  ACCOUNT_ID: 'account_id',
18
18
  ROLE_ID: 'role_id',
19
19
  USER_ID: 'user_id',
20
+ ORDER_NO: 'order_no',
20
21
  },
21
22
  STATUSES: {
22
23
  TEST: 50,
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.20",
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",