glindex/store
Read and write operations on object stores within a transaction.
All functions accept an active Transaction and a TransactionStore handle
obtained from glindex/transaction.store, and
return a Promise. Use glindex/transaction to build
and start the transaction first.
Example
import gleam/javascript/promise
import gleam/option
import glindex
import glindex/store
import glindex/transaction
pub fn get_all_tracks(db) {
let tx = transaction.prepare(db, transaction.read_only)
let #(tx, s) = transaction.store(tx, track_store())
use tx <- promise.await(transaction.begin(tx))
case tx {
Ok(tx) -> store.get_all(tx, s, glindex.All, option.None)
Error(e) -> promise.resolve(Error(e))
}
}
Values
pub fn add(
tx: transaction.Transaction(@internal ReadWrite, upgrade),
store: transaction.TransactionStore(
any,
@internal InlineKey,
t,
k,
),
value: t,
) -> promise.Promise(Result(k, transaction.TransactionError))
Insert a new record into store and return its generated primary key.
Returns Error(ConstraintError) if the record’s key already exists and
the store does not allow duplicates.
pub fn add_with_out_of_line_key(
tx: transaction.Transaction(@internal ReadWrite, upgrade),
store: transaction.TransactionStore(
any,
@internal OutOfLineKey,
t,
k,
),
value: t,
key: glindex.Value,
) -> promise.Promise(Result(k, transaction.TransactionError))
Insert a new record with an explicit out-of-line key.
Use this when the store was created with OutOfLineKey and you manage
keys yourself rather than letting IndexedDB generate them.
pub fn clear(
tx: transaction.Transaction(@internal ReadWrite, upgrade),
store: transaction.TransactionStore(any, key_mode, t, k),
) -> promise.Promise(Result(Nil, transaction.TransactionError))
Delete every record in store.
pub fn count(
tx: transaction.Transaction(rw, upgrade),
store: transaction.TransactionStore(any, key_mode, t, k),
query: glindex.Query(k),
) -> promise.Promise(Result(Int, transaction.TransactionError))
Count the records matching query in store.
pub fn delete(
tx: transaction.Transaction(@internal ReadWrite, upgrade),
store: transaction.TransactionStore(any, key_mode, t, k),
query: glindex.Query(k),
) -> promise.Promise(Result(Nil, transaction.TransactionError))
Delete all records matching query from store.
pub fn get(
tx: transaction.Transaction(rw, upgrade),
store: transaction.TransactionStore(any, key_mode, t, k),
query: glindex.Query(k),
) -> promise.Promise(Result(t, transaction.TransactionError))
Read the first record matching query from store and decode it.
Returns Error(NotFoundError) when no record matches.
pub fn get_all(
tx: transaction.Transaction(rw, upgrade),
store: transaction.TransactionStore(any, key_mode, t, k),
query: glindex.Query(k),
count: option.Option(Int),
) -> promise.Promise(
Result(List(t), transaction.TransactionError),
)
Read all records matching query from store and decode each one.
Pass option.Some(n) for count to cap the result at n records.
pub fn get_all_keys(
tx: transaction.Transaction(rw, upgrade),
store: transaction.TransactionStore(any, key_mode, t, k),
query: glindex.Query(k),
count: option.Option(Int),
) -> promise.Promise(
Result(List(k), transaction.TransactionError),
)
Read the primary keys of all records matching query in store.
Pass option.Some(n) for count to cap the result at n keys.
pub fn get_key(
tx: transaction.Transaction(rw, upgrade),
store: transaction.TransactionStore(any, key_mode, t, k),
query: glindex.Query(k),
) -> promise.Promise(Result(k, transaction.TransactionError))
Read the primary key of the first record matching query in store.
Returns Error(NotFoundError) when no record matches.
pub fn open_cursor(
tx: transaction.Transaction(rw, upgrade),
store: transaction.TransactionStore(any, key_mode, t, k),
query: glindex.Query(k),
direction: cursor.CursorDirection,
initial: state,
handler: fn(
state,
cursor.Cursor(
@internal WithValue,
rw,
@internal StoreCursor,
t,
k,
k,
),
) -> promise.Promise(
#(state, cursor.CursorNext(@internal StoreCursor, k, i)),
),
) -> promise.Promise(Result(state, transaction.TransactionError))
Open a full-value cursor over store and iterate with handler.
initial seeds the accumulator. The handler receives the current
accumulator and the cursor, and must return a Promise of the new
accumulator paired with a CursorNext instruction. Use cursor.continue()
to advance, cursor.stop() to finish early, or cursor.advance(n) to
skip records.
On a ReadWrite transaction the cursor also supports cursor.cursor_delete
and cursor.cursor_update.
pub fn open_key_cursor(
tx: transaction.Transaction(rw, upgrade),
store: transaction.TransactionStore(any, key_mode, t, k),
query: glindex.Query(k),
direction: cursor.CursorDirection,
initial: state,
handler: fn(
state,
cursor.Cursor(
@internal WithoutValue,
rw,
@internal StoreCursor,
t,
k,
k,
),
) -> promise.Promise(
#(state, cursor.CursorNext(@internal StoreCursor, k, i)),
),
) -> promise.Promise(Result(state, transaction.TransactionError))
Open a key-only cursor over store and iterate with handler.
Faster than open_cursor when you only need the key (e.g. for counting or
bulk deletes). The cursor does not carry the record value, so
cursor.cursor_value is not available.
pub fn put(
tx: transaction.Transaction(@internal ReadWrite, upgrade),
store: transaction.TransactionStore(
any,
@internal InlineKey,
t,
k,
),
value: t,
) -> promise.Promise(Result(k, transaction.TransactionError))
Insert or replace a record in store and return its primary key.
If a record with the same key already exists it is overwritten. Use
add instead when you want an error on duplicate keys.
pub fn put_with_out_of_line_key(
tx: transaction.Transaction(@internal ReadWrite, upgrade),
store: transaction.TransactionStore(
any,
@internal OutOfLineKey,
t,
k,
),
value: t,
key: glindex.Value,
) -> promise.Promise(Result(k, transaction.TransactionError))
Insert or replace a record with an explicit out-of-line key.