@betterinternship/schema 1.12.1 → 1.13.1
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/dist/db.doc.types.d.ts +107 -107
- package/dist/db.doc.types.js +2 -2
- package/dist/db.internal.types.d.ts +2 -1
- package/dist/db.iom.types.d.ts +312 -312
- package/dist/db.iom.types.js +2 -2
- package/dist/env.d.ts +1 -1
- package/dist/env.js +6 -6
- package/dist/repository/base.js +387 -387
- package/dist/repository/errors.js +15 -15
- package/dist/repository/index.js +19 -19
- package/dist/repository/json-columns.d.ts +5 -5
- package/dist/repository/json-columns.js +43 -43
- package/dist/repository/paginator.d.ts +4 -4
- package/dist/repository/paginator.js +2 -2
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
package/dist/repository/base.js
CHANGED
|
@@ -1,388 +1,388 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Repository = void 0;
|
|
4
|
-
const json_columns_1 = require("./json-columns");
|
|
5
|
-
const errors_1 = require("./errors");
|
|
6
|
-
const isOperatorObject = (v) => v !== null &&
|
|
7
|
-
typeof v === "object" &&
|
|
8
|
-
!Array.isArray(v) &&
|
|
9
|
-
!(v instanceof Date);
|
|
10
|
-
class Repository {
|
|
11
|
-
db;
|
|
12
|
-
table;
|
|
13
|
-
schema;
|
|
14
|
-
constructor(db, table, schema) {
|
|
15
|
-
this.db = db;
|
|
16
|
-
this.table = table;
|
|
17
|
-
this.schema = schema;
|
|
18
|
-
}
|
|
19
|
-
applySelect(query, fields) {
|
|
20
|
-
if (!fields || fields === "*")
|
|
21
|
-
return query.selectAll();
|
|
22
|
-
const cols = fields.split(",").map((f) => f.trim());
|
|
23
|
-
return query.select(cols);
|
|
24
|
-
}
|
|
25
|
-
applyCondition(query, key, value) {
|
|
26
|
-
const col = key;
|
|
27
|
-
if (value === null) {
|
|
28
|
-
return query.where(col, "is", null);
|
|
29
|
-
}
|
|
30
|
-
if (Array.isArray(value)) {
|
|
31
|
-
return (value.length ? query.where(col, "in", value) : query);
|
|
32
|
-
}
|
|
33
|
-
if (isOperatorObject(value)) {
|
|
34
|
-
let q = query;
|
|
35
|
-
for (const [op, operand] of Object.entries(value)) {
|
|
36
|
-
switch (op) {
|
|
37
|
-
case "gt":
|
|
38
|
-
q = q.where(col, ">", operand);
|
|
39
|
-
break;
|
|
40
|
-
case "gte":
|
|
41
|
-
q = q.where(col, ">=", operand);
|
|
42
|
-
break;
|
|
43
|
-
case "lt":
|
|
44
|
-
q = q.where(col, "<", operand);
|
|
45
|
-
break;
|
|
46
|
-
case "lte":
|
|
47
|
-
q = q.where(col, "<=", operand);
|
|
48
|
-
break;
|
|
49
|
-
case "in":
|
|
50
|
-
if (Array.isArray(operand) && operand.length)
|
|
51
|
-
q = q.where(col, "in", operand);
|
|
52
|
-
break;
|
|
53
|
-
case "not":
|
|
54
|
-
q =
|
|
55
|
-
operand === null
|
|
56
|
-
? q.where(col, "is not", null)
|
|
57
|
-
: q.where(col, "!=", operand);
|
|
58
|
-
break;
|
|
59
|
-
default:
|
|
60
|
-
q = q.where(col, "=", operand);
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return q;
|
|
65
|
-
}
|
|
66
|
-
return query.where(col, "=", value);
|
|
67
|
-
}
|
|
68
|
-
applyFilters(query, filter) {
|
|
69
|
-
let q = query;
|
|
70
|
-
for (const key in filter) {
|
|
71
|
-
q = this.applyCondition(q, key, filter[key]);
|
|
72
|
-
}
|
|
73
|
-
return q;
|
|
74
|
-
}
|
|
75
|
-
async findOne(id, fields) {
|
|
76
|
-
try {
|
|
77
|
-
let query = this.db
|
|
78
|
-
.withSchema(this.schema)
|
|
79
|
-
.selectFrom(this.table)
|
|
80
|
-
.where("id", "=", id);
|
|
81
|
-
query = this.applySelect(query, fields);
|
|
82
|
-
return (await query.executeTakeFirst());
|
|
83
|
-
}
|
|
84
|
-
catch (error) {
|
|
85
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
async findOneOrNull(id, fields) {
|
|
89
|
-
try {
|
|
90
|
-
let query = this.db
|
|
91
|
-
.withSchema(this.schema)
|
|
92
|
-
.selectFrom(this.table)
|
|
93
|
-
.where("id", "=", id);
|
|
94
|
-
query = this.applySelect(query, fields);
|
|
95
|
-
return (await query.executeTakeFirst()) ?? null;
|
|
96
|
-
}
|
|
97
|
-
catch (error) {
|
|
98
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
async checkOneBy(filter, fields) {
|
|
102
|
-
try {
|
|
103
|
-
let query = this.db
|
|
104
|
-
.withSchema(this.schema)
|
|
105
|
-
.selectFrom(this.table);
|
|
106
|
-
query = this.applyFilters(query, filter);
|
|
107
|
-
query = this.applySelect(query, fields);
|
|
108
|
-
return !!(await query.executeTakeFirst());
|
|
109
|
-
}
|
|
110
|
-
catch {
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
async countBy(filter = {}) {
|
|
115
|
-
try {
|
|
116
|
-
let query = this.db
|
|
117
|
-
.withSchema(this.schema)
|
|
118
|
-
.selectFrom(this.table);
|
|
119
|
-
query = this.applyFilters(query, filter);
|
|
120
|
-
const result = await query
|
|
121
|
-
.select((eb) => eb.fn.count("id").as("count"))
|
|
122
|
-
.executeTakeFirst();
|
|
123
|
-
return Number(result?.count ?? 0);
|
|
124
|
-
}
|
|
125
|
-
catch {
|
|
126
|
-
return 0;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
async findOneBy(filter, fields) {
|
|
130
|
-
try {
|
|
131
|
-
let query = this.db
|
|
132
|
-
.withSchema(this.schema)
|
|
133
|
-
.selectFrom(this.table);
|
|
134
|
-
query = this.applyFilters(query, filter);
|
|
135
|
-
query = this.applySelect(query, fields);
|
|
136
|
-
return (await query.executeTakeFirst());
|
|
137
|
-
}
|
|
138
|
-
catch (error) {
|
|
139
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
async findOneByOrNull(filter, fields) {
|
|
143
|
-
try {
|
|
144
|
-
let query = this.db
|
|
145
|
-
.withSchema(this.schema)
|
|
146
|
-
.selectFrom(this.table);
|
|
147
|
-
query = this.applyFilters(query, filter);
|
|
148
|
-
query = this.applySelect(query, fields);
|
|
149
|
-
return (await query.executeTakeFirst()) ?? null;
|
|
150
|
-
}
|
|
151
|
-
catch (error) {
|
|
152
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
async findLastOrderedBy(filter, sortField, sortAscending = false, fields) {
|
|
156
|
-
try {
|
|
157
|
-
let query = this.db
|
|
158
|
-
.withSchema(this.schema)
|
|
159
|
-
.selectFrom(this.table);
|
|
160
|
-
query = this.applyFilters(query, filter);
|
|
161
|
-
query = this.applySelect(query, fields);
|
|
162
|
-
const data = (await query
|
|
163
|
-
.orderBy(sortField, sortAscending ? "asc" : "desc")
|
|
164
|
-
.limit(1)
|
|
165
|
-
.executeTakeFirst());
|
|
166
|
-
return data ?? null;
|
|
167
|
-
}
|
|
168
|
-
catch (error) {
|
|
169
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
async findAllBy(filter, fields, options) {
|
|
173
|
-
try {
|
|
174
|
-
let query = this.db
|
|
175
|
-
.withSchema(this.schema)
|
|
176
|
-
.selectFrom(this.table);
|
|
177
|
-
query = this.applyFilters(query, filter);
|
|
178
|
-
query = this.applySelect(query, fields);
|
|
179
|
-
if (options?.limit)
|
|
180
|
-
query = query.limit(options.limit);
|
|
181
|
-
if (options?.offset)
|
|
182
|
-
query = query.offset(options.offset);
|
|
183
|
-
return (await query.execute());
|
|
184
|
-
}
|
|
185
|
-
catch (error) {
|
|
186
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
async findAllByOrdered(filter, sortField, sortAscending = false, options, fields) {
|
|
190
|
-
try {
|
|
191
|
-
let query = this.db
|
|
192
|
-
.withSchema(this.schema)
|
|
193
|
-
.selectFrom(this.table);
|
|
194
|
-
query = this.applyFilters(query, filter);
|
|
195
|
-
query = this.applySelect(query, fields);
|
|
196
|
-
query = query.orderBy(sortField, sortAscending ? "asc" : "desc");
|
|
197
|
-
if (options?.limit)
|
|
198
|
-
query = query.limit(options.limit);
|
|
199
|
-
if (options?.offset)
|
|
200
|
-
query = query.offset(options.offset);
|
|
201
|
-
return (await query.execute());
|
|
202
|
-
}
|
|
203
|
-
catch (error) {
|
|
204
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
async findAllIn(filterColumn, filterValues, fields) {
|
|
208
|
-
try {
|
|
209
|
-
if (!filterValues.length)
|
|
210
|
-
return [];
|
|
211
|
-
let query = this.db
|
|
212
|
-
.withSchema(this.schema)
|
|
213
|
-
.selectFrom(this.table);
|
|
214
|
-
query = query.where(filterColumn, "in", filterValues);
|
|
215
|
-
query = this.applySelect(query, fields);
|
|
216
|
-
return (await query.execute());
|
|
217
|
-
}
|
|
218
|
-
catch (error) {
|
|
219
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
async findAll(fields, options) {
|
|
223
|
-
try {
|
|
224
|
-
let query = this.db
|
|
225
|
-
.withSchema(this.schema)
|
|
226
|
-
.selectFrom(this.table);
|
|
227
|
-
query = this.applySelect(query, fields);
|
|
228
|
-
if (options?.limit)
|
|
229
|
-
query = query.limit(options.limit);
|
|
230
|
-
if (options?.offset)
|
|
231
|
-
query = query.offset(options.offset);
|
|
232
|
-
return (await query.execute());
|
|
233
|
-
}
|
|
234
|
-
catch (error) {
|
|
235
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
async findPaginated(filter, fields, options) {
|
|
239
|
-
try {
|
|
240
|
-
const page = options?.page ?? 1;
|
|
241
|
-
const limit = options?.limit ?? 20;
|
|
242
|
-
const offset = (page - 1) * limit;
|
|
243
|
-
const orderBy = options?.orderBy ?? "id";
|
|
244
|
-
const orderDir = options?.orderDir ?? "asc";
|
|
245
|
-
let query = this.db
|
|
246
|
-
.withSchema(this.schema)
|
|
247
|
-
.selectFrom(this.table);
|
|
248
|
-
query = this.applyFilters(query, filter);
|
|
249
|
-
query = this.applySelect(query, fields);
|
|
250
|
-
let countQuery = this.db
|
|
251
|
-
.withSchema(this.schema)
|
|
252
|
-
.selectFrom(this.table)
|
|
253
|
-
.select((eb) => eb.fn.countAll().as("count"));
|
|
254
|
-
countQuery = this.applyFilters(countQuery, filter);
|
|
255
|
-
const [data, countResult] = await Promise.all([
|
|
256
|
-
query.orderBy(orderBy, orderDir).limit(limit).offset(offset).execute(),
|
|
257
|
-
countQuery.executeTakeFirst(),
|
|
258
|
-
]);
|
|
259
|
-
return {
|
|
260
|
-
data: data,
|
|
261
|
-
total: Number(countResult?.count ?? 0),
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
catch (error) {
|
|
265
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
async countAll() {
|
|
269
|
-
try {
|
|
270
|
-
const result = await this.db
|
|
271
|
-
.withSchema(this.schema)
|
|
272
|
-
.selectFrom(this.table)
|
|
273
|
-
.select((eb) => eb.fn.countAll().as("count"))
|
|
274
|
-
.executeTakeFirst();
|
|
275
|
-
return Number(result?.count ?? 0);
|
|
276
|
-
}
|
|
277
|
-
catch {
|
|
278
|
-
return 0;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
async insertOne(insert, fields) {
|
|
282
|
-
try {
|
|
283
|
-
const jsonCols = await (0, json_columns_1.loadJsonColumnMap)(this.db);
|
|
284
|
-
insert = (0, json_columns_1.wrapJsonColumns)(insert, this.table, jsonCols);
|
|
285
|
-
const query = this.db
|
|
286
|
-
.withSchema(this.schema)
|
|
287
|
-
.insertInto(this.table)
|
|
288
|
-
.values(insert);
|
|
289
|
-
if (!fields || fields === "*") {
|
|
290
|
-
return (await query.returningAll().executeTakeFirstOrThrow());
|
|
291
|
-
}
|
|
292
|
-
else {
|
|
293
|
-
const cols = fields.split(",").map((f) => f.trim());
|
|
294
|
-
return (await query
|
|
295
|
-
.returning(cols)
|
|
296
|
-
.executeTakeFirstOrThrow());
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
catch (error) {
|
|
300
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not insert into ${this.table}.`);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
async updateOne(id, updated, fields) {
|
|
304
|
-
try {
|
|
305
|
-
const jsonCols = await (0, json_columns_1.loadJsonColumnMap)(this.db);
|
|
306
|
-
updated = (0, json_columns_1.wrapJsonColumns)(updated, this.table, jsonCols);
|
|
307
|
-
const query = this.db
|
|
308
|
-
.withSchema(this.schema)
|
|
309
|
-
.updateTable(this.table)
|
|
310
|
-
.set(updated)
|
|
311
|
-
.where("id", "=", id);
|
|
312
|
-
if (!fields || fields === "*") {
|
|
313
|
-
return (await query.returningAll().executeTakeFirstOrThrow());
|
|
314
|
-
}
|
|
315
|
-
else {
|
|
316
|
-
const cols = fields.split(",").map((f) => f.trim());
|
|
317
|
-
return (await query
|
|
318
|
-
.returning(cols)
|
|
319
|
-
.executeTakeFirstOrThrow());
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
catch (error) {
|
|
323
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not update ${this.table}.`);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
async updateAllBy(filters, updated, fields) {
|
|
327
|
-
if (!Object.entries(filters).length)
|
|
328
|
-
throw new Error("Illegal filters for updateAllBy().");
|
|
329
|
-
try {
|
|
330
|
-
const jsonCols = await (0, json_columns_1.loadJsonColumnMap)(this.db);
|
|
331
|
-
updated = (0, json_columns_1.wrapJsonColumns)(updated, this.table, jsonCols);
|
|
332
|
-
let query = this.db
|
|
333
|
-
.withSchema(this.schema)
|
|
334
|
-
.updateTable(this.table)
|
|
335
|
-
.set(updated);
|
|
336
|
-
query = this.applyFilters(query, filters);
|
|
337
|
-
if (!fields || fields === "*") {
|
|
338
|
-
return (await query.returningAll().execute());
|
|
339
|
-
}
|
|
340
|
-
else {
|
|
341
|
-
const cols = fields.split(",").map((f) => f.trim());
|
|
342
|
-
return (await query.returning(cols).execute());
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
catch (error) {
|
|
346
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not update ${this.table}.`);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
async deleteOne(id, fields) {
|
|
350
|
-
try {
|
|
351
|
-
const query = this.db
|
|
352
|
-
.withSchema(this.schema)
|
|
353
|
-
.deleteFrom(this.table)
|
|
354
|
-
.where("id", "=", id);
|
|
355
|
-
if (!fields || fields === "*") {
|
|
356
|
-
return (await query.returningAll().executeTakeFirstOrThrow());
|
|
357
|
-
}
|
|
358
|
-
else {
|
|
359
|
-
const cols = fields.split(",").map((f) => f.trim());
|
|
360
|
-
return (await query
|
|
361
|
-
.returning(cols)
|
|
362
|
-
.executeTakeFirstOrThrow());
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
catch (error) {
|
|
366
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not delete from ${this.table}.`);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
async deleteAllBy(filters, fields) {
|
|
370
|
-
if (!Object.entries(filters).length)
|
|
371
|
-
throw new Error("Illegal filters for deleteAllBy().");
|
|
372
|
-
try {
|
|
373
|
-
let query = this.db
|
|
374
|
-
.withSchema(this.schema)
|
|
375
|
-
.deleteFrom(this.table);
|
|
376
|
-
query = this.applyFilters(query, filters);
|
|
377
|
-
if (!fields || fields === "*")
|
|
378
|
-
return (await query.returningAll().execute());
|
|
379
|
-
const cols = fields.split(",").map((f) => f.trim());
|
|
380
|
-
return (await query.returning(cols).execute());
|
|
381
|
-
}
|
|
382
|
-
catch (error) {
|
|
383
|
-
return (0, errors_1.logAndHandleDBError)(error, `Could not delete from ${this.table}.`);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
exports.Repository = Repository;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Repository = void 0;
|
|
4
|
+
const json_columns_1 = require("./json-columns");
|
|
5
|
+
const errors_1 = require("./errors");
|
|
6
|
+
const isOperatorObject = (v) => v !== null &&
|
|
7
|
+
typeof v === "object" &&
|
|
8
|
+
!Array.isArray(v) &&
|
|
9
|
+
!(v instanceof Date);
|
|
10
|
+
class Repository {
|
|
11
|
+
db;
|
|
12
|
+
table;
|
|
13
|
+
schema;
|
|
14
|
+
constructor(db, table, schema) {
|
|
15
|
+
this.db = db;
|
|
16
|
+
this.table = table;
|
|
17
|
+
this.schema = schema;
|
|
18
|
+
}
|
|
19
|
+
applySelect(query, fields) {
|
|
20
|
+
if (!fields || fields === "*")
|
|
21
|
+
return query.selectAll();
|
|
22
|
+
const cols = fields.split(",").map((f) => f.trim());
|
|
23
|
+
return query.select(cols);
|
|
24
|
+
}
|
|
25
|
+
applyCondition(query, key, value) {
|
|
26
|
+
const col = key;
|
|
27
|
+
if (value === null) {
|
|
28
|
+
return query.where(col, "is", null);
|
|
29
|
+
}
|
|
30
|
+
if (Array.isArray(value)) {
|
|
31
|
+
return (value.length ? query.where(col, "in", value) : query);
|
|
32
|
+
}
|
|
33
|
+
if (isOperatorObject(value)) {
|
|
34
|
+
let q = query;
|
|
35
|
+
for (const [op, operand] of Object.entries(value)) {
|
|
36
|
+
switch (op) {
|
|
37
|
+
case "gt":
|
|
38
|
+
q = q.where(col, ">", operand);
|
|
39
|
+
break;
|
|
40
|
+
case "gte":
|
|
41
|
+
q = q.where(col, ">=", operand);
|
|
42
|
+
break;
|
|
43
|
+
case "lt":
|
|
44
|
+
q = q.where(col, "<", operand);
|
|
45
|
+
break;
|
|
46
|
+
case "lte":
|
|
47
|
+
q = q.where(col, "<=", operand);
|
|
48
|
+
break;
|
|
49
|
+
case "in":
|
|
50
|
+
if (Array.isArray(operand) && operand.length)
|
|
51
|
+
q = q.where(col, "in", operand);
|
|
52
|
+
break;
|
|
53
|
+
case "not":
|
|
54
|
+
q =
|
|
55
|
+
operand === null
|
|
56
|
+
? q.where(col, "is not", null)
|
|
57
|
+
: q.where(col, "!=", operand);
|
|
58
|
+
break;
|
|
59
|
+
default:
|
|
60
|
+
q = q.where(col, "=", operand);
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return q;
|
|
65
|
+
}
|
|
66
|
+
return query.where(col, "=", value);
|
|
67
|
+
}
|
|
68
|
+
applyFilters(query, filter) {
|
|
69
|
+
let q = query;
|
|
70
|
+
for (const key in filter) {
|
|
71
|
+
q = this.applyCondition(q, key, filter[key]);
|
|
72
|
+
}
|
|
73
|
+
return q;
|
|
74
|
+
}
|
|
75
|
+
async findOne(id, fields) {
|
|
76
|
+
try {
|
|
77
|
+
let query = this.db
|
|
78
|
+
.withSchema(this.schema)
|
|
79
|
+
.selectFrom(this.table)
|
|
80
|
+
.where("id", "=", id);
|
|
81
|
+
query = this.applySelect(query, fields);
|
|
82
|
+
return (await query.executeTakeFirst());
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async findOneOrNull(id, fields) {
|
|
89
|
+
try {
|
|
90
|
+
let query = this.db
|
|
91
|
+
.withSchema(this.schema)
|
|
92
|
+
.selectFrom(this.table)
|
|
93
|
+
.where("id", "=", id);
|
|
94
|
+
query = this.applySelect(query, fields);
|
|
95
|
+
return (await query.executeTakeFirst()) ?? null;
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async checkOneBy(filter, fields) {
|
|
102
|
+
try {
|
|
103
|
+
let query = this.db
|
|
104
|
+
.withSchema(this.schema)
|
|
105
|
+
.selectFrom(this.table);
|
|
106
|
+
query = this.applyFilters(query, filter);
|
|
107
|
+
query = this.applySelect(query, fields);
|
|
108
|
+
return !!(await query.executeTakeFirst());
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async countBy(filter = {}) {
|
|
115
|
+
try {
|
|
116
|
+
let query = this.db
|
|
117
|
+
.withSchema(this.schema)
|
|
118
|
+
.selectFrom(this.table);
|
|
119
|
+
query = this.applyFilters(query, filter);
|
|
120
|
+
const result = await query
|
|
121
|
+
.select((eb) => eb.fn.count("id").as("count"))
|
|
122
|
+
.executeTakeFirst();
|
|
123
|
+
return Number(result?.count ?? 0);
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return 0;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async findOneBy(filter, fields) {
|
|
130
|
+
try {
|
|
131
|
+
let query = this.db
|
|
132
|
+
.withSchema(this.schema)
|
|
133
|
+
.selectFrom(this.table);
|
|
134
|
+
query = this.applyFilters(query, filter);
|
|
135
|
+
query = this.applySelect(query, fields);
|
|
136
|
+
return (await query.executeTakeFirst());
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async findOneByOrNull(filter, fields) {
|
|
143
|
+
try {
|
|
144
|
+
let query = this.db
|
|
145
|
+
.withSchema(this.schema)
|
|
146
|
+
.selectFrom(this.table);
|
|
147
|
+
query = this.applyFilters(query, filter);
|
|
148
|
+
query = this.applySelect(query, fields);
|
|
149
|
+
return (await query.executeTakeFirst()) ?? null;
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async findLastOrderedBy(filter, sortField, sortAscending = false, fields) {
|
|
156
|
+
try {
|
|
157
|
+
let query = this.db
|
|
158
|
+
.withSchema(this.schema)
|
|
159
|
+
.selectFrom(this.table);
|
|
160
|
+
query = this.applyFilters(query, filter);
|
|
161
|
+
query = this.applySelect(query, fields);
|
|
162
|
+
const data = (await query
|
|
163
|
+
.orderBy(sortField, sortAscending ? "asc" : "desc")
|
|
164
|
+
.limit(1)
|
|
165
|
+
.executeTakeFirst());
|
|
166
|
+
return data ?? null;
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async findAllBy(filter, fields, options) {
|
|
173
|
+
try {
|
|
174
|
+
let query = this.db
|
|
175
|
+
.withSchema(this.schema)
|
|
176
|
+
.selectFrom(this.table);
|
|
177
|
+
query = this.applyFilters(query, filter);
|
|
178
|
+
query = this.applySelect(query, fields);
|
|
179
|
+
if (options?.limit)
|
|
180
|
+
query = query.limit(options.limit);
|
|
181
|
+
if (options?.offset)
|
|
182
|
+
query = query.offset(options.offset);
|
|
183
|
+
return (await query.execute());
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
async findAllByOrdered(filter, sortField, sortAscending = false, options, fields) {
|
|
190
|
+
try {
|
|
191
|
+
let query = this.db
|
|
192
|
+
.withSchema(this.schema)
|
|
193
|
+
.selectFrom(this.table);
|
|
194
|
+
query = this.applyFilters(query, filter);
|
|
195
|
+
query = this.applySelect(query, fields);
|
|
196
|
+
query = query.orderBy(sortField, sortAscending ? "asc" : "desc");
|
|
197
|
+
if (options?.limit)
|
|
198
|
+
query = query.limit(options.limit);
|
|
199
|
+
if (options?.offset)
|
|
200
|
+
query = query.offset(options.offset);
|
|
201
|
+
return (await query.execute());
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
async findAllIn(filterColumn, filterValues, fields) {
|
|
208
|
+
try {
|
|
209
|
+
if (!filterValues.length)
|
|
210
|
+
return [];
|
|
211
|
+
let query = this.db
|
|
212
|
+
.withSchema(this.schema)
|
|
213
|
+
.selectFrom(this.table);
|
|
214
|
+
query = query.where(filterColumn, "in", filterValues);
|
|
215
|
+
query = this.applySelect(query, fields);
|
|
216
|
+
return (await query.execute());
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
async findAll(fields, options) {
|
|
223
|
+
try {
|
|
224
|
+
let query = this.db
|
|
225
|
+
.withSchema(this.schema)
|
|
226
|
+
.selectFrom(this.table);
|
|
227
|
+
query = this.applySelect(query, fields);
|
|
228
|
+
if (options?.limit)
|
|
229
|
+
query = query.limit(options.limit);
|
|
230
|
+
if (options?.offset)
|
|
231
|
+
query = query.offset(options.offset);
|
|
232
|
+
return (await query.execute());
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
async findPaginated(filter, fields, options) {
|
|
239
|
+
try {
|
|
240
|
+
const page = options?.page ?? 1;
|
|
241
|
+
const limit = options?.limit ?? 20;
|
|
242
|
+
const offset = (page - 1) * limit;
|
|
243
|
+
const orderBy = options?.orderBy ?? "id";
|
|
244
|
+
const orderDir = options?.orderDir ?? "asc";
|
|
245
|
+
let query = this.db
|
|
246
|
+
.withSchema(this.schema)
|
|
247
|
+
.selectFrom(this.table);
|
|
248
|
+
query = this.applyFilters(query, filter);
|
|
249
|
+
query = this.applySelect(query, fields);
|
|
250
|
+
let countQuery = this.db
|
|
251
|
+
.withSchema(this.schema)
|
|
252
|
+
.selectFrom(this.table)
|
|
253
|
+
.select((eb) => eb.fn.countAll().as("count"));
|
|
254
|
+
countQuery = this.applyFilters(countQuery, filter);
|
|
255
|
+
const [data, countResult] = await Promise.all([
|
|
256
|
+
query.orderBy(orderBy, orderDir).limit(limit).offset(offset).execute(),
|
|
257
|
+
countQuery.executeTakeFirst(),
|
|
258
|
+
]);
|
|
259
|
+
return {
|
|
260
|
+
data: data,
|
|
261
|
+
total: Number(countResult?.count ?? 0),
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
catch (error) {
|
|
265
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not pull from ${this.table}.`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
async countAll() {
|
|
269
|
+
try {
|
|
270
|
+
const result = await this.db
|
|
271
|
+
.withSchema(this.schema)
|
|
272
|
+
.selectFrom(this.table)
|
|
273
|
+
.select((eb) => eb.fn.countAll().as("count"))
|
|
274
|
+
.executeTakeFirst();
|
|
275
|
+
return Number(result?.count ?? 0);
|
|
276
|
+
}
|
|
277
|
+
catch {
|
|
278
|
+
return 0;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
async insertOne(insert, fields) {
|
|
282
|
+
try {
|
|
283
|
+
const jsonCols = await (0, json_columns_1.loadJsonColumnMap)(this.db);
|
|
284
|
+
insert = (0, json_columns_1.wrapJsonColumns)(insert, this.table, jsonCols);
|
|
285
|
+
const query = this.db
|
|
286
|
+
.withSchema(this.schema)
|
|
287
|
+
.insertInto(this.table)
|
|
288
|
+
.values(insert);
|
|
289
|
+
if (!fields || fields === "*") {
|
|
290
|
+
return (await query.returningAll().executeTakeFirstOrThrow());
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
const cols = fields.split(",").map((f) => f.trim());
|
|
294
|
+
return (await query
|
|
295
|
+
.returning(cols)
|
|
296
|
+
.executeTakeFirstOrThrow());
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
catch (error) {
|
|
300
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not insert into ${this.table}.`);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async updateOne(id, updated, fields) {
|
|
304
|
+
try {
|
|
305
|
+
const jsonCols = await (0, json_columns_1.loadJsonColumnMap)(this.db);
|
|
306
|
+
updated = (0, json_columns_1.wrapJsonColumns)(updated, this.table, jsonCols);
|
|
307
|
+
const query = this.db
|
|
308
|
+
.withSchema(this.schema)
|
|
309
|
+
.updateTable(this.table)
|
|
310
|
+
.set(updated)
|
|
311
|
+
.where("id", "=", id);
|
|
312
|
+
if (!fields || fields === "*") {
|
|
313
|
+
return (await query.returningAll().executeTakeFirstOrThrow());
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
const cols = fields.split(",").map((f) => f.trim());
|
|
317
|
+
return (await query
|
|
318
|
+
.returning(cols)
|
|
319
|
+
.executeTakeFirstOrThrow());
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
catch (error) {
|
|
323
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not update ${this.table}.`);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
async updateAllBy(filters, updated, fields) {
|
|
327
|
+
if (!Object.entries(filters).length)
|
|
328
|
+
throw new Error("Illegal filters for updateAllBy().");
|
|
329
|
+
try {
|
|
330
|
+
const jsonCols = await (0, json_columns_1.loadJsonColumnMap)(this.db);
|
|
331
|
+
updated = (0, json_columns_1.wrapJsonColumns)(updated, this.table, jsonCols);
|
|
332
|
+
let query = this.db
|
|
333
|
+
.withSchema(this.schema)
|
|
334
|
+
.updateTable(this.table)
|
|
335
|
+
.set(updated);
|
|
336
|
+
query = this.applyFilters(query, filters);
|
|
337
|
+
if (!fields || fields === "*") {
|
|
338
|
+
return (await query.returningAll().execute());
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
const cols = fields.split(",").map((f) => f.trim());
|
|
342
|
+
return (await query.returning(cols).execute());
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
catch (error) {
|
|
346
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not update ${this.table}.`);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
async deleteOne(id, fields) {
|
|
350
|
+
try {
|
|
351
|
+
const query = this.db
|
|
352
|
+
.withSchema(this.schema)
|
|
353
|
+
.deleteFrom(this.table)
|
|
354
|
+
.where("id", "=", id);
|
|
355
|
+
if (!fields || fields === "*") {
|
|
356
|
+
return (await query.returningAll().executeTakeFirstOrThrow());
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
const cols = fields.split(",").map((f) => f.trim());
|
|
360
|
+
return (await query
|
|
361
|
+
.returning(cols)
|
|
362
|
+
.executeTakeFirstOrThrow());
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
catch (error) {
|
|
366
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not delete from ${this.table}.`);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
async deleteAllBy(filters, fields) {
|
|
370
|
+
if (!Object.entries(filters).length)
|
|
371
|
+
throw new Error("Illegal filters for deleteAllBy().");
|
|
372
|
+
try {
|
|
373
|
+
let query = this.db
|
|
374
|
+
.withSchema(this.schema)
|
|
375
|
+
.deleteFrom(this.table);
|
|
376
|
+
query = this.applyFilters(query, filters);
|
|
377
|
+
if (!fields || fields === "*")
|
|
378
|
+
return (await query.returningAll().execute());
|
|
379
|
+
const cols = fields.split(",").map((f) => f.trim());
|
|
380
|
+
return (await query.returning(cols).execute());
|
|
381
|
+
}
|
|
382
|
+
catch (error) {
|
|
383
|
+
return (0, errors_1.logAndHandleDBError)(error, `Could not delete from ${this.table}.`);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
exports.Repository = Repository;
|
|
388
388
|
//# sourceMappingURL=base.js.map
|