glindex/index
Read operations on indexes within a transaction.
All functions accept an active Transaction and a TransactionIndex handle
obtained from glindex/transaction.index, 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/index
import glindex/transaction
pub fn get_tracks_by_artist(db, artist) {
let tx = transaction.prepare(db, transaction.read_only)
let #(tx, s) = transaction.store(tx, track_store())
let idx = transaction.index(s, track_artist_index())
use tx <- promise.await(transaction.begin(tx))
case tx {
Ok(tx) -> index.get_all(tx, idx, glindex.Only(artist), option.None)
Error(e) -> promise.resolve(Error(e))
}
}
Values
pub fn count(
tx: transaction.Transaction(rw, upgrade),
index: transaction.TransactionIndex(t, k, i),
query: glindex.Query(i),
) -> promise.Promise(Result(Int, transaction.TransactionError))
Count the records matching query via index.
pub fn get(
tx: transaction.Transaction(rw, upgrade),
index: transaction.TransactionIndex(t, k, i),
query: glindex.Query(i),
) -> promise.Promise(Result(t, transaction.TransactionError))
Read the first record matching query via index and decode it.
Returns Error(NotFoundError) when no record matches.
pub fn get_all(
tx: transaction.Transaction(rw, upgrade),
index: transaction.TransactionIndex(t, k, i),
query: glindex.Query(i),
count: option.Option(Int),
) -> promise.Promise(
Result(List(t), transaction.TransactionError),
)
Read all records matching query via index 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),
index: transaction.TransactionIndex(t, k, i),
query: glindex.Query(i),
count: option.Option(Int),
) -> promise.Promise(
Result(List(k), transaction.TransactionError),
)
Read the primary keys of all records matching query via index.
Pass option.Some(n) for count to cap the result at n keys.
pub fn get_key(
tx: transaction.Transaction(rw, upgrade),
index: transaction.TransactionIndex(t, k, i),
query: glindex.Query(i),
) -> promise.Promise(Result(k, transaction.TransactionError))
Read the primary key of the first record matching query via index.
Returns Error(NotFoundError) when no record matches.
pub fn open_cursor(
tx: transaction.Transaction(rw, upgrade),
index: transaction.TransactionIndex(t, k, i),
query: glindex.Query(i),
direction: cursor.CursorDirection,
initial: state,
handler: fn(
state,
cursor.Cursor(
@internal WithValue,
rw,
@internal IndexCursor,
t,
k,
i,
),
) -> promise.Promise(
#(state, cursor.CursorNext(@internal IndexCursor, k, i)),
),
) -> promise.Promise(Result(state, transaction.TransactionError))
Open a full-value cursor over index and iterate with handler.
Same semantics as store.open_cursor but walks records sorted by the
index key rather than by primary key. Supports cursor.continue_primary_key
for efficient seeks within the index.
pub fn open_key_cursor(
tx: transaction.Transaction(rw, upgrade),
index: transaction.TransactionIndex(t, k, i),
query: glindex.Query(i),
direction: cursor.CursorDirection,
initial: state,
handler: fn(
state,
cursor.Cursor(
@internal WithoutValue,
rw,
@internal IndexCursor,
t,
k,
i,
),
) -> promise.Promise(
#(state, cursor.CursorNext(@internal IndexCursor, k, i)),
),
) -> promise.Promise(Result(state, transaction.TransactionError))
Open a key-only cursor over index and iterate with handler.
Faster than open_cursor when you only need the index key or primary key,
without fetching the full record value.