@sphereon/ssi-sdk.kv-store-temp 0.33.1-next.3 → 0.33.1-next.73

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 (60) hide show
  1. package/dist/index.cjs +709 -0
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/index.d.cts +324 -0
  4. package/dist/index.d.ts +321 -8
  5. package/dist/index.js +676 -27
  6. package/dist/index.js.map +1 -1
  7. package/package.json +25 -12
  8. package/src/__tests__/keyv.test.ts +3 -3
  9. package/src/__tests__/kvstore.test.ts +1 -0
  10. package/src/store-adapters/typeorm/entities/keyValueStoreEntity.ts +3 -5
  11. package/src/store-adapters/typeorm/index.ts +1 -3
  12. package/dist/index.d.ts.map +0 -1
  13. package/dist/key-value-store.d.ts +0 -27
  14. package/dist/key-value-store.d.ts.map +0 -1
  15. package/dist/key-value-store.js +0 -123
  16. package/dist/key-value-store.js.map +0 -1
  17. package/dist/key-value-types.d.ts +0 -118
  18. package/dist/key-value-types.d.ts.map +0 -1
  19. package/dist/key-value-types.js +0 -3
  20. package/dist/key-value-types.js.map +0 -1
  21. package/dist/keyv/keyv-types.d.ts +0 -56
  22. package/dist/keyv/keyv-types.d.ts.map +0 -1
  23. package/dist/keyv/keyv-types.js +0 -3
  24. package/dist/keyv/keyv-types.js.map +0 -1
  25. package/dist/keyv/keyv.d.ts +0 -48
  26. package/dist/keyv/keyv.d.ts.map +0 -1
  27. package/dist/keyv/keyv.js +0 -302
  28. package/dist/keyv/keyv.js.map +0 -1
  29. package/dist/store-adapters/index.d.ts +0 -3
  30. package/dist/store-adapters/index.d.ts.map +0 -1
  31. package/dist/store-adapters/index.js +0 -19
  32. package/dist/store-adapters/index.js.map +0 -1
  33. package/dist/store-adapters/tiered/index.d.ts +0 -29
  34. package/dist/store-adapters/tiered/index.d.ts.map +0 -1
  35. package/dist/store-adapters/tiered/index.js +0 -198
  36. package/dist/store-adapters/tiered/index.js.map +0 -1
  37. package/dist/store-adapters/tiered/types.d.ts +0 -14
  38. package/dist/store-adapters/tiered/types.d.ts.map +0 -1
  39. package/dist/store-adapters/tiered/types.js +0 -3
  40. package/dist/store-adapters/tiered/types.js.map +0 -1
  41. package/dist/store-adapters/typeorm/entities/keyValueStoreEntity.d.ts +0 -11
  42. package/dist/store-adapters/typeorm/entities/keyValueStoreEntity.d.ts.map +0 -1
  43. package/dist/store-adapters/typeorm/entities/keyValueStoreEntity.js +0 -38
  44. package/dist/store-adapters/typeorm/entities/keyValueStoreEntity.js.map +0 -1
  45. package/dist/store-adapters/typeorm/index.d.ts +0 -39
  46. package/dist/store-adapters/typeorm/index.d.ts.map +0 -1
  47. package/dist/store-adapters/typeorm/index.js +0 -141
  48. package/dist/store-adapters/typeorm/index.js.map +0 -1
  49. package/dist/store-adapters/typeorm/migrations/1.createKVDatabase.d.ts +0 -14
  50. package/dist/store-adapters/typeorm/migrations/1.createKVDatabase.d.ts.map +0 -1
  51. package/dist/store-adapters/typeorm/migrations/1.createKVDatabase.js +0 -59
  52. package/dist/store-adapters/typeorm/migrations/1.createKVDatabase.js.map +0 -1
  53. package/dist/store-adapters/typeorm/migrations/index.d.ts +0 -10
  54. package/dist/store-adapters/typeorm/migrations/index.d.ts.map +0 -1
  55. package/dist/store-adapters/typeorm/migrations/index.js +0 -13
  56. package/dist/store-adapters/typeorm/migrations/index.js.map +0 -1
  57. package/dist/store-adapters/typeorm/types.d.ts +0 -22
  58. package/dist/store-adapters/typeorm/types.d.ts.map +0 -1
  59. package/dist/store-adapters/typeorm/types.js +0 -3
  60. package/dist/store-adapters/typeorm/types.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,29 +1,678 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/keyv/keyv.ts
5
+ import { EventEmitter } from "events";
6
+ import JSONB from "json-buffer";
7
+ var Keyv = class extends EventEmitter {
8
+ static {
9
+ __name(this, "Keyv");
10
+ }
11
+ opts;
12
+ namespace;
13
+ iterator;
14
+ constructor(uri, options) {
15
+ super();
16
+ const emitErrors = options?.emitErrors === void 0 ? true : options.emitErrors;
17
+ uri = uri ?? options?.uri;
18
+ this.opts = {
19
+ namespace: "keyv",
20
+ serialize: JSONB.stringify,
21
+ deserialize: JSONB.parse,
22
+ ...typeof uri === "string" ? {
23
+ uri
24
+ } : uri,
25
+ ...options
26
+ };
27
+ if (!this.opts.store) {
28
+ if (typeof uri !== "string") {
29
+ this.opts.store = uri;
30
+ }
31
+ }
32
+ if (!this.opts.store) {
33
+ throw Error("No store");
34
+ }
35
+ if (this.opts.compression) {
36
+ const compression = this.opts.compression;
37
+ this.opts.serialize = compression.serialize.bind(compression);
38
+ this.opts.deserialize = compression.deserialize.bind(compression);
39
+ }
40
+ if (typeof this.opts.store.on === "function" && emitErrors) {
41
+ this.opts.store.on("error", (error) => this.emit("error", error));
42
+ }
43
+ this.opts.store.namespace = this.opts.namespace || "keyv";
44
+ this.namespace = this.opts.store.namespace;
45
+ const generateIterator = /* @__PURE__ */ __name((iterator, keyv) => async function* () {
46
+ for await (const [key, raw] of typeof iterator === "function" ? iterator(keyv.store.namespace) : iterator) {
47
+ const data = await keyv.deserialize(raw);
48
+ if (keyv.store.namespace && !key.includes(keyv.store.namespace)) {
49
+ continue;
50
+ }
51
+ if (data && typeof data.expires === "number" && Date.now() > data.expires) {
52
+ keyv.delete(key);
53
+ continue;
54
+ }
55
+ yield [
56
+ keyv._getKeyUnprefix(key),
57
+ data?.value
58
+ ];
59
+ }
60
+ }, "generateIterator");
61
+ if (typeof this.store[Symbol.iterator] === "function" && this.store instanceof Map) {
62
+ this.iterator = generateIterator(this.store, this);
63
+ } else if (typeof this.store.iterator === "function" && this.store.opts && this._checkIterableAdapter()) {
64
+ this.iterator = generateIterator(this.store.iterator.bind(this.store), this);
65
+ }
66
+ }
67
+ get store() {
68
+ return this.opts.store;
69
+ }
70
+ get deserialize() {
71
+ return this.opts.deserialize;
72
+ }
73
+ get serialize() {
74
+ return this.opts.serialize;
75
+ }
76
+ _checkIterableAdapter() {
77
+ return this.store.opts.dialect && iterableAdapters.includes(this.store.opts.dialect) || this.store.opts.url && iterableAdapters.findIndex((element) => this.store.opts.url.includes(element)) >= 0;
78
+ }
79
+ _getKeyPrefix(key) {
80
+ return `${this.opts.namespace}:${key}`;
81
+ }
82
+ _getKeyPrefixArray(keys) {
83
+ return keys.map((key) => this._getKeyPrefix(key));
84
+ }
85
+ _getKeyUnprefix(key) {
86
+ return key.split(":").splice(1).join(":");
87
+ }
88
+ async getMany(keys, options) {
89
+ const keyPrefixed = this._getKeyPrefixArray(keys);
90
+ let promise;
91
+ if (this.store.getMany !== void 0) {
92
+ promise = this.store.getMany(keyPrefixed, options);
93
+ } else if (this.isMapWithEntries(this.store)) {
94
+ promise = this.getFromMapWithPrefix(keyPrefixed);
95
+ } else {
96
+ promise = Promise.all(keyPrefixed.map((k) => this.store.get(k, options)));
97
+ }
98
+ const allValues = Promise.resolve(promise);
99
+ const results = [];
100
+ return Promise.resolve(allValues).then((all) => {
101
+ const entries = Array.isArray(all) ? all : [
102
+ all
103
+ ];
104
+ entries.forEach((data, index) => {
105
+ let result = typeof data === "string" ? this.deserialize(data) : !!data && this.opts.compression ? this.deserialize(data) : data;
106
+ if (result && typeof result === "object" && "expires" in result && typeof result.expires === "number" && Date.now() > result.expires) {
107
+ this.delete(keys[index]);
108
+ result = void 0;
109
+ }
110
+ const final = options && options.raw ? result : result && typeof result === "object" && "value" in result ? result.value : result;
111
+ results.push(final);
112
+ });
113
+ }).then(() => Promise.all(results));
114
+ }
115
+ isMapWithEntries(store) {
116
+ return store instanceof Map && typeof store.entries === "function";
117
+ }
118
+ async getFromMapWithPrefix(prefixes) {
119
+ if (!this.isMapWithEntries(this.store)) {
120
+ return [];
121
+ }
122
+ const map = this.store;
123
+ return prefixes.flatMap((prefix) => {
124
+ const matchedValues = Array.from(map.entries()).filter(([key]) => key.startsWith(prefix)).map(([, value]) => value);
125
+ return matchedValues.length > 0 ? matchedValues : [
126
+ void 0
127
+ ];
128
+ });
129
+ }
130
+ async get(key, options) {
131
+ const isArray = Array.isArray(key);
132
+ return Promise.resolve().then(() => isArray ? this.getMany(this._getKeyPrefixArray(key), options) : this.store.get(this._getKeyPrefix(key))).then((data) => typeof data === "string" ? this.deserialize(data) : this.opts.compression ? this.deserialize(data) : data).then((data) => {
133
+ if (data === void 0 || data === null) {
134
+ return void 0;
135
+ }
136
+ const rows = Array.isArray(data) ? data : [
137
+ data
138
+ ];
139
+ if (isArray) {
140
+ const result = [];
141
+ for (let row of rows) {
142
+ if (row === void 0 || row === null) {
143
+ result.push(void 0);
144
+ continue;
145
+ }
146
+ if (this.isExpired(row)) {
147
+ this.delete(key).then(() => void 0);
148
+ result.push(void 0);
149
+ } else {
150
+ result.push(options && options.raw ? row : toValue(row));
151
+ }
152
+ }
153
+ return result;
154
+ } else if (!Array.isArray(data)) {
155
+ if (this.isExpired(data)) {
156
+ return this.delete(key).then(() => void 0);
157
+ }
158
+ }
159
+ return options && options.raw ? data : Array.isArray(data) ? data.map((d) => toValue(d)) : toValue(data);
160
+ });
161
+ }
162
+ isExpired(data) {
163
+ return typeof data !== "string" && data && typeof data === "object" && "expires" in data && typeof data.expires === "number" && Date.now() > data.expires;
164
+ }
165
+ set(key, value, ttl) {
166
+ const keyPrefixed = this._getKeyPrefix(key);
167
+ if (typeof ttl === "undefined") {
168
+ ttl = this.opts.ttl;
169
+ }
170
+ if (ttl === 0) {
171
+ ttl = void 0;
172
+ }
173
+ return Promise.resolve().then(() => {
174
+ const expires = typeof ttl === "number" ? Date.now() + ttl : void 0;
175
+ if (typeof value === "symbol") {
176
+ this.emit("error", "symbol cannot be serialized");
177
+ }
178
+ const input = {
179
+ value,
180
+ expires
181
+ };
182
+ return this.serialize(input);
183
+ }).then((value2) => this.store.set(keyPrefixed, value2, ttl)).then(() => true);
184
+ }
185
+ delete(key) {
186
+ if (!Array.isArray(key)) {
187
+ const keyPrefixed2 = this._getKeyPrefix(key);
188
+ return Promise.resolve().then(() => this.store.delete(keyPrefixed2));
189
+ }
190
+ const keyPrefixed = this._getKeyPrefixArray(key);
191
+ if (this.store.deleteMany !== void 0) {
192
+ return Promise.resolve().then(() => this.store.deleteMany(keyPrefixed));
193
+ }
194
+ const promises = [];
195
+ for (const key2 of keyPrefixed) {
196
+ promises.push(this.store.delete(key2));
197
+ }
198
+ return Promise.allSettled(promises).then((values) => values.every((x) => x.valueOf() === true));
199
+ }
200
+ async clear() {
201
+ return Promise.resolve().then(() => this.store.clear());
202
+ }
203
+ has(key) {
204
+ const keyPrefixed = this._getKeyPrefix(key);
205
+ return Promise.resolve().then(async () => {
206
+ if (typeof this.store.has === "function") {
207
+ return this.store.has(keyPrefixed);
208
+ }
209
+ const value = await this.store.get(keyPrefixed);
210
+ return value !== void 0;
211
+ });
212
+ }
213
+ disconnect() {
214
+ if (typeof this.store.disconnect === "function") {
215
+ return this.store.disconnect();
216
+ }
217
+ }
218
+ };
219
+ var iterableAdapters = [
220
+ "sqlite",
221
+ "postgres",
222
+ "mysql",
223
+ "mongo",
224
+ "redis",
225
+ "tiered"
226
+ ];
227
+ function toValue(input) {
228
+ return input !== null && typeof input === "object" && "value" in input ? input.value : input;
229
+ }
230
+ __name(toValue, "toValue");
231
+
232
+ // src/key-value-store.ts
233
+ var KeyValueStore = class {
234
+ static {
235
+ __name(this, "KeyValueStore");
236
+ }
237
+ /**
238
+ * The main keyv typescript port which delegates to the storage adapters and takes care of some common functionality
239
+ *
240
+ * @internal
241
+ */
242
+ keyv;
243
+ constructor(options) {
244
+ this.keyv = new Keyv(options.uri, options);
245
+ }
246
+ async get(key) {
247
+ const result = await this.keyv.get(key, {
248
+ raw: false
249
+ });
250
+ if (result === null || result === void 0) {
251
+ return void 0;
252
+ }
253
+ return result;
254
+ }
255
+ async getAsValueData(key) {
256
+ const result = await this.keyv.get(key, {
257
+ raw: true
258
+ });
259
+ if (result === null || result === void 0) {
260
+ return {
261
+ value: void 0,
262
+ expires: void 0
263
+ };
264
+ }
265
+ return this.toDeserializedValueData(result);
266
+ }
267
+ async getMany(keys) {
268
+ if (!keys || keys.length === 0) {
269
+ return [];
270
+ }
271
+ let result = await this.keyv.getMany(keys, {
272
+ raw: false
273
+ });
274
+ if (result === null || result === void 0 || result.length === 0) {
275
+ result = new Array();
276
+ keys.forEach(() => result.push(void 0));
277
+ }
278
+ return result.map((v) => !!v ? v : void 0);
279
+ }
280
+ async getManyAsValueData(keys) {
281
+ if (!keys || keys.length === 0) {
282
+ return [];
283
+ }
284
+ let result = await this.keyv.getMany(keys, {
285
+ raw: true
286
+ });
287
+ if (result === null || result === void 0 || result.length === 0) {
288
+ result = new Array();
289
+ keys.forEach(() => result.push({
290
+ value: void 0,
291
+ expires: void 0
292
+ }));
293
+ }
294
+ return result.map((v) => !!v ? this.toDeserializedValueData(v) : {
295
+ value: void 0,
296
+ expires: void 0
297
+ });
298
+ }
299
+ async set(key, value, ttl) {
300
+ return this.keyv.set(key, value, ttl).then(() => this.getAsValueData(key));
301
+ }
302
+ async has(key) {
303
+ return this.keyv.has(key);
304
+ }
305
+ async delete(key) {
306
+ return this.keyv.delete(key);
307
+ }
308
+ async deleteMany(keys) {
309
+ return Promise.all(keys.map((key) => this.keyv.delete(key)));
310
+ }
311
+ async clear() {
312
+ return this.keyv.clear().then(() => this);
313
+ }
314
+ async disconnect() {
315
+ return this.keyv.disconnect();
316
+ }
317
+ // Public so parties using the kv store directly can add listeners if they want
318
+ async kvStoreOn(args) {
319
+ this.keyv.on(args.eventName, args.listener);
320
+ return this;
321
+ }
322
+ toDeserializedValueData(result) {
323
+ if (result === null || result === void 0) {
324
+ throw Error(`Result cannot be undefined or null at this this point`);
325
+ } else if (typeof result !== "object") {
326
+ return {
327
+ value: result,
328
+ expires: void 0
329
+ };
330
+ } else if (!("value" in result)) {
331
+ return {
332
+ value: result,
333
+ expires: void 0
334
+ };
335
+ }
336
+ if (!("expires" in result)) {
337
+ result.expires = void 0;
338
+ }
339
+ return result;
340
+ }
341
+ };
342
+
343
+ // src/store-adapters/tiered/index.ts
344
+ import { EventEmitter as EventEmitter2 } from "events";
345
+ var KeyValueTieredStoreAdapter = class extends EventEmitter2 {
346
+ static {
347
+ __name(this, "KeyValueTieredStoreAdapter");
348
+ }
349
+ opts;
350
+ remote;
351
+ local;
352
+ iterationLimit;
353
+ namespace;
354
+ constructor({ remote, local, ...options }) {
355
+ super();
356
+ this.opts = {
357
+ validator: /* @__PURE__ */ __name(() => true, "validator"),
358
+ dialect: "tiered",
359
+ ...options
360
+ };
361
+ this.local = isMap(local) ? new Keyv(local) : local;
362
+ this.remote = isMap(remote) ? new Keyv(remote) : remote;
363
+ this.namespace = this.local.namespace;
364
+ }
365
+ async get(key, options) {
366
+ if (Array.isArray(key)) {
367
+ return await this.getMany(key, options);
368
+ }
369
+ const localResult = await this.local.get(key, options);
370
+ if (localResult === void 0 || !this.opts.validator(localResult, key)) {
371
+ const remoteResult = await this.remote.get(key, options);
372
+ if (remoteResult !== localResult) {
373
+ const value = !!remoteResult ? typeof remoteResult === "object" && "value" in remoteResult ? remoteResult.value : remoteResult : void 0;
374
+ const ttl = !!remoteResult && typeof remoteResult === "object" && "expires" in remoteResult && remoteResult.expires ? remoteResult.expires - Date.now() : void 0;
375
+ if (!ttl || ttl > 0) {
376
+ await this.local.set(key, value, ttl);
377
+ } else {
378
+ this.local.delete(key);
379
+ }
380
+ }
381
+ return remoteResult;
382
+ }
383
+ return localResult;
384
+ }
385
+ async getMany(keys, options) {
386
+ const promises = [];
387
+ for (const key of keys) {
388
+ promises.push(this.get(key, options));
389
+ }
390
+ const values = await Promise.all(promises);
391
+ const data = [];
392
+ for (const value of values) {
393
+ data.push(value);
394
+ }
395
+ return data;
396
+ }
397
+ async set(key, value, ttl) {
398
+ const toSet = [
399
+ "local",
400
+ "remote"
401
+ ];
402
+ return Promise.all(toSet.map(async (store) => this[store].set(key, value, ttl)));
403
+ }
404
+ async clear() {
405
+ const toClear = [
406
+ "local"
407
+ ];
408
+ if (!this.opts.localOnly) {
409
+ toClear.push("remote");
410
+ }
411
+ await Promise.all(toClear.map(async (store) => this[store].clear()));
412
+ return void 0;
413
+ }
414
+ async delete(key) {
415
+ const toDelete = [
416
+ "local"
417
+ ];
418
+ if (!this.opts.localOnly) {
419
+ toDelete.push("remote");
420
+ }
421
+ const deleted = await Promise.all(toDelete.map(async (store) => this[store].delete(key)));
422
+ return deleted.every(Boolean);
423
+ }
424
+ async deleteMany(keys) {
425
+ const promises = [];
426
+ for (const key of keys) {
427
+ promises.push(this.delete(key));
428
+ }
429
+ const values = await Promise.all(promises);
430
+ return values.every(Boolean);
431
+ }
432
+ async has(key) {
433
+ let response;
434
+ if (typeof this.local.has === "function") {
435
+ response = this.local.has(key);
436
+ } else {
437
+ const value = await this.local.get(key);
438
+ response = value !== void 0;
439
+ }
440
+ if (!response || !this.opts.validator(response, key)) {
441
+ if (typeof this.remote.has === "function") {
442
+ response = this.remote.has(key);
443
+ } else {
444
+ const value = await this.remote.get(key);
445
+ response = value !== void 0;
446
+ }
447
+ }
448
+ return response;
449
+ }
450
+ async *iterator(namespace) {
451
+ const limit = Number.parseInt(this.iterationLimit, 10) || 10;
452
+ this.remote.opts.iterationLimit = limit;
453
+ if (this.remote && typeof this.remote.iterator === "function") {
454
+ for await (const entries of this.remote.iterator(namespace)) {
455
+ yield entries;
456
+ }
457
+ }
458
+ }
459
+ };
460
+ function isMap(map) {
461
+ if (map instanceof Map) {
462
+ return true;
463
+ } else if (map && typeof map.clear === "function" && typeof map.delete === "function" && typeof map.get === "function" && typeof map.has === "function" && typeof map.set === "function") {
464
+ return true;
465
+ }
466
+ return false;
467
+ }
468
+ __name(isMap, "isMap");
469
+
470
+ // src/store-adapters/typeorm/index.ts
471
+ import { EventEmitter as EventEmitter3 } from "events";
472
+ import { In, Like } from "typeorm";
473
+
474
+ // src/store-adapters/typeorm/entities/keyValueStoreEntity.ts
475
+ import { BaseEntity, Column, Entity, PrimaryColumn } from "typeorm";
476
+ function _ts_decorate(decorators, target, key, desc) {
477
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
478
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
479
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
480
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
481
+ }
482
+ __name(_ts_decorate, "_ts_decorate");
483
+ function _ts_metadata(k, v) {
484
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
485
+ }
486
+ __name(_ts_metadata, "_ts_metadata");
487
+ var KeyValueStoreEntity = class extends BaseEntity {
488
+ static {
489
+ __name(this, "KeyValueStoreEntity");
490
+ }
491
+ key;
492
+ data;
493
+ expires;
494
+ };
495
+ _ts_decorate([
496
+ PrimaryColumn("text"),
497
+ _ts_metadata("design:type", String)
498
+ ], KeyValueStoreEntity.prototype, "key", void 0);
499
+ _ts_decorate([
500
+ Column({
501
+ type: "text"
502
+ }),
503
+ _ts_metadata("design:type", String)
504
+ ], KeyValueStoreEntity.prototype, "data", void 0);
505
+ KeyValueStoreEntity = _ts_decorate([
506
+ Entity("keyvaluestore")
507
+ ], KeyValueStoreEntity);
508
+
509
+ // src/store-adapters/typeorm/index.ts
510
+ import JSONB2 from "json-buffer";
511
+
512
+ // src/store-adapters/typeorm/migrations/1.createKVDatabase.ts
513
+ import { Table } from "typeorm";
514
+ import Debug from "debug";
515
+ var debug = Debug("veramo:kv-store:initial-migration");
516
+ var CreateKVDatabaseMigration = class {
517
+ static {
518
+ __name(this, "CreateKVDatabaseMigration");
519
+ }
520
+ _tableName;
521
+ name;
522
+ constructor(tableName) {
523
+ this._tableName = tableName || "keyvaluestore";
524
+ this.name = `CreateKVDatabase${tableName}1680297189001`;
525
+ }
526
+ async up(queryRunner) {
527
+ function getTableName(givenName) {
528
+ return queryRunner.connection.entityMetadatas.find((meta) => meta.givenTableName === givenName)?.tableName || givenName;
529
+ }
530
+ __name(getTableName, "getTableName");
531
+ debug(`creating ${this._tableName} table`);
532
+ await queryRunner.createTable(new Table({
533
+ name: getTableName(this._tableName),
534
+ columns: [
535
+ {
536
+ name: "key",
537
+ type: "varchar",
538
+ isPrimary: true
539
+ },
540
+ {
541
+ name: "data",
542
+ type: "text",
543
+ isNullable: false
544
+ }
545
+ ],
546
+ indices: [
547
+ {
548
+ columnNames: [
549
+ "key"
550
+ ],
551
+ isUnique: true
552
+ }
553
+ ]
554
+ }), true);
555
+ }
556
+ async down(queryRunner) {
557
+ throw new Error("illegal_operation: cannot roll back initial migration");
558
+ }
559
+ };
560
+
561
+ // src/store-adapters/typeorm/migrations/index.ts
562
+ var kvStoreMigrations = [
563
+ CreateKVDatabaseMigration
564
+ ];
565
+
566
+ // src/store-adapters/typeorm/index.ts
567
+ var KeyValueTypeORMStoreAdapter = class extends EventEmitter3 {
568
+ static {
569
+ __name(this, "KeyValueTypeORMStoreAdapter");
570
+ }
571
+ dbConnection;
572
+ namespace;
573
+ opts;
574
+ constructor(options) {
575
+ super();
576
+ this.dbConnection = options.dbConnection;
577
+ this.namespace = options.namespace || "keyv";
578
+ this.opts = {
579
+ validator: /* @__PURE__ */ __name(() => true, "validator"),
580
+ dialect: "typeorm",
581
+ serialize: JSONB2.stringify,
582
+ deserialize: JSONB2.parse,
583
+ ...options
584
+ };
585
+ }
586
+ async get(key, options) {
587
+ if (Array.isArray(key)) {
588
+ return this.getMany(key, options);
589
+ }
590
+ const connection = await _getConnectedDb(this.dbConnection);
591
+ const result = await connection.getRepository(KeyValueStoreEntity).findOneBy({
592
+ key
593
+ });
594
+ return options?.raw !== true || !result ? result?.data : {
595
+ value: result?.data,
596
+ expires: result?.expires
597
+ };
598
+ }
599
+ async getMany(keys, options) {
600
+ const connection = await _getConnectedDb(this.dbConnection);
601
+ const results = await connection.getRepository(KeyValueStoreEntity).findBy({
602
+ key: In(keys)
603
+ });
604
+ const values = keys.map(async (key) => {
605
+ const result = results.find((result2) => result2.key === key);
606
+ return options?.raw !== true || !result ? result?.data : {
607
+ value: result?.data ? (await this.opts.deserialize(result.data))?.value : void 0,
608
+ expires: result?.expires
609
+ };
610
+ });
611
+ return Promise.all(values);
612
+ }
613
+ async set(key, value, ttl) {
614
+ const connection = await _getConnectedDb(this.dbConnection);
615
+ const entity = new KeyValueStoreEntity();
616
+ entity.key = key;
617
+ entity.data = value;
618
+ entity.expires = ttl;
619
+ await connection.getRepository(KeyValueStoreEntity).save(entity);
620
+ return {
621
+ value,
622
+ expires: ttl
623
+ };
624
+ }
625
+ async delete(key) {
626
+ if (Array.isArray(key)) {
627
+ return this.deleteMany(key);
628
+ }
629
+ const connection = await _getConnectedDb(this.dbConnection);
630
+ const result = await connection.getRepository(KeyValueStoreEntity).delete({
631
+ key
632
+ });
633
+ return result.affected === 1;
634
+ }
635
+ async deleteMany(keys) {
636
+ const connection = await _getConnectedDb(this.dbConnection);
637
+ const results = await connection.getRepository(KeyValueStoreEntity).delete({
638
+ key: In(keys)
639
+ });
640
+ return !!results.affected && results.affected >= 1;
641
+ }
642
+ async clear() {
643
+ const connection = await _getConnectedDb(this.dbConnection);
644
+ await connection.getRepository(KeyValueStoreEntity).delete({
645
+ key: Like(`${this.namespace}:%`)
646
+ });
647
+ }
648
+ async has(key) {
649
+ const connection = await _getConnectedDb(this.dbConnection);
650
+ const result = await connection.getRepository(KeyValueStoreEntity).countBy({
651
+ key
652
+ });
653
+ return result === 1;
654
+ }
655
+ async disconnect() {
656
+ const connection = await _getConnectedDb(this.dbConnection);
657
+ await connection.destroy();
658
+ }
659
+ };
660
+ async function _getConnectedDb(dbConnection) {
661
+ if (dbConnection instanceof Promise) {
662
+ return await dbConnection;
663
+ } else if (!dbConnection.isInitialized) {
664
+ return await dbConnection.initialize();
665
+ } else {
666
+ return dbConnection;
667
+ }
668
+ }
669
+ __name(_getConnectedDb, "_getConnectedDb");
670
+ export {
671
+ KeyValueStore,
672
+ KeyValueStoreEntity,
673
+ KeyValueTieredStoreAdapter,
674
+ KeyValueTypeORMStoreAdapter,
675
+ _getConnectedDb,
676
+ kvStoreMigrations
15
677
  };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.KeyValueStore = void 0;
18
- /**
19
- * Provides a {@link @sphereon/ssi-sdk.kv-store-temp#KeyValueStore} for the
20
- * {@link @veramo/core#Agent} plugin that implements {@link @sphereon/ssi-sdk.kv-store-temp#IKeyValueStore} interface
21
- *
22
- * @packageDocumentation
23
- */
24
- var key_value_store_1 = require("./key-value-store");
25
- Object.defineProperty(exports, "KeyValueStore", { enumerable: true, get: function () { return key_value_store_1.KeyValueStore; } });
26
- __exportStar(require("./store-adapters/tiered/index"), exports);
27
- __exportStar(require("./store-adapters/typeorm/index"), exports);
28
- __exportStar(require("./key-value-types"), exports);
29
678
  //# sourceMappingURL=index.js.map