Skip to main content
Module

x/kvdex/mod.ts>Collection

Database wrapper for Deno KV
Go to Latest
class Collection
Re-export
import { Collection } from "https://deno.land/x/kvdex@v0.3.2/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, collectionKey: KvKey)

Create a new collection for handling documents in the KV store.

Properties

protected
kv: Deno.Kv
readonly
keys: CollectionKeys

Methods

add(data: T)

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

Example:

const result = await db.users.add({
  username: "oli",
  age: 24
})
addMany(...entries: T[])

Adds multiple documents to the KV store.

Example:

// Adds 5 new document entries to the KV store.
await result = await db.numbers.add(1, 2, 3, 4, 5)

// Will fail, as "username" is defined as a primary index and cannot have duplicates
await result = await db.users.add(
  {
    username: "oli",
    age: 24
  },
  {
    username: "oli",
    age: 56
  }
)

Deletes a document with the given id from the KV store.

Example:

await db.users.delete("oliver")

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

If no options are given, all documents are deleted.

Example:

// Delete all
await db.users.deleteMany()

// Delete only users with username = "oli"
await db.users.deleteMany({
  filter: doc => doc.value.username === "oli"
})
find(id: KvId, options?: FindOptions)

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

Example:

const userDoc1 = await db.users.find("user1")

const userDoc2 = await db.users.find("user2", {
  consistency: "eventual" // "strong" by default
})
findMany(ids: KvId[], options?: FindManyOptions)

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

Example:

const userDocs1 = await db.users.findMany(["user1", "user2", "user3"])

const userDocs2 = await db.users.findMany(["user1", "user2", "user3"], {
  consistency: "eventual" // "strong" by default
})
forEach(fn: (doc: Document<T>) => void, options?: ListOptions<T>)

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.

Example:

// Print all usernames
await db.users.forEach(doc => console.log(doc.value.username))

// Print all usernames of users with age < 18
await db.users.forEach(doc => console.log(doc.value.username), {
  filter: doc => doc.value.age < 18
})
getMany(options?: ListOptions<T>)

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

If no options are given, all documents are retrieved.

Example:

// Get all users
const userDocs1 = await db.users.getMany()

// Only get users with username that starts with "a"
const userDocs2 = await db.users.getMany({
  filter: doc => doc.value.username.startsWith("a")
})
set(id: KvId, data: T)

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

Example:

const result = await db.users.add("oliver", {
  username: "oli",
  age: 24
})
update<TId extends KvId>(id: TId, data: UpdateData<T>): Promise<CommitResult<T, TId>>

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.

Example:

const result1 = await db.numbers.update("num1", 10)

const result2 = await db.users.update("oliver", {
  age: 30 // Partial update, only updates the age field
})