@sdflc/backend-helpers 1.0.49 → 1.0.50
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.
- package/build/BaseController.js +21 -62
- package/build/BaseCore.js +425 -690
- package/build/BaseGateway.js +350 -554
- package/build/DataCache.js +47 -98
- package/build/Database.js +104 -177
- package/build/DbMigratorAndSeeder.js +131 -238
- package/build/apiContext.js +102 -191
- package/build/checkItHelpers.js +10 -10
- package/build/commonValidators.js +49 -91
- package/build/configBulder.js +8 -19
- package/build/constants.js +3 -3
- package/build/dbHelpers.js +72 -126
- package/build/dbSeeder.js +38 -90
- package/build/dbTableHelpers.js +25 -26
- package/build/knexConfigBuilder.js +17 -28
- package/build/passwordHelpers.js +13 -59
- package/build/redisClient.d.ts +1 -0
- package/build/redisClient.js +50 -84
- package/build/resolversHelpers.js +17 -27
- package/build/userToken.js +24 -24
- package/build/utils.js +4 -5
- package/package.json +32 -34
package/build/DataCache.js
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -19,45 +8,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
19
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
9
|
});
|
|
21
10
|
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (_) try {
|
|
29
|
-
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;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
13
|
};
|
|
52
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
15
|
exports.DataCache = void 0;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
16
|
+
const dataloader_1 = __importDefault(require("dataloader"));
|
|
17
|
+
const redis_dataloader_1 = __importDefault(require("redis-dataloader"));
|
|
18
|
+
const utils_1 = require("@sdflc/utils");
|
|
19
|
+
const _1 = require(".");
|
|
20
|
+
class DataCache {
|
|
21
|
+
constructor(props) {
|
|
22
|
+
const { loaderFn, keyPrefix, context, buildKey, redisClient, table } = props || {};
|
|
61
23
|
if (!(props === null || props === void 0 ? void 0 : props.idField)) {
|
|
62
24
|
props.idField = _1.FIELDS.ID;
|
|
63
25
|
}
|
|
@@ -65,59 +27,49 @@ var DataCache = /** @class */ (function () {
|
|
|
65
27
|
this.buildKey = typeof buildKey === 'function' ? buildKey : utils_1.buildKey;
|
|
66
28
|
this.batchLoaderDefault = this.batchLoaderDefault.bind(this);
|
|
67
29
|
this.redisDataLoader = redisClient ? (0, redis_dataloader_1.default)({ redis: redisClient }) : null;
|
|
68
|
-
|
|
69
|
-
|
|
30
|
+
const fn = typeof loaderFn === 'function' ? loaderFn : this.batchLoaderDefault(props);
|
|
31
|
+
const prefixToUse = (context === null || context === void 0 ? void 0 : context.keyPrefix) || keyPrefix || table;
|
|
70
32
|
this.init(fn, prefixToUse, context);
|
|
71
33
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
var table = props.table, idField = props.idField, readAll = props.readAll, db = props.db;
|
|
34
|
+
batchLoaderDefault(props) {
|
|
35
|
+
const { table, idField, readAll, db } = props;
|
|
75
36
|
if (!table) {
|
|
76
|
-
throw Error(
|
|
37
|
+
throw Error(`DataCache requires 'table' property to be setup`);
|
|
77
38
|
}
|
|
78
39
|
else if (!idField && readAll == null) {
|
|
79
|
-
throw Error(
|
|
40
|
+
throw Error(`DataCache requires 'idField' or 'readAll' property to be setup`);
|
|
80
41
|
}
|
|
81
42
|
else if (!db) {
|
|
82
|
-
throw Error(
|
|
43
|
+
throw Error(`DataCache requires 'db' property to be setup`);
|
|
83
44
|
}
|
|
84
|
-
return
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
else {
|
|
106
|
-
resultItems.push.apply(resultItems, ids.map(function (id) { return dbItems[_this.buildKey(id)] || null; }));
|
|
107
|
-
}
|
|
108
|
-
return [2 /*return*/, resultItems];
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
}); };
|
|
112
|
-
};
|
|
113
|
-
DataCache.prototype.init = function (batchFunction, keyPrefix, context) {
|
|
45
|
+
return (ids) => __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
const resultItems = [];
|
|
47
|
+
const readCfg = Object.assign(Object.assign({}, props), { buildKey: this.buildKey });
|
|
48
|
+
if (!readAll) {
|
|
49
|
+
readCfg.mapBy = idField;
|
|
50
|
+
readCfg.whereIn = {
|
|
51
|
+
field: idField,
|
|
52
|
+
values: ids.filter((id) => id),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const dbItems = yield db.read(readCfg);
|
|
56
|
+
if (readAll) {
|
|
57
|
+
resultItems.push(...dbItems);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
resultItems.push(...ids.map((id) => dbItems[this.buildKey(id)] || null));
|
|
61
|
+
}
|
|
62
|
+
return resultItems;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
init(batchFunction, keyPrefix, context) {
|
|
114
66
|
if (this.redisDataLoader) {
|
|
115
67
|
this.loader = new this.redisDataLoader(
|
|
116
68
|
// set a prefix for the keys stored in redis. This way you can avoid key
|
|
117
69
|
// collisions for different data-sets in your redis instance.
|
|
118
70
|
keyPrefix,
|
|
119
71
|
// create a regular dataloader. This should always be set with caching disabled.
|
|
120
|
-
new dataloader_1.default(
|
|
72
|
+
new dataloader_1.default((ids) => batchFunction(ids, context), {
|
|
121
73
|
cache: false,
|
|
122
74
|
}), {
|
|
123
75
|
cache: true,
|
|
@@ -125,26 +77,23 @@ var DataCache = /** @class */ (function () {
|
|
|
125
77
|
});
|
|
126
78
|
}
|
|
127
79
|
else {
|
|
128
|
-
this.loader = new dataloader_1.default(
|
|
80
|
+
this.loader = new dataloader_1.default((ids) => batchFunction(ids, context));
|
|
129
81
|
}
|
|
130
|
-
}
|
|
131
|
-
|
|
82
|
+
}
|
|
83
|
+
load(key) {
|
|
132
84
|
return this.loader.load(this.buildKey(key));
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
DataCache.prototype.clear = function (key) {
|
|
139
|
-
var _this = this;
|
|
85
|
+
}
|
|
86
|
+
loadMany(keys) {
|
|
87
|
+
return this.loader.loadMany(keys.map((key) => this.buildKey(key)));
|
|
88
|
+
}
|
|
89
|
+
clear(key) {
|
|
140
90
|
var _a;
|
|
141
91
|
if (Array.isArray(key)) {
|
|
142
|
-
key.forEach(
|
|
92
|
+
key.forEach((item) => { var _a; return (_a = this.loader) === null || _a === void 0 ? void 0 : _a.clear(this.buildKey(item)); });
|
|
143
93
|
}
|
|
144
94
|
else {
|
|
145
95
|
(_a = this.loader) === null || _a === void 0 ? void 0 : _a.clear(this.buildKey(key));
|
|
146
96
|
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
}());
|
|
97
|
+
}
|
|
98
|
+
}
|
|
150
99
|
exports.DataCache = DataCache;
|
package/build/Database.js
CHANGED
|
@@ -8,43 +8,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
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
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
13
|
};
|
|
41
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
15
|
exports.Database = void 0;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
16
|
+
const lodash_1 = require("lodash");
|
|
17
|
+
const knex_1 = __importDefault(require("knex"));
|
|
18
|
+
const utils_1 = require("@sdflc/utils");
|
|
19
|
+
class Database {
|
|
20
|
+
constructor(props) {
|
|
48
21
|
this.trx = null;
|
|
49
22
|
this.knexHandle = null;
|
|
50
23
|
this.dbSchema = '';
|
|
@@ -55,159 +28,114 @@ var Database = /** @class */ (function () {
|
|
|
55
28
|
this.dbSchema = props.dbSchema || '';
|
|
56
29
|
this.trx = props.trx;
|
|
57
30
|
}
|
|
58
|
-
|
|
31
|
+
destroy() {
|
|
59
32
|
if (this.knexHandle) {
|
|
60
33
|
this.knexHandle.destroy();
|
|
61
34
|
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
35
|
+
}
|
|
36
|
+
isDbError(ex) {
|
|
37
|
+
const { code } = ex || {};
|
|
65
38
|
switch (code) {
|
|
66
39
|
default:
|
|
67
40
|
return [false, ''];
|
|
68
41
|
case '23505':
|
|
69
|
-
return [true,
|
|
42
|
+
return [true, `Such item already exists`];
|
|
70
43
|
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return (trx ? trx : _this.knexHandle).transaction(resolve);
|
|
44
|
+
}
|
|
45
|
+
createTransaction(trx) {
|
|
46
|
+
return new Promise((resolve) => {
|
|
47
|
+
return (trx ? trx : this.knexHandle).transaction(resolve);
|
|
76
48
|
});
|
|
77
|
-
}
|
|
78
|
-
|
|
49
|
+
}
|
|
50
|
+
getSchema() {
|
|
79
51
|
return this.dbSchema;
|
|
80
|
-
}
|
|
81
|
-
|
|
52
|
+
}
|
|
53
|
+
getBuilder(arg) {
|
|
82
54
|
return this.trx ? this.trx(arg).withSchema(this.getSchema()) : this.knexHandle(arg).withSchema(this.getSchema());
|
|
83
|
-
}
|
|
84
|
-
|
|
55
|
+
}
|
|
56
|
+
raw(query, bindings) {
|
|
85
57
|
return this.knexHandle.raw(query, bindings);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
89
|
-
|
|
90
|
-
return
|
|
91
|
-
switch (_a.label) {
|
|
92
|
-
case 0: return [4 /*yield*/, (this.trx ? this.trx : this.knexHandle).raw(query, bindings)];
|
|
93
|
-
case 1:
|
|
94
|
-
rows = _a.sent();
|
|
95
|
-
return [2 /*return*/, rows];
|
|
96
|
-
}
|
|
97
|
-
});
|
|
58
|
+
}
|
|
59
|
+
runRawQuery(query, bindings) {
|
|
60
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
const rows = yield (this.trx ? this.trx : this.knexHandle).raw(query, bindings);
|
|
62
|
+
return rows;
|
|
98
63
|
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
case 0: return [4 /*yield*/, this.createTransaction(this.trx)];
|
|
106
|
-
case 1:
|
|
107
|
-
trx = _a.sent();
|
|
108
|
-
this.trx = trx;
|
|
109
|
-
return [2 /*return*/, trx];
|
|
110
|
-
}
|
|
111
|
-
});
|
|
64
|
+
}
|
|
65
|
+
startTransaction() {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
const trx = yield this.createTransaction(this.trx);
|
|
68
|
+
this.trx = trx;
|
|
69
|
+
return trx;
|
|
112
70
|
});
|
|
113
|
-
}
|
|
114
|
-
|
|
71
|
+
}
|
|
72
|
+
setTransaction(trx) {
|
|
115
73
|
this.trx = trx;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return [4 /*yield*/, this.trx.commit()];
|
|
124
|
-
case 1:
|
|
125
|
-
_a.sent();
|
|
126
|
-
this.trx = undefined;
|
|
127
|
-
_a.label = 2;
|
|
128
|
-
case 2: return [2 /*return*/];
|
|
129
|
-
}
|
|
130
|
-
});
|
|
74
|
+
}
|
|
75
|
+
commitTransaction() {
|
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
if (this.trx) {
|
|
78
|
+
yield this.trx.commit();
|
|
79
|
+
this.trx = undefined;
|
|
80
|
+
}
|
|
131
81
|
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
case 0:
|
|
139
|
-
if (!this.trx) return [3 /*break*/, 4];
|
|
140
|
-
_a.label = 1;
|
|
141
|
-
case 1:
|
|
142
|
-
_a.trys.push([1, 3, , 4]);
|
|
143
|
-
return [4 /*yield*/, this.trx.rollback()];
|
|
144
|
-
case 2:
|
|
145
|
-
_a.sent();
|
|
146
|
-
return [3 /*break*/, 4];
|
|
147
|
-
case 3:
|
|
148
|
-
ex_1 = _a.sent();
|
|
149
|
-
return [3 /*break*/, 4];
|
|
150
|
-
case 4:
|
|
151
|
-
this.trx = undefined;
|
|
152
|
-
return [2 /*return*/];
|
|
82
|
+
}
|
|
83
|
+
rollbackTransaction() {
|
|
84
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
if (this.trx) {
|
|
86
|
+
try {
|
|
87
|
+
yield this.trx.rollback();
|
|
153
88
|
}
|
|
154
|
-
|
|
89
|
+
catch (ex) {
|
|
90
|
+
// Do nothing here
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
this.trx = undefined;
|
|
155
94
|
});
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
calculatedFields.forEach(
|
|
95
|
+
}
|
|
96
|
+
doCalculated(item, calculatedFields) {
|
|
97
|
+
calculatedFields.forEach((calcField) => {
|
|
159
98
|
if (calcField.name) {
|
|
160
99
|
item[calcField.name] = typeof calcField.value === 'function' ? calcField.value(item) : calcField.value;
|
|
161
100
|
}
|
|
162
101
|
});
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
acc[key] = item;
|
|
195
|
-
return acc;
|
|
196
|
-
}, {});
|
|
197
|
-
}
|
|
198
|
-
else {
|
|
199
|
-
if (Array.isArray(calculated) && calculated.length > 0) {
|
|
200
|
-
dbResults.forEach(function (row) {
|
|
201
|
-
_this.doCalculated(row, calculated);
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return [2 /*return*/, dbResults];
|
|
102
|
+
}
|
|
103
|
+
read(args) {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
const { where, whereIn, extendQuery, table, selectFields, mapBy, calculated, buildKey } = args || {};
|
|
106
|
+
const query = this.getBuilder(table).select(Array.isArray(selectFields) && selectFields.length > 0 ? selectFields : '*');
|
|
107
|
+
if (where) {
|
|
108
|
+
query.where(where);
|
|
109
|
+
}
|
|
110
|
+
if (whereIn) {
|
|
111
|
+
query.whereIn(whereIn.field, whereIn.values);
|
|
112
|
+
}
|
|
113
|
+
if (typeof extendQuery === 'function') {
|
|
114
|
+
yield extendQuery(query);
|
|
115
|
+
}
|
|
116
|
+
let dbResults = yield query;
|
|
117
|
+
if (mapBy) {
|
|
118
|
+
const fnBuildKey = typeof buildKey === 'function' ? buildKey : utils_1.buildKey;
|
|
119
|
+
dbResults = dbResults.reduce((acc, item) => {
|
|
120
|
+
const key = fnBuildKey(item[(0, lodash_1.camelCase)(mapBy)]);
|
|
121
|
+
if (Array.isArray(calculated) && calculated.length > 0) {
|
|
122
|
+
this.doCalculated(item, calculated);
|
|
123
|
+
}
|
|
124
|
+
acc[key] = item;
|
|
125
|
+
return acc;
|
|
126
|
+
}, {});
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
if (Array.isArray(calculated) && calculated.length > 0) {
|
|
130
|
+
dbResults.forEach((row) => {
|
|
131
|
+
this.doCalculated(row, calculated);
|
|
132
|
+
});
|
|
206
133
|
}
|
|
207
|
-
}
|
|
134
|
+
}
|
|
135
|
+
return dbResults;
|
|
208
136
|
});
|
|
209
|
-
}
|
|
210
|
-
|
|
137
|
+
}
|
|
138
|
+
createSortBy(sortBy, defaultSortBy) {
|
|
211
139
|
if (!sortBy) {
|
|
212
140
|
if (Array.isArray(defaultSortBy)) {
|
|
213
141
|
return this.createSortBy(defaultSortBy);
|
|
@@ -216,32 +144,31 @@ var Database = /** @class */ (function () {
|
|
|
216
144
|
return null;
|
|
217
145
|
}
|
|
218
146
|
}
|
|
219
|
-
return sortBy.reduce(
|
|
147
|
+
return sortBy.reduce((acc, item) => {
|
|
220
148
|
acc.push({
|
|
221
149
|
column: item.name,
|
|
222
150
|
order: item.order,
|
|
223
151
|
});
|
|
224
152
|
return acc;
|
|
225
153
|
}, []);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
154
|
+
}
|
|
155
|
+
createLimitOffset(page, pageSize) {
|
|
156
|
+
const limit = isNaN(pageSize) || pageSize == null ? undefined : Number(pageSize);
|
|
157
|
+
const pageToUse = isNaN(page) || page < 1 ? 1 : Number(page);
|
|
230
158
|
return {
|
|
231
|
-
limit
|
|
159
|
+
limit,
|
|
232
160
|
offset: limit ? (pageToUse - 1) * limit : 0,
|
|
233
161
|
};
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
162
|
+
}
|
|
163
|
+
createLimitOffsetSortBy(pagination, defaultSortBy) {
|
|
164
|
+
const { page, pageSize, sortBy } = pagination || {};
|
|
165
|
+
const { limit, offset } = this.createLimitOffset(page, pageSize);
|
|
166
|
+
const sortByToUse = this.createSortBy(sortBy, defaultSortBy);
|
|
239
167
|
return {
|
|
240
|
-
limit
|
|
241
|
-
offset
|
|
168
|
+
limit,
|
|
169
|
+
offset,
|
|
242
170
|
sortBy: sortByToUse,
|
|
243
171
|
};
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
}());
|
|
172
|
+
}
|
|
173
|
+
}
|
|
247
174
|
exports.Database = Database;
|