-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
863 lines (835 loc) · 23.4 KB
/
index.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
import {
DocumentByName,
GenericDataModel,
GenericMutationCtx,
GenericQueryCtx,
TableNamesInDataModel,
} from "convex/server";
import { Key } from "../component/btree.js";
import { api } from "../component/_generated/api.js";
import { UseApi } from "./useApi.js";
import {
Position,
positionToKey,
boundToPosition,
keyToPosition,
Bound,
Bounds,
boundsToPositions,
} from "./positions.js";
import { GenericId, Value as ConvexValue } from "convex/values";
export type UsedAPI = UseApi<typeof api>;
// e.g. `ctx` from a Convex query or mutation or action.
export type RunQueryCtx = {
runQuery: GenericQueryCtx<GenericDataModel>["runQuery"];
};
// e.g. `ctx` from a Convex mutation or action.
export type RunMutationCtx = {
runMutation: GenericMutationCtx<GenericDataModel>["runMutation"];
};
export type Item<K extends Key, ID extends string> = {
key: K;
id: ID;
sumValue: number;
};
export type { Key, Bound };
/**
* Write data to be aggregated, and read aggregated data.
*
* The data structure is effectively a key-value store sorted by key, where the
* value is an ID and an optional sumValue.
* 1. The key can be any Convex value (number, string, array, etc.).
* 2. The ID is a string which should be unique.
* 3. The sumValue is a number which is aggregated by summing. If not provided,
* it's assumed to be zero.
*
* Once values have been added to the data structure, you can query for the
* count and sum of items between a range of keys.
*/
export class Aggregate<
K extends Key,
ID extends string,
Namespace extends ConvexValue | undefined = undefined,
> {
constructor(protected component: UsedAPI) {}
/// Aggregate queries.
/**
* Counts items between the given bounds.
*/
async count(
ctx: RunQueryCtx,
...opts: NamespacedOpts<{ bounds: Bounds<K, ID> }, Namespace>
): Promise<number> {
const { count } = await ctx.runQuery(
this.component.btree.aggregateBetween,
{
...boundsToPositions(opts[0]?.bounds),
namespace: namespaceFromOpts(opts),
}
);
return count;
}
/**
* Adds up the sumValue of items between the given bounds.
*/
async sum(
ctx: RunQueryCtx,
...opts: NamespacedOpts<{ bounds: Bounds<K, ID> }, Namespace>
): Promise<number> {
const { sum } = await ctx.runQuery(this.component.btree.aggregateBetween, {
...boundsToPositions(opts[0]?.bounds),
namespace: namespaceFromOpts(opts),
});
return sum;
}
/**
* Returns the item at the given offset/index/rank in the order of key,
* within the bounds. Zero-indexed, so at(0) is the smallest key within the
* bounds.
*
* If offset is negative, it counts from the end of the list, so at(-1) is the
* item with the largest key within the bounds.
*/
async at(
ctx: RunQueryCtx,
offset: number,
...opts: NamespacedOpts<{ bounds?: Bounds<K, ID> }, Namespace>
): Promise<Item<K, ID>> {
if (offset < 0) {
const item = await ctx.runQuery(this.component.btree.atNegativeOffset, {
offset: -offset - 1,
namespace: namespaceFromOpts(opts),
...boundsToPositions(opts[0]?.bounds),
});
return btreeItemToAggregateItem(item);
}
const item = await ctx.runQuery(this.component.btree.atOffset, {
offset,
namespace: namespaceFromOpts(opts),
...boundsToPositions(opts[0]?.bounds),
});
return btreeItemToAggregateItem(item);
}
/**
* Returns the rank/offset/index of the given key, within the bounds.
* Specifically, it returns the index of the first item with
*
* - key >= the given key if `order` is "asc" (default)
* - key <= the given key if `order` is "desc"
*/
async indexOf(
ctx: RunQueryCtx,
key: K,
...opts: NamespacedOpts<
{ id?: ID; bounds?: Bounds<K, ID>; order?: "asc" | "desc" },
Namespace
>
): Promise<number> {
const { k1, k2 } = boundsToPositions(opts[0]?.bounds);
if (opts[0]?.order === "desc") {
return await ctx.runQuery(this.component.btree.offsetUntil, {
key: boundToPosition("upper", {
key,
id: opts[0]?.id,
inclusive: true,
}),
k2,
namespace: namespaceFromOpts(opts),
});
}
return await ctx.runQuery(this.component.btree.offset, {
key: boundToPosition("lower", { key, id: opts[0]?.id, inclusive: true }),
k1,
namespace: namespaceFromOpts(opts),
});
}
/**
* @deprecated Use `indexOf` instead.
*/
async offsetOf(
ctx: RunQueryCtx,
key: K,
namespace: Namespace,
id?: ID,
bounds?: Bounds<K, ID>
): Promise<number> {
return this.indexOf(ctx, key, { id, bounds, order: "asc", namespace });
}
/**
* @deprecated Use `indexOf` instead.
*/
async offsetUntil(
ctx: RunQueryCtx,
key: K,
namespace: Namespace,
id?: ID,
bounds?: Bounds<K, ID>
): Promise<number> {
return this.indexOf(ctx, key, { id, bounds, order: "desc", namespace });
}
/**
* Gets the minimum item within the given bounds.
*/
async min(
ctx: RunQueryCtx,
...opts: NamespacedOpts<{ bounds: Bounds<K, ID> }, Namespace>
): Promise<Item<K, ID> | null> {
const { page } = await this.paginate(ctx, {
namespace: namespaceFromOpts(opts),
bounds: opts[0]?.bounds,
order: "asc",
pageSize: 1,
});
return page[0] ?? null;
}
/**
* Gets the maximum item within the given bounds.
*/
async max(
ctx: RunQueryCtx,
...opts: NamespacedOpts<{ bounds: Bounds<K, ID> }, Namespace>
): Promise<Item<K, ID> | null> {
const { page } = await this.paginate(ctx, {
namespace: namespaceFromOpts(opts),
bounds: opts[0]?.bounds,
order: "desc",
pageSize: 1,
});
return page[0] ?? null;
}
/**
* Gets a uniformly random item within the given bounds.
*/
async random(
ctx: RunQueryCtx,
...opts: NamespacedOpts<{ bounds: Bounds<K, ID> }, Namespace>
): Promise<Item<K, ID> | null> {
const count = await this.count(ctx, ...opts);
if (count === 0) {
return null;
}
const index = Math.floor(Math.random() * count);
return await this.at(ctx, index, ...opts);
}
/**
* Get a page of items between the given bounds, with a cursor to paginate.
* Use `iter` to iterate over all items within the bounds.
*/
async paginate(
ctx: RunQueryCtx,
...opts: NamespacedOpts<
{
bounds?: Bounds<K, ID>;
cursor?: string;
order?: "asc" | "desc";
pageSize?: number;
},
Namespace
>
): Promise<{ page: Item<K, ID>[]; cursor: string; isDone: boolean }> {
const order = opts[0]?.order ?? "asc";
const pageSize = opts[0]?.pageSize ?? 100;
const {
page,
cursor: newCursor,
isDone,
} = await ctx.runQuery(this.component.btree.paginate, {
namespace: namespaceFromOpts(opts),
...boundsToPositions(opts[0]?.bounds),
cursor: opts[0]?.cursor,
order,
limit: pageSize,
});
return {
page: page.map(btreeItemToAggregateItem<K, ID>),
cursor: newCursor,
isDone,
};
}
/**
* Example usage:
* ```ts
* for await (const item of aggregate.iter(ctx, bounds)) {
* console.log(item);
* }
* ```
*/
async *iter(
ctx: RunQueryCtx,
...opts: NamespacedOpts<
{ bounds?: Bounds<K, ID>; order?: "asc" | "desc"; pageSize?: number },
Namespace
>
): AsyncGenerator<Item<K, ID>, void, undefined> {
const order = opts[0]?.order ?? "asc";
const pageSize = opts[0]?.pageSize ?? 100;
const bounds = opts[0]?.bounds;
const namespace = namespaceFromOpts(opts);
let isDone = false;
let cursor: string | undefined = undefined;
while (!isDone) {
const {
page,
cursor: newCursor,
isDone: newIsDone,
} = await this.paginate(ctx, {
namespace,
bounds,
cursor,
order,
pageSize,
});
for (const item of page) {
yield item;
}
isDone = newIsDone;
cursor = newCursor;
}
}
/** Write operations. See {@link DirectAggregate} for docstrings. */
async _insert(
ctx: RunMutationCtx,
namespace: Namespace,
key: K,
id: ID,
summand?: number
): Promise<void> {
await ctx.runMutation(this.component.public.insert, {
key: keyToPosition(key, id),
summand,
value: id,
namespace,
});
}
async _delete(
ctx: RunMutationCtx,
namespace: Namespace,
key: K,
id: ID
): Promise<void> {
await ctx.runMutation(this.component.public.delete_, {
key: keyToPosition(key, id),
namespace,
});
}
async _replace(
ctx: RunMutationCtx,
currentNamespace: Namespace,
currentKey: K,
newNamespace: Namespace,
newKey: K,
id: ID,
summand?: number
): Promise<void> {
await ctx.runMutation(this.component.public.replace, {
currentKey: keyToPosition(currentKey, id),
newKey: keyToPosition(newKey, id),
summand,
value: id,
namespace: currentNamespace,
newNamespace,
});
}
async _insertIfDoesNotExist(
ctx: RunMutationCtx,
namespace: Namespace,
key: K,
id: ID,
summand?: number
): Promise<void> {
await this._replaceOrInsert(
ctx,
namespace,
key,
namespace,
key,
id,
summand
);
}
async _deleteIfExists(
ctx: RunMutationCtx,
namespace: Namespace,
key: K,
id: ID
): Promise<void> {
await ctx.runMutation(this.component.public.deleteIfExists, {
key: keyToPosition(key, id),
namespace,
});
}
async _replaceOrInsert(
ctx: RunMutationCtx,
currentNamespace: Namespace,
currentKey: K,
newNamespace: Namespace,
newKey: K,
id: ID,
summand?: number
): Promise<void> {
await ctx.runMutation(this.component.public.replaceOrInsert, {
currentKey: keyToPosition(currentKey, id),
newKey: keyToPosition(newKey, id),
summand,
value: id,
namespace: currentNamespace,
newNamespace,
});
}
/// Initialization and maintenance.
/**
* (re-)initialize the data structure, removing all items if it exists.
*
* Change the maxNodeSize if provided, otherwise keep it the same.
* maxNodeSize is how you tune the data structure's width and depth.
* Larger values can reduce write contention but increase read latency.
* Default is 16.
* Set rootLazy = false to eagerly compute aggregates on the root node, which
* improves aggregation latency at the expense of making all writes contend
* with each other, so it's only recommended for read-heavy workloads.
* Default is true.
*/
async clear(
ctx: RunMutationCtx,
...opts: NamespacedOpts<
{ maxNodeSize?: number; rootLazy?: boolean },
Namespace
>
): Promise<void> {
await ctx.runMutation(this.component.public.clear, {
maxNodeSize: opts[0]?.maxNodeSize,
rootLazy: opts[0]?.rootLazy,
namespace: namespaceFromOpts(opts),
});
}
/**
* If rootLazy is false (the default is true but it can be set to false by
* `clear`), the aggregates data structure writes to a single root node on
* every insert/delete/replace, which can cause contention.
*
* If your data structure has frequent writes, you can reduce contention by
* calling makeRootLazy, which removes the frequent writes to the root node.
* With a lazy root node, updates will only contend with other updates to the
* same shard of the tree. The number of shards is determined by maxNodeSize,
* so larger maxNodeSize can also help.
*/
async makeRootLazy(ctx: RunMutationCtx, namespace: Namespace): Promise<void> {
await ctx.runMutation(this.component.public.makeRootLazy, { namespace });
}
async paginateNamespaces(
ctx: RunQueryCtx,
cursor?: string,
pageSize: number = 100
): Promise<{ page: Namespace[]; cursor: string; isDone: boolean }> {
const {
page,
cursor: newCursor,
isDone,
} = await ctx.runQuery(this.component.btree.paginateNamespaces, {
cursor,
limit: pageSize,
});
return {
page: page as Namespace[],
cursor: newCursor,
isDone,
};
}
async *iterNamespaces(
ctx: RunQueryCtx,
pageSize: number = 100
): AsyncGenerator<Namespace, void, undefined> {
let isDone = false;
let cursor: string | undefined = undefined;
while (!isDone) {
const {
page,
cursor: newCursor,
isDone: newIsDone,
} = await this.paginateNamespaces(ctx, cursor, pageSize);
for (const item of page) {
yield item;
}
isDone = newIsDone;
cursor = newCursor;
}
}
async clearAll(
ctx: RunMutationCtx & RunQueryCtx,
opts?: { maxNodeSize?: number; rootLazy?: boolean }
): Promise<void> {
for await (const namespace of this.iterNamespaces(ctx)) {
await this.clear(ctx, { ...opts, namespace });
}
// In case there are no namespaces, make sure we create at least one tree,
// at namespace=undefined. This is where the default settings are stored.
await this.clear(ctx, { ...opts, namespace: undefined as Namespace });
}
async makeAllRootsLazy(ctx: RunMutationCtx & RunQueryCtx): Promise<void> {
for await (const namespace of this.iterNamespaces(ctx)) {
await this.makeRootLazy(ctx, namespace);
}
}
}
export type DirectAggregateType<
K extends Key,
ID extends string,
Namespace extends ConvexValue | undefined,
> = {
Key: K;
Id: ID;
Namespace: Namespace;
};
type AnyDirectAggregateType = DirectAggregateType<
Key,
string,
ConvexValue | undefined
>;
/**
* A DirectAggregate is an Aggregate where you can insert, delete, and replace
* items directly, and keys and IDs can be customized.
*
* Contrast with TableAggregate, which follows a table with Triggers and
* computes keys and sumValues from the table's documents.
*/
export class DirectAggregate<
T extends AnyDirectAggregateType,
> extends Aggregate<T["Key"], T["Id"], T["Namespace"]> {
/**
* Insert a new key into the data structure.
* The id should be unique.
* If not provided, the sumValue is assumed to be zero.
* If the tree does not exist yet, it will be initialized with the default
* maxNodeSize and lazyRoot=true.
* If the [key, id] pair already exists, this will throw.
*/
async insert(
ctx: RunMutationCtx,
args: NamespacedArgs<
{ key: T["Key"]; id: T["Id"]; sumValue?: number },
T["Namespace"]
>
): Promise<void> {
await this._insert(
ctx,
namespaceFromArg(args),
args.key,
args.id,
args.sumValue
);
}
/**
* Delete the key with the given ID from the data structure.
* Throws if the given key and ID do not exist.
*/
async delete(
ctx: RunMutationCtx,
args: NamespacedArgs<{ key: T["Key"]; id: T["Id"] }, T["Namespace"]>
): Promise<void> {
await this._delete(ctx, namespaceFromArg(args), args.key, args.id);
}
/**
* Update an existing item in the data structure.
* This is effectively a delete followed by an insert, but it's performed
* atomically so it's impossible to view the data structure with the key missing.
*/
async replace(
ctx: RunMutationCtx,
currentItem: NamespacedArgs<{ key: T["Key"]; id: T["Id"] }, T["Namespace"]>,
newItem: NamespacedArgs<
{ key: T["Key"]; sumValue?: number },
T["Namespace"]
>
): Promise<void> {
await this._replace(
ctx,
namespaceFromArg(currentItem),
currentItem.key,
namespaceFromArg(newItem),
newItem.key,
currentItem.id,
newItem.sumValue
);
}
/**
* Equivalents to `insert`, `delete`, and `replace` where the item may or may not exist.
* This can be useful for live backfills:
* 1. Update live writes to use these methods to write into the new Aggregate.
* 2. Run a background backfill, paginating over existing data, calling `insertIfDoesNotExist` on each item.
* 3. Once the backfill is complete, use `insert`, `delete`, and `replace` for live writes.
* 4. Begin using the Aggregate read methods.
*/
async insertIfDoesNotExist(
ctx: RunMutationCtx,
args: NamespacedArgs<
{ key: T["Key"]; id: T["Id"]; sumValue?: number },
T["Namespace"]
>
): Promise<void> {
await this._insertIfDoesNotExist(
ctx,
namespaceFromArg(args),
args.key,
args.id,
args.sumValue
);
}
async deleteIfExists(
ctx: RunMutationCtx,
args: NamespacedArgs<{ key: T["Key"]; id: T["Id"] }, T["Namespace"]>
): Promise<void> {
await this._deleteIfExists(ctx, namespaceFromArg(args), args.key, args.id);
}
async replaceOrInsert(
ctx: RunMutationCtx,
currentItem: NamespacedArgs<{ key: T["Key"]; id: T["Id"] }, T["Namespace"]>,
newItem: NamespacedArgs<
{ key: T["Key"]; sumValue?: number },
T["Namespace"]
>
): Promise<void> {
await this._replaceOrInsert(
ctx,
namespaceFromArg(currentItem),
currentItem.key,
namespaceFromArg(newItem),
newItem.key,
currentItem.id,
newItem.sumValue
);
}
}
export type TableAggregateType<
K extends Key,
DataModel extends GenericDataModel,
TableName extends TableNamesInDataModel<DataModel>,
Namespace extends ConvexValue | undefined,
> = {
Key: K;
DataModel: DataModel;
TableName: TableName;
Namespace: Namespace;
};
type AnyTableAggregateType = TableAggregateType<
Key,
GenericDataModel,
TableNamesInDataModel<GenericDataModel>,
ConvexValue | undefined
>;
type TableAggregateDocument<T extends AnyTableAggregateType> = DocumentByName<
T["DataModel"],
T["TableName"]
>;
type TableAggregateId<T extends AnyTableAggregateType> = GenericId<
T["TableName"]
>;
type TableAggregateTrigger<Ctx, T extends AnyTableAggregateType> = Trigger<
Ctx,
T["DataModel"],
T["TableName"]
>;
export class TableAggregate<T extends AnyTableAggregateType> extends Aggregate<
T["Key"],
GenericId<T["TableName"]>,
T["Namespace"]
> {
constructor(
component: UsedAPI,
private options: {
namespace: (d: TableAggregateDocument<T>) => T["Namespace"];
sortKey: (d: TableAggregateDocument<T>) => T["Key"];
sumValue?: (d: TableAggregateDocument<T>) => number;
}
) {
super(component);
}
async insert(
ctx: RunMutationCtx,
doc: TableAggregateDocument<T>
): Promise<void> {
await this._insert(
ctx,
this.options.namespace(doc),
this.options.sortKey(doc),
doc._id as TableAggregateId<T>,
this.options.sumValue?.(doc)
);
}
async delete(
ctx: RunMutationCtx,
doc: TableAggregateDocument<T>
): Promise<void> {
await this._delete(
ctx,
this.options.namespace(doc),
this.options.sortKey(doc),
doc._id as TableAggregateId<T>
);
}
async replace(
ctx: RunMutationCtx,
oldDoc: TableAggregateDocument<T>,
newDoc: TableAggregateDocument<T>
): Promise<void> {
await this._replace(
ctx,
this.options.namespace(oldDoc),
this.options.sortKey(oldDoc),
this.options.namespace(newDoc),
this.options.sortKey(newDoc),
newDoc._id as TableAggregateId<T>,
this.options.sumValue?.(newDoc)
);
}
async insertIfDoesNotExist(
ctx: RunMutationCtx,
doc: TableAggregateDocument<T>
): Promise<void> {
await this._insertIfDoesNotExist(
ctx,
this.options.namespace(doc),
this.options.sortKey(doc),
doc._id as TableAggregateId<T>,
this.options.sumValue?.(doc)
);
}
async deleteIfExists(
ctx: RunMutationCtx,
doc: TableAggregateDocument<T>
): Promise<void> {
await this._deleteIfExists(
ctx,
this.options.namespace(doc),
this.options.sortKey(doc),
doc._id as TableAggregateId<T>
);
}
async replaceOrInsert(
ctx: RunMutationCtx,
oldDoc: TableAggregateDocument<T>,
newDoc: TableAggregateDocument<T>
): Promise<void> {
await this._replaceOrInsert(
ctx,
this.options.namespace(oldDoc),
this.options.sortKey(oldDoc),
this.options.namespace(newDoc),
this.options.sortKey(newDoc),
newDoc._id as TableAggregateId<T>,
this.options.sumValue?.(newDoc)
);
}
/**
* Returns the rank/offset/index of the given document, within the bounds.
* This differs from `indexOf` in that it take the document rather than key.
* Specifically, it returns the index of the first item with
*
* - key >= the given doc's key if `order` is "asc" (default)
* - key <= the given doc's key if `order` is "desc"
*/
async indexOfDoc(
ctx: RunQueryCtx,
doc: TableAggregateDocument<T>,
opts?: {
id?: TableAggregateId<T>;
bounds?: Bounds<T["Key"], TableAggregateId<T>>;
order?: "asc" | "desc";
}
): Promise<number> {
const key = this.options.sortKey(doc);
return this.indexOf(ctx, key, {
namespace: this.options.namespace(doc),
...opts,
});
}
trigger<Ctx extends RunMutationCtx>(): TableAggregateTrigger<Ctx, T> {
return async (ctx, change) => {
if (change.operation === "insert") {
await this.insert(ctx, change.newDoc);
} else if (change.operation === "update") {
await this.replace(ctx, change.oldDoc, change.newDoc);
} else if (change.operation === "delete") {
await this.delete(ctx, change.oldDoc);
}
};
}
idempotentTrigger<Ctx extends RunMutationCtx>(): TableAggregateTrigger<
Ctx,
T
> {
return async (ctx, change) => {
if (change.operation === "insert") {
await this.insertIfDoesNotExist(ctx, change.newDoc);
} else if (change.operation === "update") {
await this.replaceOrInsert(ctx, change.oldDoc, change.newDoc);
} else if (change.operation === "delete") {
await this.deleteIfExists(ctx, change.oldDoc);
}
};
}
}
export type Trigger<
Ctx,
DataModel extends GenericDataModel,
TableName extends TableNamesInDataModel<DataModel>,
> = (ctx: Ctx, change: Change<DataModel, TableName>) => Promise<void>;
export type Change<
DataModel extends GenericDataModel,
TableName extends TableNamesInDataModel<DataModel>,
> = {
id: GenericId<TableName>;
} & (
| {
operation: "insert";
oldDoc: null;
newDoc: DocumentByName<DataModel, TableName>;
}
| {
operation: "update";
oldDoc: DocumentByName<DataModel, TableName>;
newDoc: DocumentByName<DataModel, TableName>;
}
| {
operation: "delete";
oldDoc: DocumentByName<DataModel, TableName>;
newDoc: null;
}
);
export function btreeItemToAggregateItem<K extends Key, ID extends string>({
k,
s,
}: {
k: unknown;
s: number;
}): Item<K, ID> {
const { key, id } = positionToKey(k as Position);
return {
key: key as K,
id: id as ID,
sumValue: s,
};
}
export type NamespacedArgs<Args, Namespace> =
| (Args & { namespace: Namespace })
| (Namespace extends undefined ? Args : never);
export type NamespacedOpts<Opts, Namespace> =
| [{ namespace: Namespace } & Opts]
| (Namespace extends undefined ? [Opts?] : never);
function namespaceFromArg<Args extends object, Namespace>(
args: NamespacedArgs<Args, Namespace>
): Namespace {
if ("namespace" in args) {
return args["namespace"];
}
return undefined as Namespace;
}
function namespaceFromOpts<Opts, Namespace>(
opts: NamespacedOpts<Opts, Namespace>
): Namespace {
if (opts.length === 0) {
// Only possible if Namespace extends undefined, so undefined is the only valid namespace.
return undefined as Namespace;
}
const [{ namespace }] = opts as [{ namespace: Namespace }];
return namespace;
}