@sdflc/backend-helpers 1.0.19 → 1.0.22

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.
@@ -45,6 +45,7 @@ var createContextDefault = function (args) {
45
45
  var authorization = req.headers['authorization'];
46
46
  var spaceId = req.headers['x-space-id'];
47
47
  var apiKey = req.headers['x-api-key'];
48
+ var managementApiKey = req.headers['x-management-api-key'];
48
49
  var accountId = req.headers['x-account-id'];
49
50
  var roleId = req.headers['x-role-id'];
50
51
  var userId = req.headers['x-user-id'];
@@ -74,6 +75,7 @@ var createContextDefault = function (args) {
74
75
  context: {
75
76
  spaceId: spaceId,
76
77
  apiKey: apiKey,
78
+ managementApiKey: managementApiKey,
77
79
  userAgent: userAgent,
78
80
  referer: referer,
79
81
  ipAddress: ipAddress,
@@ -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;
@@ -3,17 +3,18 @@ import { BaseCore } from '.';
3
3
  declare class BaseGateway {
4
4
  protected config: BaseGatewayInterface;
5
5
  protected loader: any;
6
- protected idGenerator: any;
7
- protected insertFormatter: any;
8
- protected updateFormatter: any;
9
- protected removeFormatter: any;
10
- protected updateFilter: any;
11
- protected removeFilter: any;
12
6
  protected returnFields: any[];
13
7
  constructor(props: BaseGatewayInterface);
8
+ generateId(args?: any): Promise<any>;
9
+ onListFilter(query: any, filterParams: any): Promise<void>;
10
+ onUpdateFilter(query: any, filterParams: any): Promise<void>;
11
+ onRemoveFilter(query: any, filterParams: any): Promise<void>;
12
+ transformOnCreate(args: any): Promise<any>;
13
+ transformOnUpdate(params: any, where?: any): Promise<any>;
14
+ transformOnRemove(args?: any): Promise<any>;
14
15
  getDb(): import("./database").Database;
15
16
  getBuilder(): any;
16
- getIdField(): any;
17
+ getIdField(): string;
17
18
  getCore(): BaseCore;
18
19
  setCore(core: BaseCore): void;
19
20
  get(id: any): Promise<any>;
@@ -55,15 +55,6 @@ var dataCache_1 = require("./dataCache");
55
55
  var constants_1 = __importDefault(require("./constants"));
56
56
  var lodash_1 = require("lodash");
57
57
  var ID_FIELD = constants_1.default.ID_FIELD;
58
- var defaultIdGenerator = function () {
59
- return undefined;
60
- };
61
- var defaultInsertFormatter = function (args) {
62
- return args;
63
- };
64
- var defaultUpdateFormatter = function (args) {
65
- return args;
66
- };
67
58
  var defaultUpdateRemoveFilter = function (config) { return function (query, params) {
68
59
  var idName = config.idField || ID_FIELD;
69
60
  query.where(idName, params[idName]);
@@ -71,26 +62,111 @@ var defaultUpdateRemoveFilter = function (config) { return function (query, para
71
62
  var BaseGateway = /** @class */ (function () {
72
63
  function BaseGateway(props) {
73
64
  this.config = props;
74
- this.idGenerator = typeof this.config.idGenerator === 'function' ? this.config.idGenerator : defaultIdGenerator;
75
- this.insertFormatter =
76
- typeof this.config.insertFormatter === 'function' ? this.config.insertFormatter : defaultInsertFormatter;
77
- this.updateFormatter =
78
- typeof this.config.updateFormatter === 'function' ? this.config.updateFormatter : defaultUpdateFormatter;
79
- this.removeFormatter = typeof this.config.removeFormatter === 'function' ? this.config.removeFormatter : null;
80
- this.updateFilter =
81
- typeof this.config.updateFilter === 'function'
82
- ? this.config.updateFilter
83
- : defaultUpdateRemoveFilter(this.config);
84
- this.removeFilter =
85
- typeof this.config.removeFilter === 'function'
86
- ? this.config.removeFilter
87
- : defaultUpdateRemoveFilter(this.config);
88
65
  this.returnFields =
89
66
  Array.isArray(this.config.returnFields) && this.config.returnFields.length > 0
90
67
  ? this.config.returnFields
91
68
  : [this.getIdField()];
92
69
  this.loader = typeof this.config.loader === 'function' ? this.config.loader : new dataCache_1.DataCache(this.config);
93
70
  }
71
+ BaseGateway.prototype.generateId = function (args) {
72
+ return __awaiter(this, void 0, void 0, function () {
73
+ return __generator(this, function (_a) {
74
+ return [2 /*return*/, typeof this.config.idGenerator === 'function' ? this.config.idGenerator(args) : undefined];
75
+ });
76
+ });
77
+ };
78
+ BaseGateway.prototype.onListFilter = function (query, filterParams) {
79
+ return __awaiter(this, void 0, void 0, function () {
80
+ return __generator(this, function (_a) {
81
+ switch (_a.label) {
82
+ case 0:
83
+ if (!(typeof this.config.filter === 'function')) return [3 /*break*/, 2];
84
+ return [4 /*yield*/, this.config.filter(query, filterParams)];
85
+ case 1:
86
+ _a.sent();
87
+ _a.label = 2;
88
+ case 2: return [2 /*return*/];
89
+ }
90
+ });
91
+ });
92
+ };
93
+ BaseGateway.prototype.onUpdateFilter = function (query, filterParams) {
94
+ return __awaiter(this, void 0, void 0, function () {
95
+ return __generator(this, function (_a) {
96
+ switch (_a.label) {
97
+ case 0:
98
+ if (!(typeof this.config.updateFilter === 'function')) return [3 /*break*/, 2];
99
+ return [4 /*yield*/, this.config.updateFilter(query, filterParams)];
100
+ case 1:
101
+ _a.sent();
102
+ return [3 /*break*/, 4];
103
+ case 2: return [4 /*yield*/, defaultUpdateRemoveFilter(this.config)(query, filterParams)];
104
+ case 3:
105
+ _a.sent();
106
+ _a.label = 4;
107
+ case 4: return [2 /*return*/];
108
+ }
109
+ });
110
+ });
111
+ };
112
+ BaseGateway.prototype.onRemoveFilter = function (query, filterParams) {
113
+ return __awaiter(this, void 0, void 0, function () {
114
+ return __generator(this, function (_a) {
115
+ switch (_a.label) {
116
+ case 0:
117
+ if (!(typeof this.config.removeFilter === 'function')) return [3 /*break*/, 2];
118
+ return [4 /*yield*/, this.config.removeFilter(query, filterParams)];
119
+ case 1:
120
+ _a.sent();
121
+ return [3 /*break*/, 4];
122
+ case 2: return [4 /*yield*/, defaultUpdateRemoveFilter(this.config)(query, filterParams)];
123
+ case 3:
124
+ _a.sent();
125
+ _a.label = 4;
126
+ case 4: return [2 /*return*/];
127
+ }
128
+ });
129
+ });
130
+ };
131
+ BaseGateway.prototype.transformOnCreate = function (args) {
132
+ return __awaiter(this, void 0, void 0, function () {
133
+ return __generator(this, function (_a) {
134
+ switch (_a.label) {
135
+ case 0:
136
+ if (!(typeof this.config.insertFormatter === 'function')) return [3 /*break*/, 2];
137
+ return [4 /*yield*/, this.config.insertFormatter(args)];
138
+ case 1: return [2 /*return*/, _a.sent()];
139
+ case 2: return [2 /*return*/, args];
140
+ }
141
+ });
142
+ });
143
+ };
144
+ BaseGateway.prototype.transformOnUpdate = function (params, where) {
145
+ return __awaiter(this, void 0, void 0, function () {
146
+ return __generator(this, function (_a) {
147
+ switch (_a.label) {
148
+ case 0:
149
+ if (!(typeof this.config.updateFormatter === 'function')) return [3 /*break*/, 2];
150
+ return [4 /*yield*/, this.config.updateFormatter(params, where)];
151
+ case 1: return [2 /*return*/, _a.sent()];
152
+ case 2: return [2 /*return*/, params];
153
+ }
154
+ });
155
+ });
156
+ };
157
+ BaseGateway.prototype.transformOnRemove = function (args) {
158
+ return __awaiter(this, void 0, void 0, function () {
159
+ return __generator(this, function (_a) {
160
+ switch (_a.label) {
161
+ case 0:
162
+ if (!(typeof this.config.removeFormatter === 'function')) return [3 /*break*/, 2];
163
+ return [4 /*yield*/, this.config.removeFormatter(args)];
164
+ case 1: return [2 /*return*/, _a.sent()];
165
+ case 2: return [2 /*return*/, args];
166
+ }
167
+ });
168
+ });
169
+ };
94
170
  BaseGateway.prototype.getDb = function () {
95
171
  return this.config.db;
96
172
  };
@@ -136,12 +212,9 @@ var BaseGateway = /** @class */ (function () {
136
212
  ? this.config.selectFields
137
213
  : this.getIdField();
138
214
  query.select(fieldsToUse);
139
- if (!(typeof this.config.filter === 'function')) return [3 /*break*/, 2];
140
- return [4 /*yield*/, this.config.filter(query, filter)];
215
+ return [4 /*yield*/, this.onListFilter(query, filter)];
141
216
  case 1:
142
217
  _d.sent();
143
- _d.label = 2;
144
- case 2:
145
218
  if (limit && !isNaN(limit) && limit > 0) {
146
219
  query.offset(offset).limit(limit);
147
220
  }
@@ -149,23 +222,23 @@ var BaseGateway = /** @class */ (function () {
149
222
  query.orderBy(sortBy);
150
223
  }
151
224
  return [4 /*yield*/, query];
152
- case 3:
225
+ case 2:
153
226
  items = _d.sent();
154
- if (!(this.config.noCache === true)) return [3 /*break*/, 4];
227
+ if (!(this.config.noCache === true)) return [3 /*break*/, 3];
155
228
  _c = items;
156
- return [3 /*break*/, 6];
157
- case 4: return [4 /*yield*/, this.getMany(items.map(function (row) { return row.id; }))];
158
- case 5:
229
+ return [3 /*break*/, 5];
230
+ case 3: return [4 /*yield*/, this.getMany(items.map(function (row) { return row.id; }))];
231
+ case 4:
159
232
  _c = _d.sent();
160
- _d.label = 6;
161
- case 6: return [2 /*return*/, _c];
233
+ _d.label = 5;
234
+ case 5: return [2 /*return*/, _c];
162
235
  }
163
236
  });
164
237
  });
165
238
  };
166
239
  BaseGateway.prototype.create = function (args) {
167
240
  return __awaiter(this, void 0, void 0, function () {
168
- var objects, idx, len, arg, id, items;
241
+ var objects, idx, len, arg, id, obj, items;
169
242
  return __generator(this, function (_a) {
170
243
  switch (_a.label) {
171
244
  case 0:
@@ -179,18 +252,21 @@ var BaseGateway = /** @class */ (function () {
179
252
  idx = 0, len = args.length;
180
253
  _a.label = 1;
181
254
  case 1:
182
- if (!(idx < len)) return [3 /*break*/, 4];
255
+ if (!(idx < len)) return [3 /*break*/, 5];
183
256
  arg = args[idx];
184
- return [4 /*yield*/, this.idGenerator(arg)];
257
+ return [4 /*yield*/, this.generateId(arg)];
185
258
  case 2:
186
259
  id = _a.sent();
187
- objects.push(this.insertFormatter(__assign({ id: id }, arg)));
188
- _a.label = 3;
260
+ return [4 /*yield*/, this.transformOnCreate(__assign({ id: id }, arg))];
189
261
  case 3:
262
+ obj = _a.sent();
263
+ objects.push(obj);
264
+ _a.label = 4;
265
+ case 4:
190
266
  ++idx;
191
267
  return [3 /*break*/, 1];
192
- case 4: return [4 /*yield*/, this.getBuilder().insert(objects).returning(this.returnFields)];
193
- case 5:
268
+ case 5: return [4 /*yield*/, this.getBuilder().insert(objects).returning(this.returnFields)];
269
+ case 6:
194
270
  items = _a.sent();
195
271
  return [2 /*return*/, items];
196
272
  }
@@ -203,11 +279,11 @@ var BaseGateway = /** @class */ (function () {
203
279
  var _this = this;
204
280
  return __generator(this, function (_a) {
205
281
  switch (_a.label) {
206
- case 0: return [4 /*yield*/, this.updateFormatter(params)];
282
+ case 0: return [4 /*yield*/, this.transformOnUpdate(params, where)];
207
283
  case 1:
208
284
  object = _a.sent();
209
285
  query = this.getBuilder().update(object).returning(this.returnFields);
210
- return [4 /*yield*/, this.updateFilter(query, where)];
286
+ return [4 /*yield*/, this.onUpdateFilter(query, where)];
211
287
  case 2:
212
288
  _a.sent();
213
289
  return [4 /*yield*/, query];
@@ -260,11 +336,11 @@ var BaseGateway = /** @class */ (function () {
260
336
  var _this = this;
261
337
  return __generator(this, function (_a) {
262
338
  switch (_a.label) {
263
- case 0: return [4 /*yield*/, this.updateFormatter(params)];
339
+ case 0: return [4 /*yield*/, this.transformOnUpdate(params, where)];
264
340
  case 1:
265
341
  object = _a.sent();
266
342
  query = this.getBuilder().increment(object).returning(this.returnFields);
267
- return [4 /*yield*/, this.updateFilter(query, where)];
343
+ return [4 /*yield*/, this.onUpdateFilter(query, where)];
268
344
  case 2:
269
345
  _a.sent();
270
346
  return [4 /*yield*/, query];
@@ -284,11 +360,11 @@ var BaseGateway = /** @class */ (function () {
284
360
  var _this = this;
285
361
  return __generator(this, function (_a) {
286
362
  switch (_a.label) {
287
- case 0: return [4 /*yield*/, this.updateFormatter(params)];
363
+ case 0: return [4 /*yield*/, this.transformOnUpdate(params, where)];
288
364
  case 1:
289
365
  object = _a.sent();
290
366
  query = this.getBuilder().decrement(object).returning(this.returnFields);
291
- return [4 /*yield*/, this.updateFilter(query, where)];
367
+ return [4 /*yield*/, this.onUpdateFilter(query, where)];
292
368
  case 2:
293
369
  _a.sent();
294
370
  return [4 /*yield*/, query];
@@ -333,24 +409,27 @@ var BaseGateway = /** @class */ (function () {
333
409
  BaseGateway.prototype.remove = function (where, hardRemove) {
334
410
  if (hardRemove === void 0) { hardRemove = false; }
335
411
  return __awaiter(this, void 0, void 0, function () {
336
- var query, items;
412
+ var query, _a, _b, items;
337
413
  var _this = this;
338
- return __generator(this, function (_a) {
339
- switch (_a.label) {
414
+ return __generator(this, function (_c) {
415
+ switch (_c.label) {
340
416
  case 0:
341
417
  query = this.getBuilder();
342
- if (hardRemove) {
343
- query.delete(this.returnFields);
344
- }
345
- else {
346
- query.update(this.removeFormatter()).returning(this.returnFields);
347
- }
348
- return [4 /*yield*/, this.removeFilter(query, where)];
418
+ if (!hardRemove) return [3 /*break*/, 1];
419
+ query.delete(this.returnFields);
420
+ return [3 /*break*/, 3];
349
421
  case 1:
350
- _a.sent();
351
- return [4 /*yield*/, query];
422
+ _b = (_a = query).update;
423
+ return [4 /*yield*/, this.transformOnRemove()];
352
424
  case 2:
353
- items = _a.sent();
425
+ _b.apply(_a, [_c.sent()]).returning(this.returnFields);
426
+ _c.label = 3;
427
+ case 3: return [4 /*yield*/, this.onRemoveFilter(query, where)];
428
+ case 4:
429
+ _c.sent();
430
+ return [4 /*yield*/, query];
431
+ case 5:
432
+ items = _c.sent();
354
433
  if (items.length > 0 && this.config.noCache !== true) {
355
434
  items.forEach(function (item) { return _this.loader.clear(item[_this.getIdField()]); });
356
435
  }
@@ -16,5 +16,5 @@ export declare const buildDefaultGatewayProps: (args: any) => {
16
16
  updateFilter: (query: any, params: any) => void;
17
17
  removeFormatter: () => any;
18
18
  removeFilter: (query: any, params: any) => void;
19
- returnFields: any[];
19
+ returnFields: any;
20
20
  };
@@ -10,7 +10,7 @@ var lodash_1 = require("lodash");
10
10
  var constants_1 = __importDefault(require("./constants"));
11
11
  var FIELDS = constants_1.default.FIELDS, STATUSES = constants_1.default.STATUSES;
12
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;
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
14
  return {
15
15
  filter: function (query, params) {
16
16
  if (hasStatus === undefined || hasStatus === true) {
@@ -80,7 +80,7 @@ var buildDefaultGatewayProps = function (args) {
80
80
  query.where(FIELDS.STATUS, '!=', STATUSES.REMOVED);
81
81
  }
82
82
  },
83
- returnFields: [FIELDS.ID],
83
+ returnFields: returnFields || [FIELDS.ID],
84
84
  };
85
85
  };
86
86
  exports.buildDefaultGatewayProps = buildDefaultGatewayProps;
@@ -1,2 +1,8 @@
1
- declare const constants: any;
2
- export default constants;
1
+ declare const _default: {
2
+ ID_FIELD: string;
3
+ FIELDS: any;
4
+ STATUSES: any;
5
+ ACCESS_RIGHTS: any;
6
+ VALUE_TYPES: any;
7
+ };
8
+ export default _default;
@@ -1,34 +1,57 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var constants = {
4
- ID_FIELD: 'id',
5
- FIELDS: {
6
- ID: 'id',
7
- STATUS: 'status',
8
- VERSION: 'version',
9
- CREATED_BY: 'created_by',
10
- UPDATED_BY: 'updated_by',
11
- REMOVED_BY: 'removed_by',
12
- CREATED_AT: 'created_at',
13
- UPDATED_AT: 'updated_at',
14
- REMOVED_AT: 'removed_at',
15
- REMOVED_AT_STR: 'removed_at_str',
16
- },
17
- STATUSES: {
18
- TEST: 50,
19
- ACTIVE: 100,
20
- DRAFT: 200,
21
- PENDING: 300,
22
- BLOCKED: 1000,
23
- DISABLED: 5000,
24
- REMOVED: 10000,
25
- },
26
- ACCESS_RIGHTS: {
27
- LIST: 1,
28
- GET: 2,
29
- CREATE: 4,
30
- UPDATE: 8,
31
- REMOVE: 16,
32
- },
3
+ var ID_FIELD = 'id';
4
+ var FIELDS = {
5
+ ID: 'id',
6
+ STATUS: 'status',
7
+ VERSION: 'version',
8
+ CREATED_BY: 'created_by',
9
+ UPDATED_BY: 'updated_by',
10
+ REMOVED_BY: 'removed_by',
11
+ CREATED_AT: 'created_at',
12
+ UPDATED_AT: 'updated_at',
13
+ REMOVED_AT: 'removed_at',
14
+ REMOVED_AT_STR: 'removed_at_str',
15
+ LANG: 'LANG',
16
+ ACCOUNT_ID: 'account_id',
17
+ ROLE_ID: 'role_id',
18
+ USER_ID: 'user_id',
19
+ ORDER_NO: 'order_no',
20
+ SPACE_ID: 'space_id',
21
+ };
22
+ var STATUSES = {
23
+ TEST: 50,
24
+ ACTIVE: 100,
25
+ DRAFT: 200,
26
+ PENDING: 300,
27
+ BLOCKED: 1000,
28
+ DISABLED: 5000,
29
+ REMOVED: 10000,
30
+ };
31
+ var ACCESS_RIGHTS = {
32
+ LIST: 1,
33
+ GET: 2,
34
+ CREATE: 4,
35
+ UPDATE: 8,
36
+ REMOVE: 16,
37
+ RUN: 32,
38
+ };
39
+ var VALUE_TYPES = {
40
+ NUMBER: 100,
41
+ INTEGER: 101,
42
+ FLOAT: 102,
43
+ CURRENCY: 103,
44
+ BOOLEAN: 200,
45
+ STRING: 300,
46
+ JSON: 400,
47
+ EMAIL: 1001,
48
+ PHONE: 1002,
49
+ URL: 1003,
50
+ };
51
+ exports.default = {
52
+ ID_FIELD: ID_FIELD,
53
+ FIELDS: FIELDS,
54
+ STATUSES: STATUSES,
55
+ ACCESS_RIGHTS: ACCESS_RIGHTS,
56
+ VALUE_TYPES: VALUE_TYPES,
33
57
  };
34
- exports.default = constants;
@@ -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,84 @@ 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
  }
34
+ table.index([FIELDS.CREATED_AT]);
35
+ table.index([FIELDS.UPDATED_AT]);
32
36
  }
33
37
  if (addStatus) {
34
- table.index([constants_1.default.FIELDS.STATUS]);
38
+ table.index([FIELDS.STATUS]);
35
39
  }
36
40
  if (skipRemovedAtStr !== true) {
37
- table.index([constants_1.default.FIELDS.REMOVED_AT_STR]);
41
+ table.index([FIELDS.REMOVED_AT_STR]);
38
42
  }
39
43
  };
40
44
  exports.dbFieldAddDefaults = dbFieldAddDefaults;
45
+ var addOrderNo = function (table, opt) {
46
+ var chain = table.text(FIELDS.ORDER_NO).notNullable().defaultTo('10000000');
47
+ if ((opt === null || opt === void 0 ? void 0 : opt.addIndex) === true) {
48
+ table.index([FIELDS.ORDER_NO]);
49
+ }
50
+ return chain;
51
+ };
52
+ exports.addOrderNo = addOrderNo;
53
+ var addAmount = function (table, fieldName) {
54
+ return table.decimal(fieldName, 19, 4);
55
+ };
56
+ exports.addAmount = addAmount;
57
+ var addSpaceId = function (table, opt) {
58
+ var chain = table.text(FIELDS.SPACE_ID).notNullable().comment('Reference to the space table');
59
+ if ((opt === null || opt === void 0 ? void 0 : opt.addForeignKey) === true) {
60
+ table.foreign(FIELDS.SPACE_ID).references("space." + FIELDS.ID).onUpdate('CASCADE').onDelete('CASCADE');
61
+ }
62
+ return chain;
63
+ };
64
+ exports.addSpaceId = addSpaceId;
65
+ var addUserId = function (table, opt) {
66
+ var chain = table.uuid(FIELDS.USER_ID).notNullable().comment('Reference to the user table');
67
+ if ((opt === null || opt === void 0 ? void 0 : opt.addForeignKey) === true) {
68
+ table.foreign(FIELDS.userId).references("user." + FIELDS.ID).onUpdate('CASCADE').onDelete('CASCADE');
69
+ }
70
+ return chain;
71
+ };
72
+ exports.addUserId = addUserId;
73
+ var defaultFields = function (opt) {
74
+ var _a;
75
+ var addUserInfo = (opt || {}).addUserInfo;
76
+ var obj = (_a = {},
77
+ _a[FIELDS.CREATED_AT] = '2021-01-01T00:00:00Z',
78
+ _a[FIELDS.UPDATED_AT] = '2021-01-01T00:00:00Z',
79
+ _a);
80
+ if (addUserInfo === true) {
81
+ obj[FIELDS.CREATED_BY] = utils_1.UUID_EMPTY;
82
+ obj[FIELDS.UPDATED_BY] = utils_1.UUID_EMPTY;
83
+ }
84
+ return obj;
85
+ };
86
+ exports.defaultFields = defaultFields;
package/build/index.d.ts CHANGED
@@ -18,3 +18,4 @@ export { buildDefaultResolvers } from './resolversHelpers';
18
18
  export { createUserToken, verifyUserToken } from './userToken';
19
19
  export { constants };
20
20
  export { buildDefaultGatewayProps } from './baseGatewayHelpers';
21
+ export { getSaltAndHash, checkPassword } from './passwordHelpers';
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.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;
6
+ exports.checkPassword = exports.getSaltAndHash = 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");
@@ -49,3 +49,6 @@ Object.defineProperty(exports, "createUserToken", { enumerable: true, get: funct
49
49
  Object.defineProperty(exports, "verifyUserToken", { enumerable: true, get: function () { return userToken_1.verifyUserToken; } });
50
50
  var baseGatewayHelpers_1 = require("./baseGatewayHelpers");
51
51
  Object.defineProperty(exports, "buildDefaultGatewayProps", { enumerable: true, get: function () { return baseGatewayHelpers_1.buildDefaultGatewayProps; } });
52
+ var passwordHelpers_1 = require("./passwordHelpers");
53
+ Object.defineProperty(exports, "getSaltAndHash", { enumerable: true, get: function () { return passwordHelpers_1.getSaltAndHash; } });
54
+ Object.defineProperty(exports, "checkPassword", { enumerable: true, get: function () { return passwordHelpers_1.checkPassword; } });
@@ -16,7 +16,7 @@ export interface BaseGatewayInterface {
16
16
  noCache?: boolean;
17
17
  idGenerator?(args?: any): any;
18
18
  insertFormatter?(args?: any): any;
19
- updateFormatter?(args?: any): any;
19
+ updateFormatter?(args?: any, where?: any): any;
20
20
  updateFilter?(query: any, params: any): any;
21
21
  removeFormatter?(args?: any): any;
22
22
  removeFilter?(query: any, params: any): any;
@@ -0,0 +1,5 @@
1
+ export declare const getSaltAndHash: (password: string, saltRounds?: number) => Promise<{
2
+ salt: string;
3
+ hash: string;
4
+ }>;
5
+ export declare const checkPassword: (password: string, hashedPassword: string) => Promise<boolean>;
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.checkPassword = exports.getSaltAndHash = void 0;
43
+ var bcrypt_1 = __importDefault(require("bcrypt"));
44
+ var getSaltAndHash = function (password, saltRounds) {
45
+ if (saltRounds === void 0) { saltRounds = 10; }
46
+ return __awaiter(void 0, void 0, void 0, function () {
47
+ var salt, hash;
48
+ return __generator(this, function (_a) {
49
+ switch (_a.label) {
50
+ case 0: return [4 /*yield*/, bcrypt_1.default.genSalt(saltRounds)];
51
+ case 1:
52
+ salt = _a.sent();
53
+ return [4 /*yield*/, bcrypt_1.default.hash(password, salt)];
54
+ case 2:
55
+ hash = _a.sent();
56
+ return [2 /*return*/, {
57
+ salt: salt,
58
+ hash: hash,
59
+ }];
60
+ }
61
+ });
62
+ });
63
+ };
64
+ exports.getSaltAndHash = getSaltAndHash;
65
+ var checkPassword = function (password, hashedPassword) { return __awaiter(void 0, void 0, void 0, function () {
66
+ var isCorrect;
67
+ return __generator(this, function (_a) {
68
+ switch (_a.label) {
69
+ case 0: return [4 /*yield*/, bcrypt_1.default.compare(password, hashedPassword)];
70
+ case 1:
71
+ isCorrect = _a.sent();
72
+ return [2 /*return*/, isCorrect];
73
+ }
74
+ });
75
+ }); };
76
+ exports.checkPassword = checkPassword;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.19",
3
+ "version": "1.0.22",
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",
@@ -24,6 +24,7 @@
24
24
  "license": "ISC",
25
25
  "peerDependencies": {},
26
26
  "devDependencies": {
27
+ "@types/bcrypt": "^5.0.0",
27
28
  "@types/bluebird": "^3.5.35",
28
29
  "@types/dotenv-safe": "^8.1.1",
29
30
  "@types/jest": "^26.0.16",
@@ -51,6 +52,7 @@
51
52
  "dependencies": {
52
53
  "@sdflc/api-helpers": "^1.1.7",
53
54
  "@sdflc/utils": "^1.0.12",
55
+ "bcrypt": "^5.0.1",
54
56
  "bluebird": "^3.7.2",
55
57
  "dataloader": "^2.0.0",
56
58
  "dayjs": "^1.10.7",