1
1
# kvdex
2
2
3
- ` kvdex ` is an ORM-like wrapper for Deno KV with zero third-party dependencies.
4
- It's purpose is to enhance the experience of using Deno's KV store through
5
- additional features such as indexing and typed collections, while maintaining as
6
- much of the native functionality as possible, such as atomic operations.
3
+ ` kvdex ` is a high level abstraction layer for Deno KV with zero third-party
4
+ dependencies. It's purpose is to enhance the experience of using Deno's KV store
5
+ through additional features such as indexing, and strongly typed collections,
6
+ and cron jobs, while maintaining as much of the native functionality as
7
+ possible, like atomic operations and queue listeners.
7
8
8
9
## Highlights
9
10
10
11
- CRUD operations for selected and ranged documents with strong typing.
11
12
- Primary (unique) and secondary (non-unique) indexing.
12
13
- Segmented storage for large objects that exceed the native size limit.
13
14
- Support for pagination and filtering.
14
- - Message queues at database and collection level.
15
+ - Repeating cron jobs.
16
+ - Message queues at database and collection level with topics.
15
17
- Support for atomic operations.
16
18
17
19
## Table of Contents
@@ -53,6 +55,7 @@ much of the native functionality as possible, such as atomic operations.
53
55
- [ enqueue()] ( #enqueue-1 )
54
56
- [ listenQueue()] ( #listenqueue-1 )
55
57
- [ findUndelivered()] ( #findundelivered-1 )
58
+ - [ cron()] ( #cron )
56
59
- [ atomic()] ( #atomic )
57
60
- [ Atomic Operations] ( #atomic-operations )
58
61
- [ Without checking] ( #without-checking )
@@ -432,26 +435,28 @@ const count = await db.users.count({
432
435
433
436
### enqueue()
434
437
435
- Add data (of any type) to the collection queue to be delivered to the queue
436
- listener via ` db.collection.listenQueue() ` . The data will only be received by
437
- queue listeners on the specified collection. The method takes an optional
438
- options argument that can be used to set a delivery delay.
438
+ Add data to the collection queue to be delivered to the queue listener via
439
+ ` db.collection.listenQueue() ` . The data will only be received by queue listeners
440
+ on the specified collection and topic . The method takes an optional options
441
+ argument that can be used to set a delivery delay and topic .
439
442
440
443
``` ts
441
444
// Immediate delivery
442
445
await db .users .enqueue (" some data" )
443
446
444
447
// Delay of 2 seconds before delivery
445
- await db .users .enqueue (" some data " , {
448
+ await db .users .enqueue (" cake " , {
446
449
delay: 2_000 ,
450
+ topic: " food" ,
447
451
})
448
452
```
449
453
450
454
### listenQueue()
451
455
452
456
Listen for data from the collection queue that was enqueued with
453
457
` db.collection.enqueue() ` . Will only receive data that was enqueued to the
454
- specific collection queue. Takes a handler function as argument.
458
+ specific collection queue and topic. Expects a handler function as argument, as
459
+ well as optional options that can be used to set the topic.
455
460
456
461
``` ts
457
462
// Prints the data to console when recevied
@@ -463,11 +468,11 @@ db.users.listenQueue(async (data) => {
463
468
464
469
const res = await fetch (" ..." , {
465
470
method: " POST" ,
466
- body: dataBody ,
471
+ body: data ,
467
472
})
468
473
469
474
console .log (" POSTED:" , dataBody , res .ok )
470
- })
475
+ }, { topic: " posts " } )
471
476
```
472
477
473
478
## Indexable Collection Methods
@@ -594,26 +599,28 @@ await db.deleteAll()
594
599
595
600
### enqueue()
596
601
597
- Add data (of any type) to the database queue to be delivered to the queue
598
- listener via ` db.listenQueue() ` . The data will only be received by queue
599
- listeners on the database queue . The method takes an optional options argument
600
- that can be used to set a delivery delay.
602
+ Add data to the database queue to be delivered to the queue listener via
603
+ ` db.listenQueue() ` . The data will only be received by queue listeners on the
604
+ database queue and specified topic . The method takes an optional options
605
+ argument that can be used to set a delivery delay and topic .
601
606
602
607
``` ts
603
608
// Immediate delivery
604
609
await db .enqueue (" some data" )
605
610
606
611
// Delay of 2 seconds before delivery
607
- await db .enqueue (" some data " , {
612
+ await db .enqueue (" cake " , {
608
613
delay: 2_000 ,
614
+ topic: " food" ,
609
615
})
610
616
```
611
617
612
618
### listenQueue()
613
619
614
620
Listen for data from the database queue that was enqueued with ` db.enqueue() ` .
615
- Will only receive data that was enqueued to the database queue. Takes a handler
616
- function as argument.
621
+ Will only receive data that was enqueued to the database queue and specified
622
+ topic. Expects a handler function as argument, as well as optional options that
623
+ can be used to set the topic.
617
624
618
625
``` ts
619
626
// Prints the data to console when recevied
@@ -625,11 +632,11 @@ db.listenQueue(async (data) => {
625
632
626
633
const res = await fetch (" ..." , {
627
634
method: " POST" ,
628
- body: dataBody ,
635
+ body: data ,
629
636
})
630
637
631
638
console .log (" POSTED:" , dataBody , res .ok )
632
- })
639
+ }, { topic: " posts " } )
633
640
```
634
641
635
642
### findUndelivered()
@@ -646,6 +653,26 @@ const doc2 = await db.findUndelivered("undelivered_id", {
646
653
})
647
654
```
648
655
656
+ ### cron()
657
+
658
+ Create a cron job that will run on interval, either indefinitely or until an
659
+ exit condition is met. If no interval is set, the next job will run immediately
660
+ after the previous has finished. Like with queue listeners, there can be
661
+ multiple cron jobs defined.
662
+
663
+ ``` ts
664
+ // Will repeat indeefinitely without delay
665
+ db .cron (() => console .log (" Hello World!" ))
666
+
667
+ // First job starts with a 10 second delay, after that there is a 5 second delay between jobs
668
+ // Will terminate after the 10th run (count starts at 0), or if the job returns n < 0.25
669
+ db .cron (() => Math .random (), {
670
+ startDelay: 10_000 ,
671
+ interval: 5_000 ,
672
+ exit : ({ count , result }) => count >= 10 || result < 0.25 ,
673
+ })
674
+ ```
675
+
649
676
### atomic()
650
677
651
678
Initiate an atomic operation. The method takes a selection function as argument
0 commit comments