@travetto/model-redis 8.0.0-alpha.16 → 8.0.0-alpha.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +6 -6
- package/src/service.ts +48 -16
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ This module provides an [redis](https://redis.io)-based implementation for the [
|
|
|
18
18
|
Supported features:
|
|
19
19
|
* [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/types/crud.ts#L11)
|
|
20
20
|
* [Expiry](https://github.com/travetto/travetto/tree/main/module/model/src/types/expiry.ts#L10)
|
|
21
|
-
* [Indexed](https://github.com/travetto/travetto/tree/main/module/model-indexed/src/types/service.ts#
|
|
21
|
+
* [Indexed](https://github.com/travetto/travetto/tree/main/module/model-indexed/src/types/service.ts#L16)
|
|
22
22
|
|
|
23
23
|
Out of the box, by installing the module, everything should be wired up by default.If you need to customize any aspect of the source or config, you can override and register it with the [Dependency Injection](https://github.com/travetto/travetto/tree/main/module/di#readme "Dependency registration/management and injection support.") module.
|
|
24
24
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-redis",
|
|
3
|
-
"version": "8.0.0-alpha.
|
|
3
|
+
"version": "8.0.0-alpha.17",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Redis backing for the travetto model module.",
|
|
6
6
|
"keywords": [
|
|
@@ -26,13 +26,13 @@
|
|
|
26
26
|
"directory": "module/model-redis"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@redis/client": "^5.
|
|
30
|
-
"@travetto/config": "^8.0.0-alpha.
|
|
31
|
-
"@travetto/model": "^8.0.0-alpha.
|
|
32
|
-
"@travetto/model-indexed": "^8.0.0-alpha.
|
|
29
|
+
"@redis/client": "^5.12.1",
|
|
30
|
+
"@travetto/config": "^8.0.0-alpha.15",
|
|
31
|
+
"@travetto/model": "^8.0.0-alpha.15",
|
|
32
|
+
"@travetto/model-indexed": "^8.0.0-alpha.17"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"@travetto/cli": "^8.0.0-alpha.
|
|
35
|
+
"@travetto/cli": "^8.0.0-alpha.20"
|
|
36
36
|
},
|
|
37
37
|
"peerDependenciesMeta": {
|
|
38
38
|
"@travetto/cli": {
|
package/src/service.ts
CHANGED
|
@@ -10,14 +10,16 @@ import {
|
|
|
10
10
|
type ModelIndexedSupport, type KeyedIndexSelection, type KeyedIndexBody, type ModelPageOptions, ModelIndexedUtil,
|
|
11
11
|
type SingleItemIndex, type SortedIndexSelection, type ModelPageResult, type SortedIndex, isModelIndexedIndex,
|
|
12
12
|
type FullKeyedIndexWithPartialBody, type FullKeyedIndexBody, ModelIndexedComputedIndex,
|
|
13
|
-
warnIfIndexedUniqueIndex, warnIfNonIndexedIndex,
|
|
13
|
+
warnIfIndexedUniqueIndex, warnIfNonIndexedIndex, type ModelIndexedSearchOptions, type SortedIndexSelectionType,
|
|
14
14
|
} from '@travetto/model-indexed';
|
|
15
15
|
|
|
16
16
|
import { Injectable, PostConstruct } from '@travetto/di';
|
|
17
17
|
|
|
18
18
|
import type { RedisModelConfig } from './config.ts';
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
const TERMINATOR = '\xff';
|
|
21
|
+
|
|
22
|
+
type RedisScan = ({ key: string } | { match: string } | { prefix: string }) & { reverse?: boolean };
|
|
21
23
|
type RedisClient = ReturnType<typeof createClient>;
|
|
22
24
|
type RedisMulti = ReturnType<RedisClient['multi']>;
|
|
23
25
|
type ScanState = { cursor?: string, ids: string[] };
|
|
@@ -59,10 +61,20 @@ export class RedisModelService implements ModelCrudSupport, ModelExpirySupport,
|
|
|
59
61
|
case 'sScan': output = await this.client.sScan(key!, cursor ?? '0', flags).then(result => ({ cursor: result.cursor, ids: result.members })); break;
|
|
60
62
|
case 'zRange': {
|
|
61
63
|
const offset = cursor ? +cursor : 0;
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
const prefix = 'prefix' in search ? search.prefix : undefined;
|
|
65
|
+
|
|
66
|
+
let bounds: [number | string, number | string];
|
|
67
|
+
|
|
68
|
+
if (prefix !== undefined) {
|
|
69
|
+
bounds = [prefix ? `[${prefix}` : '-', `(${prefix}${TERMINATOR}`];
|
|
70
|
+
} else {
|
|
71
|
+
bounds = [Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY];
|
|
72
|
+
}
|
|
73
|
+
if (search.reverse) {
|
|
74
|
+
bounds = [bounds[1], bounds[0]];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const result = await this.client.zRange(key!, ...bounds, { BY: prefix ? 'LEX' : 'SCORE', REV: search.reverse, LIMIT: { offset, count } });
|
|
66
78
|
output = { cursor: result.length ? (offset + result.length).toString() : undefined, ids: result };
|
|
67
79
|
break;
|
|
68
80
|
}
|
|
@@ -90,15 +102,11 @@ export class RedisModelService implements ModelCrudSupport, ModelExpirySupport,
|
|
|
90
102
|
} while (matched.cursor && produced < limit && !(options?.abort?.aborted));
|
|
91
103
|
}
|
|
92
104
|
|
|
93
|
-
#scanIndex<
|
|
94
|
-
T extends ModelType,
|
|
95
|
-
K extends KeyedIndexSelection<T>,
|
|
96
|
-
S extends SortedIndexSelection<T>
|
|
97
|
-
>(
|
|
105
|
+
#scanIndex<T extends ModelType>(
|
|
98
106
|
cls: Class<T>,
|
|
99
|
-
idx: SortedIndex<T
|
|
100
|
-
body: KeyedIndexBody<T
|
|
101
|
-
options?: ModelPageOptions
|
|
107
|
+
idx: SortedIndex<T>,
|
|
108
|
+
body: KeyedIndexBody<T>,
|
|
109
|
+
options?: ModelPageOptions & { prefix?: string }
|
|
102
110
|
): AsyncIterable<ScanState> {
|
|
103
111
|
ModelCrudUtil.ensureNotSubType(cls);
|
|
104
112
|
const computed = ModelIndexedComputedIndex.get(idx, body).validate();
|
|
@@ -106,7 +114,7 @@ export class RedisModelService implements ModelCrudSupport, ModelExpirySupport,
|
|
|
106
114
|
switch (idx.type) {
|
|
107
115
|
// case 'indexed:keyed': return this.#streamValues('sScan', { key: fullKey }, options);
|
|
108
116
|
case 'indexed:sorted': {
|
|
109
|
-
return this.#streamValues('zRange', { key: fullKey }, options);
|
|
117
|
+
return this.#streamValues('zRange', { key: fullKey, prefix: options?.prefix }, options);
|
|
110
118
|
}
|
|
111
119
|
}
|
|
112
120
|
}
|
|
@@ -141,7 +149,15 @@ export class RedisModelService implements ModelCrudSupport, ModelExpirySupport,
|
|
|
141
149
|
|
|
142
150
|
switch (idx.type) {
|
|
143
151
|
case 'indexed:keyed': multi.sAdd(resolvedKey, item.id); break;
|
|
144
|
-
case 'indexed:sorted':
|
|
152
|
+
case 'indexed:sorted': {
|
|
153
|
+
const value = computed.getSort();
|
|
154
|
+
if (typeof value === 'string') {
|
|
155
|
+
multi.zAdd(resolvedKey, { score: 0, value: `${value}${TERMINATOR}${item.id}` });
|
|
156
|
+
} else {
|
|
157
|
+
multi.zAdd(resolvedKey, { score: computed.getSort(), value: item.id });
|
|
158
|
+
}
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
145
161
|
}
|
|
146
162
|
}
|
|
147
163
|
}
|
|
@@ -428,4 +444,20 @@ export class RedisModelService implements ModelCrudSupport, ModelExpirySupport,
|
|
|
428
444
|
yield await this.#getBodies(cls, ids, id => this.#resolveKey(cls, id));
|
|
429
445
|
}
|
|
430
446
|
}
|
|
447
|
+
|
|
448
|
+
async suggestByIndex<
|
|
449
|
+
T extends ModelType,
|
|
450
|
+
S extends SortedIndexSelection<T>,
|
|
451
|
+
K extends KeyedIndexSelection<T>,
|
|
452
|
+
B extends SortedIndexSelectionType<T, S> & string
|
|
453
|
+
>(cls: Class<T>, idx: SortedIndex<T, K, S>, body: KeyedIndexBody<T, K>, prefix: B, options?: ModelIndexedSearchOptions): Promise<T[]> {
|
|
454
|
+
|
|
455
|
+
const items: T[] = [];
|
|
456
|
+
for await (const { ids } of this.#scanIndex(cls, idx, body, { limit: 10, ...options, prefix })) {
|
|
457
|
+
const cleaned = ids.map(id => id.split(TERMINATOR).at(-1)!);
|
|
458
|
+
items.push(...await this.#getBodies(cls, cleaned, id => this.#resolveKey(cls, id)));
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
return items;
|
|
462
|
+
}
|
|
431
463
|
}
|