-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathquery.ts
1317 lines (1263 loc) Β· 32.7 KB
/
query.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Utilities for querying/filtering entries from a {@linkcode Deno.Kv} instance.
*
* @module
*/
import {
keyToJSON,
type KvKeyJSON,
type KvValueJSON,
toKey,
toValue,
valueToJSON,
} from "@deno/kv-utils/json";
import { equal } from "@std/assert/equal";
import { assert } from "@std/assert/assert";
import {
keys,
type KeyTree,
tree,
unique,
uniqueCount,
type UniqueCountElement,
} from "./keys.ts";
/**
* The supported operations for filtering entries.
*
* The following operations are supported:
*
* - `"<"` - less than
* - `"<="` - less than or equal
* - `"=="` - equal
* - `">="` - greater than or equal
* - `">"` - greater than
* - `"!="` - not equal
* - `"array-contains"` - array contains the value
* - `"array-contains-any"` - array contains any of the values
* - `"in"` - value is in the array of supplied values
* - `"not-in"` - value is not in the array of supplied values
* - `"matches"` - value matches the regular expression
* - `"kind-of"` - value is of the specified kind
*/
export type Operation =
| "<"
| "<="
| "=="
| ">="
| ">"
| "!="
| "array-contains"
| "array-contains-any"
| "in"
| "not-in"
| "matches"
| "kind-of";
type Mappable = Record<string, unknown> | Map<string, unknown>;
/**
* The different kinds of values that can be stored in a {@linkcode Deno.Kv}.
*/
export type Kinds = KvValueJSON["type"];
export interface QueryLike<T = unknown> {
readonly selector: Deno.KvListSelector;
get(): Deno.KvListIterator<T>;
}
/**
* A representation of a {@linkcode Deno.KvListSelector} as a JSON object.
*/
export type KvListSelectorJSON =
| { prefix: KvKeyJSON }
| { prefix: KvKeyJSON; start: KvKeyJSON }
| { prefix: KvKeyJSON; end: KvKeyJSON }
| { start: KvKeyJSON; end: KvKeyJSON };
/**
* A representation of an _and_ filter as a JSON object.
*/
export interface KvFilterAndJSON {
kind: "and";
filters: KvFilterJSON[];
}
/**
* A representation of an _or_ filter as a JSON object.
*/
export interface KvFilterOrJSON {
kind: "or";
filters: KvFilterJSON[];
}
/**
* A representation of a _where_ filter as a JSON object.
*/
export interface KvFilterWhereJSON {
kind: "where";
property: string | string[];
operation: Operation;
value: KvValueJSON;
}
/**
* A representation of a _value_ filter as a JSON object.
*/
export interface KvFilterValueJSON {
kind: "value";
operation: Operation;
value: KvValueJSON;
}
/**
* A representation of a filter as a JSON object.
*/
export type KvFilterJSON =
| KvFilterAndJSON
| KvFilterOrJSON
| KvFilterWhereJSON
| KvFilterValueJSON;
/**
* A representation of a query as a JSON object.
*/
export interface KvQueryJSON {
selector: KvListSelectorJSON;
options?: Deno.KvListOptions;
filters: KvFilterJSON[];
}
function getKind(value: unknown): Kinds {
const type = typeof value;
switch (type) {
case "bigint":
case "boolean":
case "number":
case "string":
case "undefined":
return type;
case "object":
if (Array.isArray(value)) {
return "Array";
}
if (value instanceof DataView) {
return "DataView";
}
if (ArrayBuffer.isView(value)) {
if (value instanceof Int8Array) {
return "Int8Array";
}
if (value instanceof Uint8Array) {
return "Uint8Array";
}
if (value instanceof Uint8ClampedArray) {
return "Uint8ClampedArray";
}
if (value instanceof Int16Array) {
return "Int16Array";
}
if (value instanceof Uint16Array) {
return "Uint16Array";
}
if (value instanceof Int32Array) {
return "Int32Array";
}
if (value instanceof Uint32Array) {
return "Uint32Array";
}
if (value instanceof Float32Array) {
return "Float32Array";
}
if (value instanceof Float64Array) {
return "Float64Array";
}
if (value instanceof BigInt64Array) {
return "BigInt64Array";
}
if (value instanceof BigUint64Array) {
return "BigUint64Array";
}
}
if (value instanceof ArrayBuffer) {
return "ArrayBuffer";
}
if (value instanceof Date) {
return "Date";
}
if ("Deno" in globalThis && value instanceof Deno.KvU64) {
return "KvU64";
}
if (value instanceof Error) {
if (value instanceof EvalError) {
return "EvalError";
}
if (value instanceof RangeError) {
return "RangeError";
}
if (value instanceof ReferenceError) {
return "ReferenceError";
}
if (value instanceof SyntaxError) {
return "SyntaxError";
}
if (value instanceof TypeError) {
return "TypeError";
}
if (value instanceof URIError) {
return "URIError";
}
return "Error";
}
if (value instanceof Map) {
return "Map";
}
if (value === null) {
return "null";
}
if (value instanceof RegExp) {
return "RegExp";
}
if (value instanceof Set) {
return "Set";
}
return "object";
default:
return "undefined";
}
}
function getValue(obj: Mappable, key: string): unknown {
if (obj instanceof Map) {
return obj.get(key);
}
return obj[key];
}
function hasProperty(obj: Mappable, key: string): boolean {
if (obj instanceof Map) {
return obj.has(key);
}
return key in obj;
}
function isMappable(value: unknown): value is Mappable {
return typeof value === "object" && value !== null;
}
function selectorToJSON(selector: Deno.KvListSelector): KvListSelectorJSON {
if ("prefix" in selector) {
if ("start" in selector) {
return {
prefix: keyToJSON(selector.prefix),
start: keyToJSON(selector.start),
};
}
if ("end" in selector) {
return {
prefix: keyToJSON(selector.prefix),
end: keyToJSON(selector.end),
};
}
return { prefix: keyToJSON(selector.prefix) };
}
return { start: keyToJSON(selector.start), end: keyToJSON(selector.end) };
}
function toSelector(json: KvListSelectorJSON): Deno.KvListSelector {
if ("prefix" in json) {
if ("start" in json) {
return {
prefix: toKey(json.prefix),
start: toKey(json.start),
};
}
if ("end" in json) {
return {
prefix: toKey(json.prefix),
end: toKey(json.end),
};
}
return { prefix: toKey(json.prefix) };
}
return { start: toKey(json.start), end: toKey(json.end) };
}
/**
* A representation of a property path to a value in an object. This is used to
* be able to query/filter entries based on nested properties.
*
* @example
*
* ```ts
* import { PropertyPath } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const path = new PropertyPath("a", "b", "c");
* assert(path.exists({ a: { b: { c: 1 } } }));
* assert(!path.exists({ a: { b: { d: 1 } } }));
* assert(path.value({ a: { b: { c: 1 } } }) === 1);
* ```
*/
export class PropertyPath {
#parts: string[];
constructor(...parts: string[]) {
this.#parts = parts;
}
/**
* Returns `true` if the property path exists in the object.
*/
exists(other: unknown): other is Mappable {
let current = other;
for (const part of this.#parts) {
if (!isMappable(current)) {
return false;
}
if (!hasProperty(current, part)) {
return false;
}
current = getValue(current, part);
}
return true;
}
/**
* Returns the value of the property represented by the path. If the property
* does not exist, an error is thrown.
*/
value(other: Mappable): unknown {
// deno-lint-ignore no-explicit-any
let current: any = other;
for (const part of this.#parts) {
if (!isMappable(current)) {
throw new TypeError("Value is not mappable");
}
if (!hasProperty(current, part)) {
throw new Error("Property does not exist");
}
current = getValue(current, part);
}
return current;
}
/**
* Convert the property path to a JSON array.
*/
toJSON(): string[] {
return this.#parts;
}
/**
* Create a property path from an array of parts.
*/
static from(parts: string[]): PropertyPath {
return new PropertyPath(...parts);
}
}
// deno-lint-ignore no-explicit-any
function exec(other: any, operation: Operation, value: any | any[]): boolean {
switch (operation) {
case "<":
return other < value;
case "<=":
return other <= value;
case "!=":
return !equal(other, value);
case "==":
return equal(other, value);
case ">":
return other > value;
case ">=":
return other >= value;
case "array-contains":
if (Array.isArray(other)) {
return other.some((v) => equal(v, value));
}
return false;
case "array-contains-any":
if (Array.isArray(other) && Array.isArray(value)) {
return other.some((v) => value.some((w) => equal(v, w)));
}
return false;
case "in":
if (Array.isArray(value)) {
return value.some((v) => equal(other, v));
}
return false;
case "not-in":
if (Array.isArray(value)) {
return !value.some((v) => equal(other, v));
}
break;
case "matches":
if (typeof other === "string" && value instanceof RegExp) {
return value.test(other);
}
break;
case "kind-of":
return getKind(other) === value;
}
return false;
}
/**
* A filter instance which can be used to filter entries based on a set of
* conditions. Users should use the static methods to create instances of this
* class.
*
* @example Creating a filter based on a property value
*
* ```ts
* import { Filter } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.where("age", "<=", 10);
* assert(filter.test({ age: 10 }));
* assert(!filter.test({ age: 11 }));
* ```
*
* @example Creating a filter based on a property value using a `PropertyPath`
*
* ```ts
* import { Filter, PropertyPath } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.where(new PropertyPath("a", "b", "c"), "==", 1);
* assert(filter.test({ a: { b: { c: 1 } } }));
* assert(!filter.test({ a: { b: { c: 2 } } }));
* assert(!filter.test({ a: { b: { d: 1 } } }));
* ```
*
* @example Creating a filter based on an _or_ condition
*
* ```ts
* import { Filter } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.or(
* Filter.where("age", "<", 10),
* Filter.where("age", ">", 20),
* );
* assert(filter.test({ age: 5 }));
* assert(filter.test({ age: 25 }));
* assert(!filter.test({ age: 15 }));
* ```
*
* @example Creating a filter based on an _and_ condition
*
* ```ts
* import { Filter } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.and(
* Filter.where("age", ">", 10),
* Filter.where("age", "<", 20),
* );
* assert(filter.test({ age: 15 }));
* assert(!filter.test({ age: 10 }));
* assert(!filter.test({ age: 20 }));
* ```
*/
export class Filter {
#kind: "and" | "or" | "value" | "where";
#property?: string | PropertyPath;
#filters: Filter[];
#operation?: Operation;
#value?: unknown | unknown[];
private constructor(kind: "and" | "or", filters: Filter[]);
private constructor(
kind: "where",
filters: undefined,
property: string | PropertyPath,
operation: Operation,
value: unknown | unknown[],
);
private constructor(
kind: "value",
filters: undefined,
property: undefined,
operation: Operation,
value: unknown | unknown[],
);
private constructor(
kind: "and" | "or" | "value" | "where",
filters: Filter[] = [],
property?: string | PropertyPath,
operation?: Operation,
value?: unknown | unknown[],
) {
this.#kind = kind;
this.#filters = filters;
this.#property = property;
this.#operation = operation;
this.#value = value;
}
/**
* Test the value against the filter.
*/
test(value: unknown): boolean {
switch (this.#kind) {
case "and":
return this.#filters.every((f) => f.test(value));
case "or":
return this.#filters.some((f) => f.test(value));
case "where":
assert(this.#property);
assert(this.#operation);
if (this.#property instanceof PropertyPath) {
if (this.#property.exists(value)) {
const propValue = this.#property.value(value);
return exec(propValue, this.#operation, this.#value);
}
return false;
}
if (isMappable(value)) {
if (hasProperty(value, this.#property)) {
const propValue = getValue(value, this.#property);
return exec(propValue, this.#operation, this.#value);
}
return false;
}
return false;
case "value":
assert(this.#operation);
return exec(value, this.#operation, this.#value);
}
throw new TypeError("Invalid filter kind");
}
/**
* Convert the filter to a JSON object.
*/
toJSON(): KvFilterJSON {
switch (this.#kind) {
case "and":
return {
kind: "and",
filters: this.#filters.map((f) => f.toJSON()),
};
case "or":
return {
kind: "or",
filters: this.#filters.map((f) => f.toJSON()),
};
case "where":
assert(this.#property);
assert(this.#operation);
return {
kind: "where",
property: this.#property instanceof PropertyPath
? this.#property.toJSON()
: this.#property,
operation: this.#operation,
value: valueToJSON(this.#value),
};
case "value":
assert(this.#operation);
return {
kind: "value",
operation: this.#operation,
value: valueToJSON(this.#value),
};
}
}
/**
* Create a filter which will return `true` if all the filters return `true`.
*
* @example
*
* ```ts
* import { Filter } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.and(
* Filter.where("age", ">", 10),
* Filter.where("age", "<", 20),
* );
* assert(filter.test({ age: 15 }));
* assert(!filter.test({ age: 10 }));
* assert(!filter.test({ age: 20 }));
* ```
*/
static and(...filters: Filter[]): Filter {
return new Filter("and", filters);
}
/**
* Create a filter which will return `true` if any of the filters return
* `true`.
*
* @example
*
* ```ts
* import { Filter } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.or(
* Filter.where("age", "<", 10),
* Filter.where("age", ">", 20),
* );
* assert(filter.test({ age: 5 }));
* assert(filter.test({ age: 25 }));
* assert(!filter.test({ age: 15 }));
* ```
*/
static or(...filters: Filter[]): Filter {
return new Filter("or", filters);
}
/**
* Create a filter which will return `true` if the value matches the
* operation and value.
*
* @example
*
* ```ts
* import { Filter } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.value("==", 10);
* assert(filter.test(10));
* assert(!filter.test(11));
* ```
*/
static value(operation: "kind-of", value: Kinds): Filter;
/**
* Create a filter which will return `true` if the value matches the
* operation and value.
*
* @example
*
* ```ts
* import { Filter } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.value("==", 10);
* assert(filter.test(10));
* assert(!filter.test(11));
* ```
*/
static value(operation: "matches", value: RegExp): Filter;
/**
* Create a filter which will return `true` if the value matches the
* operation and value.
*
* @example
*
* ```ts
* import { Filter } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.value("==", 10);
* assert(filter.test(10));
* assert(!filter.test(11));
* ```
*/
static value(
operation: "in" | "not-in" | "array-contains-any",
value: unknown[],
): Filter;
/**
* Create a filter which will return `true` if the value matches the
* operation and value.
*
* @example
*
* ```ts
* import { Filter } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.value("==", 10);
* assert(filter.test(10));
* assert(!filter.test(11));
* ```
*/
static value(operation: Operation, value: unknown): Filter;
static value(operation: Operation, value: unknown | unknown[]): Filter {
return new Filter("value", undefined, undefined, operation, value);
}
/**
* Create a filter which will return `true` if the value of the property
* matches the operation and value.
*
* @example
*
* ```ts
* import { Filter } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.where("age", "<=", 10);
* assert(filter.test({ age: 10 }));
* assert(!filter.test({ age: 11 }));
* ```
*/
static where(
property: string | PropertyPath,
operation: "kind-of",
value: Kinds,
): Filter;
/**
* Create a filter which will return `true` if the value of the property
* matches the operation and value.
*
* @example
*
* ```ts
* import { Filter } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.where("age", "<=", 10);
* assert(filter.test({ age: 10 }));
* assert(!filter.test({ age: 11 }));
* ```
*/
static where(
property: string | PropertyPath,
operation: "matches",
value: RegExp,
): Filter;
/**
* Create a filter which will return `true` if the value of the property
* matches the operation and value.
*
* @example
*
* ```ts
* import { Filter } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.where("age", "<=", 10);
* assert(filter.test({ age: 10 }));
* assert(!filter.test({ age: 11 }));
* ```
*/
static where(
property: string | PropertyPath,
operation: "in" | "not-in" | "array-contains-any",
value: unknown[],
): Filter;
/**
* Create a filter which will return `true` if the value of the property
* matches the operation and value.
*
* @example
*
* ```ts
* import { Filter } from "@kitsonk/kv-toolbox/query";
* import { assert } from "@std/assert/assert";
*
* const filter = Filter.where("age", "<=", 10);
* assert(filter.test({ age: 10 }));
* assert(!filter.test({ age: 11 }));
* ```
*/
static where(
property: string | PropertyPath,
operation: Operation,
value: unknown,
): Filter;
static where(
property: string | PropertyPath,
operation: Operation,
value: unknown | unknown[],
): Filter {
return new Filter("where", undefined, property, operation, value);
}
/**
* Parse a filter from a JSON object.
*/
static parse(json: KvFilterJSON): Filter {
switch (json.kind) {
case "and":
return Filter.and(...json.filters.map(Filter.parse));
case "or":
return Filter.or(...json.filters.map(Filter.parse));
case "where":
return Filter.where(
Array.isArray(json.property)
? PropertyPath.from(json.property)
: json.property,
json.operation,
toValue(json.value),
);
case "value":
return Filter.value(json.operation, toValue(json.value));
}
}
}
const AsyncIterator = Object.getPrototypeOf(async function* () {}).constructor;
class QueryListIterator<T = unknown> extends AsyncIterator
implements Deno.KvListIterator<T> {
#iterator: Deno.KvListIterator<T>;
#query: Filter[];
get cursor(): string {
return this.#iterator.cursor;
}
constructor(iterator: Deno.KvListIterator<T>, query: Filter[]) {
super();
this.#iterator = iterator;
this.#query = query;
}
async next(): Promise<IteratorResult<Deno.KvEntry<T>, undefined>> {
for await (const entry of this.#iterator) {
if (this.#query.every((f) => f.test(entry.value))) {
return { value: entry, done: false };
}
}
return { value: undefined, done: true };
}
[Symbol.asyncIterator](): AsyncIterableIterator<Deno.KvEntry<T>> {
return this;
}
}
/**
* Query instance for filtering entries from a {@linkcode Deno.Kv} instance.
*/
export class Query<T = unknown> implements QueryLike<T> {
#kv: Deno.Kv;
#selector: Deno.KvListSelector;
#options: Deno.KvListOptions;
#query: Filter[] = [];
/**
* The selector that is used to query the entries.
*/
get selector(): Deno.KvListSelector {
return { ...this.#selector };
}
constructor(
kv: Deno.Kv,
selector: Deno.KvListSelector,
options: Deno.KvListOptions = {},
) {
this.#kv = kv;
this.#selector = selector;
this.#options = options;
}
/**
* Resolves with an array of unique sub keys/prefixes for the provided prefix
* along with the number of sub keys that match that prefix. The `count`
* represents the number of sub keys, a value of `0` indicates that only the
* exact key exists with no sub keys.
*
* This is useful when storing keys and values in a hierarchical/tree view,
* where you are retrieving a list including counts and you want to know all
* the unique _descendants_ of a key in order to be able to enumerate them.
*
* @example
*
* If you had the following keys stored in a datastore and the query matched
* the keys:
*
* ```
* ["a", "b"]
* ["a", "b", "c"]
* ["a", "d", "e"]
* ["a", "d", "f"]
* ```
*
* And you would get the following results when using `.counts()`:
*
* ```ts
* import { query } from "@kitsonk/kv-toolbox/query";
*
* const kv = await Deno.openKv();
* console.log(await query(kv, { prefix: ["a"] }).counts());
* // { key: ["a", "b"], count: 1 }
* // { key: ["a", "d"], count: 2 }
* await kv.close();
* ```
*/
counts(): Promise<UniqueCountElement[]> {
return uniqueCount(this);
}
/**
* Get the entries that match the query conditions.
*/
get(): Deno.KvListIterator<T> {
return new QueryListIterator<T>(
this.#kv.list<T>(this.#selector, this.#options),
this.#query,
);
}
/**
* Return an array of keys that match the query.
*
* @example
*
* ```ts
* import { query } from "@kitsonk/kv-toolbox/query";
*
* const kv = await Deno.openKv();
* console.log(await query(kv, { prefix: ["hello"] }).keys());
* await kv.close();
* ```
*/
keys(): Promise<Deno.KvKey[]> {
return keys(this);
}
/**
* Query a Deno KV store for keys and resolve with any matching keys
* organized into a tree structure.
*
* Each child node indicates if it also has a value and any children of that
* node.
*
* @example
*
* If you had the following keys stored in a datastore and the query matched
* the values of all the entries:
*
* ```
* ["a", "b"]
* ["a", "b", "c"]
* ["a", "d", "e"]
* ["a", "d", "f"]
* ```
*
* And you would get the following results when using `.tree()`:
*
* ```ts
* import { query } from "@kitsonk/kv-toolbox/query";
*
* const kv = await Deno.openKv();
* console.log(await query(kv, { prefix: ["a"] }).tree());
* // {
* // prefix: ["a"],
* // children: [
* // {
* // part: "b",
* // hasValue: true,
* // children: [{ part: "c", hasValue: true }]
* // }, {
* // part: "d",
* // children: [
* // { part: "e", hasValue: true },
* // { part: "f", hasValue: true }
* // ]
* // }
* // ]
* // }
* await kv.close();
* ```
*/
tree(): Promise<KeyTree> {
return tree(this);
}
/**
* Resolves with an array of unique sub keys/prefixes for the matched query.
*
* This is useful when storing keys and values in a hierarchical/tree view,
* where you are retrieving a list and you want to know all the unique
* _descendants_ of a key in order to be able to enumerate them.
*
* @example
*
* The following keys stored in a datastore that matched the query:
*
* ```
* ["a", "b"]
* ["a", "b", "c"]
* ["a", "d", "e"]
* ["a", "d", "f"]
* ```
*
* The following results when using `.unique()`:
*
* ```ts
* import { query } from "@kitsonk/kv-toolbox/query";
*
* const kv = await Deno.openKv();
* console.log(await query(kv, { prefix: ["a"] }).unique());
* // ["a", "b"]
* // ["a", "d"]
* await kv.close();
* ```
*/
unique(): Promise<Deno.KvKey[]> {