Skip to main content
Module

x/kvdex/mod.ts>AtomicBuilder

Database wrapper for Deno KV
Go to Latest
class AtomicBuilder
Re-export
import { AtomicBuilder } from "https://deno.land/x/kvdex@v0.3.2/mod.ts";

Builder object for creating and executing atomic operations in the KV store.

Handles a single collection context at a time, with the option of selecting a new collection context during build.

Constructors

new
AtomicBuilder(
kv: Deno.Kv,
schema: TSchema,
collection: Collection<TValue>,
operations?: Operations,
)

Create a new AtomicBuilder for building and executing atomic operations in the KV store.

Type Parameters

TSchema extends Schema
TValue extends KvValue

Properties

private
collection: Collection<TValue>
private
kv: Deno.Kv
private
operations: Operations
private
schema: TSchema

Methods

add(data: TValue)

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

Example:

db
 .atomic(schema => schema.users)
 .add({
   username: "user1",
   age: 32
 })
check(...atomicChecks: AtomicCheck<TValue>[])

Check if documents have been changed since a specific versionstamp.

Example:

db
 .atomic(schema => schema.users)
 .check({
   id: "user1",
   versionstamp: null // Check that document does not already exist
 })

Executes the built atomic operation. Will always fail if trying to delete and add/set to the same indexable collection in the same operation.

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

Example:

db
 .atomic(schema => schema.users)
 .delete("user1")
mutate(...mutations: AtomicMutation<TValue>[])

Specifies atomic mutations to be formed on documents.

Example:

db
 .atomic(schema => schema.u64s)
 .mutate(
   {
     type: "delete",
     id: "num1"
   },
   {
      type: "set",
     id: "num2",
     value: new Deno.KvU64(200n)
   }
 )
select<TValue extends KvValue>(selector: CollectionSelector<TSchema, TValue>)

Select a new collection context.

Example:

operation.select(schema => schema.users)
set(id: KvId, data: TValue)

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

Example:

db
 .atomic(schema => schema.users)
 .set("user1", {
   username: "user1",
   age: 32
 })
sum(id: KvId, value: bigint)

Adds the given value to the value of the document with the given id. Sum only works for documents of type Deno.KvU64 and will throw an error for documents of any other type.

Example:

db
 .atomic(schema => schema.u64s) // Select collection of Deno.KvU64 values
 .sum("num1", 100n)