Skip to main content
Module

x/kvdex/mod.ts>Collection

High-level abstraction layer for Deno KV 🦕📁
Go to Latest
class Collection
Re-export
import { Collection } from "https://deno.land/x/kvdex@v0.18.4/mod.ts";

Represents a collection of documents stored in the KV store.

Contains methods for working on documents in the collection.

Constructors

new
Collection(
kv: Deno.Kv,
key: KvKey,
model: Model<T1>,
queueHandlers: Map<string, QueueMessageHandler<QueueValue>[]>,
idempotentListener: () => void,
options?: T2,
)

Properties

private
idempotentListener: () => void
private
queueHandlers: Map<string, QueueMessageHandler<QueueValue>[]>
protected
kv: Deno.Kv
readonly
_idGenerator: IdGenerator<KvValue>
readonly
_keys: CollectionKeys
readonly
_model: Model<T1>

Methods

protected
handleMany<T>(
prefixKey: KvKey,
fn: (doc: Document<T1>) => T,
options: ListOptions<T1> | undefined,
)

Perform operations on lists of documents in the collection.

protected
setDocument(
id: KvId | null,
value: T1,
options: SetOptions | undefined,
overwrite?,
): Promise<CommitResult<T1> | Deno.KvCommitError>

Set a document entry in the KV store.

protected
updateDocument(
doc: Document<T1>,
data: UpdateData<T1>,
options: SetOptions | undefined,
)

Update a document with new data.

add(value: T1, options?: SetOptions)

Adds a new document to the KV store with a randomely generated id.

addMany(values: T1[], options?: SetOptions)

Adds multiple documents to the KV store.

count(options?: CountOptions<T1>)

Counts the number of documents in the collection.

delete(...ids: KvId[])

Deletes one or more documents with the given ids from the KV store.

Deletes multiple documents from the KV store according to the given options.

If no options are given, all documents are deleted.

Delete an undelivered document entry by id from the collection queue.

enqueue<T extends QueueValue>(data: T, options?: EnqueueOptions)

Add data to the collection queue to be delivered to the queue listener via db.collection.listenQueue(). The data will only be received by queue listeners on the specified collection and topic. The method takes an optional options argument that can be used to set a delivery delay and topic.

find(id: KvId, options?: FindOptions)

Finds a document with the given id in the KV store.

findMany(ids: KvId[], options?: FindManyOptions)

Finds multiple documents with the given array of ids in the KV store.

findUndelivered<T extends KvValue = KvValue>(id: KvId, options?: FindOptions)

Find an undelivered document entry by id from the collection queue.

forEach(fn: (doc: Document<T1>) => void, options?: ListOptions<T1>)

Executes a callback function for every document according to the given options.

If no options are given, the callback function is executed for all documents in the collection.

getMany(options?: ListOptions<T1>)

Retrieves multiple documents from the KV store according to the given options.

If no options are given, all documents are retrieved.

Listen for data from the collection queue that was enqueued with db.collection.enqueue(). Will only receive data that was enqueued to the specific collection queue. Takes a handler function as argument.

map<T>(fn: (doc: Document<T1>) => T, options?: ListOptions<T1>)

Executes a callback function for every document according to the given options.

If no options are given, the callback function is executed for all documents in the collection.

The results from the callback function are returned as a list.

set(
id: KvId,
data: T1,
options?: SetOptions,
)

Adds a new document with the given id to the KV store.

update(
id: KvId,
data: UpdateData<T1>,
options?: SetOptions,
): Promise<CommitResult<T1> | Deno.KvCommitError>

Updates a document with the given id in the KV store.

For primitive values, arrays and built-in objects, this method overrides the old value with the new one.

For custom object types, this method merges the new data with the exisiting data.

Update the value of multiple existing documents in the collection.

write(
id: KvId,
value: T1,
options?: SetOptions,
)

Write a document to the KV store.

Sets a new document entry if no matching id already exists, overwrites the exisiting entry if it exists.

Does not overwrite existing entries if there is a primary index collision.