1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.DefaultKeyValueRepository = void 0;
|
8 | const legacy_juggler_bridge_1 = require("./legacy-juggler-bridge");
|
9 |
|
10 |
|
11 |
|
12 | class DefaultKeyValueRepository {
|
13 | |
14 |
|
15 |
|
16 |
|
17 | constructor(entityClass, ds) {
|
18 | this.entityClass = entityClass;
|
19 |
|
20 |
|
21 | this.kvModelClass =
|
22 | ds.createModel('_kvModel');
|
23 | }
|
24 | delete(key, options) {
|
25 | return (0, legacy_juggler_bridge_1.ensurePromise)(this.kvModelClass.delete(key, options));
|
26 | }
|
27 | deleteAll(options) {
|
28 | return (0, legacy_juggler_bridge_1.ensurePromise)(this.kvModelClass.deleteAll(options));
|
29 | }
|
30 | toEntity(modelData) {
|
31 | if (modelData == null)
|
32 | return modelData;
|
33 | let data = modelData;
|
34 | if (typeof modelData.toObject === 'function') {
|
35 |
|
36 | data = modelData.toObject();
|
37 | }
|
38 | return new this.entityClass(data);
|
39 | }
|
40 | async get(key, options) {
|
41 | const val = this.kvModelClass.get(key, options);
|
42 | const result = await (0, legacy_juggler_bridge_1.ensurePromise)(val);
|
43 | return this.toEntity(result);
|
44 | }
|
45 | set(key, value, options) {
|
46 | return (0, legacy_juggler_bridge_1.ensurePromise)(this.kvModelClass.set(key, value, options));
|
47 | }
|
48 | expire(key, ttl, options) {
|
49 | return (0, legacy_juggler_bridge_1.ensurePromise)(this.kvModelClass.expire(key, ttl, options));
|
50 | }
|
51 | ttl(key, options) {
|
52 | return (0, legacy_juggler_bridge_1.ensurePromise)(this.kvModelClass.ttl(key, options));
|
53 | }
|
54 | keys(filter, options) {
|
55 | const kvModelClass = this.kvModelClass;
|
56 | const iterator = {
|
57 | [Symbol.asyncIterator]() {
|
58 | return new AsyncKeyIteratorImpl(kvModelClass.iterateKeys(filter, options));
|
59 | },
|
60 | };
|
61 | return iterator;
|
62 | }
|
63 | }
|
64 | exports.DefaultKeyValueRepository = DefaultKeyValueRepository;
|
65 | class AsyncKeyIteratorImpl {
|
66 | constructor(keys) {
|
67 | this.keys = keys;
|
68 | }
|
69 | next() {
|
70 | const key = (0, legacy_juggler_bridge_1.ensurePromise)(this.keys.next());
|
71 | return key.then(k => {
|
72 | return { done: k === undefined, value: k !== null && k !== void 0 ? k : '' };
|
73 | });
|
74 | }
|
75 | }
|
76 |
|
\ | No newline at end of file |