Skip to content

Commit 4302ee1

Browse files
Merge pull request #80 from oliver-oloughlin/feature/update-readme
Feature/update readme
2 parents 9b2a46b + 82d939e commit 4302ee1

File tree

4 files changed

+189
-181
lines changed

4 files changed

+189
-181
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,8 @@ while (!result || !result.ok) {
752752

753753
## Document Methods
754754

755-
Document functions.
755+
These are methods on the Document object which perform actions/mutations on the
756+
document contents.
756757

757758
### flat()
758759

mod.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export {
77
} from "./src/collection_builder.ts"
88

99
// Expose classes
10+
export { KvDex } from "./src/kvdex.ts"
1011
export { Collection } from "./src/collection.ts"
1112
export { IndexableCollection } from "./src/indexable_collection.ts"
1213
export { LargeCollection } from "./src/large_collection.ts"

src/kvdex.ts

+131-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {
22
CollectionOptions,
3+
CollectionSelector,
34
CountAllOptions,
4-
DB,
55
DeleteAllOptions,
66
EnqueueOptions,
77
FindOptions,
@@ -57,16 +57,137 @@ import { KVDEX_KEY_PREFIX, UNDELIVERED_KEY_PREFIX } from "./constants.ts"
5757
export function kvdex<const T extends SchemaDefinition>(
5858
kv: Deno.Kv,
5959
schemaDefinition: T,
60-
): DB<T> {
60+
) {
6161
const schema = _createSchema(schemaDefinition, kv) as Schema<T>
62-
return {
63-
...schema,
64-
atomic: (selector) => new AtomicBuilder(kv, schema, selector(schema)),
65-
countAll: async (opts) => await _countAll(kv, schema, opts),
66-
deleteAll: async (opts) => await _deleteAll(kv, schema, opts),
67-
enqueue: async (data, opts) => await _enqueue(kv, data, opts),
68-
listenQueue: async (handler) => await _listenQueue(kv, handler),
69-
findUndelivered: async (id, opts) => await _findUndelivered(kv, id, opts),
62+
const db = new KvDex(kv, schema)
63+
return Object.assign(db, schema)
64+
}
65+
66+
export class KvDex<const T extends Schema<SchemaDefinition>> {
67+
private kv: Deno.Kv
68+
private schema: T
69+
70+
constructor(kv: Deno.Kv, schema: T) {
71+
this.kv = kv
72+
this.schema = schema
73+
}
74+
75+
/**
76+
* Initiates an atomic operation.
77+
* Takes a selector function as argument which is used to select an initial collection.
78+
*
79+
* @example
80+
* ```ts
81+
* db.atomic(schema => schema.users)
82+
* ```
83+
*
84+
* @param selector - Collection selector function.
85+
* @returns A new AtomicBuilder instance.
86+
*/
87+
atomic<const T1 extends KvValue>(selector: CollectionSelector<T, T1>) {
88+
return new AtomicBuilder(this.kv, this.schema, selector(this.schema))
89+
}
90+
91+
/**
92+
* Count all document entries in the KV store.
93+
*
94+
* @example
95+
* ```ts
96+
* // Returns the total number of documents in the KV store across all collections
97+
* const count = await db.countAll()
98+
* ```
99+
*
100+
* @param options - Count all options, optional.
101+
* @returns Promise resolving to a number representing the total count of documents in the KV store.
102+
*/
103+
async countAll(options?: CountAllOptions) {
104+
return await _countAll(this.kv, this.schema, options)
105+
}
106+
107+
/**
108+
* Delete all document entries in the KV store.
109+
*
110+
* @example
111+
* ```ts
112+
* // Deletes all documents across all collections
113+
* await db.deleteAll()
114+
* ```
115+
* @param options - Delete all options, optional.
116+
* @returns Promise resolving to void.
117+
*/
118+
async deleteAll(options?: DeleteAllOptions) {
119+
return await _deleteAll(this.kv, this.schema, options)
120+
}
121+
122+
/**
123+
* Add data to the database queue to be delivered to the queue listener
124+
* via ``db.listenQueue()``. The data will only be received by queue
125+
* listeners on the database queue. The method takes an optional options
126+
* argument that can be used to set a delivery delay.
127+
*
128+
* @example
129+
* ```ts
130+
* // Immediate delivery
131+
* await db.enqueue("some data")
132+
*
133+
* // Delay of 2 seconds before delivery
134+
* await db.enqueue("some data", {
135+
* delay: 2_000
136+
* })
137+
* ```
138+
*
139+
* @param data - Data to be added to the database queue.
140+
* @param options - Enqueue options, optional.
141+
*/
142+
async enqueue(data: KvValue, options?: EnqueueOptions) {
143+
return await _enqueue(this.kv, data, options)
144+
}
145+
146+
/**
147+
* Listen for data from the database queue that was enqueued with ``db.enqueue()``. Will only receive data that was enqueued to the database queue. Takes a handler function as argument.
148+
*
149+
* @example
150+
* ```ts
151+
* // Prints the data to console when recevied
152+
* db.listenQueue((data) => console.log(data))
153+
*
154+
* // Sends post request when data is received
155+
* db.listenQueue(async (data) => {
156+
* const dataBody = JSON.stringify(data)
157+
*
158+
* const res = await fetch("...", {
159+
* method: "POST",
160+
* body: dataBody
161+
* })
162+
*
163+
* console.log("POSTED:", dataBody, res.ok)
164+
* })
165+
* ```
166+
*
167+
* @param handler - Message handler function.
168+
*/
169+
async listenQueue(handler: QueueMessageHandler<KvValue>) {
170+
return await _listenQueue(this.kv, handler)
171+
}
172+
173+
/**
174+
* Find an undelivered document entry by id from the database queue.
175+
*
176+
* @example
177+
* ```ts
178+
* const doc1 = await db.findUndelivered("undelivered_id")
179+
*
180+
* const doc2 = await db.findUndelivered("undelivered_id", {
181+
* consistency: "eventual", // "strong" by default
182+
* })
183+
* ```
184+
*
185+
* @param id - Document id.
186+
* @param options - Find options, optional.
187+
* @returns Document if found, null if not.
188+
*/
189+
async findUndelivered(id: KvId, options?: FindOptions) {
190+
return await _findUndelivered(this.kv, id, options)
70191
}
71192
}
72193

0 commit comments

Comments
 (0)