import { IndexableCollection } from "https://deno.land/x/kvdex@v0.11.0/mod.ts";
Represents a collection of object documents stored in the KV store.
Contains methods for working on documents in a collection, including methods exclusive to indexable collections.
Type Parameters
Properties
Methods
Perform operations on lists of documents in the collection by secondary index.
PROTECTED METHODS
Delete a document by a primary index.
Example:
// Deletes user with username = "oliver"
await db.users.deleteByPrimaryIndex("username", "oliver")
Delete documents by a secondary index. The method takes an optional options argument that can be used for filtering of documents, and pagination.
Example:
// Deletes all users with age = 24
await db.users.deleteBySecondaryIndex("age", 24)
// Deletes all users with age = 24 AND username that starts with "o"
await db.users.deleteBySecondaryIndex("age", 24, {
filter: (doc) => doc.value.username.startsWith("o")
})
Find a document by a primary index.
Example:
// Finds a user document with the username = "oli"
const userDoc = await db.users.findByPrimaryIndex("username", "oli")
Find documents by a secondary index. Secondary indices are not unique, and therefore the result is an array of documents. The method takes an optional options argument that can be used for filtering of documents, and pagination.
Example:
// Returns all users with age = 24
const { result } = await db.users.findBySecondaryIndex("age", 24)
// Returns all users with age = 24 AND username that starts with "o"
const { result } = await db.users.findBySecondaryIndex("age", 24, {
filter: (doc) => doc.value.username.startsWith("o")
})
Update a document by a primary index.
Example:
// Updates a user with username = "oliver" to have age = 56
const result = await db.users.updateByPrimaryIndex("username", "oliver", { age: 56 })
Update documents in the collection by a secondary index.
Example:
// Updates all user documents with age = 24 and sets age = 67
const { result } = await db.users.updateBySecondaryIndex("age", 24, { age: 67 })
// Updates all user documents where the user's age is 24 and username starts with "o"
const { result } = await db.users.updateBySecondaryIndex(
"age",
24,
{ age: 67 },
{
filter: (doc) => doc.value.username.startsWith("o"),
}
)