@sdflc/backend-helpers 1.0.9 → 1.0.10

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.
@@ -16,6 +16,7 @@ declare class BaseCore {
16
16
  protected beforeCreateMany: any;
17
17
  protected beforeUpdate: any;
18
18
  protected beforeUpdateMany: any;
19
+ protected beforeSet: any;
19
20
  protected beforeRemove: any;
20
21
  protected beforeRemoveMany: any;
21
22
  protected afterList: any;
@@ -23,6 +24,7 @@ declare class BaseCore {
23
24
  protected afterCreateMany: any;
24
25
  protected afterUpdate: any;
25
26
  protected afterUpdateMany: any;
27
+ protected afterSet: any;
26
28
  protected afterRemove: any;
27
29
  protected afterRemoveMany: any;
28
30
  protected requestId: string;
@@ -47,6 +49,7 @@ declare class BaseCore {
47
49
  createMany(args: any): Promise<any>;
48
50
  update(args: any): Promise<any>;
49
51
  updateMany(args: any): Promise<any>;
52
+ set(args: any): Promise<any>;
50
53
  remove(args: any): Promise<any>;
51
54
  removeMany(args: any): Promise<any>;
52
55
  }
package/build/baseCore.js CHANGED
@@ -70,6 +70,7 @@ var defaultValidators = {
70
70
  createMany: defaultValidate,
71
71
  update: defaultValidate,
72
72
  updateMany: defaultValidate,
73
+ set: defaultValidate,
73
74
  remove: defaultValidate,
74
75
  removeMany: defaultValidate,
75
76
  };
@@ -93,6 +94,7 @@ var BaseCore = /** @class */ (function () {
93
94
  this.beforeUpdate = typeof props.beforeUpdate === 'function' ? props.beforeUpdate : defaultAfterAndBefore;
94
95
  this.beforeUpdateMany =
95
96
  typeof props.beforeUpdateMany === 'function' ? props.beforeUpdateMany : defaultAfterAndBefore;
97
+ this.beforeSet = typeof props.beforeSet === 'function' ? props.beforeSet : defaultAfterAndBefore;
96
98
  this.beforeRemove = typeof props.beforeRemove === 'function' ? props.beforeRemove : defaultAfterAndBefore;
97
99
  this.beforeRemoveMany =
98
100
  typeof props.beforeRemoveMany === 'function' ? props.beforeRemoveMany : defaultAfterAndBefore;
@@ -101,6 +103,7 @@ var BaseCore = /** @class */ (function () {
101
103
  this.afterCreateMany = typeof props.afterCreateMany === 'function' ? props.afterCreateMany : defaultAfterAndBefore;
102
104
  this.afterUpdate = typeof props.afterUpdate === 'function' ? props.afterUpdate : defaultAfterAndBefore;
103
105
  this.afterUpdateMany = typeof props.afterUpdateMany === 'function' ? props.afterUpdateMany : defaultAfterAndBefore;
106
+ this.afterSet = typeof props.afterSet === 'function' ? props.afterSet : defaultAfterAndBefore;
104
107
  this.afterRemove = typeof props.afterRemove === 'function' ? props.afterRemove : defaultAfterAndBefore;
105
108
  this.afterRemoveMany = typeof props.afterRemoveMany === 'function' ? props.afterRemoveMany : defaultAfterAndBefore;
106
109
  this.validators = utils_2.setFunctions((props === null || props === void 0 ? void 0 : props.validators) || {}, this.validators);
@@ -513,6 +516,42 @@ var BaseCore = /** @class */ (function () {
513
516
  });
514
517
  });
515
518
  };
519
+ BaseCore.prototype.set = function (args) {
520
+ var _a, _b, _c;
521
+ return __awaiter(this, void 0, void 0, function () {
522
+ var _this = this;
523
+ return __generator(this, function (_d) {
524
+ return [2 /*return*/, this.runAction({
525
+ args: args,
526
+ validate: this.validators.set,
527
+ accessRights: [ACCESS_RIGHTS.UPDATE, ACCESS_RIGHTS.UPDATE],
528
+ action: function (args) { return __awaiter(_this, void 0, void 0, function () {
529
+ var params, object, items, result;
530
+ var _a;
531
+ return __generator(this, function (_b) {
532
+ switch (_b.label) {
533
+ case 0:
534
+ params = (args || {}).params;
535
+ this.logger.log("Requesting setting of item(s) for " + this.name + ":", args);
536
+ return [4 /*yield*/, this.beforeSet(params, this.getGateways())];
537
+ case 1:
538
+ object = _b.sent();
539
+ return [4 /*yield*/, ((_a = this.gateway) === null || _a === void 0 ? void 0 : _a.set(object))];
540
+ case 2:
541
+ items = _b.sent();
542
+ return [4 /*yield*/, this.afterSet(items, this.getGateways())];
543
+ case 3:
544
+ result = _b.sent();
545
+ return [2 /*return*/, this.success(result)];
546
+ }
547
+ });
548
+ }); },
549
+ hasTransaction: false,
550
+ 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',
551
+ })];
552
+ });
553
+ });
554
+ };
516
555
  BaseCore.prototype.remove = function (args) {
517
556
  var _a, _b, _c;
518
557
  return __awaiter(this, void 0, void 0, function () {
@@ -19,6 +19,14 @@ declare class BaseGateway {
19
19
  create(args: any): Promise<any>;
20
20
  update(where: any, params: any): Promise<any>;
21
21
  updateMany(params: any): Promise<any[]>;
22
+ /**
23
+ * Inserts or updates data in the database.
24
+ * If the element of given array has not empty id field then the record is updated
25
+ * oterwise inserted
26
+ * @param params Array of objects to update or create
27
+ * @returns Array of updated or inserted objects
28
+ */
29
+ set(params: any): Promise<any[]>;
22
30
  remove(where: any, hardRemove?: boolean): Promise<any>;
23
31
  }
24
32
  export { BaseGateway };
@@ -216,7 +216,7 @@ var BaseGateway = /** @class */ (function () {
216
216
  };
217
217
  BaseGateway.prototype.updateMany = function (params) {
218
218
  return __awaiter(this, void 0, void 0, function () {
219
- var results, idx, len, items;
219
+ var results, idx, len, item, items;
220
220
  return __generator(this, function (_a) {
221
221
  switch (_a.label) {
222
222
  case 0:
@@ -228,7 +228,8 @@ var BaseGateway = /** @class */ (function () {
228
228
  _a.label = 1;
229
229
  case 1:
230
230
  if (!(idx < len)) return [3 /*break*/, 4];
231
- return [4 /*yield*/, this.update(params.where, params.params)];
231
+ item = params[idx];
232
+ return [4 /*yield*/, this.update(item.where, item.params)];
232
233
  case 2:
233
234
  items = _a.sent();
234
235
  results.push(items);
@@ -241,6 +242,51 @@ var BaseGateway = /** @class */ (function () {
241
242
  });
242
243
  });
243
244
  };
245
+ /**
246
+ * Inserts or updates data in the database.
247
+ * If the element of given array has not empty id field then the record is updated
248
+ * oterwise inserted
249
+ * @param params Array of objects to update or create
250
+ * @returns Array of updated or inserted objects
251
+ */
252
+ BaseGateway.prototype.set = function (params) {
253
+ return __awaiter(this, void 0, void 0, function () {
254
+ var results, idField, idx, len, item, items, items;
255
+ var _a;
256
+ return __generator(this, function (_b) {
257
+ switch (_b.label) {
258
+ case 0:
259
+ if (!Array.isArray(params)) {
260
+ params = [params];
261
+ }
262
+ results = [];
263
+ idField = this.getIdField();
264
+ idx = 0, len = params.length;
265
+ _b.label = 1;
266
+ case 1:
267
+ if (!(idx < len)) return [3 /*break*/, 6];
268
+ item = params[idx];
269
+ if (!(item === null || item === void 0 ? void 0 : item[idField])) return [3 /*break*/, 3];
270
+ return [4 /*yield*/, this.update((_a = {},
271
+ _a[idField] = item === null || item === void 0 ? void 0 : item[idField],
272
+ _a), item)];
273
+ case 2:
274
+ items = _b.sent();
275
+ results.push.apply(results, items);
276
+ return [3 /*break*/, 5];
277
+ case 3: return [4 /*yield*/, this.create(item)];
278
+ case 4:
279
+ items = _b.sent();
280
+ results.push.apply(results, items);
281
+ _b.label = 5;
282
+ case 5:
283
+ ++idx;
284
+ return [3 /*break*/, 1];
285
+ case 6: return [2 /*return*/, results];
286
+ }
287
+ });
288
+ });
289
+ };
244
290
  BaseGateway.prototype.remove = function (where, hardRemove) {
245
291
  if (hardRemove === void 0) { hardRemove = false; }
246
292
  return __awaiter(this, void 0, void 0, function () {
@@ -1,5 +1,7 @@
1
1
  /**
2
- * Removes all the records from provided table
2
+ * Removes all the records from provided table.
3
+ * If appSettings has cleanTable object with table names then
4
+ * it is used to add extra filters to clear the table to avoid for example users data
3
5
  * @param table Name of table from which to remove all the records
4
6
  * @param knexHandle Knex handle object with configured connection to DB
5
7
  * @param appSettings settings with db related information
@@ -42,21 +42,40 @@ Object.defineProperty(exports, "__esModule", { value: true });
42
42
  exports.dbRunMigrations = exports.dbTransformSeedsToObject = exports.dbReset = exports.dbWipe = exports.dbClean = exports.dbListTables = exports.dbCleanTable = void 0;
43
43
  var lodash_1 = require("lodash");
44
44
  var bluebird_1 = __importDefault(require("bluebird"));
45
+ var SQLS = {
46
+ pg: {
47
+ listTablesSchema: function (appSettings) { return "\n SELECT TABLE_NAME as table_name\n FROM INFORMATION_SCHEMA.TABLES\n WHERE TABLE_SCHEMA = '" + (appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema) + "';\n "; },
48
+ listTables: function () { return "\n SELECT TABLE_NAME as table_name\n FROM INFORMATION_SCHEMA.TABLES\n "; },
49
+ dropTablesSchema: function (appSettings) { return "\n SELECT 'DROP TABLE IF EXISTS " + (appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema) + ".' || TABLE_NAME || ';' as cmd\n FROM INFORMATION_SCHEMA.TABLES\n WHERE TABLE_SCHEMA = '" + (appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema) + "';\n "; },
50
+ dropTables: function () { return "\n SELECT 'DROP TABLE IF EXISTS ' || TABLE_NAME || ';' as cmd\n FROM INFORMATION_SCHEMA.TABLES';\n "; },
51
+ },
52
+ };
45
53
  /**
46
- * Removes all the records from provided table
54
+ * Removes all the records from provided table.
55
+ * If appSettings has cleanTable object with table names then
56
+ * it is used to add extra filters to clear the table to avoid for example users data
47
57
  * @param table Name of table from which to remove all the records
48
58
  * @param knexHandle Knex handle object with configured connection to DB
49
59
  * @param appSettings settings with db related information
50
60
  */
51
61
  var dbCleanTable = function (table, knexHandle, appSettings) { return __awaiter(void 0, void 0, void 0, function () {
52
- var tableName;
53
- return __generator(this, function (_a) {
54
- switch (_a.label) {
62
+ var query, cleanTable;
63
+ var _a;
64
+ return __generator(this, function (_b) {
65
+ switch (_b.label) {
55
66
  case 0:
56
- tableName = appSettings.dbSchema ? appSettings.dbSchema + "." + table : table;
57
- return [4 /*yield*/, knexHandle.raw("DELETE FROM " + tableName)];
67
+ query = knexHandle(table).withSchema(appSettings.dbSchema).delete();
68
+ cleanTable = (_a = appSettings === null || appSettings === void 0 ? void 0 : appSettings.cleanTable) === null || _a === void 0 ? void 0 : _a[table];
69
+ if (cleanTable != undefined) {
70
+ if (Array.isArray(cleanTable.where)) {
71
+ cleanTable.where.forEach(function (whereItem) {
72
+ query.where(whereItem.fieldName, whereItem.operand, whereItem.value);
73
+ });
74
+ }
75
+ }
76
+ return [4 /*yield*/, query];
58
77
  case 1:
59
- _a.sent();
78
+ _b.sent();
60
79
  return [2 /*return*/];
61
80
  }
62
81
  });
@@ -74,9 +93,9 @@ var dbListTables = function (knexHandle, appSettings) { return __awaiter(void 0,
74
93
  switch (_a.label) {
75
94
  case 0:
76
95
  skipTables = (appSettings || {}).skipTables || [];
77
- query = "\n SELECT TABLE_NAME as table_name\n FROM INFORMATION_SCHEMA.TABLES\n WHERE TABLE_SCHEMA = '" + appSettings.dbSchema + "';";
96
+ query = SQLS[appSettings.dbType].listTablesSchema(appSettings);
78
97
  if (!appSettings.dbSchema) {
79
- query = "\n SELECT TABLE_NAME as table_name\n FROM INFORMATION_SCHEMA.TABLES";
98
+ query = SQLS[appSettings.dbType].listTables(appSettings);
80
99
  }
81
100
  return [4 /*yield*/, knexHandle.raw(query)];
82
101
  case 1:
@@ -124,12 +143,12 @@ exports.dbClean = dbClean;
124
143
  * TODO: Remove all constraints and then drop tables.
125
144
  */
126
145
  var dbWipe = function (knexHandle, appSettings) {
127
- var qyery = "\n SELECT 'DROP TABLE IF EXISTS " + appSettings.dbSchema + ".' || TABLE_NAME || ';' as cmd\n FROM INFORMATION_SCHEMA.TABLES\n WHERE TABLE_SCHEMA = '" + appSettings.dbSchema + "';\n ";
146
+ var query = SQLS[appSettings.dbType].dropTablesSchema(appSettings);
128
147
  if (!appSettings.dbSchema) {
129
- qyery = "\n SELECT 'DROP TABLE IF EXISTS ' || TABLE_NAME || ';' as cmd\n FROM INFORMATION_SCHEMA.TABLES';\n ";
148
+ query = SQLS[appSettings.dbType].dropTables(appSettings);
130
149
  }
131
150
  return knexHandle
132
- .raw(qyery)
151
+ .raw(query)
133
152
  .then(function (result) { return result.rows.map(function (r) { return r.cmd; }); })
134
153
  .then(function (commands) { return bluebird_1.default.each(commands, function (cmd) { return knexHandle.raw(cmd); }); });
135
154
  };
@@ -10,6 +10,7 @@ export interface DoingWhatInterface {
10
10
  createMany: string;
11
11
  update: string;
12
12
  updateMany: string;
13
+ set: string;
13
14
  remove: string;
14
15
  removeMany: string;
15
16
  [key: string]: string;
@@ -29,6 +30,7 @@ export interface BaseCoreInterface {
29
30
  beforeCreateMany?(params: any, gateways?: any): any;
30
31
  beforeUpdate?(params: any, gateways?: any): any;
31
32
  beforeUpdateMany?(params: any, gateways?: any): any;
33
+ beforeSet?(params: any, gateways?: any): any;
32
34
  beforeRemove?(params: any, gateways?: any): any;
33
35
  beforeRemoveMany?(params: any, gateways?: any): any;
34
36
  afterList?(params: any, gateways?: any): any;
@@ -36,6 +38,7 @@ export interface BaseCoreInterface {
36
38
  afterCreateMany?(params: any, gateways?: any): any;
37
39
  afterUpdate?(params: any, gateways?: any): any;
38
40
  afterUpdateMany?(params: any, gateways?: any): any;
41
+ afterSet?(params: any, gateways?: any): any;
39
42
  afterRemove?(params: any, gateways?: any): any;
40
43
  afterRemoveMany?(params: any, gateways?: any): any;
41
44
  validateList?(params: any, gateways?: any): Promise<any[]> | any[];
@@ -45,6 +48,7 @@ export interface BaseCoreInterface {
45
48
  validateCreateMany?(params: any, gateways?: any): Promise<any[]> | any[];
46
49
  validateUpdate?(params: any, gateways?: any): Promise<any[]> | any[];
47
50
  validateUpdateMany?(params: any, gateways?: any): Promise<any[]> | any[];
51
+ validateSet?(params: any, gateways?: any): Promise<any[]> | any[];
48
52
  validateRemove?(params: any, gateways?: any): Promise<any[]> | any[];
49
53
  validateRemoveMany?(params: any, gateways?: any): Promise<any[]> | any[];
50
54
  validators?: BaseCoreValidatorsInterface;
@@ -6,6 +6,7 @@ export interface BaseCoreValidatorsInterface {
6
6
  createMany?(params: any, gateways?: any): Promise<any[]> | any[];
7
7
  update?(params: any, gateways?: any): Promise<any[]> | any[];
8
8
  updateMany?(params: any, gateways?: any): Promise<any[]> | any[];
9
+ set?(params: any, gateways?: any): Promise<any[]> | any[];
9
10
  remove?(params: any, gateways?: any): Promise<any[]> | any[];
10
11
  removeMany?(params: any, gateways?: any): Promise<any[]> | any[];
11
12
  }
@@ -11,6 +11,7 @@ export interface BuildDefaultResolvers {
11
11
  disableCreateMany?: boolean;
12
12
  disableUpdate?: boolean;
13
13
  disableUpdateMany?: boolean;
14
+ disableSet?: boolean;
14
15
  disableRemove?: boolean;
15
16
  disableRemoveMany?: boolean;
16
17
  }
@@ -3,4 +3,5 @@ export interface PaginationInterface {
3
3
  page: number;
4
4
  pageSize?: number;
5
5
  sortBy: SortItemInterface[];
6
+ cursor: string;
6
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
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",