Skip to content

Commit c7315f5

Browse files
Merge pull request #83 from oliver-oloughlin/patch/readme-update
updated README and documentation
2 parents d0907b0 + a9f6f5b commit c7315f5

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
`kvdex` is a high level abstraction layer for Deno KV with zero third-party
44
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.
5+
through additional features such as indexing, strongly typed collections, and
6+
cron jobs, while maintaining as much of the native functionality as possible,
7+
like atomic operations and queue listeners.
88

99
## Highlights
1010

1111
- CRUD operations for selected and ranged documents with strong typing.
1212
- Primary (unique) and secondary (non-unique) indexing.
1313
- Segmented storage for large objects that exceed the native size limit.
1414
- Support for pagination and filtering.
15-
- Repeating cron jobs.
15+
- Create repeating cron jobs.
1616
- Message queues at database and collection level with topics.
1717
- Support for atomic operations.
1818

@@ -575,7 +575,7 @@ if you believe your document values will exceed size limit.
575575
## Database Methods
576576

577577
These are methods which can be found at the top level of your database object,
578-
and perform operations across multiple collections.
578+
and perform operations across multiple collections or unrelated to collections.
579579

580580
### countAll()
581581

@@ -626,7 +626,7 @@ can be used to set the topic.
626626
// Prints the data to console when recevied
627627
db.listenQueue((data) => console.log(data))
628628

629-
// Sends post request when data is received
629+
// Sends post request when data is received in the "posts" topic
630630
db.listenQueue(async (data) => {
631631
const dataBody = JSON.stringify(data)
632632

@@ -675,7 +675,7 @@ db.cron(() => Math.random(), {
675675

676676
### atomic()
677677

678-
Initiate an atomic operation. The method takes a selection function as argument
678+
Initiate an atomic operation. The method takes a selector function as argument
679679
for selecting the initial collection context.
680680

681681
```ts

src/atomic_builder.ts

+28
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,34 @@ export class AtomicBuilder<
438438
return this
439439
}
440440

441+
/**
442+
* Add data to the collection queue to be delivered to the queue listener
443+
* via ``db.collection.listenQueue()``. The data will only be received by queue
444+
* listeners on the specified collection and topic. The method takes an optional options
445+
* argument that can be used to set a delivery delay and topic.
446+
*
447+
* @example
448+
* ```ts
449+
* // Immediate delivery
450+
* await db
451+
* .atomic(schema => schema.users)
452+
* .enqueue("soem data")
453+
* .commit()
454+
*
455+
* // Delay of 2 seconds before delivery, sent to the "food" topic
456+
* await db
457+
* .atomic(schema => schema.users)
458+
* .enqueue("cake", {
459+
* delay: 2_000,
460+
* topic: "food"
461+
* })
462+
* .commit()
463+
* ```
464+
*
465+
* @param data - Data to be added to the collection queue.
466+
* @param options - Enqueue options, optional.
467+
* @returns - Promise resolving to Deno.KvCommitResult.
468+
*/
441469
enqueue(data: QueueValue, options?: EnqueueOptions) {
442470
// Prepare and add enqueue operation
443471
const prep = prepareEnqueue(

src/collection.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -532,17 +532,18 @@ export class Collection<
532532
/**
533533
* Add data to the collection queue to be delivered to the queue listener
534534
* via ``db.collection.listenQueue()``. The data will only be received by queue
535-
* listeners on the specified collection. The method takes an optional options
536-
* argument that can be used to set a delivery delay.
535+
* listeners on the specified collection and topic. The method takes an optional options
536+
* argument that can be used to set a delivery delay and topic.
537537
*
538538
* @example
539539
* ```ts
540540
* // Immediate delivery
541541
* await db.users.enqueue("some data")
542542
*
543-
* // Delay of 2 seconds before delivery
544-
* await db.users.enqueue("some data", {
545-
* delay: 2_000
543+
* // Delay of 2 seconds before delivery, sent to the "food" topic
544+
* await db.users.enqueue("cake", {
545+
* delay: 2_000,
546+
* topic: "food"
546547
* })
547548
* ```
548549
*
@@ -572,7 +573,7 @@ export class Collection<
572573
* // Prints the data to console when recevied
573574
* db.users.listenQueue((data) => console.log(data))
574575
*
575-
* // Sends post request when data is received
576+
* // Sends post request when data is received in the "posts" topic
576577
* db.users.listenQueue(async (data) => {
577578
* const dataBody = JSON.stringify(data)
578579
*
@@ -582,7 +583,7 @@ export class Collection<
582583
* })
583584
*
584585
* console.log("POSTED:", dataBody, res.ok)
585-
* })
586+
* }, { topic: "posts" })
586587
* ```
587588
*
588589
* @param handler - Message handler function.

src/kvdex.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,21 @@ export function kvdex<const T extends SchemaDefinition>(
6363
kv: Deno.Kv,
6464
schemaDefinition: T,
6565
) {
66+
// Set listener activated flag and queue handlers map
6667
let listenerIsActivated = false
6768
const queueHandlers = new Map<string, QueueMessageHandler<QueueValue>[]>()
6869

70+
// Create idempotent listener activator
6971
const idempotentListener = () => {
72+
// If listener is already activated, cancel
7073
if (listenerIsActivated) {
7174
return
7275
}
7376

77+
// Set listener activated flag
7478
listenerIsActivated = true
7579

80+
// Add queue listener
7681
kv.listenQueue(async (msg) => {
7782
const parsed = parseQueueMessage(msg)
7883
if (!parsed.ok) {
@@ -86,15 +91,18 @@ export function kvdex<const T extends SchemaDefinition>(
8691
})
8792
}
8893

94+
// Create schema
8995
const schema = _createSchema(
9096
schemaDefinition,
9197
kv,
9298
queueHandlers,
9399
idempotentListener,
94100
) as Schema<T>
95101

102+
// Create KvDex object
96103
const db = new KvDex(kv, schema, queueHandlers, idempotentListener)
97104

105+
// Return schema and db combination
98106
return Object.assign(db, schema)
99107
}
100108

@@ -170,17 +178,18 @@ export class KvDex<const T extends Schema<SchemaDefinition>> {
170178
/**
171179
* Add data to the database queue to be delivered to the queue listener
172180
* via ``db.listenQueue()``. The data will only be received by queue
173-
* listeners on the database queue. The method takes an optional options
174-
* argument that can be used to set a delivery delay.
181+
* listeners on the database queue and specified topic. The method takes an optional options
182+
* argument that can be used to set a delivery delay and topic.
175183
*
176184
* @example
177185
* ```ts
178186
* // Immediate delivery
179187
* await db.enqueue("some data")
180188
*
181189
* // Delay of 2 seconds before delivery
182-
* await db.enqueue("some data", {
183-
* delay: 2_000
190+
* await db.enqueue("cake", {
191+
* delay: 2_000,
192+
* topic: "food"
184193
* })
185194
* ```
186195
*
@@ -206,7 +215,7 @@ export class KvDex<const T extends Schema<SchemaDefinition>> {
206215
* // Prints the data to console when recevied
207216
* db.listenQueue((data) => console.log(data))
208217
*
209-
* // Sends post request when data is received
218+
* // Sends post request when data is received in the "posts" topic
210219
* db.listenQueue(async (data) => {
211220
* const dataBody = JSON.stringify(data)
212221
*
@@ -216,7 +225,7 @@ export class KvDex<const T extends Schema<SchemaDefinition>> {
216225
* })
217226
*
218227
* console.log("POSTED:", dataBody, res.ok)
219-
* })
228+
* }, { topic: "posts" })
220229
* ```
221230
*
222231
* @param handler - Message handler function.

0 commit comments

Comments
 (0)