dinostore/kv

Bindings to the Deno.Kv database.

Types

pub type Connection

An entry in the database. Since the value is Dynamic, it needs to be parsed before it can be used in any useful way.

pub type Entry {
  Entry(key: Key, value: Dynamic, versionstamp: String)
}

Constructors

  • Entry(key: Key, value: Dynamic, versionstamp: String)

Errors that can be returned from functions that retrieve entries from the database.

pub type GetError {
  NotFound(Key)
}

Constructors

  • NotFound(Key)

    The key was not found in the database.

Functions

pub fn close(conn: Connection) -> Nil

Use this function to close a database connection manually.

Official Documentation

pub fn delete(
  conn: Connection,
  key key: List(KeyPart),
) -> Promise(Nil)

Delete the value for the given key from the database. If no value exists for the key, this operation is a no-op (hence the Nil return).

Official Documentation

pub fn get(
  conn: Connection,
  key key: List(KeyPart),
) -> Promise(Result(Entry, GetError))

Retrieve the value and versionstamp for the given key from the database, returning an Entry or GetError.

Official Documentation

pub fn list(
  conn: Connection,
  prefix prefix: List(KeyPart),
  reverse reverse: Bool,
  limit limit: Option(Int),
  cursor cursor: Option(String),
) -> Promise(List(Entry))

Retrieve all entries with the given key prefix from the database. The entries will be returned in reverse lexicographical order if reverse is True. cursor can be used to start the retrieval from a specific point.

Official Documentation

Read more about key ordering

use entries <- promise.await(list(["books"], reverse: False, limit: None))
use entry <- list.each(entries)
io.debug(entry)
pub fn open() -> Promise(Connection)

Open a new Deno.Kv connection to the default database location. This may be local or remote, depending on where the program is run.

Official Documentation

use conn <- promise.await(open())
pub fn open_with_path(path: String) -> Promise(Connection)

Open a new Deno.Kv connection to the database at the given path. The path :memory: can be used to connect to an in-memory database.

Official Documentation

use conn <- promise.await(open_with_path(":memory:"))
pub fn set(
  conn: Connection,
  key key: List(KeyPart),
  value value: a,
  expiration expiration: Option(Int),
) -> Promise(String)

Set the value for the given key in the database. If a value already exists for the key, it will be overwritten. This operation, by definition, cannot fail, so it returns the new versionstamp of the key.

An expiration (time-to-live, or TTL) can be specified in milliseconds. The key will be deleted from the database at earliest after the specified number of milliseconds have elapsed. Once the duration has passed, the key may still be visible or some additional time. If an expiration is not specified, the key will not expire.

Official Documentation

Search Document