@bsv/wallet-toolbox-client 1.2.4 → 1.2.5

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.
Files changed (73) hide show
  1. package/out/src/Setup.d.ts +329 -0
  2. package/out/src/Setup.d.ts.map +1 -0
  3. package/out/src/Setup.js +394 -0
  4. package/out/src/Setup.js.map +1 -0
  5. package/out/src/index.all.d.ts +19 -0
  6. package/out/src/index.all.d.ts.map +1 -0
  7. package/out/src/index.all.js +58 -0
  8. package/out/src/index.all.js.map +1 -0
  9. package/out/src/monitor/Monitor.d.ts.map +1 -1
  10. package/out/src/monitor/Monitor.js +3 -0
  11. package/out/src/monitor/Monitor.js.map +1 -1
  12. package/out/src/monitor/MonitorDaemon.d.ts +30 -0
  13. package/out/src/monitor/MonitorDaemon.d.ts.map +1 -0
  14. package/out/src/monitor/MonitorDaemon.js +134 -0
  15. package/out/src/monitor/MonitorDaemon.js.map +1 -0
  16. package/out/src/monitor/tasks/TaskUnFail.d.ts +40 -0
  17. package/out/src/monitor/tasks/TaskUnFail.d.ts.map +1 -0
  18. package/out/src/monitor/tasks/TaskUnFail.js +145 -0
  19. package/out/src/monitor/tasks/TaskUnFail.js.map +1 -0
  20. package/out/src/sdk/types.d.ts +3 -1
  21. package/out/src/sdk/types.d.ts.map +1 -1
  22. package/out/src/sdk/types.js.map +1 -1
  23. package/out/src/storage/StorageKnex.d.ts +176 -0
  24. package/out/src/storage/StorageKnex.d.ts.map +1 -0
  25. package/out/src/storage/StorageKnex.js +1017 -0
  26. package/out/src/storage/StorageKnex.js.map +1 -0
  27. package/out/src/storage/StorageProvider.js +2 -2
  28. package/out/src/storage/StorageProvider.js.map +1 -1
  29. package/out/src/storage/index.all.d.ts +11 -0
  30. package/out/src/storage/index.all.d.ts.map +1 -0
  31. package/out/src/storage/index.all.js +50 -0
  32. package/out/src/storage/index.all.js.map +1 -0
  33. package/out/src/storage/methods/listActions.d.ts +5 -0
  34. package/out/src/storage/methods/listActions.d.ts.map +1 -0
  35. package/out/src/storage/methods/listActions.js +162 -0
  36. package/out/src/storage/methods/listActions.js.map +1 -0
  37. package/out/src/storage/methods/listOutputs.d.ts +5 -0
  38. package/out/src/storage/methods/listOutputs.d.ts.map +1 -0
  39. package/out/src/storage/methods/listOutputs.js +300 -0
  40. package/out/src/storage/methods/listOutputs.js.map +1 -0
  41. package/out/src/storage/methods/purgeData.d.ts +4 -0
  42. package/out/src/storage/methods/purgeData.d.ts.map +1 -0
  43. package/out/src/storage/methods/purgeData.js +207 -0
  44. package/out/src/storage/methods/purgeData.js.map +1 -0
  45. package/out/src/storage/methods/reviewStatus.d.ts +9 -0
  46. package/out/src/storage/methods/reviewStatus.d.ts.map +1 -0
  47. package/out/src/storage/methods/reviewStatus.js +73 -0
  48. package/out/src/storage/methods/reviewStatus.js.map +1 -0
  49. package/out/src/storage/remoting/StorageServer.d.ts +39 -0
  50. package/out/src/storage/remoting/StorageServer.d.ts.map +1 -0
  51. package/out/src/storage/remoting/StorageServer.js +220 -0
  52. package/out/src/storage/remoting/StorageServer.js.map +1 -0
  53. package/out/src/storage/schema/KnexMigrations.d.ts +39 -0
  54. package/out/src/storage/schema/KnexMigrations.d.ts.map +1 -0
  55. package/out/src/storage/schema/KnexMigrations.js +395 -0
  56. package/out/src/storage/schema/KnexMigrations.js.map +1 -0
  57. package/out/src/storage/sync/StorageMySQLDojoReader.d.ts +71 -0
  58. package/out/src/storage/sync/StorageMySQLDojoReader.d.ts.map +1 -0
  59. package/out/src/storage/sync/StorageMySQLDojoReader.js +609 -0
  60. package/out/src/storage/sync/StorageMySQLDojoReader.js.map +1 -0
  61. package/out/src/storage/sync/index.d.ts +2 -0
  62. package/out/src/storage/sync/index.d.ts.map +1 -0
  63. package/out/src/storage/sync/index.js +18 -0
  64. package/out/src/storage/sync/index.js.map +1 -0
  65. package/out/src/utility/index.all.d.ts +7 -0
  66. package/out/src/utility/index.all.d.ts.map +1 -0
  67. package/out/src/utility/index.all.js +23 -0
  68. package/out/src/utility/index.all.js.map +1 -0
  69. package/out/src/utility/utilityHelpers.buffer.d.ts +18 -0
  70. package/out/src/utility/utilityHelpers.buffer.d.ts.map +1 -0
  71. package/out/src/utility/utilityHelpers.buffer.js +45 -0
  72. package/out/src/utility/utilityHelpers.buffer.js.map +1 -0
  73. package/package.json +1 -1
@@ -0,0 +1,1017 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StorageKnex = void 0;
4
+ const index_all_1 = require("../index.all");
5
+ const index_all_2 = require("./index.all");
6
+ const StorageProvider_1 = require("./StorageProvider");
7
+ const purgeData_1 = require("./methods/purgeData");
8
+ const listActions_1 = require("./methods/listActions");
9
+ const listOutputs_1 = require("./methods/listOutputs");
10
+ const reviewStatus_1 = require("./methods/reviewStatus");
11
+ class StorageKnex extends StorageProvider_1.StorageProvider {
12
+ constructor(options) {
13
+ super(options);
14
+ this._verifiedReadyForDatabaseAccess = false;
15
+ if (!options.knex)
16
+ throw new index_all_1.sdk.WERR_INVALID_PARAMETER('options.knex', `valid`);
17
+ this.knex = options.knex;
18
+ }
19
+ async readSettings() {
20
+ return this.validateEntity((0, index_all_1.verifyOne)(await this.toDb(undefined)('settings')));
21
+ }
22
+ async getProvenOrRawTx(txid, trx) {
23
+ const k = this.toDb(trx);
24
+ const r = {
25
+ proven: undefined,
26
+ rawTx: undefined,
27
+ inputBEEF: undefined
28
+ };
29
+ r.proven = (0, index_all_1.verifyOneOrNone)(await this.findProvenTxs({ partial: { txid: txid } }));
30
+ if (!r.proven) {
31
+ const reqRawTx = (0, index_all_1.verifyOneOrNone)(await k('proven_tx_reqs')
32
+ .where('txid', txid)
33
+ .whereIn('status', ['unsent', 'unmined', 'unconfirmed', 'sending', 'nosend', 'completed'])
34
+ .select('rawTx', 'inputBEEF'));
35
+ if (reqRawTx) {
36
+ r.rawTx = Array.from(reqRawTx.rawTx);
37
+ r.inputBEEF = Array.from(reqRawTx.inputBEEF);
38
+ }
39
+ }
40
+ return r;
41
+ }
42
+ dbTypeSubstring(source, fromOffset, forLength) {
43
+ if (this.dbtype === 'MySQL')
44
+ return `substring(${source} from ${fromOffset} for ${forLength})`;
45
+ return `substr(${source}, ${fromOffset}, ${forLength})`;
46
+ }
47
+ async getRawTxOfKnownValidTransaction(txid, offset, length, trx) {
48
+ if (!txid)
49
+ return undefined;
50
+ if (!this.isAvailable())
51
+ await this.makeAvailable();
52
+ let rawTx = undefined;
53
+ if (Number.isInteger(offset) && Number.isInteger(length)) {
54
+ let rs = await this.toDb(trx).raw(`select ${this.dbTypeSubstring('rawTx', offset + 1, length)} as rawTx from proven_txs where txid = '${txid}'`);
55
+ if (this.dbtype === 'MySQL')
56
+ rs = rs[0];
57
+ const r = (0, index_all_1.verifyOneOrNone)(rs);
58
+ if (r && r.rawTx) {
59
+ rawTx = Array.from(r.rawTx);
60
+ }
61
+ else {
62
+ let rs = await this.toDb(trx).raw(`select ${this.dbTypeSubstring('rawTx', offset + 1, length)} as rawTx from proven_tx_reqs where txid = '${txid}' and status in ('unsent', 'nosend', 'sending', 'unmined', 'completed', 'unfail')`);
63
+ if (this.dbtype === 'MySQL')
64
+ rs = rs[0];
65
+ const r = (0, index_all_1.verifyOneOrNone)(rs);
66
+ if (r && r.rawTx) {
67
+ rawTx = Array.from(r.rawTx);
68
+ }
69
+ }
70
+ }
71
+ else {
72
+ const r = await this.getProvenOrRawTx(txid, trx);
73
+ if (r.proven)
74
+ rawTx = r.proven.rawTx;
75
+ else
76
+ rawTx = r.rawTx;
77
+ }
78
+ return rawTx;
79
+ }
80
+ getProvenTxsForUserQuery(args) {
81
+ const k = this.toDb(args.trx);
82
+ let q = k('proven_txs').where(function () {
83
+ this.whereExists(k
84
+ .select('*')
85
+ .from('transactions')
86
+ .whereRaw(`proven_txs.provenTxId = transactions.provenTxId and transactions.userId = ${args.userId}`));
87
+ });
88
+ if (args.paged) {
89
+ q = q.limit(args.paged.limit);
90
+ q = q.offset(args.paged.offset || 0);
91
+ }
92
+ if (args.since)
93
+ q = q.where('updated_at', '>=', this.validateDateForWhere(args.since));
94
+ return q;
95
+ }
96
+ async getProvenTxsForUser(args) {
97
+ const q = this.getProvenTxsForUserQuery(args);
98
+ const rs = await q;
99
+ return this.validateEntities(rs);
100
+ }
101
+ getProvenTxReqsForUserQuery(args) {
102
+ const k = this.toDb(args.trx);
103
+ let q = k('proven_tx_reqs').where(function () {
104
+ this.whereExists(k
105
+ .select('*')
106
+ .from('transactions')
107
+ .whereRaw(`proven_tx_reqs.txid = transactions.txid and transactions.userId = ${args.userId}`));
108
+ });
109
+ if (args.paged) {
110
+ q = q.limit(args.paged.limit);
111
+ q = q.offset(args.paged.offset || 0);
112
+ }
113
+ if (args.since)
114
+ q = q.where('updated_at', '>=', this.validateDateForWhere(args.since));
115
+ return q;
116
+ }
117
+ async getProvenTxReqsForUser(args) {
118
+ const q = this.getProvenTxReqsForUserQuery(args);
119
+ const rs = await q;
120
+ return this.validateEntities(rs, undefined, ['notified']);
121
+ }
122
+ getTxLabelMapsForUserQuery(args) {
123
+ const k = this.toDb(args.trx);
124
+ let q = k('tx_labels_map').whereExists(k
125
+ .select('*')
126
+ .from('tx_labels')
127
+ .whereRaw(`tx_labels.txLabelId = tx_labels_map.txLabelId and tx_labels.userId = ${args.userId}`));
128
+ if (args.since)
129
+ q = q.where('updated_at', '>=', this.validateDateForWhere(args.since));
130
+ if (args.paged) {
131
+ q = q.limit(args.paged.limit);
132
+ q = q.offset(args.paged.offset || 0);
133
+ }
134
+ return q;
135
+ }
136
+ async getTxLabelMapsForUser(args) {
137
+ const q = this.getTxLabelMapsForUserQuery(args);
138
+ const rs = await q;
139
+ return this.validateEntities(rs, undefined, ['isDeleted']);
140
+ }
141
+ getOutputTagMapsForUserQuery(args) {
142
+ const k = this.toDb(args.trx);
143
+ let q = k('output_tags_map').whereExists(k
144
+ .select('*')
145
+ .from('output_tags')
146
+ .whereRaw(`output_tags.outputTagId = output_tags_map.outputTagId and output_tags.userId = ${args.userId}`));
147
+ if (args.since)
148
+ q = q.where('updated_at', '>=', this.validateDateForWhere(args.since));
149
+ if (args.paged) {
150
+ q = q.limit(args.paged.limit);
151
+ q = q.offset(args.paged.offset || 0);
152
+ }
153
+ return q;
154
+ }
155
+ async getOutputTagMapsForUser(args) {
156
+ const q = this.getOutputTagMapsForUserQuery(args);
157
+ const rs = await q;
158
+ return this.validateEntities(rs, undefined, ['isDeleted']);
159
+ }
160
+ async listActions(auth, vargs) {
161
+ if (!auth.userId)
162
+ throw new index_all_1.sdk.WERR_UNAUTHORIZED();
163
+ return await (0, listActions_1.listActions)(this, auth, vargs);
164
+ }
165
+ async listOutputs(auth, vargs) {
166
+ if (!auth.userId)
167
+ throw new index_all_1.sdk.WERR_UNAUTHORIZED();
168
+ return await (0, listOutputs_1.listOutputs)(this, auth, vargs);
169
+ }
170
+ async insertProvenTx(tx, trx) {
171
+ const e = await this.validateEntityForInsert(tx, trx);
172
+ if (e.provenTxId === 0)
173
+ delete e.provenTxId;
174
+ const [id] = await this.toDb(trx)('proven_txs').insert(e);
175
+ tx.provenTxId = id;
176
+ return tx.provenTxId;
177
+ }
178
+ async insertProvenTxReq(tx, trx) {
179
+ const e = await this.validateEntityForInsert(tx, trx);
180
+ if (e.provenTxReqId === 0)
181
+ delete e.provenTxReqId;
182
+ const [id] = await this.toDb(trx)('proven_tx_reqs').insert(e);
183
+ tx.provenTxReqId = id;
184
+ return tx.provenTxReqId;
185
+ }
186
+ async insertUser(user, trx) {
187
+ const e = await this.validateEntityForInsert(user, trx);
188
+ if (e.userId === 0)
189
+ delete e.userId;
190
+ const [id] = await this.toDb(trx)('users').insert(e);
191
+ user.userId = id;
192
+ return user.userId;
193
+ }
194
+ async insertCertificateAuth(auth, certificate) {
195
+ if (!auth.userId || (certificate.userId && certificate.userId !== auth.userId))
196
+ throw new index_all_1.sdk.WERR_UNAUTHORIZED();
197
+ certificate.userId = auth.userId;
198
+ return await this.insertCertificate(certificate);
199
+ }
200
+ async insertCertificate(certificate, trx) {
201
+ const e = await this.validateEntityForInsert(certificate, trx, undefined, ['isDeleted']);
202
+ const fields = e.fields;
203
+ if (e.fields)
204
+ delete e.fields;
205
+ if (e.certificateId === 0)
206
+ delete e.certificateId;
207
+ const [id] = await this.toDb(trx)('certificates').insert(e);
208
+ certificate.certificateId = id;
209
+ if (fields) {
210
+ for (const field of fields) {
211
+ field.certificateId = id;
212
+ field.userId = certificate.userId;
213
+ await this.insertCertificateField(field, trx);
214
+ }
215
+ }
216
+ return certificate.certificateId;
217
+ }
218
+ async insertCertificateField(certificateField, trx) {
219
+ const e = await this.validateEntityForInsert(certificateField, trx);
220
+ await this.toDb(trx)('certificate_fields').insert(e);
221
+ }
222
+ async insertOutputBasket(basket, trx) {
223
+ const e = await this.validateEntityForInsert(basket, trx, undefined, ['isDeleted']);
224
+ if (e.basketId === 0)
225
+ delete e.basketId;
226
+ const [id] = await this.toDb(trx)('output_baskets').insert(e);
227
+ basket.basketId = id;
228
+ return basket.basketId;
229
+ }
230
+ async insertTransaction(tx, trx) {
231
+ const e = await this.validateEntityForInsert(tx, trx);
232
+ if (e.transactionId === 0)
233
+ delete e.transactionId;
234
+ const [id] = await this.toDb(trx)('transactions').insert(e);
235
+ tx.transactionId = id;
236
+ return tx.transactionId;
237
+ }
238
+ async insertCommission(commission, trx) {
239
+ const e = await this.validateEntityForInsert(commission, trx);
240
+ if (e.commissionId === 0)
241
+ delete e.commissionId;
242
+ const [id] = await this.toDb(trx)('commissions').insert(e);
243
+ commission.commissionId = id;
244
+ return commission.commissionId;
245
+ }
246
+ async insertOutput(output, trx) {
247
+ const e = await this.validateEntityForInsert(output, trx);
248
+ if (e.outputId === 0)
249
+ delete e.outputId;
250
+ const [id] = await this.toDb(trx)('outputs').insert(e);
251
+ output.outputId = id;
252
+ return output.outputId;
253
+ }
254
+ async insertOutputTag(tag, trx) {
255
+ const e = await this.validateEntityForInsert(tag, trx, undefined, ['isDeleted']);
256
+ if (e.outputTagId === 0)
257
+ delete e.outputTagId;
258
+ const [id] = await this.toDb(trx)('output_tags').insert(e);
259
+ tag.outputTagId = id;
260
+ return tag.outputTagId;
261
+ }
262
+ async insertOutputTagMap(tagMap, trx) {
263
+ const e = await this.validateEntityForInsert(tagMap, trx, undefined, ['isDeleted']);
264
+ const [id] = await this.toDb(trx)('output_tags_map').insert(e);
265
+ }
266
+ async insertTxLabel(label, trx) {
267
+ const e = await this.validateEntityForInsert(label, trx, undefined, ['isDeleted']);
268
+ if (e.txLabelId === 0)
269
+ delete e.txLabelId;
270
+ const [id] = await this.toDb(trx)('tx_labels').insert(e);
271
+ label.txLabelId = id;
272
+ return label.txLabelId;
273
+ }
274
+ async insertTxLabelMap(labelMap, trx) {
275
+ const e = await this.validateEntityForInsert(labelMap, trx, undefined, ['isDeleted']);
276
+ const [id] = await this.toDb(trx)('tx_labels_map').insert(e);
277
+ }
278
+ async insertMonitorEvent(event, trx) {
279
+ const e = await this.validateEntityForInsert(event, trx);
280
+ if (e.id === 0)
281
+ delete e.id;
282
+ const [id] = await this.toDb(trx)('monitor_events').insert(e);
283
+ event.id = id;
284
+ return event.id;
285
+ }
286
+ async insertSyncState(syncState, trx) {
287
+ const e = await this.validateEntityForInsert(syncState, trx, ['when'], ['init']);
288
+ if (e.syncStateId === 0)
289
+ delete e.syncStateId;
290
+ const [id] = await this.toDb(trx)('sync_states').insert(e);
291
+ syncState.syncStateId = id;
292
+ return syncState.syncStateId;
293
+ }
294
+ async updateCertificateField(certificateId, fieldName, update, trx) {
295
+ await this.verifyReadyForDatabaseAccess(trx);
296
+ return await this.toDb(trx)('certificate_fields')
297
+ .where({ certificateId, fieldName })
298
+ .update(this.validatePartialForUpdate(update));
299
+ }
300
+ async updateCertificate(id, update, trx) {
301
+ await this.verifyReadyForDatabaseAccess(trx);
302
+ return await this.toDb(trx)('certificates')
303
+ .where({ certificateId: id })
304
+ .update(this.validatePartialForUpdate(update, undefined, ['isDeleted']));
305
+ }
306
+ async updateCommission(id, update, trx) {
307
+ await this.verifyReadyForDatabaseAccess(trx);
308
+ return await this.toDb(trx)('commissions')
309
+ .where({ commissionId: id })
310
+ .update(this.validatePartialForUpdate(update));
311
+ }
312
+ async updateOutputBasket(id, update, trx) {
313
+ await this.verifyReadyForDatabaseAccess(trx);
314
+ return await this.toDb(trx)('output_baskets')
315
+ .where({ basketId: id })
316
+ .update(this.validatePartialForUpdate(update, undefined, ['isDeleted']));
317
+ }
318
+ async updateOutput(id, update, trx) {
319
+ await this.verifyReadyForDatabaseAccess(trx);
320
+ return await this.toDb(trx)('outputs')
321
+ .where({ outputId: id })
322
+ .update(this.validatePartialForUpdate(update));
323
+ }
324
+ async updateOutputTagMap(outputId, tagId, update, trx) {
325
+ await this.verifyReadyForDatabaseAccess(trx);
326
+ return await this.toDb(trx)('output_tags_map')
327
+ .where({ outputId, outputTagId: tagId })
328
+ .update(this.validatePartialForUpdate(update, undefined, ['isDeleted']));
329
+ }
330
+ async updateOutputTag(id, update, trx) {
331
+ await this.verifyReadyForDatabaseAccess(trx);
332
+ return await this.toDb(trx)('output_tags')
333
+ .where({ outputTagId: id })
334
+ .update(this.validatePartialForUpdate(update, undefined, ['isDeleted']));
335
+ }
336
+ async updateProvenTxReq(id, update, trx) {
337
+ await this.verifyReadyForDatabaseAccess(trx);
338
+ let r;
339
+ if (Array.isArray(id)) {
340
+ r = await this.toDb(trx)('proven_tx_reqs')
341
+ .whereIn('provenTxReqId', id)
342
+ .update(this.validatePartialForUpdate(update));
343
+ }
344
+ else if (Number.isInteger(id)) {
345
+ r = await this.toDb(trx)('proven_tx_reqs')
346
+ .where({ provenTxReqId: id })
347
+ .update(this.validatePartialForUpdate(update));
348
+ }
349
+ else {
350
+ throw new index_all_1.sdk.WERR_INVALID_PARAMETER('id', 'transactionId or array of transactionId');
351
+ }
352
+ return r;
353
+ }
354
+ async updateProvenTx(id, update, trx) {
355
+ await this.verifyReadyForDatabaseAccess(trx);
356
+ return await this.toDb(trx)('proven_txs')
357
+ .where({ provenTxId: id })
358
+ .update(this.validatePartialForUpdate(update));
359
+ }
360
+ async updateSyncState(id, update, trx) {
361
+ await this.verifyReadyForDatabaseAccess(trx);
362
+ return await this.toDb(trx)('sync_states')
363
+ .where({ syncStateId: id })
364
+ .update(this.validatePartialForUpdate(update, ['when'], ['init']));
365
+ }
366
+ async updateTransaction(id, update, trx) {
367
+ await this.verifyReadyForDatabaseAccess(trx);
368
+ let r;
369
+ if (Array.isArray(id)) {
370
+ r = await this.toDb(trx)('transactions')
371
+ .whereIn('transactionId', id)
372
+ .update(await this.validatePartialForUpdate(update));
373
+ }
374
+ else if (Number.isInteger(id)) {
375
+ r = await this.toDb(trx)('transactions')
376
+ .where({ transactionId: id })
377
+ .update(await this.validatePartialForUpdate(update));
378
+ }
379
+ else {
380
+ throw new index_all_1.sdk.WERR_INVALID_PARAMETER('id', 'transactionId or array of transactionId');
381
+ }
382
+ return r;
383
+ }
384
+ async updateTxLabelMap(transactionId, txLabelId, update, trx) {
385
+ await this.verifyReadyForDatabaseAccess(trx);
386
+ return await this.toDb(trx)('tx_labels_map')
387
+ .where({ transactionId, txLabelId })
388
+ .update(this.validatePartialForUpdate(update, undefined, ['isDeleted']));
389
+ }
390
+ async updateTxLabel(id, update, trx) {
391
+ await this.verifyReadyForDatabaseAccess(trx);
392
+ return await this.toDb(trx)('tx_labels')
393
+ .where({ txLabelId: id })
394
+ .update(this.validatePartialForUpdate(update, undefined, ['isDeleted']));
395
+ }
396
+ async updateUser(id, update, trx) {
397
+ await this.verifyReadyForDatabaseAccess(trx);
398
+ return await this.toDb(trx)('users').where({ userId: id }).update(this.validatePartialForUpdate(update));
399
+ }
400
+ async updateMonitorEvent(id, update, trx) {
401
+ await this.verifyReadyForDatabaseAccess(trx);
402
+ return await this.toDb(trx)('monitor_events')
403
+ .where({ id })
404
+ .update(this.validatePartialForUpdate(update));
405
+ }
406
+ setupQuery(table, args) {
407
+ let q = this.toDb(args.trx)(table);
408
+ if (args.partial && Object.keys(args.partial).length > 0)
409
+ q.where(args.partial);
410
+ if (args.since)
411
+ q.where('updated_at', '>=', this.validateDateForWhere(args.since));
412
+ if (args.paged) {
413
+ q.limit(args.paged.limit);
414
+ q.offset(args.paged.offset || 0);
415
+ }
416
+ return q;
417
+ }
418
+ findCertificateFieldsQuery(args) {
419
+ return this.setupQuery('certificate_fields', args);
420
+ }
421
+ findCertificatesQuery(args) {
422
+ const q = this.setupQuery('certificates', args);
423
+ if (args.certifiers && args.certifiers.length > 0)
424
+ q.whereIn('certifier', args.certifiers);
425
+ if (args.types && args.types.length > 0)
426
+ q.whereIn('type', args.types);
427
+ return q;
428
+ }
429
+ findCommissionsQuery(args) {
430
+ if (args.partial.lockingScript)
431
+ throw new index_all_1.sdk.WERR_INVALID_PARAMETER('partial.lockingScript', `undefined. Commissions may not be found by lockingScript value.`);
432
+ return this.setupQuery('commissions', args);
433
+ }
434
+ findOutputBasketsQuery(args) {
435
+ return this.setupQuery('output_baskets', args);
436
+ }
437
+ findOutputsQuery(args, count) {
438
+ if (args.partial.lockingScript)
439
+ throw new index_all_1.sdk.WERR_INVALID_PARAMETER('args.partial.lockingScript', `undefined. Outputs may not be found by lockingScript value.`);
440
+ const q = this.setupQuery('outputs', args);
441
+ if (args.txStatus && args.txStatus.length > 0) {
442
+ q.whereRaw(`(select status from transactions where transactions.transactionId = outputs.transactionId) in (${args.txStatus.map(s => `'${s}'`).join(',')})`);
443
+ }
444
+ if (args.noScript && !count) {
445
+ const columns = index_all_2.outputColumnsWithoutLockingScript.map(c => `outputs.${c}`);
446
+ q.select(columns);
447
+ }
448
+ return q;
449
+ }
450
+ findOutputTagMapsQuery(args) {
451
+ const q = this.setupQuery('output_tags_map', args);
452
+ if (args.tagIds && args.tagIds.length > 0)
453
+ q.whereIn('outputTagId', args.tagIds);
454
+ return q;
455
+ }
456
+ findOutputTagsQuery(args) {
457
+ return this.setupQuery('output_tags', args);
458
+ }
459
+ findProvenTxReqsQuery(args) {
460
+ if (args.partial.rawTx)
461
+ throw new index_all_1.sdk.WERR_INVALID_PARAMETER('args.partial.rawTx', `undefined. ProvenTxReqs may not be found by rawTx value.`);
462
+ if (args.partial.inputBEEF)
463
+ throw new index_all_1.sdk.WERR_INVALID_PARAMETER('args.partial.inputBEEF', `undefined. ProvenTxReqs may not be found by inputBEEF value.`);
464
+ const q = this.setupQuery('proven_tx_reqs', args);
465
+ if (args.status && args.status.length > 0)
466
+ q.whereIn('status', args.status);
467
+ if (args.txids && args.txids.length > 0)
468
+ q.whereIn('txid', args.txids);
469
+ return q;
470
+ }
471
+ findProvenTxsQuery(args) {
472
+ if (args.partial.rawTx)
473
+ throw new index_all_1.sdk.WERR_INVALID_PARAMETER('args.partial.rawTx', `undefined. ProvenTxs may not be found by rawTx value.`);
474
+ if (args.partial.merklePath)
475
+ throw new index_all_1.sdk.WERR_INVALID_PARAMETER('args.partial.merklePath', `undefined. ProvenTxs may not be found by merklePath value.`);
476
+ return this.setupQuery('proven_txs', args);
477
+ }
478
+ findSyncStatesQuery(args) {
479
+ return this.setupQuery('sync_states', args);
480
+ }
481
+ findTransactionsQuery(args, count) {
482
+ if (args.partial.rawTx)
483
+ throw new index_all_1.sdk.WERR_INVALID_PARAMETER('args.partial.rawTx', `undefined. Transactions may not be found by rawTx value.`);
484
+ if (args.partial.inputBEEF)
485
+ throw new index_all_1.sdk.WERR_INVALID_PARAMETER('args.partial.inputBEEF', `undefined. Transactions may not be found by inputBEEF value.`);
486
+ const q = this.setupQuery('transactions', args);
487
+ if (args.status && args.status.length > 0)
488
+ q.whereIn('status', args.status);
489
+ if (args.noRawTx && !count) {
490
+ const columns = index_all_2.transactionColumnsWithoutRawTx.map(c => `transactions.${c}`);
491
+ q.select(columns);
492
+ }
493
+ return q;
494
+ }
495
+ findTxLabelMapsQuery(args) {
496
+ const q = this.setupQuery('tx_labels_map', args);
497
+ if (args.labelIds && args.labelIds.length > 0)
498
+ q.whereIn('txLabelId', args.labelIds);
499
+ return q;
500
+ }
501
+ findTxLabelsQuery(args) {
502
+ return this.setupQuery('tx_labels', args);
503
+ }
504
+ findUsersQuery(args) {
505
+ return this.setupQuery('users', args);
506
+ }
507
+ findMonitorEventsQuery(args) {
508
+ return this.setupQuery('monitor_events', args);
509
+ }
510
+ async findCertificatesAuth(auth, args) {
511
+ if (!auth.userId || (args.partial.userId && args.partial.userId !== auth.userId))
512
+ throw new index_all_1.sdk.WERR_UNAUTHORIZED();
513
+ args.partial.userId = auth.userId;
514
+ return await this.findCertificates(args);
515
+ }
516
+ async findOutputBasketsAuth(auth, args) {
517
+ if (!auth.userId || (args.partial.userId && args.partial.userId !== auth.userId))
518
+ throw new index_all_1.sdk.WERR_UNAUTHORIZED();
519
+ args.partial.userId = auth.userId;
520
+ return await this.findOutputBaskets(args);
521
+ }
522
+ async findOutputsAuth(auth, args) {
523
+ if (!auth.userId || (args.partial.userId && args.partial.userId !== auth.userId))
524
+ throw new index_all_1.sdk.WERR_UNAUTHORIZED();
525
+ args.partial.userId = auth.userId;
526
+ return await this.findOutputs(args);
527
+ }
528
+ async findCertificateFields(args) {
529
+ return this.validateEntities(await this.findCertificateFieldsQuery(args));
530
+ }
531
+ async findCertificates(args) {
532
+ const q = this.findCertificatesQuery(args);
533
+ let r = await q;
534
+ r = this.validateEntities(r, undefined, ['isDeleted']);
535
+ if (args.includeFields) {
536
+ for (const c of r) {
537
+ c.fields = this.validateEntities(await this.findCertificateFields({
538
+ partial: { certificateId: c.certificateId, userId: c.userId },
539
+ trx: args.trx
540
+ }));
541
+ }
542
+ }
543
+ return r;
544
+ }
545
+ async findCommissions(args) {
546
+ const q = this.findCommissionsQuery(args);
547
+ const r = await q;
548
+ return this.validateEntities(r, undefined, ['isRedeemed']);
549
+ }
550
+ async findOutputBaskets(args) {
551
+ const q = this.findOutputBasketsQuery(args);
552
+ const r = await q;
553
+ return this.validateEntities(r, undefined, ['isDeleted']);
554
+ }
555
+ async findOutputs(args) {
556
+ const q = this.findOutputsQuery(args);
557
+ const r = await q;
558
+ if (!args.noScript) {
559
+ for (const o of r) {
560
+ await this.validateOutputScript(o, args.trx);
561
+ }
562
+ }
563
+ return this.validateEntities(r, undefined, ['spendable', 'change']);
564
+ }
565
+ async findOutputTagMaps(args) {
566
+ const q = this.findOutputTagMapsQuery(args);
567
+ const r = await q;
568
+ return this.validateEntities(r, undefined, ['isDeleted']);
569
+ }
570
+ async findOutputTags(args) {
571
+ const q = this.findOutputTagsQuery(args);
572
+ const r = await q;
573
+ return this.validateEntities(r, undefined, ['isDeleted']);
574
+ }
575
+ async findProvenTxReqs(args) {
576
+ const q = this.findProvenTxReqsQuery(args);
577
+ const r = await q;
578
+ return this.validateEntities(r, undefined, ['notified']);
579
+ }
580
+ async findProvenTxs(args) {
581
+ const q = this.findProvenTxsQuery(args);
582
+ const r = await q;
583
+ return this.validateEntities(r);
584
+ }
585
+ async findSyncStates(args) {
586
+ const q = this.findSyncStatesQuery(args);
587
+ const r = await q;
588
+ return this.validateEntities(r, ['when'], ['init']);
589
+ }
590
+ async findTransactions(args) {
591
+ const q = this.findTransactionsQuery(args);
592
+ const r = await q;
593
+ if (!args.noRawTx) {
594
+ for (const t of r) {
595
+ await this.validateRawTransaction(t, args.trx);
596
+ }
597
+ }
598
+ return this.validateEntities(r, undefined, ['isOutgoing']);
599
+ }
600
+ async findTxLabelMaps(args) {
601
+ const q = this.findTxLabelMapsQuery(args);
602
+ const r = await q;
603
+ return this.validateEntities(r, undefined, ['isDeleted']);
604
+ }
605
+ async findTxLabels(args) {
606
+ const q = this.findTxLabelsQuery(args);
607
+ const r = await q;
608
+ return this.validateEntities(r, undefined, ['isDeleted']);
609
+ }
610
+ async findUsers(args) {
611
+ const q = this.findUsersQuery(args);
612
+ const r = await q;
613
+ return this.validateEntities(r);
614
+ }
615
+ async findMonitorEvents(args) {
616
+ const q = this.findMonitorEventsQuery(args);
617
+ const r = await q;
618
+ return this.validateEntities(r, ['when'], undefined);
619
+ }
620
+ async getCount(q) {
621
+ q.count();
622
+ const r = await q;
623
+ return r[0]['count(*)'];
624
+ }
625
+ async countCertificateFields(args) {
626
+ return await this.getCount(this.findCertificateFieldsQuery(args));
627
+ }
628
+ async countCertificates(args) {
629
+ return await this.getCount(this.findCertificatesQuery(args));
630
+ }
631
+ async countCommissions(args) {
632
+ return await this.getCount(this.findCommissionsQuery(args));
633
+ }
634
+ async countOutputBaskets(args) {
635
+ return await this.getCount(this.findOutputBasketsQuery(args));
636
+ }
637
+ async countOutputs(args) {
638
+ return await this.getCount(this.findOutputsQuery(args, true));
639
+ }
640
+ async countOutputTagMaps(args) {
641
+ return await this.getCount(this.findOutputTagMapsQuery(args));
642
+ }
643
+ async countOutputTags(args) {
644
+ return await this.getCount(this.findOutputTagsQuery(args));
645
+ }
646
+ async countProvenTxReqs(args) {
647
+ return await this.getCount(this.findProvenTxReqsQuery(args));
648
+ }
649
+ async countProvenTxs(args) {
650
+ return await this.getCount(this.findProvenTxsQuery(args));
651
+ }
652
+ async countSyncStates(args) {
653
+ return await this.getCount(this.findSyncStatesQuery(args));
654
+ }
655
+ async countTransactions(args) {
656
+ return await this.getCount(this.findTransactionsQuery(args, true));
657
+ }
658
+ async countTxLabelMaps(args) {
659
+ return await this.getCount(this.findTxLabelMapsQuery(args));
660
+ }
661
+ async countTxLabels(args) {
662
+ return await this.getCount(this.findTxLabelsQuery(args));
663
+ }
664
+ async countUsers(args) {
665
+ return await this.getCount(this.findUsersQuery(args));
666
+ }
667
+ async countMonitorEvents(args) {
668
+ return await this.getCount(this.findMonitorEventsQuery(args));
669
+ }
670
+ async destroy() {
671
+ var _a;
672
+ await ((_a = this.knex) === null || _a === void 0 ? void 0 : _a.destroy());
673
+ }
674
+ async migrate(storageName, storageIdentityKey) {
675
+ const config = {
676
+ migrationSource: new index_all_2.KnexMigrations(this.chain, storageName, storageIdentityKey, 1024)
677
+ };
678
+ await this.knex.migrate.latest(config);
679
+ const version = await this.knex.migrate.currentVersion(config);
680
+ return version;
681
+ }
682
+ async dropAllData() {
683
+ // Only using migrations to migrate down, don't need valid properties for settings table.
684
+ const config = {
685
+ migrationSource: new index_all_2.KnexMigrations('test', '', '', 1024)
686
+ };
687
+ const count = Object.keys(config.migrationSource.migrations).length;
688
+ for (let i = 0; i < count; i++) {
689
+ try {
690
+ const r = await this.knex.migrate.down(config);
691
+ expect(r).toBeTruthy();
692
+ }
693
+ catch (eu) {
694
+ break;
695
+ }
696
+ }
697
+ }
698
+ async transaction(scope, trx) {
699
+ if (trx)
700
+ return await scope(trx);
701
+ return await this.knex.transaction(async (knextrx) => {
702
+ const trx = knextrx;
703
+ return await scope(trx);
704
+ });
705
+ }
706
+ /**
707
+ * Convert the standard optional `TrxToken` parameter into either a direct knex database instance,
708
+ * or a Knex.Transaction as appropriate.
709
+ */
710
+ toDb(trx) {
711
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
712
+ const db = !trx ? this.knex : trx;
713
+ this.whenLastAccess = new Date();
714
+ return db;
715
+ }
716
+ async validateRawTransaction(t, trx) {
717
+ // if there is no txid or there is a rawTransaction return what we have.
718
+ if (t.rawTx || !t.txid)
719
+ return;
720
+ // rawTransaction is missing, see if we moved it ...
721
+ const rawTx = await this.getRawTxOfKnownValidTransaction(t.txid, undefined, undefined, trx);
722
+ if (!rawTx)
723
+ return;
724
+ t.rawTx = rawTx;
725
+ }
726
+ async validateOutputScript(o, trx) {
727
+ // without offset and length values return what we have (make no changes)
728
+ if (!o.scriptLength || !o.scriptOffset || !o.txid)
729
+ return;
730
+ // if there is an outputScript and its length is the expected length return what we have.
731
+ if (o.lockingScript && o.lockingScript.length === o.scriptLength)
732
+ return;
733
+ // outputScript is missing or has incorrect length...
734
+ const script = await this.getRawTxOfKnownValidTransaction(o.txid, o.scriptOffset, o.scriptLength, trx);
735
+ if (!script)
736
+ return;
737
+ o.lockingScript = script;
738
+ }
739
+ /**
740
+ * Make sure database is ready for access:
741
+ *
742
+ * - dateScheme is known
743
+ * - foreign key constraints are enabled
744
+ *
745
+ * @param trx
746
+ */
747
+ async verifyReadyForDatabaseAccess(trx) {
748
+ if (!this._settings) {
749
+ this._settings = await this.readSettings();
750
+ }
751
+ if (!this._verifiedReadyForDatabaseAccess) {
752
+ // Make sure foreign key constraint checking is turned on in SQLite.
753
+ if (this._settings.dbtype === 'SQLite') {
754
+ await this.toDb(trx).raw('PRAGMA foreign_keys = ON;');
755
+ }
756
+ this._verifiedReadyForDatabaseAccess = true;
757
+ }
758
+ return this._settings.dbtype;
759
+ }
760
+ /**
761
+ * Helper to force uniform behavior across database engines.
762
+ * Use to process the update template for entities being updated.
763
+ */
764
+ validatePartialForUpdate(update, dateFields, booleanFields) {
765
+ if (!this.dbtype)
766
+ throw new index_all_1.sdk.WERR_INTERNAL('must call verifyReadyForDatabaseAccess first');
767
+ const v = update;
768
+ if (v.created_at)
769
+ v.created_at = this.validateEntityDate(v.created_at);
770
+ if (v.updated_at)
771
+ v.updated_at = this.validateEntityDate(v.updated_at);
772
+ if (!v.created_at)
773
+ delete v.created_at;
774
+ if (!v.updated_at)
775
+ v.updated_at = this.validateEntityDate(new Date());
776
+ if (dateFields) {
777
+ for (const df of dateFields) {
778
+ if (v[df])
779
+ v[df] = this.validateOptionalEntityDate(v[df]);
780
+ }
781
+ }
782
+ if (booleanFields) {
783
+ for (const df of booleanFields) {
784
+ if (update[df] !== undefined)
785
+ update[df] = !!update[df] ? 1 : 0;
786
+ }
787
+ }
788
+ for (const key of Object.keys(v)) {
789
+ const val = v[key];
790
+ if (Array.isArray(val) && (val.length === 0 || typeof val[0] === 'number')) {
791
+ v[key] = Buffer.from(val);
792
+ }
793
+ else if (val === undefined) {
794
+ v[key] = null;
795
+ }
796
+ }
797
+ this.isDirty = true;
798
+ return v;
799
+ }
800
+ /**
801
+ * Helper to force uniform behavior across database engines.
802
+ * Use to process new entities being inserted into the database.
803
+ */
804
+ async validateEntityForInsert(entity, trx, dateFields, booleanFields) {
805
+ await this.verifyReadyForDatabaseAccess(trx);
806
+ const v = { ...entity };
807
+ v.created_at = this.validateOptionalEntityDate(v.created_at, true);
808
+ v.updated_at = this.validateOptionalEntityDate(v.updated_at, true);
809
+ if (!v.created_at)
810
+ delete v.created_at;
811
+ if (!v.updated_at)
812
+ delete v.updated_at;
813
+ if (dateFields) {
814
+ for (const df of dateFields) {
815
+ if (v[df])
816
+ v[df] = this.validateOptionalEntityDate(v[df]);
817
+ }
818
+ }
819
+ if (booleanFields) {
820
+ for (const df of booleanFields) {
821
+ if (entity[df] !== undefined)
822
+ entity[df] = !!entity[df] ? 1 : 0;
823
+ }
824
+ }
825
+ for (const key of Object.keys(v)) {
826
+ const val = v[key];
827
+ if (Array.isArray(val) && (val.length === 0 || typeof val[0] === 'number')) {
828
+ v[key] = Buffer.from(val);
829
+ }
830
+ else if (val === undefined) {
831
+ v[key] = null;
832
+ }
833
+ }
834
+ this.isDirty = true;
835
+ return v;
836
+ }
837
+ async getLabelsForTransactionId(transactionId, trx) {
838
+ if (transactionId === undefined)
839
+ return [];
840
+ const labels = await this.toDb(trx)('tx_labels')
841
+ .join('tx_labels_map', 'tx_labels_map.txLabelId', 'tx_labels.txLabelId')
842
+ .where('tx_labels_map.transactionId', transactionId)
843
+ .whereNot('tx_labels_map.isDeleted', true)
844
+ .whereNot('tx_labels.isDeleted', true);
845
+ return this.validateEntities(labels, undefined, ['isDeleted']);
846
+ }
847
+ async extendOutput(o, includeBasket = false, includeTags = false, trx) {
848
+ const ox = o;
849
+ if (includeBasket && ox.basketId)
850
+ ox.basket = await this.findOutputBasketById(o.basketId, trx);
851
+ if (includeTags) {
852
+ ox.tags = await this.getTagsForOutputId(o.outputId);
853
+ }
854
+ return o;
855
+ }
856
+ async getTagsForOutputId(outputId, trx) {
857
+ const tags = await this.toDb(trx)('output_tags')
858
+ .join('output_tags_map', 'output_tags_map.outputTagId', 'output_tags.outputTagId')
859
+ .where('output_tags_map.outputId', outputId)
860
+ .whereNot('output_tags_map.isDeleted', true)
861
+ .whereNot('output_tags.isDeleted', true);
862
+ return this.validateEntities(tags, undefined, ['isDeleted']);
863
+ }
864
+ async purgeData(params, trx) {
865
+ return await (0, purgeData_1.purgeData)(this, params, trx);
866
+ }
867
+ async reviewStatus(args) {
868
+ return await (0, reviewStatus_1.reviewStatus)(this, args);
869
+ }
870
+ /**
871
+ * Finds closest matching available change output to use as input for new transaction.
872
+ *
873
+ * Transactionally allocate the output such that
874
+ */
875
+ async countChangeInputs(userId, basketId, excludeSending) {
876
+ const status = ['completed', 'unproven'];
877
+ if (!excludeSending)
878
+ status.push('sending');
879
+ const statusText = status.map(s => `'${s}'`).join(',');
880
+ const txStatusCondition = `(SELECT status FROM transactions WHERE outputs.transactionId = transactions.transactionId) in (${statusText})`;
881
+ let q = this.knex('outputs').where({ userId, spendable: true, basketId }).whereRaw(txStatusCondition);
882
+ const count = await this.getCount(q);
883
+ return count;
884
+ }
885
+ /**
886
+ * Finds closest matching available change output to use as input for new transaction.
887
+ *
888
+ * Transactionally allocate the output such that
889
+ */
890
+ async allocateChangeInput(userId, basketId, targetSatoshis, exactSatoshis, excludeSending, transactionId) {
891
+ const status = ['completed', 'unproven'];
892
+ if (!excludeSending)
893
+ status.push('sending');
894
+ const statusText = status.map(s => `'${s}'`).join(',');
895
+ const r = await this.knex.transaction(async (trx) => {
896
+ const txStatusCondition = `AND (SELECT status FROM transactions WHERE outputs.transactionId = transactions.transactionId) in (${statusText})`;
897
+ let outputId;
898
+ const setOutputId = async (rawQuery) => {
899
+ let oidr = await trx.raw(rawQuery);
900
+ outputId = undefined;
901
+ if (!oidr['outputId'] && oidr.length > 0)
902
+ oidr = oidr[0];
903
+ if (!oidr['outputId'] && oidr.length > 0)
904
+ oidr = oidr[0];
905
+ if (oidr['outputId'])
906
+ outputId = Number(oidr['outputId']);
907
+ };
908
+ if (exactSatoshis !== undefined) {
909
+ // Find outputId of output that with exactSatoshis
910
+ await setOutputId(`
911
+ SELECT outputId
912
+ FROM outputs
913
+ WHERE userId = ${userId}
914
+ AND spendable = 1
915
+ AND basketId = ${basketId}
916
+ ${txStatusCondition}
917
+ AND satoshis = ${exactSatoshis}
918
+ LIMIT 1;
919
+ `);
920
+ }
921
+ if (outputId === undefined) {
922
+ // Find outputId of output that would at least fund targetSatoshis
923
+ await setOutputId(`
924
+ SELECT outputId
925
+ FROM outputs
926
+ WHERE userId = ${userId}
927
+ AND spendable = 1
928
+ AND basketId = ${basketId}
929
+ ${txStatusCondition}
930
+ AND satoshis - ${targetSatoshis} = (
931
+ SELECT MIN(satoshis - ${targetSatoshis})
932
+ FROM outputs
933
+ WHERE userId = ${userId}
934
+ AND spendable = 1
935
+ AND basketId = ${basketId}
936
+ ${txStatusCondition}
937
+ AND satoshis - ${targetSatoshis} >= 0
938
+ )
939
+ LIMIT 1;
940
+ `);
941
+ }
942
+ if (outputId === undefined) {
943
+ // Find outputId of output that would add the most fund targetSatoshis
944
+ await setOutputId(`
945
+ SELECT outputId
946
+ FROM outputs
947
+ WHERE userId = ${userId}
948
+ AND spendable = 1
949
+ AND basketId = ${basketId}
950
+ ${txStatusCondition}
951
+ AND satoshis - ${targetSatoshis} = (
952
+ SELECT MAX(satoshis - ${targetSatoshis})
953
+ FROM outputs
954
+ WHERE userId = ${userId}
955
+ AND spendable = 1
956
+ AND basketId = ${basketId}
957
+ ${txStatusCondition}
958
+ AND satoshis - ${targetSatoshis} < 0
959
+ )
960
+ LIMIT 1;
961
+ `);
962
+ }
963
+ if (outputId === undefined)
964
+ return undefined;
965
+ await this.updateOutput(outputId, {
966
+ spendable: false,
967
+ spentBy: transactionId
968
+ }, trx);
969
+ const r = (0, index_all_1.verifyTruthy)(await this.findOutputById(outputId, trx));
970
+ return r;
971
+ });
972
+ return r;
973
+ }
974
+ /**
975
+ * Helper to force uniform behavior across database engines.
976
+ * Use to process all individual records with time stamps retreived from database.
977
+ */
978
+ validateEntity(entity, dateFields, booleanFields) {
979
+ entity.created_at = this.validateDate(entity.created_at);
980
+ entity.updated_at = this.validateDate(entity.updated_at);
981
+ if (dateFields) {
982
+ for (const df of dateFields) {
983
+ if (entity[df])
984
+ entity[df] = this.validateDate(entity[df]);
985
+ }
986
+ }
987
+ if (booleanFields) {
988
+ for (const df of booleanFields) {
989
+ if (entity[df] !== undefined)
990
+ entity[df] = !!entity[df];
991
+ }
992
+ }
993
+ for (const key of Object.keys(entity)) {
994
+ const val = entity[key];
995
+ if (val === null) {
996
+ entity[key] = undefined;
997
+ }
998
+ else if (Buffer.isBuffer(val)) {
999
+ entity[key] = Array.from(val);
1000
+ }
1001
+ }
1002
+ return entity;
1003
+ }
1004
+ /**
1005
+ * Helper to force uniform behavior across database engines.
1006
+ * Use to process all arrays of records with time stamps retreived from database.
1007
+ * @returns input `entities` array with contained values validated.
1008
+ */
1009
+ validateEntities(entities, dateFields, booleanFields) {
1010
+ for (let i = 0; i < entities.length; i++) {
1011
+ entities[i] = this.validateEntity(entities[i], dateFields, booleanFields);
1012
+ }
1013
+ return entities;
1014
+ }
1015
+ }
1016
+ exports.StorageKnex = StorageKnex;
1017
+ //# sourceMappingURL=StorageKnex.js.map