kvite
Types
Create a new Kvite instance with the builder, then use it like so:
import kvite
// If no custom config is required, this can be shortened to `kvite.default()`.
let kv = kvite.new() |> kvite.open()
// Now you can use `kv` to interact with the key-value store.
let assert Ok(_) = kv |> kvite.set("hello", <<"world">>)
let assert Ok(Some(<<"world">>)) = kv |> kvite.get("hello")
pub opaque type Kvite
Usage:
import kvite
let kv = kvite.new()
// Optionally set the path to the SQLite database file.
// Default is `:memory:` for an in-memory database.
|> kvite.with_path("path/to/my_database.sqlite?cache=shared")
// Optionally set the name of the table to use.
// Default is "kv".
|> kvite.with_table("my_table")
|> kvite.open()
pub opaque type KviteBuilder
pub type KviteError {
SqlError(code: Int, message: String)
}
Constructors
-
SqlError(code: Int, message: String)
The error codes are documented here: https://sqlite.org/rescode.html
sqlight
offers thesqlight.error_code_from_int
function to convert an integer code to asqlight.ErrorCode
.
Values
pub fn begin_transaction(kv: Kvite) -> Result(Nil, KviteError)
Begin a transaction. All operations after this will be part of the transaction and will only be applied to the
database when commit_transaction
is called. Call cancel_transaction
to discard the changes made during the transaction.
pub fn cancel_transaction(kv: Kvite) -> Result(Nil, KviteError)
Cancel the current transaction. All changes made during the transaction will be discarded.
pub fn commit_transaction(kv: Kvite) -> Result(Nil, KviteError)
Commit the current transaction. All changes made during the transaction will be applied.
pub fn default() -> Result(Kvite, KviteError)
Create a new Kvite instance with default settings: in-memory database and “kv” table.
pub fn del(kv: Kvite, key key: String) -> Result(Nil, KviteError)
Delete a key from the database.
pub fn get(
kv: Kvite,
key key: String,
) -> Result(option.Option(BitArray), KviteError)
Get the value associated with a key. Returns Ok(None)
if the key does not exist.
pub fn keys(kv: Kvite) -> Result(List(String), KviteError)
Get a list of all keys in the database. Returns an empty list if there are no keys.
pub fn new() -> KviteBuilder
Create a new KviteBuilder with default values: in-memory SQlite database and a table named “kv”.
pub fn open(builder: KviteBuilder) -> Result(Kvite, KviteError)
Open a Kvite database with the provided builder configuration.
pub fn set(
kv: Kvite,
key key: String,
value value: BitArray,
) -> Result(Nil, KviteError)
Set the value for a key. If the key already exists, it will be replaced.
pub fn sqlight_connection(kv: Kvite) -> sqlight.Connection
pub fn truncate(kv: Kvite) -> Result(Nil, KviteError)
Delete all keys and values from the Kvite instance. This operation is irreversible.
pub fn with_connection(
builder: KviteBuilder,
connection conn: sqlight.Connection,
) -> KviteBuilder
Tell Kvite to use an existing connection. If not provided, a new connection will be created when open
is called on the builder.
pub fn with_path(
builder: KviteBuilder,
path path: String,
) -> KviteBuilder
Set the path to the SQLite database file. Default is :memory:
for an in-memory database.
pub fn with_table(
builder: KviteBuilder,
table_name table: String,
) -> KviteBuilder
Set the name of the table to use. Default is “kv”.
Note: The table name must be a valid SQLite identifier and cannot contain special characters or spaces.
WARNING: Do not use unverified user-input for the table name, as the name is injected as-is into the SQL query.