Skip to main content
Module

x/kvdex/mod.ts>IndexableCollection

Database wrapper for Deno KV
Go to Latest
class IndexableCollection
extends Collection<T1, T2>
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.

Constructors

new
IndexableCollection(
kv: Deno.Kv,
key: KvKey,
options: T2,
)

Create a new IndexableCollection for handling object documents in the KV store.

Example:

const kv = await Deno.openKv()
const users = new IndexableCollection<User>(kv, ["users"], {
  indices: {
    username: "primary",
    age: "secondary"
  }
})

Properties

readonly
primaryIndexList: string[]
readonly
secondaryIndexList: string[]

Methods

protected
handleBySecondaryIndex<K extends SecondaryIndexKeys<T1, T2["indices"]>>(
index: K,
value: CheckKeyOf<K, T1>,
fn: (doc: Document<T1>) => unknown,
options?: ListOptions<T1>,
)

Perform operations on lists of documents in the collection by secondary index.

protected
updateDocument(doc: Document<T1>, data: UpdateData<T1>): Promise<CommitResult<T1>>

PROTECTED METHODS

delete(...ids: KvId[])
deleteByPrimaryIndex<K extends PrimaryIndexKeys<T1, T2["indices"]>>(
index: K,
value: CheckKeyOf<K, T1>,
options?: FindOptions,
)

Delete a document by a primary index.

Example:

// Deletes user with username = "oliver"
await db.users.deleteByPrimaryIndex("username", "oliver")
deleteBySecondaryIndex<K extends SecondaryIndexKeys<T1, T2["indices"]>>(
index: K,
value: CheckKeyOf<K, T1>,
options?: ListOptions<T1>,
)

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")
})
findByPrimaryIndex<K extends PrimaryIndexKeys<T1, T2["indices"]>>(
index: K,
value: CheckKeyOf<K, T1>,
options?: FindOptions,
)

Find a document by a primary index.

Example:

// Finds a user document with the username = "oli"
const userDoc = await db.users.findByPrimaryIndex("username", "oli")
findBySecondaryIndex<K extends SecondaryIndexKeys<T1, T2["indices"]>>(
index: K,
value: CheckKeyOf<K, T1>,
options?: ListOptions<T1>,
)

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")
})
set(id: KvId, data: T1)
updateByPrimaryIndex<K extends PrimaryIndexKeys<T1, T2["indices"]>>(
index: K,
value: CheckKeyOf<K, T1>,
data: UpdateData<T1>,
): Promise<CommitResult<T1>>

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 })
updateBySecondaryIndex<K extends SecondaryIndexKeys<T1, T2["indices"]>>(
index: K,
value: CheckKeyOf<K, T1>,
data: UpdateData<T1>,
options?: ListOptions<T1>,
)

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"),
  }
)