import { Collection } from "https://deno.land/x/kvdex@v0.3.0/mod.ts";
Represents a collection of documents stored in the KV store.
Contains methods for working on documents in the collection.
Type Parameters
Properties
Methods
Adds a new document to the KV store with a randomely generated id.
Example:
const result = await db.users.add({
username: "oli",
age: 24
})
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 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"
})
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
})
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
})
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
})
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")
})
Adds a new document with the given id to the KV store.
Example:
const result = await db.users.add("oliver", {
username: "oli",
age: 24
})
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
})