@sdflc/backend-helpers 1.0.48 → 1.0.51
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.d.ts +2 -2
- package/build/BaseCore.js +425 -690
- package/build/BaseGateway.js +358 -554
- package/build/DataCache.d.ts +1 -1
- package/build/DataCache.js +55 -104
- package/build/Database.js +104 -177
- package/build/DbMigratorAndSeeder.js +131 -238
- package/build/RedisDataLoader.d.ts +41 -0
- package/build/RedisDataLoader.js +248 -0
- package/build/apiContext.js +103 -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/interfaces/BaseGatewayPropsInterface.d.ts +1 -0
- package/build/interfaces/CoreActionInterface.d.ts +2 -1
- package/build/knexConfigBuilder.js +17 -28
- package/build/passwordHelpers.js +13 -59
- package/build/redisClient.d.ts +6 -0
- package/build/redisClient.js +87 -88
- package/build/resolversHelpers.js +17 -27
- package/build/userToken.js +24 -24
- package/build/utils.js +4 -5
- package/package.json +33 -34
package/build/DataCache.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { CacheInterface, LoaderInterface } from './interfaces';
|
|
2
2
|
declare class DataCache implements LoaderInterface {
|
|
3
3
|
private buildKey;
|
|
4
|
-
private redisDataLoader;
|
|
5
4
|
private loader;
|
|
6
5
|
private config;
|
|
6
|
+
private redisClient;
|
|
7
7
|
constructor(props: CacheInterface);
|
|
8
8
|
batchLoaderDefault(props: CacheInterface): (ids: any[]) => Promise<any[]>;
|
|
9
9
|
init(batchFunction: any, keyPrefix?: string, context?: any): void;
|
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,132 +8,94 @@ 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 RedisDataLoader_1 = require("./RedisDataLoader");
|
|
18
|
+
const utils_1 = require("@sdflc/utils");
|
|
19
|
+
const _1 = require(".");
|
|
20
|
+
const lodash_1 = require("lodash");
|
|
21
|
+
class DataCache {
|
|
22
|
+
constructor(props) {
|
|
23
|
+
const { loaderFn, keyPrefix, context, buildKey, redisClient, table } = props || {};
|
|
61
24
|
if (!(props === null || props === void 0 ? void 0 : props.idField)) {
|
|
62
25
|
props.idField = _1.FIELDS.ID;
|
|
63
26
|
}
|
|
64
27
|
this.config = props;
|
|
28
|
+
this.redisClient = redisClient;
|
|
65
29
|
this.buildKey = typeof buildKey === 'function' ? buildKey : utils_1.buildKey;
|
|
66
30
|
this.batchLoaderDefault = this.batchLoaderDefault.bind(this);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
var prefixToUse = (context === null || context === void 0 ? void 0 : context.keyPrefix) || keyPrefix || table;
|
|
31
|
+
const fn = typeof loaderFn === 'function' ? loaderFn : this.batchLoaderDefault(props);
|
|
32
|
+
const prefixToUse = (context === null || context === void 0 ? void 0 : context.keyPrefix) || keyPrefix || table;
|
|
70
33
|
this.init(fn, prefixToUse, context);
|
|
71
34
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
var table = props.table, idField = props.idField, readAll = props.readAll, db = props.db;
|
|
35
|
+
batchLoaderDefault(props) {
|
|
36
|
+
const { table, idField, readAll, db } = props;
|
|
75
37
|
if (!table) {
|
|
76
|
-
throw Error(
|
|
38
|
+
throw Error(`DataCache requires 'table' property to be setup`);
|
|
77
39
|
}
|
|
78
40
|
else if (!idField && readAll == null) {
|
|
79
|
-
throw Error(
|
|
41
|
+
throw Error(`DataCache requires 'idField' or 'readAll' property to be setup`);
|
|
80
42
|
}
|
|
81
43
|
else if (!db) {
|
|
82
|
-
throw Error(
|
|
44
|
+
throw Error(`DataCache requires 'db' property to be setup`);
|
|
83
45
|
}
|
|
84
|
-
return
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
return [2 /*return*/, resultItems];
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
}); };
|
|
112
|
-
};
|
|
113
|
-
DataCache.prototype.init = function (batchFunction, keyPrefix, context) {
|
|
114
|
-
if (this.redisDataLoader) {
|
|
115
|
-
this.loader = new this.redisDataLoader(
|
|
46
|
+
return (ids) => __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
const resultItems = [];
|
|
48
|
+
const readCfg = Object.assign(Object.assign({}, props), { buildKey: this.buildKey });
|
|
49
|
+
const idsToUse = (0, lodash_1.castArray)(ids);
|
|
50
|
+
if (!readAll) {
|
|
51
|
+
readCfg.mapBy = idField;
|
|
52
|
+
readCfg.whereIn = {
|
|
53
|
+
field: idField,
|
|
54
|
+
values: idsToUse.filter((id) => id),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const dbItems = yield db.read(readCfg);
|
|
58
|
+
if (readAll) {
|
|
59
|
+
resultItems.push(...dbItems);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
resultItems.push(...idsToUse.map((id) => dbItems[this.buildKey(id)] || null));
|
|
63
|
+
}
|
|
64
|
+
return resultItems;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
init(batchFunction, keyPrefix, context) {
|
|
68
|
+
if (this.redisClient) {
|
|
69
|
+
this.loader = new RedisDataLoader_1.RedisDataLoader(this.redisClient,
|
|
116
70
|
// set a prefix for the keys stored in redis. This way you can avoid key
|
|
117
71
|
// collisions for different data-sets in your redis instance.
|
|
118
72
|
keyPrefix,
|
|
119
|
-
// create a regular
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
73
|
+
// create a regular loader. This should always be set with caching disabled.
|
|
74
|
+
{
|
|
75
|
+
load: (ids) => batchFunction(ids, context),
|
|
76
|
+
}, {
|
|
123
77
|
cache: true,
|
|
124
78
|
expire: this.config.cacheTtl,
|
|
125
79
|
});
|
|
126
80
|
}
|
|
127
81
|
else {
|
|
128
|
-
this.loader = new dataloader_1.default(
|
|
82
|
+
this.loader = new dataloader_1.default((ids) => batchFunction(ids, context));
|
|
129
83
|
}
|
|
130
|
-
}
|
|
131
|
-
|
|
84
|
+
}
|
|
85
|
+
load(key) {
|
|
132
86
|
return this.loader.load(this.buildKey(key));
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
DataCache.prototype.clear = function (key) {
|
|
139
|
-
var _this = this;
|
|
87
|
+
}
|
|
88
|
+
loadMany(keys) {
|
|
89
|
+
return this.loader.loadMany(keys.map((key) => this.buildKey(key)));
|
|
90
|
+
}
|
|
91
|
+
clear(key) {
|
|
140
92
|
var _a;
|
|
141
93
|
if (Array.isArray(key)) {
|
|
142
|
-
key.forEach(
|
|
94
|
+
key.forEach((item) => { var _a; return (_a = this.loader) === null || _a === void 0 ? void 0 : _a.clear(this.buildKey(item)); });
|
|
143
95
|
}
|
|
144
96
|
else {
|
|
145
97
|
(_a = this.loader) === null || _a === void 0 ? void 0 : _a.clear(this.buildKey(key));
|
|
146
98
|
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
}());
|
|
99
|
+
}
|
|
100
|
+
}
|
|
150
101
|
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;
|