@sdflc/backend-helpers 1.0.76 → 1.0.78
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/README.md +178 -178
- package/build/BaseCore.d.ts +88 -88
- package/build/BaseCore.js +819 -819
- package/build/BaseGateway.d.ts +49 -49
- package/build/BaseGateway.js +484 -483
- package/build/DataCache.d.ts +14 -14
- package/build/DataCache.js +104 -104
- package/build/Database.d.ts +32 -32
- package/build/Database.js +198 -198
- package/build/DbMigratorAndSeeder.d.ts +49 -49
- package/build/DbMigratorAndSeeder.js +238 -228
- package/build/RedisDataLoader.d.ts +41 -41
- package/build/RedisDataLoader.js +248 -248
- package/build/apiContext.d.ts +11 -11
- package/build/apiContext.js +143 -143
- package/build/{BaseController.d.ts → baseController.d.ts} +17 -17
- package/build/{BaseController.js → baseController.js} +66 -66
- package/build/checkItHelpers.d.ts +3 -3
- package/build/checkItHelpers.js +28 -28
- package/build/commonValidators.d.ts +17 -17
- package/build/commonValidators.js +298 -298
- package/build/configBulder.d.ts +2 -2
- package/build/configBulder.js +32 -32
- package/build/constants.d.ts +30 -30
- package/build/constants.js +80 -81
- package/build/dbHelpers.d.ts +43 -43
- package/build/dbHelpers.js +166 -166
- package/build/dbSeeder.d.ts +2 -2
- package/build/dbSeeder.js +92 -82
- package/build/dbTableHelpers.d.ts +11 -11
- package/build/dbTableHelpers.js +110 -110
- package/build/downloadHelpers.d.ts +2 -2
- package/build/downloadHelpers.js +83 -83
- package/build/index.d.ts +21 -21
- package/build/index.js +37 -37
- package/build/interfaces/ApiContext.d.ts +24 -24
- package/build/interfaces/ApiContext.js +2 -2
- package/build/interfaces/ApiContextInterface.d.ts +12 -12
- package/build/interfaces/ApiContextInterface.js +2 -2
- package/build/interfaces/BaseCoreActionsInterface.d.ts +38 -38
- package/build/interfaces/BaseCoreActionsInterface.js +2 -2
- package/build/interfaces/BaseCoreDoAuthOnActionInterface.d.ts +12 -12
- package/build/interfaces/BaseCoreDoAuthOnActionInterface.js +2 -2
- package/build/interfaces/BaseCoreDoingWhatInterface.d.ts +13 -13
- package/build/interfaces/BaseCoreDoingWhatInterface.js +2 -2
- package/build/interfaces/BaseCorePropsInterface.d.ts +36 -36
- package/build/interfaces/BaseCorePropsInterface.js +2 -2
- package/build/interfaces/BaseCoreValidatorsInterface.d.ts +13 -13
- package/build/interfaces/BaseCoreValidatorsInterface.js +2 -2
- package/build/interfaces/BaseGatewayPropsInterface.d.ts +39 -39
- package/build/interfaces/BaseGatewayPropsInterface.js +2 -2
- package/build/interfaces/BuildConfigOptions.d.ts +6 -6
- package/build/interfaces/BuildConfigOptions.js +2 -2
- package/build/interfaces/BuildDefaultResolversOptions.d.ts +17 -17
- package/build/interfaces/BuildDefaultResolversOptions.js +2 -2
- package/build/interfaces/BuildListQueryArgsInterface.d.ts +6 -6
- package/build/interfaces/BuildListQueryArgsInterface.js +2 -2
- package/build/interfaces/CacheInterface.d.ts +15 -15
- package/build/interfaces/CacheInterface.js +2 -2
- package/build/interfaces/CommonValidatorArgs.d.ts +3 -3
- package/build/interfaces/CommonValidatorArgs.js +2 -2
- package/build/interfaces/CoreActionInterface.d.ts +14 -14
- package/build/interfaces/CoreActionInterface.js +2 -2
- package/build/interfaces/CreateUserTokenArgs.d.ts +11 -11
- package/build/interfaces/CreateUserTokenArgs.js +2 -2
- package/build/interfaces/CreateUserTokenResult.d.ts +4 -4
- package/build/interfaces/CreateUserTokenResult.js +2 -2
- package/build/interfaces/DatabaseOptions.d.ts +22 -22
- package/build/interfaces/DatabaseOptions.js +2 -2
- package/build/interfaces/DatabaseReadInterface.d.ts +14 -14
- package/build/interfaces/DatabaseReadInterface.js +2 -2
- package/build/interfaces/DbFieldAddDefaultInterface.d.ts +9 -9
- package/build/interfaces/DbFieldAddDefaultInterface.js +2 -2
- package/build/interfaces/DownloadFileArgs.d.ts +6 -6
- package/build/interfaces/DownloadFileArgs.js +2 -2
- package/build/interfaces/ExtractCredentialsArgs.d.ts +19 -19
- package/build/interfaces/ExtractCredentialsArgs.js +2 -2
- package/build/interfaces/JwtTokenSecretAndSettings.d.ts +5 -5
- package/build/interfaces/JwtTokenSecretAndSettings.js +2 -2
- package/build/interfaces/ListArgsInterface.d.ts +5 -5
- package/build/interfaces/ListArgsInterface.js +2 -2
- package/build/interfaces/LoaderInterface.d.ts +4 -4
- package/build/interfaces/LoaderInterface.js +2 -2
- package/build/interfaces/PaginationInterface.d.ts +7 -7
- package/build/interfaces/PaginationInterface.js +2 -2
- package/build/interfaces/SortItemInterface.d.ts +4 -4
- package/build/interfaces/SortItemInterface.js +2 -2
- package/build/interfaces/UserTokenParams.d.ts +8 -8
- package/build/interfaces/UserTokenParams.js +2 -2
- package/build/interfaces/VerifyUserTokenArgs.d.ts +13 -13
- package/build/interfaces/VerifyUserTokenArgs.js +2 -2
- package/build/interfaces/index.d.ts +28 -28
- package/build/interfaces/index.js +43 -43
- package/build/knexConfigBuilder.d.ts +1 -1
- package/build/knexConfigBuilder.js +80 -80
- package/build/passwordHelpers.d.ts +5 -5
- package/build/passwordHelpers.js +30 -30
- package/build/redisClient.d.ts +17 -17
- package/build/redisClient.js +97 -97
- package/build/resolversHelpers.d.ts +3 -3
- package/build/resolversHelpers.js +62 -62
- package/build/userToken.d.ts +8 -8
- package/build/userToken.js +86 -86
- package/build/utils.d.ts +2 -2
- package/build/utils.js +21 -21
- package/package.json +71 -71
package/build/Database.js
CHANGED
|
@@ -1,198 +1,198 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.Database = void 0;
|
|
16
|
-
const lodash_1 = require("lodash");
|
|
17
|
-
const knex_1 = __importDefault(require("knex"));
|
|
18
|
-
const utils_1 = require("@sdflc/utils");
|
|
19
|
-
const knexHandles = {};
|
|
20
|
-
const buildKey = (args) => {
|
|
21
|
-
const { connection, dbSchema } = args || {};
|
|
22
|
-
const { host, port, user, password, database } = connection || {};
|
|
23
|
-
const parts = [host, port, user, password, database, dbSchema];
|
|
24
|
-
return parts.join('|');
|
|
25
|
-
};
|
|
26
|
-
class Database {
|
|
27
|
-
constructor(props) {
|
|
28
|
-
this.trx = null;
|
|
29
|
-
this.knexHandle = null;
|
|
30
|
-
this.dbSchema = '';
|
|
31
|
-
this.logger = new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
|
|
32
|
-
if (!props.knexConfig) {
|
|
33
|
-
throw new Error('The Database class expects to have `knexConfig` in its constructor props');
|
|
34
|
-
}
|
|
35
|
-
if (props.logger instanceof utils_1.Logger) {
|
|
36
|
-
this.logger = props.logger;
|
|
37
|
-
}
|
|
38
|
-
const cfgKey = buildKey({ connection: props.knexConfig.connection, dbSchema: props.dbSchema });
|
|
39
|
-
if (!knexHandles[cfgKey]) {
|
|
40
|
-
this.logger.debug(`Create a new KnexJs configuration`);
|
|
41
|
-
knexHandles[cfgKey] = (0, knex_1.default)(props.knexConfig);
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
this.logger.debug(`Reusing existing KnexJs configuration`);
|
|
45
|
-
}
|
|
46
|
-
this.knexHandle = knexHandles[cfgKey];
|
|
47
|
-
this.dbSchema = props.dbSchema || '';
|
|
48
|
-
this.trx = props.trx;
|
|
49
|
-
}
|
|
50
|
-
destroy() {
|
|
51
|
-
if (this.knexHandle) {
|
|
52
|
-
this.knexHandle.destroy();
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
isDbError(ex) {
|
|
56
|
-
const { code } = ex || {};
|
|
57
|
-
switch (code) {
|
|
58
|
-
default:
|
|
59
|
-
return [false, ''];
|
|
60
|
-
case '23503':
|
|
61
|
-
// ex.detail: 'Key (message_id)=(78403d9e-4e95-4091-8136-ae99ddbfa3ca) is not present in table "message".',
|
|
62
|
-
// ex.constraint: 'message_recipient_status_message_id_foreign',
|
|
63
|
-
return [true, `Reference does not exist`];
|
|
64
|
-
case '23505':
|
|
65
|
-
return [true, `Such item already exists`];
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
createTransaction(trx) {
|
|
69
|
-
return new Promise((resolve) => {
|
|
70
|
-
return (trx ? trx : this.knexHandle).transaction(resolve);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
getSchema() {
|
|
74
|
-
return this.dbSchema;
|
|
75
|
-
}
|
|
76
|
-
getBuilder(arg) {
|
|
77
|
-
return this.trx ? this.trx(arg).withSchema(this.getSchema()) : this.knexHandle(arg).withSchema(this.getSchema());
|
|
78
|
-
}
|
|
79
|
-
raw(query, bindings) {
|
|
80
|
-
return this.knexHandle.raw(query, bindings);
|
|
81
|
-
}
|
|
82
|
-
runRawQuery(query, bindings) {
|
|
83
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
-
const rows = yield (this.trx ? this.trx : this.knexHandle).raw(query, bindings);
|
|
85
|
-
return rows;
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
startTransaction() {
|
|
89
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
-
const trx = yield this.createTransaction(this.trx);
|
|
91
|
-
this.trx = trx;
|
|
92
|
-
return trx;
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
setTransaction(trx) {
|
|
96
|
-
this.trx = trx;
|
|
97
|
-
}
|
|
98
|
-
commitTransaction() {
|
|
99
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
-
if (this.trx) {
|
|
101
|
-
yield this.trx.commit();
|
|
102
|
-
this.trx = undefined;
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
rollbackTransaction() {
|
|
107
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
-
if (this.trx) {
|
|
109
|
-
try {
|
|
110
|
-
yield this.trx.rollback();
|
|
111
|
-
}
|
|
112
|
-
catch (ex) {
|
|
113
|
-
// Do nothing here
|
|
114
|
-
this.logger.error(`Failed to rollback a transaction:`, ex.message);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
this.trx = undefined;
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
doCalculated(item, calculatedFields) {
|
|
121
|
-
calculatedFields.forEach((calcField) => {
|
|
122
|
-
if (calcField.name) {
|
|
123
|
-
item[calcField.name] = typeof calcField.value === 'function' ? calcField.value(item) : calcField.value;
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
read(args) {
|
|
128
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
129
|
-
const { where, whereIn, extendQuery, table, selectFields, mapBy, calculated, buildKey } = args || {};
|
|
130
|
-
const query = this.getBuilder(table).select(Array.isArray(selectFields) && selectFields.length > 0 ? selectFields : '*');
|
|
131
|
-
if (where) {
|
|
132
|
-
query.where(where);
|
|
133
|
-
}
|
|
134
|
-
if (whereIn) {
|
|
135
|
-
query.whereIn(whereIn.field, whereIn.values);
|
|
136
|
-
}
|
|
137
|
-
if (typeof extendQuery === 'function') {
|
|
138
|
-
yield extendQuery(query);
|
|
139
|
-
}
|
|
140
|
-
let dbResults = yield query;
|
|
141
|
-
if (mapBy) {
|
|
142
|
-
const fnBuildKey = typeof buildKey === 'function' ? buildKey : utils_1.buildKey;
|
|
143
|
-
dbResults = dbResults.reduce((acc, item) => {
|
|
144
|
-
const key = fnBuildKey(item[(0, lodash_1.camelCase)(mapBy)]);
|
|
145
|
-
if (Array.isArray(calculated) && calculated.length > 0) {
|
|
146
|
-
this.doCalculated(item, calculated);
|
|
147
|
-
}
|
|
148
|
-
acc[key] = item;
|
|
149
|
-
return acc;
|
|
150
|
-
}, {});
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
if (Array.isArray(calculated) && calculated.length > 0) {
|
|
154
|
-
dbResults.forEach((row) => {
|
|
155
|
-
this.doCalculated(row, calculated);
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
return dbResults;
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
createSortBy(sortBy, defaultSortBy) {
|
|
163
|
-
if (!sortBy) {
|
|
164
|
-
if (Array.isArray(defaultSortBy)) {
|
|
165
|
-
return this.createSortBy(defaultSortBy);
|
|
166
|
-
}
|
|
167
|
-
else {
|
|
168
|
-
return null;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
return sortBy.reduce((acc, item) => {
|
|
172
|
-
acc.push({
|
|
173
|
-
column: item.name,
|
|
174
|
-
order: item.order,
|
|
175
|
-
});
|
|
176
|
-
return acc;
|
|
177
|
-
}, []);
|
|
178
|
-
}
|
|
179
|
-
createLimitOffset(page, pageSize) {
|
|
180
|
-
const limit = isNaN(pageSize) || pageSize == null ? undefined : Number(pageSize);
|
|
181
|
-
const pageToUse = isNaN(page) || page < 1 ? 1 : Number(page);
|
|
182
|
-
return {
|
|
183
|
-
limit,
|
|
184
|
-
offset: limit ? (pageToUse - 1) * limit : 0,
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
createLimitOffsetSortBy(pagination, defaultSortBy) {
|
|
188
|
-
const { page, pageSize, sortBy } = pagination || {};
|
|
189
|
-
const { limit, offset } = this.createLimitOffset(page, pageSize);
|
|
190
|
-
const sortByToUse = this.createSortBy(sortBy, defaultSortBy);
|
|
191
|
-
return {
|
|
192
|
-
limit,
|
|
193
|
-
offset,
|
|
194
|
-
sortBy: sortByToUse,
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
exports.Database = Database;
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.Database = void 0;
|
|
16
|
+
const lodash_1 = require("lodash");
|
|
17
|
+
const knex_1 = __importDefault(require("knex"));
|
|
18
|
+
const utils_1 = require("@sdflc/utils");
|
|
19
|
+
const knexHandles = {};
|
|
20
|
+
const buildKey = (args) => {
|
|
21
|
+
const { connection, dbSchema } = args || {};
|
|
22
|
+
const { host, port, user, password, database } = connection || {};
|
|
23
|
+
const parts = [host, port, user, password, database, dbSchema];
|
|
24
|
+
return parts.join('|');
|
|
25
|
+
};
|
|
26
|
+
class Database {
|
|
27
|
+
constructor(props) {
|
|
28
|
+
this.trx = null;
|
|
29
|
+
this.knexHandle = null;
|
|
30
|
+
this.dbSchema = '';
|
|
31
|
+
this.logger = new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
|
|
32
|
+
if (!props.knexConfig) {
|
|
33
|
+
throw new Error('The Database class expects to have `knexConfig` in its constructor props');
|
|
34
|
+
}
|
|
35
|
+
if (props.logger instanceof utils_1.Logger) {
|
|
36
|
+
this.logger = props.logger;
|
|
37
|
+
}
|
|
38
|
+
const cfgKey = buildKey({ connection: props.knexConfig.connection, dbSchema: props.dbSchema });
|
|
39
|
+
if (!knexHandles[cfgKey]) {
|
|
40
|
+
this.logger.debug(`Create a new KnexJs configuration`);
|
|
41
|
+
knexHandles[cfgKey] = (0, knex_1.default)(props.knexConfig);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this.logger.debug(`Reusing existing KnexJs configuration`);
|
|
45
|
+
}
|
|
46
|
+
this.knexHandle = knexHandles[cfgKey];
|
|
47
|
+
this.dbSchema = props.dbSchema || '';
|
|
48
|
+
this.trx = props.trx;
|
|
49
|
+
}
|
|
50
|
+
destroy() {
|
|
51
|
+
if (this.knexHandle) {
|
|
52
|
+
this.knexHandle.destroy();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
isDbError(ex) {
|
|
56
|
+
const { code } = ex || {};
|
|
57
|
+
switch (code) {
|
|
58
|
+
default:
|
|
59
|
+
return [false, ''];
|
|
60
|
+
case '23503':
|
|
61
|
+
// ex.detail: 'Key (message_id)=(78403d9e-4e95-4091-8136-ae99ddbfa3ca) is not present in table "message".',
|
|
62
|
+
// ex.constraint: 'message_recipient_status_message_id_foreign',
|
|
63
|
+
return [true, `Reference does not exist`];
|
|
64
|
+
case '23505':
|
|
65
|
+
return [true, `Such item already exists`];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
createTransaction(trx) {
|
|
69
|
+
return new Promise((resolve) => {
|
|
70
|
+
return (trx ? trx : this.knexHandle).transaction(resolve);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
getSchema() {
|
|
74
|
+
return this.dbSchema;
|
|
75
|
+
}
|
|
76
|
+
getBuilder(arg) {
|
|
77
|
+
return this.trx ? this.trx(arg).withSchema(this.getSchema()) : this.knexHandle(arg).withSchema(this.getSchema());
|
|
78
|
+
}
|
|
79
|
+
raw(query, bindings) {
|
|
80
|
+
return this.knexHandle.raw(query, bindings);
|
|
81
|
+
}
|
|
82
|
+
runRawQuery(query, bindings) {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
const rows = yield (this.trx ? this.trx : this.knexHandle).raw(query, bindings);
|
|
85
|
+
return rows;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
startTransaction() {
|
|
89
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
+
const trx = yield this.createTransaction(this.trx);
|
|
91
|
+
this.trx = trx;
|
|
92
|
+
return trx;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
setTransaction(trx) {
|
|
96
|
+
this.trx = trx;
|
|
97
|
+
}
|
|
98
|
+
commitTransaction() {
|
|
99
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
+
if (this.trx) {
|
|
101
|
+
yield this.trx.commit();
|
|
102
|
+
this.trx = undefined;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
rollbackTransaction() {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
if (this.trx) {
|
|
109
|
+
try {
|
|
110
|
+
yield this.trx.rollback();
|
|
111
|
+
}
|
|
112
|
+
catch (ex) {
|
|
113
|
+
// Do nothing here
|
|
114
|
+
this.logger.error(`Failed to rollback a transaction:`, ex.message);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
this.trx = undefined;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
doCalculated(item, calculatedFields) {
|
|
121
|
+
calculatedFields.forEach((calcField) => {
|
|
122
|
+
if (calcField.name) {
|
|
123
|
+
item[calcField.name] = typeof calcField.value === 'function' ? calcField.value(item) : calcField.value;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
read(args) {
|
|
128
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
const { where, whereIn, extendQuery, table, selectFields, mapBy, calculated, buildKey } = args || {};
|
|
130
|
+
const query = this.getBuilder(table).select(Array.isArray(selectFields) && selectFields.length > 0 ? selectFields : '*');
|
|
131
|
+
if (where) {
|
|
132
|
+
query.where(where);
|
|
133
|
+
}
|
|
134
|
+
if (whereIn) {
|
|
135
|
+
query.whereIn(whereIn.field, whereIn.values);
|
|
136
|
+
}
|
|
137
|
+
if (typeof extendQuery === 'function') {
|
|
138
|
+
yield extendQuery(query);
|
|
139
|
+
}
|
|
140
|
+
let dbResults = yield query;
|
|
141
|
+
if (mapBy) {
|
|
142
|
+
const fnBuildKey = typeof buildKey === 'function' ? buildKey : utils_1.buildKey;
|
|
143
|
+
dbResults = dbResults.reduce((acc, item) => {
|
|
144
|
+
const key = fnBuildKey(item[(0, lodash_1.camelCase)(mapBy)]);
|
|
145
|
+
if (Array.isArray(calculated) && calculated.length > 0) {
|
|
146
|
+
this.doCalculated(item, calculated);
|
|
147
|
+
}
|
|
148
|
+
acc[key] = item;
|
|
149
|
+
return acc;
|
|
150
|
+
}, {});
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
if (Array.isArray(calculated) && calculated.length > 0) {
|
|
154
|
+
dbResults.forEach((row) => {
|
|
155
|
+
this.doCalculated(row, calculated);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return dbResults;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
createSortBy(sortBy, defaultSortBy) {
|
|
163
|
+
if (!sortBy) {
|
|
164
|
+
if (Array.isArray(defaultSortBy)) {
|
|
165
|
+
return this.createSortBy(defaultSortBy);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return sortBy.reduce((acc, item) => {
|
|
172
|
+
acc.push({
|
|
173
|
+
column: item.name,
|
|
174
|
+
order: item.order,
|
|
175
|
+
});
|
|
176
|
+
return acc;
|
|
177
|
+
}, []);
|
|
178
|
+
}
|
|
179
|
+
createLimitOffset(page, pageSize) {
|
|
180
|
+
const limit = isNaN(pageSize) || pageSize == null ? undefined : Number(pageSize);
|
|
181
|
+
const pageToUse = isNaN(page) || page < 1 ? 1 : Number(page);
|
|
182
|
+
return {
|
|
183
|
+
limit,
|
|
184
|
+
offset: limit ? (pageToUse - 1) * limit : 0,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
createLimitOffsetSortBy(pagination, defaultSortBy) {
|
|
188
|
+
const { page, pageSize, sortBy } = pagination || {};
|
|
189
|
+
const { limit, offset } = this.createLimitOffset(page, pageSize);
|
|
190
|
+
const sortByToUse = this.createSortBy(sortBy, defaultSortBy);
|
|
191
|
+
return {
|
|
192
|
+
limit,
|
|
193
|
+
offset,
|
|
194
|
+
sortBy: sortByToUse,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
exports.Database = Database;
|
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
declare class DbMigratorAndSeeder {
|
|
2
|
-
private knexHandle;
|
|
3
|
-
private config;
|
|
4
|
-
private logger;
|
|
5
|
-
constructor(knexHandle: any, config?: any);
|
|
6
|
-
runMigrations(): Promise<void>;
|
|
7
|
-
/**
|
|
8
|
-
* Go from array of objects which contain keys of object names which have an array value of seeded objects
|
|
9
|
-
* to a single object with keys of object names and arrays of seeded objects
|
|
10
|
-
* ie from [{table1: [], table2: [] }, { table3: [] }]
|
|
11
|
-
* to { table1: [], table2: [], table3: []}
|
|
12
|
-
*/
|
|
13
|
-
transformSeedsToObject(generatedSeedArrays: any[]): any;
|
|
14
|
-
/**
|
|
15
|
-
* Drop all tables, run migrations, then seed the tables
|
|
16
|
-
*/
|
|
17
|
-
resetDb(): Promise<void>;
|
|
18
|
-
/**
|
|
19
|
-
* Selects all the tables in the schema and drops them.
|
|
20
|
-
* This approach does not work in case we have constraints.
|
|
21
|
-
* TODO: Remove all constraints and then drop tables.
|
|
22
|
-
*/
|
|
23
|
-
wipeDb(): Promise<void>;
|
|
24
|
-
/**
|
|
25
|
-
* Removes all the records from provided list of tables
|
|
26
|
-
* @param tablesToClean array of table names
|
|
27
|
-
* @param knexHandle Knex handle object with configured connection to DB
|
|
28
|
-
* @param appSettings settings with db related information
|
|
29
|
-
* @returns promise
|
|
30
|
-
*/
|
|
31
|
-
cleanDb(tablesToClean: any[]): Promise<unknown>;
|
|
32
|
-
/**
|
|
33
|
-
* Removes all the records from provided table.
|
|
34
|
-
* If appSettings has cleanTable object with table names then
|
|
35
|
-
* it is used to add extra filters to clear the table to avoid for example users data
|
|
36
|
-
* @param table Name of table from which to remove all the records
|
|
37
|
-
*/
|
|
38
|
-
cleanDbTable(table: string): Promise<void>;
|
|
39
|
-
/**
|
|
40
|
-
* Returns a list of tables in db schedm specified in settings
|
|
41
|
-
* @returns arrays of table names in the provided db schema
|
|
42
|
-
*/
|
|
43
|
-
listDbTables(): Promise<any>;
|
|
44
|
-
generateSeeds(dirname: string, seedFiles: any): Promise<any>;
|
|
45
|
-
removeSeedsFromDb(seedFiles: any): Promise<unknown>;
|
|
46
|
-
insertRecords(seedGroups: any): Promise<any>;
|
|
47
|
-
runSeeds(dirname: string, seedFiles: any): Promise<any>;
|
|
48
|
-
}
|
|
49
|
-
export { DbMigratorAndSeeder };
|
|
1
|
+
declare class DbMigratorAndSeeder {
|
|
2
|
+
private knexHandle;
|
|
3
|
+
private config;
|
|
4
|
+
private logger;
|
|
5
|
+
constructor(knexHandle: any, config?: any);
|
|
6
|
+
runMigrations(): Promise<void>;
|
|
7
|
+
/**
|
|
8
|
+
* Go from array of objects which contain keys of object names which have an array value of seeded objects
|
|
9
|
+
* to a single object with keys of object names and arrays of seeded objects
|
|
10
|
+
* ie from [{table1: [], table2: [] }, { table3: [] }]
|
|
11
|
+
* to { table1: [], table2: [], table3: []}
|
|
12
|
+
*/
|
|
13
|
+
transformSeedsToObject(generatedSeedArrays: any[]): any;
|
|
14
|
+
/**
|
|
15
|
+
* Drop all tables, run migrations, then seed the tables
|
|
16
|
+
*/
|
|
17
|
+
resetDb(): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Selects all the tables in the schema and drops them.
|
|
20
|
+
* This approach does not work in case we have constraints.
|
|
21
|
+
* TODO: Remove all constraints and then drop tables.
|
|
22
|
+
*/
|
|
23
|
+
wipeDb(): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Removes all the records from provided list of tables
|
|
26
|
+
* @param tablesToClean array of table names
|
|
27
|
+
* @param knexHandle Knex handle object with configured connection to DB
|
|
28
|
+
* @param appSettings settings with db related information
|
|
29
|
+
* @returns promise
|
|
30
|
+
*/
|
|
31
|
+
cleanDb(tablesToClean: any[]): Promise<unknown>;
|
|
32
|
+
/**
|
|
33
|
+
* Removes all the records from provided table.
|
|
34
|
+
* If appSettings has cleanTable object with table names then
|
|
35
|
+
* it is used to add extra filters to clear the table to avoid for example users data
|
|
36
|
+
* @param table Name of table from which to remove all the records
|
|
37
|
+
*/
|
|
38
|
+
cleanDbTable(table: string): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Returns a list of tables in db schedm specified in settings
|
|
41
|
+
* @returns arrays of table names in the provided db schema
|
|
42
|
+
*/
|
|
43
|
+
listDbTables(): Promise<any>;
|
|
44
|
+
generateSeeds(dirname: string, seedFiles: any): Promise<any>;
|
|
45
|
+
removeSeedsFromDb(seedFiles: any): Promise<unknown>;
|
|
46
|
+
insertRecords(seedGroups: any): Promise<any>;
|
|
47
|
+
runSeeds(dirname: string, seedFiles: any): Promise<any>;
|
|
48
|
+
}
|
|
49
|
+
export { DbMigratorAndSeeder };
|