@sdflc/backend-helpers 1.0.41 → 1.0.44

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.
@@ -38,6 +38,6 @@ declare class BaseGateway {
38
38
  * @returns Array of updated or inserted objects
39
39
  */
40
40
  set(params: any): Promise<any[]>;
41
- remove(where: any, hardRemove?: boolean): Promise<any>;
41
+ remove(where: any): Promise<any>;
42
42
  }
43
43
  export { BaseGateway };
@@ -84,10 +84,10 @@ var BaseGateway = /** @class */ (function () {
84
84
  };
85
85
  BaseGateway.prototype.onListFilter = function (query, filterParams) {
86
86
  return __awaiter(this, void 0, void 0, function () {
87
- var _a, hasStatus, hasRemovedAt, hasSpace, activeStatuses, hasLang, fieldForKeyword;
88
- var _b;
89
- return __generator(this, function (_c) {
90
- _a = this.config || {}, hasStatus = _a.hasStatus, hasRemovedAt = _a.hasRemovedAt, hasSpace = _a.hasSpace, activeStatuses = _a.activeStatuses, hasLang = _a.hasLang, fieldForKeyword = _a.fieldForKeyword;
87
+ var _a, hasStatus, hasRemovedAt, hasSpace, activeStatuses, hasLang, fieldForKeyword, filterByUserField;
88
+ var _b, _c;
89
+ return __generator(this, function (_d) {
90
+ _a = this.config || {}, hasStatus = _a.hasStatus, hasRemovedAt = _a.hasRemovedAt, hasSpace = _a.hasSpace, activeStatuses = _a.activeStatuses, hasLang = _a.hasLang, fieldForKeyword = _a.fieldForKeyword, filterByUserField = _a.filterByUserField;
91
91
  // This function should be overriden
92
92
  if (hasStatus) {
93
93
  query.whereIn(constants_1.FIELDS.STATUS, Array.isArray(filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS]) ? filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS] : (0, lodash_1.castArray)(activeStatuses));
@@ -109,6 +109,9 @@ var BaseGateway = /** @class */ (function () {
109
109
  if (fieldForKeyword && filterParams.keyword) {
110
110
  query.whereRaw("".concat(fieldForKeyword, " ILIKE ?"), "%".concat(filterParams.keyword, "%"));
111
111
  }
112
+ if (filterByUserField) {
113
+ query.where((_c = {}, _c[filterByUserField] = this.getContext().userId, _c));
114
+ }
112
115
  return [2 /*return*/];
113
116
  });
114
117
  });
@@ -143,10 +146,11 @@ var BaseGateway = /** @class */ (function () {
143
146
  BaseGateway.prototype.onRemoveFilter = function (query, filterParams) {
144
147
  return __awaiter(this, void 0, void 0, function () {
145
148
  var hasStatus, arr;
149
+ var _this = this;
146
150
  return __generator(this, function (_a) {
147
151
  hasStatus = this.config.hasStatus;
148
152
  arr = (0, lodash_1.castArray)(filterParams);
149
- query = query.whereIn(constants_1.FIELDS.ID, arr.map(function (item) { return item[constants_1.FIELDS.ID]; }));
153
+ query = query.whereIn(this.getIdField(), arr.map(function (item) { return item[_this.getIdField()]; }));
150
154
  if (hasStatus === true) {
151
155
  query.where(constants_1.FIELDS.STATUS, '!=', utils_1.STATUSES.REMOVED);
152
156
  }
@@ -156,9 +160,9 @@ var BaseGateway = /** @class */ (function () {
156
160
  };
157
161
  BaseGateway.prototype.transformOnCreate = function (params) {
158
162
  return __awaiter(this, void 0, void 0, function () {
159
- var _a, hasCreatedAt, hasCreatedBy, hasSpace, hasVersion, hasLang, localizationForField;
163
+ var _a, hasCreatedAt, hasCreatedBy, hasSpace, hasVersion, hasLang, localizationForField, hasUserId;
160
164
  return __generator(this, function (_b) {
161
- _a = this.config, hasCreatedAt = _a.hasCreatedAt, hasCreatedBy = _a.hasCreatedBy, hasSpace = _a.hasSpace, hasVersion = _a.hasVersion, hasLang = _a.hasLang, localizationForField = _a.localizationForField;
165
+ _a = this.config, hasCreatedAt = _a.hasCreatedAt, hasCreatedBy = _a.hasCreatedBy, hasSpace = _a.hasSpace, hasVersion = _a.hasVersion, hasLang = _a.hasLang, localizationForField = _a.localizationForField, hasUserId = _a.hasUserId;
162
166
  if (hasCreatedAt) {
163
167
  params[constants_1.FIELDS.CREATED_AT] = this.dateTime.dbFormat(this.dateTimeNow);
164
168
  }
@@ -167,6 +171,11 @@ var BaseGateway = /** @class */ (function () {
167
171
  params[constants_1.FIELDS.CREATED_BY] = this.getContext().userId;
168
172
  }
169
173
  }
174
+ if (hasUserId) {
175
+ if (params[constants_1.FIELDS.USER_ID] === undefined) {
176
+ params[constants_1.FIELDS.USER_ID] = this.getContext().userId;
177
+ }
178
+ }
170
179
  if (hasSpace) {
171
180
  params[constants_1.FIELDS.SPACE_ID] = this.getContext().spaceId;
172
181
  }
@@ -174,7 +183,7 @@ var BaseGateway = /** @class */ (function () {
174
183
  params[constants_1.FIELDS.VERSION] = 0;
175
184
  }
176
185
  if (hasLang && localizationForField) {
177
- params[this.getIdField()] = (0, utils_1.buildKey)([params[localizationForField], params[constants_1.FIELDS.LANG]]);
186
+ params[this.getIdField()] = (0, utils_1.buildKey)([params[(0, lodash_1.camelCase)(localizationForField)], params[(0, lodash_1.camelCase)(constants_1.FIELDS.LANG)]]);
178
187
  }
179
188
  return [2 /*return*/, params];
180
189
  });
@@ -200,7 +209,7 @@ var BaseGateway = /** @class */ (function () {
200
209
  }
201
210
  }
202
211
  if (hasLang && localizationForField) {
203
- params[this.getIdField()] = (0, utils_1.buildKey)([params[localizationForField], params[constants_1.FIELDS.LANG]]);
212
+ params[this.getIdField()] = (0, utils_1.buildKey)([params[(0, lodash_1.camelCase)(localizationForField)], params[(0, lodash_1.camelCase)(constants_1.FIELDS.LANG)]]);
204
213
  }
205
214
  return [2 /*return*/, params];
206
215
  });
@@ -208,9 +217,20 @@ var BaseGateway = /** @class */ (function () {
208
217
  };
209
218
  BaseGateway.prototype.transformOnSet = function (params) {
210
219
  return __awaiter(this, void 0, void 0, function () {
211
- var _a, hasUpdatedAt, hasUpdatedBy, hasVersion, hasLang, localizationForField;
220
+ var _a, hasCreatedAt, hasCreatedBy, hasSpace, hasUpdatedAt, hasUpdatedBy, hasVersion, hasLang, localizationForField;
212
221
  return __generator(this, function (_b) {
213
- _a = this.config, hasUpdatedAt = _a.hasUpdatedAt, hasUpdatedBy = _a.hasUpdatedBy, hasVersion = _a.hasVersion, hasLang = _a.hasLang, localizationForField = _a.localizationForField;
222
+ _a = this.config, hasCreatedAt = _a.hasCreatedAt, hasCreatedBy = _a.hasCreatedBy, hasSpace = _a.hasSpace, hasUpdatedAt = _a.hasUpdatedAt, hasUpdatedBy = _a.hasUpdatedBy, hasVersion = _a.hasVersion, hasLang = _a.hasLang, localizationForField = _a.localizationForField;
223
+ if (hasCreatedAt) {
224
+ params[constants_1.FIELDS.CREATED_AT] = this.dateTime.dbFormat(this.dateTimeNow);
225
+ }
226
+ if (hasCreatedBy) {
227
+ if (params[constants_1.FIELDS.CREATED_BY] === undefined) {
228
+ params[constants_1.FIELDS.CREATED_BY] = this.getContext().userId;
229
+ }
230
+ }
231
+ if (hasSpace) {
232
+ params[constants_1.FIELDS.SPACE_ID] = this.getContext().spaceId;
233
+ }
214
234
  if (hasUpdatedAt) {
215
235
  params[constants_1.FIELDS.UPDATED_AT] = this.dateTime.dbFormat(this.dateTimeNow);
216
236
  }
@@ -223,9 +243,12 @@ var BaseGateway = /** @class */ (function () {
223
243
  if (params[constants_1.FIELDS.VERSION] != null && !isNaN(params[constants_1.FIELDS.VERSION])) {
224
244
  params[constants_1.FIELDS.VERSION] = params[constants_1.FIELDS.VERSION] + 1;
225
245
  }
246
+ else {
247
+ params[constants_1.FIELDS.VERSION] = 0;
248
+ }
226
249
  }
227
250
  if (hasLang && localizationForField) {
228
- params[this.getIdField()] = (0, utils_1.buildKey)([params[localizationForField], params[constants_1.FIELDS.LANG]]);
251
+ params[this.getIdField()] = (0, utils_1.buildKey)([params[(0, lodash_1.camelCase)(localizationForField)], params[(0, lodash_1.camelCase)(constants_1.FIELDS.LANG)]]);
229
252
  }
230
253
  return [2 /*return*/, params];
231
254
  });
@@ -233,16 +256,17 @@ var BaseGateway = /** @class */ (function () {
233
256
  };
234
257
  BaseGateway.prototype.transformOnRemove = function (args) {
235
258
  return __awaiter(this, void 0, void 0, function () {
236
- var _a, hasRemovedAt, hasRemovedAtStr, hasStatus, hasRemovedBy, removalTime, obj;
259
+ var _a, hasRemovedAt, hasRemovedAtStr, hasStatus, hasRemovedBy, hasLang, localizationForField, removalTime, removalTimeStr, obj;
237
260
  return __generator(this, function (_b) {
238
- _a = this.config, hasRemovedAt = _a.hasRemovedAt, hasRemovedAtStr = _a.hasRemovedAtStr, hasStatus = _a.hasStatus, hasRemovedBy = _a.hasRemovedBy;
261
+ _a = this.config, hasRemovedAt = _a.hasRemovedAt, hasRemovedAtStr = _a.hasRemovedAtStr, hasStatus = _a.hasStatus, hasRemovedBy = _a.hasRemovedBy, hasLang = _a.hasLang, localizationForField = _a.localizationForField;
239
262
  removalTime = (0, dayjs_1.default)();
263
+ removalTimeStr = removalTime.format('YYYYMMDDTHHmmssZ');
240
264
  obj = __assign({}, (args || {}));
241
265
  if (hasRemovedAt) {
242
266
  obj[constants_1.FIELDS.REMOVED_AT] = removalTime.toDate();
243
267
  }
244
268
  if (hasRemovedAtStr === true) {
245
- obj[constants_1.FIELDS.REMOVED_AT_STR] = removalTime.format('YYYYMMDDTHHmmssZ');
269
+ obj[constants_1.FIELDS.REMOVED_AT_STR] = removalTimeStr;
246
270
  }
247
271
  if (hasStatus) {
248
272
  obj[constants_1.FIELDS.STATUS] = utils_1.STATUSES.REMOVED;
@@ -252,6 +276,9 @@ var BaseGateway = /** @class */ (function () {
252
276
  obj[constants_1.FIELDS.REMOVED_BY] = this.getContext().userId;
253
277
  }
254
278
  }
279
+ if (hasLang && localizationForField) {
280
+ obj[this.getIdField()] = this.getDb().raw("CONCAT(id, '-".concat(removalTimeStr, "')"));
281
+ }
255
282
  return [2 /*return*/, obj];
256
283
  });
257
284
  });
@@ -492,11 +519,11 @@ var BaseGateway = /** @class */ (function () {
492
519
  BaseGateway.prototype.set = function (params) {
493
520
  var _a;
494
521
  return __awaiter(this, void 0, void 0, function () {
495
- var _b, hasCreatedAt, hasCreatedBy, mergeFields, objects, skipFields, fields, idx, len, object, _c, _d, _e, _f, result, chunks, idx, len, items;
522
+ var _b, hasCreatedAt, hasCreatedBy, hasSpace, mergeFields, objects, skipFields, idx, len, object, _c, _d, _e, _f, fields, result, chunks, idx, len, items;
496
523
  return __generator(this, function (_g) {
497
524
  switch (_g.label) {
498
525
  case 0:
499
- _b = this.config || {}, hasCreatedAt = _b.hasCreatedAt, hasCreatedBy = _b.hasCreatedBy, mergeFields = _b.mergeFields;
526
+ _b = this.config || {}, hasCreatedAt = _b.hasCreatedAt, hasCreatedBy = _b.hasCreatedBy, hasSpace = _b.hasSpace, mergeFields = _b.mergeFields;
500
527
  objects = (0, lodash_1.castArray)(params);
501
528
  if (!params || objects.length === 0) {
502
529
  return [2 /*return*/, []];
@@ -508,7 +535,9 @@ var BaseGateway = /** @class */ (function () {
508
535
  if (hasCreatedBy) {
509
536
  skipFields.push(constants_1.FIELDS.CREATED_BY);
510
537
  }
511
- fields = Array.isArray(mergeFields) ? mergeFields : Object.keys(objects[0]);
538
+ if (hasSpace) {
539
+ skipFields.push(constants_1.FIELDS.SPACE_ID);
540
+ }
512
541
  idx = 0, len = objects.length;
513
542
  _g.label = 1;
514
543
  case 1:
@@ -519,7 +548,7 @@ var BaseGateway = /** @class */ (function () {
519
548
  return [4 /*yield*/, this.transformOnSet(object)];
520
549
  case 2:
521
550
  _c[_d] = _g.sent();
522
- if (!!objects[idx][this.getIdField()]) return [3 /*break*/, 4];
551
+ if (!(objects[idx][this.getIdField()] == null)) return [3 /*break*/, 4];
523
552
  _e = objects[idx];
524
553
  _f = this.getIdField();
525
554
  return [4 /*yield*/, this.generateId(object)];
@@ -530,6 +559,7 @@ var BaseGateway = /** @class */ (function () {
530
559
  ++idx;
531
560
  return [3 /*break*/, 1];
532
561
  case 5:
562
+ fields = Array.isArray(mergeFields) ? mergeFields : Object.keys(objects[0]);
533
563
  result = [];
534
564
  chunks = (0, utils_1.arrToChunks)(objects, ((_a = this.config) === null || _a === void 0 ? void 0 : _a.insertChunkSize) || DEFAULT_CHUNCK_SIZE);
535
565
  idx = 0, len = chunks.length;
@@ -540,7 +570,7 @@ var BaseGateway = /** @class */ (function () {
540
570
  .insert(chunks[idx])
541
571
  .returning(this.config.returnFields)
542
572
  .onConflict(this.config.setFields || this.getIdField())
543
- .merge((0, lodash_1.omit)(fields, skipFields))];
573
+ .merge((0, lodash_1.pullAll)(fields, skipFields))];
544
574
  case 7:
545
575
  items = _g.sent();
546
576
  result.push.apply(result, items);
@@ -553,30 +583,29 @@ var BaseGateway = /** @class */ (function () {
553
583
  });
554
584
  });
555
585
  };
556
- BaseGateway.prototype.remove = function (where, hardRemove) {
557
- if (hardRemove === void 0) { hardRemove = false; }
586
+ BaseGateway.prototype.remove = function (where) {
558
587
  return __awaiter(this, void 0, void 0, function () {
559
- var query, _a, _b, items;
588
+ var hardRemove, query, object, items;
560
589
  var _this = this;
561
- return __generator(this, function (_c) {
562
- switch (_c.label) {
590
+ return __generator(this, function (_a) {
591
+ switch (_a.label) {
563
592
  case 0:
593
+ hardRemove = (this.config || {}).hardRemove;
564
594
  query = this.getBuilder();
565
595
  if (!hardRemove) return [3 /*break*/, 1];
566
596
  query.delete(this.config.returnFields);
567
597
  return [3 /*break*/, 3];
568
- case 1:
569
- _b = (_a = query).update;
570
- return [4 /*yield*/, this.transformOnRemove()];
598
+ case 1: return [4 /*yield*/, this.transformOnRemove()];
571
599
  case 2:
572
- _b.apply(_a, [_c.sent()]).returning(this.config.returnFields);
573
- _c.label = 3;
600
+ object = _a.sent();
601
+ query.update(object).returning(this.config.returnFields);
602
+ _a.label = 3;
574
603
  case 3: return [4 /*yield*/, this.onRemoveFilter(query, where)];
575
604
  case 4:
576
- _c.sent();
605
+ _a.sent();
577
606
  return [4 /*yield*/, query];
578
607
  case 5:
579
- items = _c.sent();
608
+ items = _a.sent();
580
609
  if (items.length > 0 && this.config.noCache !== true) {
581
610
  items.forEach(function (item) { return _this.loader.clear(item[_this.getIdField()]); });
582
611
  }
@@ -9,7 +9,7 @@ declare class Database {
9
9
  createTransaction(trx: any): Promise<unknown>;
10
10
  getSchema(): string;
11
11
  getBuilder(arg: any): any;
12
- raw(query: string, bindings: any): any;
12
+ raw(query: string, bindings?: any): any;
13
13
  runRawQuery(query: string, bindings: any): Promise<any>;
14
14
  startTransaction(): Promise<unknown>;
15
15
  setTransaction(trx: any): void;
@@ -6,7 +6,7 @@ import { SortItemInterface } from './SortItemInterface';
6
6
  export interface BaseGatewayPropsInterface extends CacheInterface {
7
7
  context: ApiContext;
8
8
  setFields?: string[] | string;
9
- mergeFields: string[];
9
+ mergeFields?: string[];
10
10
  hasStatus?: boolean;
11
11
  hasCreatedBy?: boolean;
12
12
  hasUpdatedBy?: boolean;
@@ -18,10 +18,13 @@ export interface BaseGatewayPropsInterface extends CacheInterface {
18
18
  hasSpace?: boolean;
19
19
  hasVersion?: boolean;
20
20
  hasLang?: boolean;
21
- localizationForField: string;
21
+ hasUserId?: boolean;
22
+ localizationForField?: string;
22
23
  defaultSorting?: SortItemInterface[];
23
24
  fieldForKeyword?: string;
25
+ filterByUserField?: string;
24
26
  insertChunkSize?: number;
27
+ hardRemove?: boolean;
25
28
  loader?: LoaderInterface;
26
29
  noCache?: boolean;
27
30
  returnFields?: any[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.41",
3
+ "version": "1.0.44",
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",