Reference / SQL API for Rust
SQL API for Rust
Every public type, trait and function of the SQL engine in tinystore::sql and tinystore::aio::sql, generated from its source. The SQL guide explains how to use them, and the Bun and Node, Python and Go pages list the same API.
Store
impl StoreStore.database
pub fn database(&self, name: &str) -> DatabaseBuilderThe application's database name, sql/<name>.db in the store's
directory, or local/<name>.db when no backup takes it, made the
first time it opens:
let db = store.database("app").migrations("migrations").open()?;aio::Store
impl aio::Storeaio::Store.database
pub fn database(&self, name: &str) -> DatabaseBuilderThe application's database name, sql/<name>.db in the store's
directory, as crate::Store::database opens it:
let db = store.database("app").migrations("migrations").open()?;Write
pub struct Write(_)A write of a batch: a statement, a job's add of a queue opened from the
database, queue.adding(&job), or an event's append of a stream opened
from it, audit.appending(name, &fields). A statement becomes one by
into:
let db = store.database("app").open()?;
let indexing = db.queue::<Index>("indexing").open()?;
db.batch([
tinystore::sql!("update notes set body = ? where id = ?", body, note).into(),
indexing.adding(&Index { note }), // committed with the row, or not at all
])?;Database
pub struct Database { /* private fields */ }An application's database: its reads, its writes, its transactions. Clones share the database, which closes with its store.
Database.bucket
pub fn bucket<V: Value>(&self, name: &str) -> BucketBuilder<V>A bucket of values by key kept in this database's file, as
Store::bucket keeps one in kv.db: the same
handle with the same calls, whose writes commit with the rows in
Database::tx through tx.with(&bucket).
Database.queue
pub fn queue<V>(&self, name: &str) -> QueueBuilder<V>
where
V: Serialize + DeserializeOwned + Send + 'static,A queue of jobs kept in this database's file, as
Store::queue keeps one in jobs.db: the same
handle with the same calls and workers, whose jobs commit with the rows
in Database::tx through tx.with(&queue). It shares the database's
writer, which is the point and its cost.
Database.files
pub fn files(&self, name: &str) -> FilesBuilder<'static>A set of files kept in this database's file, as
Store::files keeps one in blobs.db: the same
handle with the same calls, whose writes commit with the rows in
Database::tx through tx.with(&files). The bytes too large to keep
inline lie in the folder beside the file, sql/app.blobs/.
let db = store.database("app").open()?;
let packages = db.files("packages").open()?;
db.tx(|tx| {
tx.exec(tinystore::sql!("insert into plugins (file) values (?)", "stripe-1.0.0.js"))?;
tx.with(&packages).put("stripe-1.0.0.js", b"export const charges = 1")?;
Ok::<_, tinystore::Error>(())
})?;Database.search_index
pub fn search_index<T: DeserializeOwned + 'static>(&self, name: &str) -> TableIndexBuilder<T>An index of a table of this database, kept in its file: the table's
rows found again by their words, their vectors or both, as
Store::index finds documents, each hit's value
its row as Database::all reads one. The table owns the rows and the
index follows them: every change of a row, by any statement, trigger
or migration, reaches the index in the commit that makes it or not at
all, and one by a process beside the store at the store's next write
or within a second.
let db = store.database("app").migrations("migrations").open()?;
let articles =
db.search_index::<Article>("articles_search").table("articles").field("title", 2.0).field("body", 1.0).open()?;
let found = articles.search("новый заголовок").run()?;Database.drop_search_index
pub fn drop_search_index(&self, name: &str) -> Result<bool>Drops the table's index name this database keeps: its triggers come
off the table in one commit and its rows go in the background; says
whether there was one. A handle of it is refused from then on.
Database.events
pub fn events(&self, stream: &str) -> EventsBuilder<'static>The store's stream of events stream, opened through this database:
the same handle as Store::events opens, with
the same calls and reads, the stream in records.db where every stream
lives. Its appends inside Database::tx, through tx.with(&audit),
and in Database::batch, through
appending, are kept in this
database's file with the rows and written to the stream once they
commit, before the call returns: an event exists exactly when the
change it records does. An append outside them goes to the stream at
once, and no write is atomic across two files.
let db = store.database("app").open()?;
let audit = db.events("audit").open()?;
db.tx(|tx| {
tx.exec(tinystore::sql!("update roles set role = ? where user_id = ?", "admin", 42))?;
tx.with(&audit).append("role.changed", &RoleChanged { user_id: 42, role: "admin" })?;
Ok::<_, tinystore::Error>(())
})?;
let page = audit.list().newest().limit(50).page()?; // the line is in the streamDatabase.name
pub fn name(&self) -> &strDatabase.all
pub fn all<T: DeserializeOwned>(&self, statement: impl Into<Sql>) -> Result<Vec<T>>The rows a statement gives, each read as a T: a struct by its
columns' names, a tuple by their order, a single value when a row has
one column. A write with returning runs on the writer and gives its
rows once its commit is durable.
Database.one
pub fn one<T: DeserializeOwned>(&self, statement: impl Into<Sql>) -> Result<Option<T>>The one row a statement gives, or none; a statement that gives two is
invalid, and a write that gave them rolls back.
Database.scalar
pub fn scalar<T: DeserializeOwned>(&self, statement: impl Into<Sql>) -> Result<T>The one value of the one row a statement gives, as count(*) does;
Option takes a NULL, as max over no rows gives.
Database.each
pub fn each<T: DeserializeOwned>(&self, statement: impl Into<Sql>) -> Result<Each<T>>The rows a statement gives, one at a time, each read as all reads
it: read a part at a time from one snapshot, so that memory holds a
part and not the rows, however many there are. The snapshot lasts
five seconds at most: a walk that takes longer fails limit, and is
read a page at a time with a table's list, or ordered by its key
with a table's each, which reads on past it. A write with
returning gives its rows as all does.
for order in db.each::<Order>("select * from orders")? {
let order = order?;
}Database.describe
pub fn describe(&self, statement: impl Into<Sql>) -> Result<Vec<Column>>The columns a statement answers, in its order, each with the type its
table declares where it is a table's own column, whether or not the
statement would answer a row: it is compiled on a reader and nothing
runs. The values of its ?s are not read, and may be left out. A
statement that writes, returning or not, is invalid, and one that
names what its file lacks fails as all fails it.
let columns = db.describe("select id, title, length(title) as size from notes where author_id = ?")?;
// id: Some("TEXT"), title: Some("TEXT"), size: NoneDatabase.statement
pub fn statement(&self, statement: impl Into<Sql>) -> BoundStatementA statement bound to this database, for a stream's where, which
keeps a key to the values its one column gives: nothing runs until a
query reads it, once as the query starts, on a reader of this file.
let db = store.database("app").open()?;
let teachers = db.statement("select id from teachers");
let day = std::time::Duration::from_secs(24 * 60 * 60);
let used = store.events("activity").open()?.list().since(day).filter("userId", &teachers).count()?;Database.exec
pub fn exec(&self, statement: impl Into<Sql>) -> Result<Done>Runs a write once it is durable. Writes from many callers share a commit, each in a savepoint of it: one that fails rolls back alone.
Database.batch
pub fn batch<I>(&self, writes: I) -> Result<Vec<Done>>
where
I: IntoIterator,
I::Item: Into<Write>,Runs writes known before they run as one, all of them or none, in one
savepoint of the next shared commit: it costs what one write does, and
gives what each did, in their order. A write is a statement, a job's
add of a queue opened from the database, queue.adding(&job), which
commits with the rows and changes one job, or none when its id was
taken, or an event's append of a stream opened from it,
audit.appending(name, &fields), written to the stream before the
batch returns; see Write.
Database.tx
pub fn tx<T, E: From<Error>>(&self, work: impl FnOnce(&Tx<'_>) -> Result<T, E>) -> Result<T, E>Runs work in a transaction that holds the writer, for writes that
depend on what its reads found: Ok commits, an error or a panic rolls
back. It runs once. Past five seconds it rolls back and is limit.
A bucket or a queue opened from the database joins it through
tx.with(&handle), so that its keys and jobs commit with the rows,
and so does a stream opened from it: its events are written to the
stream once the transaction commits, before this returns.
Database.mirror
pub fn mirror<T>(&self, table: &str) -> MirrorBuilder<T>A table that mirrors another system, T its rows: its replace
takes the source's list of a scope and its upsert a change of some
rows, and each answers what was added, changed and gone. The key,
what names a row at the source, is given before it opens.
Database.table
pub fn table<T>(&self, name: &str) -> Table<'static, T>A table of the database, typed where it opens: its rows' inserts and upserts, and queries built without SQL text. It costs nothing until its first call.
DatabaseBuilder
pub struct DatabaseBuilder { /* private fields */ }How a database opens; Store::database makes one.
DatabaseBuilder.migrations
pub fn migrations(self, migrations: impl Into<Migrations>) -> SelfThe migrations the file must have applied: opening applies those it has not, all in one transaction, and refuses a file that applied an edited, renamed or missing one, or more than the program has. Without them the file opens as it is.
The newest may be drafts, named 0007_messages.draft.sql: they apply
in a transaction of their own once the file is copied to
sql/<name>.before-drafts, and one edited since it was applied
refuses the file, naming tinystore migrate --redo-drafts, which
takes it back to the copy and applies them again. A draft renamed
without .draft, its text the same, has shipped, and the copy goes.
DatabaseBuilder.durability
pub fn durability(self, durability: Durability) -> SelfHow far this database's commits go before they return, where the
store's own setting is not this file's: what may be lost to a power
loss beside what may not. Its buckets and queues commit with it. A
database open already keeps the durability it was opened with, and
asking for another is Invalid.
DatabaseBuilder.backup
pub fn backup(self, backup: bool) -> SelfWhether backups take the database. false keeps its file and the
bytes of its files in local/<name>.db and local/<name>.blobs/
rather than sql/, where no backup looks, this release's or an older
one's, open or not, and no node copies it: each node that opens it
writes its own, and what a backup leaves out nothing else keeps. A
name lives in one folder: one whose file lies in the other is
Invalid, naming both.
let uploads = store.database("uploads").backup(false).open()?;
let pending = uploads.files("pending").ttl(std::time::Duration::from_secs(15 * 60)).open()?;DatabaseBuilder.open
pub fn open(self) -> Result<Database>Done
pub struct Done {
pub changes: u64,
pub last_insert_rowid: i64,
}What a write changed: the rows, and SQLite's rowid of the row it inserted,
0 when it inserted none. The rowid is a table's key only for an integer primary key.
Column
pub struct Column {
pub name: String,
pub declared: Option<String>,
}A column a statement answers: its name as its rows name it, and the type
its table declares for it where the column is a table's own, as SQLite
keeps it: a type a strict table takes in capitals, INTEGER or TEXT,
and any other as written, varchar(80); None for an expression, and
for a column its table declares no type for.
Each
pub struct Each<T> { /* private fields */ }The rows of a statement, each as a T, as Database::each and
Query::each give them. Dropped, it stops its read
and gives its reader back.
Mark
pub struct Mark { /* private fields */ }A column that marks a row with when it was marked, and the unit it keeps
that time in: Mark::seconds("deleted_at"). A column's name alone is a
mark in milliseconds, "deleted_at".into().
Mark.millis
pub fn millis(column: impl Into<String>) -> MarkA column that keeps the time in unix milliseconds.
Mark.seconds
pub fn seconds(column: impl Into<String>) -> MarkA column that keeps the time in unix seconds, as unixepoch() does.
Mark.column
pub fn column(&self) -> &strMigrations
pub enum Migrations {
Dir(PathBuf),
Files(Vec<(String, String)>),
}A database's migrations: a directory of .sql files named
0001_notes.sql, 0002_tags.sql, applied in the order of their numbers,
or the files themselves by name, for a program that carries them in its
binary:
let db = store.database("app").migrations("migrations").open()?;
let db = store
.database("app")
.migrations([("0001_notes.sql", "create table notes (id text primary key, title text not null) strict")])
.open()?;Changed
pub struct Changed<T> {
pub before: T,
pub after: T,
}A row that changed: as the table held it, and as the source gave it.
Diff
pub struct Diff<T> {
pub added: Vec<T>,
pub changed: Vec<Changed<T>>,
pub gone: Vec<T>,
pub same: u64,
pub suspect: bool,
}What a mirror's call changed, each row a T: the rows added, the rows
changed as they were and as they are, the rows the source dropped, and
how many it named as they were held. A suspect answer wrote nothing.
A row answers by the columns the list's rows carry: an added row and a
changed one's after as the list gave it, a changed one's before and a
gone one as the table held them.
Guarded
pub struct Guarded<'m, T> { /* private fields */ }A mirror's replace with its min_kept guard; Mirror::min_kept makes one.
Guarded.replace
pub fn replace<S: AsRef<str>, V: Serialize>(&self, scope: &[(S, V)], rows: &[T]) -> Result<Diff<T>>Mirror::replace, which writes nothing when the guard holds it back.
Guarded.begin
pub fn begin<S: AsRef<str>, V: Serialize>(&self, scope: &[(S, V)]) -> Result<Run<T>>Mirror::begin, whose commit writes nothing when the guard holds it back.
Mirror
pub struct Mirror<T> { /* private fields */ }A table that mirrors another system; Database::mirror opens one.
Clones share it.
Mirror.replace
pub fn replace<S: AsRef<str>, V: Serialize>(&self, scope: &[(S, V)], rows: &[T]) -> Result<Diff<T>>Makes the scope, the rows whose columns hold the scope's values, &[]
for the whole table, what rows lists, in one write, all of it or
none: adds what the table lacks, writes the compared columns' changes,
and deletes, or marks gone, what the list leaves out. A row the list
names outside the scope is Invalid, and so is one whose key holds a
null. Values compare as the table keeps them: a column converts what
it is given as SQLite stores it, 4 given to a text column is "4",
an integer and a real compare by their numbers, and a key's text by
the collation of the index that makes it unique. On a copy of a node
it is Unavailable: run it at the file's home.
A scope of mixed types takes serde_json::Values, json!(1) beside
json!("fall").
Mirror.upsert
pub fn upsert(&self, rows: &[T]) -> Result<Diff<T>>Writes what rows adds and changes, a delta of the source's, and
marks nothing gone: its gone is always empty.
Mirror.min_kept
pub fn min_kept(&self, fraction: f64) -> Guarded<'_, T>The mirror's replace guarded against an answer that cannot be
right: when the scope holds live rows and the list would leave fewer
than fraction of them live, an empty list among them, it writes
nothing and answers what it would have done, suspect. A fraction
outside (0, 1] is Invalid.
let diff = works.min_kept(0.5).replace(&[("course_id", 1)], &[])?;
assert!(diff.suspect); // a course that held works: nothing was deletedMirror.begin
pub fn begin<S: AsRef<str>, V: Serialize>(&self, scope: &[(S, V)]) -> Result<Run<T>>Begins a replace of the scope whose list comes in parts: each
Run::add gives a part, and Run::commit makes the scope what
every part listed, in one write, so that only what no part named is
gone. A run dropped before its commit writes nothing. For a list too
large for one call, or one read a page at a time from its source.
let run = works.begin(&[("course_id", 1)])?;
for page in &pages {
run.add(page)?;
}
let diff = run.commit()?; // gone: what no page namedMirrorBuilder
pub struct MirrorBuilder<T> { /* private fields */ }How a mirror opens; Database::mirror makes one.
MirrorBuilder.key
pub fn key<I: IntoIterator<Item = S>, S: Into<String>>(self, columns: I) -> SelfThe columns that name a row at the source, ["course_id", "id"]: a
unique index or the table's primary key must cover them.
MirrorBuilder.gone
pub fn gone(self, mark: impl Into<Mark>) -> SelfA column that marks what the source dropped with the store's time
instead of deleting the row, an integer of unix milliseconds or, with
Mark::seconds("gone_at"), seconds. A row marked is neither marked
nor answered again, and one the source names once more loses its mark
and is added again.
MirrorBuilder.change_on
pub fn change_on<I: IntoIterator<Item = S>, S: Into<String>>(self, columns: I) -> SelfThe columns whose change makes a row changed; every column the rows
carry but the key's when not given. A row whose compared columns are
the same is not written at all, even where its other columns differ:
a fetched_at the source writes at every read changes nothing.
MirrorBuilder.open
pub fn open(self) -> Result<Mirror<T>>Opens the mirror once its table has every column it names and its
key names one row; Invalid otherwise.
Run
pub struct Run<T> { /* private fields */ }A mirror's replace whose list comes in parts; Mirror::begin makes
one. Its parts wait outside memory until the commit; a run dropped
before it, or that no call reached for five minutes, writes nothing.
Run.add
pub fn add(&self, rows: &[T]) -> Result<()>Gives a part of the list, checked as a replace's list is: rows of
the first part's shape, inside the scope, each key once in the whole
run. A part that fails ends the run.
Run.commit
pub fn commit(self) -> Result<Diff<T>>Makes the scope what every part listed, as Mirror::replace makes
it of one list, and answers what changed.
TxMirror
pub struct TxMirror<'t, T> { /* private fields */ }A mirror's calls inside a transaction of its database, tx.with(&works):
what they write commits with the transaction or not at all, and they see
what it wrote.
TxMirror.replace
pub fn replace<S: AsRef<str>, V: Serialize>(&self, scope: &[(S, V)], rows: &[T]) -> Result<Diff<T>>Mirror::replace, inside the transaction.
TxMirror.upsert
pub fn upsert(&self, rows: &[T]) -> Result<Diff<T>>Mirror::upsert, inside the transaction.
and
pub fn and<I>(conditions: I) -> Sql
where
I: IntoIterator,
I::Item: Into<Sql>,Conditions joined with and, in parentheses, for a group inside an or;
an empty and finds everything.
eq
pub fn eq<T: Serialize + ?Sized>(column: &str, value: &T) -> SqlA column equal to a value, its name quoted; None is is null.
has
pub fn has<T: Serialize + ?Sized>(column: &str, value: &T) -> SqlA JSON list in column holding value.
ident
pub fn ident(name: &str) -> SqlA name, quoted, never SQL: a column a request chooses.
is_in
pub fn is_in<T: Serialize>(column: &str, values: &[T]) -> SqlA column holding one of values: in (…), written as one value SQLite
reads with json_each, so a list of any length is the same statement and
an empty one finds nothing.
json
pub fn json<T: Serialize + ?Sized>(value: &T) -> SqlA value kept as its JSON.
list
pub fn list<T: Serialize>(values: &[T]) -> SqlA list for in (…), as one JSON value SQLite reads with json_each.
or
pub fn or<I>(conditions: I) -> Sql
where
I: IntoIterator,
I::Item: Into<Sql>,Conditions joined with or, in parentheses; an empty or finds nothing.
value
pub fn value<T: Serialize + ?Sized>(value: &T) -> SqlA value as a piece of SQL, for a column an update sets.
AtVersion
pub struct AtVersion<'r, T> { /* private fields */ }The edits of a query made from a version a program saw, from
Query::if_version: tasks.filter(sql::eq("id", &1)).if_version(3).update(…).
AtVersion.update
pub fn update<I, S>(&self, sets: I) -> Result<Done>
where
I: IntoIterator<Item = (S, Sql)>,
S: AsRef<str>,Query::update of the rows at the version, all of them or none.
AtVersion.delete
pub fn delete(&self) -> Result<Done>Query::delete of the rows at the version, all of them or none: a
mark, on a table with a deleted column, which a delete for good is not.
Direction
pub enum Direction {
Asc,
Desc,
}The way an order goes.
Page
pub struct Page<T> {
pub rows: Vec<T>,
pub next: Option<String>,
}A page of a query's rows, and the cursor that reads the next; none after the last.
Query
pub struct Query<'r, T> { /* private fields */ }A query on a table: a value, each call returning a new one, so that the
first stays as it was. all, one, scalar, count and list read;
update and delete write.
let urgent: Vec<Order> = db
.table::<Order>("orders")
.filter(sql::eq("status", "pending"))
.filter(sql::or([tinystore::sql!("amount >= ?", 1000), sql::eq("priority", "high")]))
.order_by("created_at", Direction::Desc)
.limit(50)
.all()?;Query.filter
pub fn filter(&self, condition: impl Into<Sql>) -> SelfAdds a condition with and: a piece from sql!, sql::eq, sql::or
and the rest. filter, since where is Rust's own word.
Query.select
pub fn select<S>(&self, columns: impl Into<Sql>) -> Query<'r, S>The columns a row holds, as SQL text, and the shape of the row they make.
Query.include
pub fn include<C>(&self, name: &str, query: &Query<'_, C>) -> SelfGives each row the rows of another query as a list under name, a
field of the row's type, from the same statement: the query names the
row it belongs to in its filter, by this table's alias, names its
columns with select, and has a limit, which counts for each row.
Every value comes back as it is kept.
let latest = db
.table::<Post>("posts as p")
.select::<Post>("p.id, p.title")
.filter("p.author_id = u.id")
.order_by("p.id", Direction::Desc)
.limit(3);
let authors: Vec<Author> = db.table::<Author>("users as u").include("posts", &latest).all()?;Query.join
pub fn join(&self, table: &str, on: impl Into<Sql>) -> SelfJoins a table, "users as u", on a condition.
Query.left_join
pub fn left_join(&self, table: &str, on: impl Into<Sql>) -> SelfJoins a table whose columns may be null where no row of it meets the condition.
Query.group_by
pub fn group_by<I: IntoIterator<Item = S>, S: AsRef<str>>(&self, columns: I) -> SelfGroups the rows by columns, quoted as names.
Query.having
pub fn having(&self, condition: impl Into<Sql>) -> SelfA condition on the groups, as filter takes one.
Query.order_by
pub fn order_by(&self, column: &str, direction: Direction) -> SelfOrders the rows by a column, quoted as a name, so that a sort a request chooses cannot be SQL.
Query.order_by_sql
pub fn order_by_sql(&self, piece: impl Into<Sql>, direction: Direction) -> SelfOrders the rows by a piece of SQL: sql!("lower(title)").
Query.limitNew in 0.13.1
pub fn limit(&self, rows: u64) -> SelfQuery.offset
pub fn offset(&self, rows: u64) -> SelfQuery.to_sql
pub fn to_sql(&self) -> Result<Sql>The text and the values the query sends, to read or to log.
Query.count
pub fn count(&self) -> Result<u64>How many rows the conditions match, or how many groups with
group_by, without the order and the limit.
Query.explain
pub fn explain(&self) -> Result<Vec<String>>SQLite's plan for the query, a line a step.
Query.update
pub fn update<I, S>(&self, sets: I) -> Result<Done>
where
I: IntoIterator<Item = (S, Sql)>,
S: AsRef<str>,Sets columns of the rows the conditions match, each a value from
sql::value or a piece of SQL, and says how many changed. A versioned
table's version goes up by one.
Query.delete
pub fn delete(&self) -> Result<Done>Deletes the rows the conditions match, and says how many it deleted. On a table with a deleted column it marks the live ones instead, with the store's clock, and raises a versioned table's version.
Query.with_deleted
pub fn with_deleted(&self) -> SelfThe query of every row, those the deleted column marks among them.
Query.deleted_only
pub fn deleted_only(&self) -> SelfThe query of the rows the deleted column marks, and of no other.
Query.restore
pub fn restore(&self) -> Result<Done>Clears the mark of the marked rows the conditions match, whichever rows the handle shows, and says how many came back; a versioned table's version goes up by one.
Query.purge
pub fn purge(&self) -> Result<Done>Deletes for good the rows the conditions match of those the handle
shows: the marked ones of Query::deleted_only, or every one of
Query::with_deleted. A handle that shows the live rows alone
refuses it, Invalid, so that nothing live is purged by a handle
that forgot to ask.
Query.if_version
pub fn if_version(&self, version: i64) -> AtVersion<'r, T>The edits of a versioned table made from version: they write only
when every row the conditions match is at it, all of those rows, and
otherwise nothing, failing Conflict with the version a row is at.
No row at all is no conflict: the edit changes none.
Query.all
pub fn all(&self) -> Result<Vec<T>>Query.one
pub fn one(&self) -> Result<Option<T>>Query.scalar
pub fn scalar<V: DeserializeOwned>(&self) -> Result<V>Query.each
pub fn each(&self) -> Result<Each<T>>The rows one at a time, read a part at a time from one snapshot, so
that memory holds a part and not the rows: for more than fits in
memory. A snapshot lasts five seconds at most. Ordered by columns by
their names, a unique one last, as list is, the walk reads on past
that in a snapshot of its own, after the last row it gave, which sees
the rows as they are then; any other walk fails limit there. Inside
a transaction it gives the rows all reads.
for order in db.table::<Order>("orders").order_by("id", Direction::Asc).each()? {
let order = order?;
}Query.list
pub fn list(&self, limit: u64, after: Option<&str>) -> Result<Page<T>>A page after the last row of the one before, by the query's order,
which ends in a unique column so that no row is read twice or skipped;
next reads the next page, and only of this query.
Table
pub struct Table<'r, T> { /* private fields */ }
impl Deref<Target = Query<'r, T>>A table: its queries, and its rows' inserts and upserts. Derefs to the
query of all its rows.
A table may keep two columns of its rows for the program: deleted, which
marks a row deleted rather than deleting it, so that every query and write
of the handle passes it by, and version, which every update raises, so
that an edit made from an old version is refused rather than written over
another's. A statement written by hand is not rewritten: they are the
handle's.
let tasks = db.table::<Task>("tasks").deleted(sql::Mark::seconds("deleted_at")).version("version");
let edit = tasks.filter(sql::eq("id", &1)).if_version(0).update([("title", sql::value("from b"))]);
if edit.as_ref().is_err_and(|error| error.kind() == ErrorKind::Conflict) {
// sql app: table tasks: the row is at version 1, not 0
}
tasks.filter(sql::eq("id", &1)).delete()?; // marks deleted_at; no query of tasks finds it
tasks.deleted_only().filter(sql::eq("id", &1)).restore()?;Table.name
pub fn name(&self) -> &strTable.deleted
pub fn deleted(self, mark: impl Into<Mark>) -> SelfMarks a row deleted rather than deleting it: the mark's column, a
nullable integer, holds when, by the store's clock, in unix
milliseconds or, with Mark::seconds("deleted_at"), seconds. Every
query and write of the handle passes the marked rows by, unless
Query::with_deleted or Query::deleted_only asks for them;
delete marks, Query::restore clears the mark, and
Query::purge deletes for good. A marked row is still in the file
and holds its key, so an insert of that key fails Conflict; an
upsert whose key a marked row holds updates that row and clears its
mark: the row came back.
Table.version
pub fn version(self, column: &str) -> SelfCounts a row's versions in column, an integer, not null, default 0:
every update, delete, restore and upsert of the handle raises it by
one, and an edit after Query::if_version writes only rows at the
version it names. The handle sets the column itself: an update or an
upsert's update that names it is Invalid, and an insert starts a row
at version 0 whatever the row gives for it.
Table.insert
pub fn insert(&self, row: &T) -> Result<T>Inserts a row, every field it has, and gives it back as the file keeps it: the defaults and the generated columns the database filled. On a versioned table the row starts at version 0, whatever it gives for the column, since the version is the handle's to count.
Table.insert_all
pub fn insert_all(&self, rows: &[T]) -> Result<Vec<T>>Inserts rows of one shape as one write, all of them or none.
Table.upsert
pub fn upsert(&self, row: &T, upsert: Upsert) -> Result<Option<T>>Inserts a row, or, when its key is taken, sets the columns update
names on the row that holds it, if that row meets the upsert's
condition; None when the condition kept it. On a table with a
deleted column the update clears a mark, and on a versioned one it
raises the version.
Table.upsert_all
pub fn upsert_all(&self, rows: &[T], upsert: Upsert) -> Result<Vec<T>>Upserts rows of one shape as one statement, all of them or none, each
as Table::upsert does one. It gives back the rows it wrote as the
file keeps them, without a row whose key is held by a row the
condition kept; SQLite refuses a list that holds one key twice.
Upsert
pub struct Upsert { /* private fields */ }What an upsert does when the row's key is taken:
Upsert::on("id").update(["title"]).only_if(sql::eq("author_id", &user_id)).
Upsert.on
pub fn on<I: IntoIterator<Item = S>, S: Into<String>>(conflict: I) -> UpsertThe columns whose value, taken, makes the insert an update: the key.
Upsert.update
pub fn update<I: IntoIterator<Item = S>, S: Into<String>>(self, columns: I) -> UpsertThe columns the update sets from the row; none other changes.
Upsert.only_if
pub fn only_if(self, condition: impl Into<Sql>) -> UpsertThe condition the row that holds the key must meet, its owner's, or it stays as it is.
BoundStatement
pub struct BoundStatement { /* private fields */ }A statement bound to the database that runs it, made by
Database::statement: what a stream's
where keeps a key to, as SQL's in (select …) does. A stream lives in
records.db and belongs to no database, so the statement says whose it is.
Nothing runs until a query reads it.
Sql
pub struct Sql { /* private fields */ }A statement's text and the values of its ?s, which are always values and
never SQL, so a value cannot inject any. sql! makes one:
let title: Option<String> = db.one(tinystore::sql!("select title from notes where id = ?", id))?;Text alone is a statement without values: db.scalar("select count(*) from notes").
Sql.new
pub fn new(text: impl Into<String>) -> SqlSql.bind
pub fn bind<T: Serialize + ?Sized>(self, value: &T) -> SqlThe value of the next ?. A value SQLite would change, a NaN or an
integer past i64, makes the statement invalid when it runs.
Sql.text
pub fn text(&self) -> &strTx
pub struct Tx<'t> { /* private fields */ }A database's transaction, from Database::tx: its
reads see its writes, and a call that fails leaves it as it was before the
call.
Tx.table
pub fn table<T>(&self, name: &str) -> Table<'t, T>A table inside the transaction: its calls are the transaction's.
Tx.all
pub fn all<T: DeserializeOwned>(&self, statement: impl Into<Sql>) -> Result<Vec<T>>The rows a statement gives, each read as a T: a struct by its
columns' names, a tuple by their order, a single value when a row has
one column. A write with returning runs on the writer and gives its
rows once its commit is durable.
Tx.one
pub fn one<T: DeserializeOwned>(&self, statement: impl Into<Sql>) -> Result<Option<T>>The one row a statement gives, or none; a statement that gives two is
invalid, and a write that gave them rolls back.
Tx.scalar
pub fn scalar<T: DeserializeOwned>(&self, statement: impl Into<Sql>) -> Result<T>The one value of the one row a statement gives, as count(*) does;
Option takes a NULL, as max over no rows gives.
Tx.exec
pub fn exec(&self, statement: impl Into<Sql>) -> Result<Done>Tx.with
pub fn with<'h, H: TxHandle>(&self, handle: &'h H) -> H::InTxThe calls of a bucket or a queue opened from this database, inside the transaction: what they write commits with the rows or not at all.
let db = store.database("app").open()?;
let emails = db.queue::<Email>("emails").open()?;
db.tx(|tx| -> tinystore::Result<()> {
let order: i64 = tx.scalar(tinystore::sql!("insert into orders (total) values (?) returning id", 70))?;
tx.with(&emails).add(&Email { order })?; // sent only if the order is there
Ok(())
})?;aio::sql::Each
pub struct Each<T> { /* private fields */ }The rows of a statement, each as a T, as
Database::each and
Query::each give them: a Stream that begins its
walk at its first poll. Dropped, it stops its read at the next row and
gives its reader back. A failure is its last item.
aio::sql::DatabaseBuilder
pub struct DatabaseBuilder { /* private fields */ }How a database opens; Store::database makes
one, with the blocking builder's options.
aio::sql::DatabaseBuilder.migrations
pub fn migrations(self, migrations: impl Into<Migrations>) -> SelfThe migrations the file must have applied, as
sql::DatabaseBuilder::migrations takes them.
aio::sql::DatabaseBuilder.durability
pub fn durability(self, durability: Durability) -> SelfHow far its commits go before they return, as
sql::DatabaseBuilder::durability says.
aio::sql::DatabaseBuilder.open
pub fn open(self) -> Result<Database>Opens the database as sql::DatabaseBuilder::open does: a plain
call, made once at start, since a first open applies and checks the
migrations.
aio::sql::Database
pub struct Database { /* private fields */ }An application's database whose calls are awaited: the same file,
writer and readers as sql::Database, and the same database when one
is made from the other. Clones share it, and it closes with its store.
aio::sql::Database.mirror
pub fn mirror<T>(&self, table: &str) -> MirrorBuilder<T>A table that mirrors another system, as sql::Database::mirror
opens one: its replace and upsert awaited.
aio::sql::Database.name
pub fn name(&self) -> &straio::sql::Database.blocking
pub fn blocking(&self) -> &DatabaseThe same database, for its blocking calls.
aio::sql::Database.statement
pub fn statement(&self, statement: impl Into<Sql>) -> BoundStatementA statement bound to this database, for a stream's where, as
sql::Database::statement binds one: nothing runs until a query
reads it, as it starts on the store's pool.
aio::sql::Database.all
pub fn all<T>(
&self,
statement: impl Into<Sql>,
) -> impl Future<Output = Result<Vec<T>>> + Send + 'static
where
T: DeserializeOwned + Send + 'static,The rows a statement gives, each read as a T, as
sql::Database::all reads them. A statement that writes, as one
with returning does, is queued at the writer by the read that finds
it writes, and from then on it commits whether or not its future is
still awaited.
aio::sql::Database.one
pub fn one<T>(
&self,
statement: impl Into<Sql>,
) -> impl Future<Output = Result<Option<T>>> + Send + 'static
where
T: DeserializeOwned + Send + 'static,The one row a statement gives, or none, as sql::Database::one
reads it; it runs as Database::all does.
aio::sql::Database.scalar
pub fn scalar<T>(
&self,
statement: impl Into<Sql>,
) -> impl Future<Output = Result<T>> + Send + 'static
where
T: DeserializeOwned + Send + 'static,The one value of the one row a statement gives, as
sql::Database::scalar reads it; it runs as Database::all does.
aio::sql::Database.describe
pub fn describe(
&self,
statement: impl Into<Sql>,
) -> impl Future<Output = Result<Vec<Column>>> + Send + 'staticThe columns a statement answers, in its order, with the types their
tables declare, as sql::Database::describe says them: compiled on
a reader on the store's pool, and nothing of it run.
aio::sql::Database.each
pub fn each<T>(&self, statement: impl Into<Sql>) -> Each<T>
where
T: DeserializeOwned + Send + 'static,The rows a statement gives, one at a time, as sql::Database::each
walks them: a Stream whose parts are read on the store's pool from
one snapshot, each row decoded as it is polled, so that memory holds
a part and not the rows. Its walk begins at its first poll; past five
seconds of one snapshot it fails limit.
aio::sql::Database.exec
pub fn exec(&self, statement: impl Into<Sql>) -> impl Future<Output = Result<Done>> + Send + 'staticRuns a write once it is durable, sharing a commit with the writes of
other callers, as sql::Database::exec does. On a copy it is passed
to the home from a thread of its own, which waits for the home's
answer.
aio::sql::Database.batch
pub fn batch<I>(&self, writes: I) -> impl Future<Output = Result<Vec<Done>>> + Send + 'static
where
I: IntoIterator,
I::Item: Into<Write>,Runs writes known before they run as one, all of them or none, as
sql::Database::batch does: statements, and jobs' adds of queues
opened from the database, queue.adding(&job).
aio::sql::Database.tx
pub fn tx<T, E, F>(&self, work: F) -> impl Future<Output = Result<T, E>> + Send + 'static
where
F: FnOnce(&Tx<'_>) -> Result<T, E> + Send + 'static,
T: Send + 'static,
E: From<Error> + Send + 'static,Runs work in a transaction that holds the writer, as
sql::Database::tx does, on a thread of the store's pool: work is
blocking, so it must not wait for the program's awaits, and it runs on
no executor's thread. Inside, a bucket or a queue opened from the
database joins it through tx.with(&handle).
The database's transactions wait in one line, one at a time in the
order they were polled, whether awaited or asked for over the wire;
those waiting hold no thread. Its five seconds count from its first
poll, its wait in the line among them. A panic in work rolls the
transaction back and is resumed at the .await.
let booked = db
.tx(move |tx| -> tinystore::Result<bool> {
let left: i64 = tx.scalar(sql!("select seats_left from flights where id = ?", flight_id))?;
if left == 0 {
return Ok(false); // sold out: nothing is written
}
tx.exec(sql!("update flights set seats_left = seats_left - 1 where id = ?", flight_id))?;
Ok(true)
})
.await?;aio::sql::Database.table
pub fn table<T>(&self, name: &str) -> Table<T>A table of the database, typed where it opens, as
sql::Database::table gives one: its inserts and upserts, and its
queries, each awaited. It costs nothing until its first call.
aio::sql::Database.bucket
pub fn bucket<V: Value>(&self, name: &str) -> BucketBuilder<V>A bucket kept in this database's file, as Database::bucket opens
one, its calls awaited: its writes commit with the rows in
tx through tx.with(&bucket).
aio::sql::Database.events
pub fn events(&self, stream: &str) -> EventsBuilder<'_>The store's stream stream opened through this database, as
Database::events opens it, its calls awaited: its appends commit
with the rows in tx through
tx.with(&audit), and in batch
through appending.
aio::sql::Database.files
pub fn files(&self, name: &str) -> FilesBuilder<'_>A set of files kept in this database's file, as Database::files
opens one, its calls awaited: its writes commit with the rows in
tx through tx.with(&files).
aio::sql::Database.queue
pub fn queue<V>(&self, name: &str) -> QueueBuilder<V>
where
V: Serialize + DeserializeOwned + Send + 'static,A queue kept in this database's file, as Database::queue opens
one, its calls awaited: its jobs commit with the rows in
tx through tx.with(&queue), and
in batch through
adding.
aio::sql::Database.search_index
pub fn search_index<T: DeserializeOwned + Send + 'static>(&self, name: &str) -> TableIndexBuilder<T>An index of a table of this database, as Database::search_index
opens one, its calls awaited. Its rows change with the table's, in
their commit; a search inside tx
goes through tx.with(&index) and sees what the transaction wrote.
aio::sql::Database.drop_search_index
pub fn drop_search_index(&self, name: &str) -> impl Future<Output = Result<bool>> + Send + 'staticDrops the table's index name this database keeps, as
Database::drop_search_index does. It runs in one transaction, so
it waits in the database's line with its transactions, on no thread,
and once polled it runs to its end: the triggers come off the table
in one commit or not at all, whether or not its future is still awaited.
aio::sql::Query
pub struct Query<T> { /* private fields */ }A query on a table whose calls are awaited, built as
sql::Query is: each step returns a new query, so that the first
stays as it was. all, one, scalar, count, explain and list
read on the store's pool; update and delete write.
let urgent: Vec<Order> = db
.table::<Order>("orders")
.filter(sql::eq("status", "pending"))
.order_by("created_at", Direction::Desc)
.limit(50)
.all()
.await?;aio::sql::Query.filter
pub fn filter(&self, condition: impl Into<Sql>) -> SelfAdds a condition with and, as sql::Query::filter does.
aio::sql::Query.select
pub fn select<S>(&self, columns: impl Into<Sql>) -> Query<S>The columns a row holds, as SQL text, and the shape of the row they
make, as sql::Query::select says.
aio::sql::Query.include
pub fn include<C>(&self, name: &str, query: &Query<C>) -> SelfGives each row the rows of another query as a list under name, as
sql::Query::include does, in the same statement.
aio::sql::Query.join
pub fn join(&self, table: &str, on: impl Into<Sql>) -> SelfJoins a table, "users as u", on a condition.
aio::sql::Query.left_join
pub fn left_join(&self, table: &str, on: impl Into<Sql>) -> SelfJoins a table whose columns may be null where no row of it meets the condition.
aio::sql::Query.group_by
pub fn group_by<I: IntoIterator<Item = S>, S: AsRef<str>>(&self, columns: I) -> SelfGroups the rows by columns, quoted as names.
aio::sql::Query.having
pub fn having(&self, condition: impl Into<Sql>) -> SelfA condition on the groups, as filter takes one.
aio::sql::Query.order_by
pub fn order_by(&self, column: &str, direction: Direction) -> SelfOrders the rows by a column, quoted as a name.
aio::sql::Query.order_by_sql
pub fn order_by_sql(&self, piece: impl Into<Sql>, direction: Direction) -> SelfOrders the rows by a piece of SQL: sql!("lower(title)").
aio::sql::Query.limit
pub fn limit(&self, rows: u64) -> Selfaio::sql::Query.offset
pub fn offset(&self, rows: u64) -> Selfaio::sql::Query.to_sql
pub fn to_sql(&self) -> Result<Sql>The text and the values the query sends, to read or to log.
aio::sql::Query.count
pub fn count(&self) -> impl Future<Output = Result<u64>> + Send + 'staticHow many rows the conditions match, or how many groups, as
sql::Query::count counts them.
aio::sql::Query.explain
pub fn explain(&self) -> impl Future<Output = Result<Vec<String>>> + Send + 'staticSQLite's plan for the query, a line a step.
aio::sql::Query.update
pub fn update<I, S>(&self, sets: I) -> impl Future<Output = Result<Done>> + Send + 'static
where
I: IntoIterator<Item = (S, Sql)>,
S: AsRef<str>,Sets columns of the rows the conditions match, as
sql::Query::update does, and says how many changed.
aio::sql::Query.delete
pub fn delete(&self) -> impl Future<Output = Result<Done>> + Send + 'staticDeletes the rows the conditions match, or marks them on a table with
a deleted column, as sql::Query::delete does, and says how many.
aio::sql::Query.with_deleted
pub fn with_deleted(&self) -> SelfThe query of every row, as sql::Query::with_deleted reads them.
aio::sql::Query.deleted_only
pub fn deleted_only(&self) -> SelfThe query of the marked rows alone, as sql::Query::deleted_only reads them.
aio::sql::Query.restore
pub fn restore(&self) -> impl Future<Output = Result<Done>> + Send + 'staticClears the mark of the marked rows the conditions match, as
sql::Query::restore does.
aio::sql::Query.purge
pub fn purge(&self) -> impl Future<Output = Result<Done>> + Send + 'staticDeletes for good what the handle shows of the rows the conditions
match, as sql::Query::purge does; refused on the live rows alone.
aio::sql::Query.if_version
pub fn if_version(&self, version: i64) -> AtVersion<T>The edits made from version, as sql::Query::if_version gives them.
aio::sql::Query.all
pub fn all(&self) -> impl Future<Output = Result<Vec<T>>> + Send + 'staticaio::sql::Query.one
pub fn one(&self) -> impl Future<Output = Result<Option<T>>> + Send + 'staticaio::sql::Query.scalar
pub fn scalar<V: DeserializeOwned + Send + 'static>(
&self,
) -> impl Future<Output = Result<V>> + Send + 'staticaio::sql::Query.list
pub fn list(
&self,
limit: u64,
after: Option<&str>,
) -> impl Future<Output = Result<Page<T>>> + Send + 'staticA page after the last row of the one before, by the query's order, as
sql::Query::list reads one; its next reads the next page, and
only of this query.
aio::sql::Query.each
pub fn each(&self) -> Each<T>The rows one at a time, as sql::Query::each walks them: a Stream
whose parts are read on the store's pool, each row decoded as it is
polled. Ordered by columns by their names, a unique one last, it reads
on past one snapshot's five seconds.
let mut orders = db.table::<Order>("orders").order_by("id", Direction::Asc).each();
while let Some(order) = orders.next().await {
let order = order?;
}aio::sql::Table
pub struct Table<T> { /* private fields */ }
impl Deref<Target = Query<T>>A table whose calls are awaited: its queries, and its rows' inserts and
upserts, as sql::Table has them. Derefs to the query of all its
rows.
aio::sql::Table.name
pub fn name(&self) -> &straio::sql::Table.deleted
pub fn deleted(self, mark: impl Into<Mark>) -> SelfMarks a row deleted rather than deleting it, as sql::Table::deleted does.
aio::sql::Table.version
pub fn version(self, column: &str) -> SelfCounts a row's versions, as sql::Table::version does.
aio::sql::Table.insert
pub fn insert(&self, row: &T) -> impl Future<Output = Result<T>> + Send + 'staticInserts a row and gives it back as the file keeps it, as
sql::Table::insert does. The row is encoded when the call is made.
aio::sql::Table.insert_all
pub fn insert_all(&self, rows: &[T]) -> impl Future<Output = Result<Vec<T>>> + Send + 'staticInserts rows of one shape as one write, all of them or none, as
sql::Table::insert_all does.
aio::sql::Table.upsert
pub fn upsert(
&self,
row: &T,
upsert: Upsert,
) -> impl Future<Output = Result<Option<T>>> + Send + 'staticInserts a row, or updates the row that holds its key, as
sql::Table::upsert does; None when the upsert's condition kept
that row.
aio::sql::Table.upsert_all
pub fn upsert_all(
&self,
rows: &[T],
upsert: Upsert,
) -> impl Future<Output = Result<Vec<T>>> + Send + 'staticUpserts rows of one shape as one statement, all of them or none, as
sql::Table::upsert_all does. The rows are encoded when the call is
made.
aio::sql::AtVersion
pub struct AtVersion<T> { /* private fields */ }The awaited edits made from a version a program saw, as
sql::AtVersion has them.
aio::sql::AtVersion.update
pub fn update<I, S>(&self, sets: I) -> impl Future<Output = Result<Done>> + Send + 'static
where
I: IntoIterator<Item = (S, Sql)>,
S: AsRef<str>,An update of the rows at the version, all of them or none.
aio::sql::AtVersion.delete
pub fn delete(&self) -> impl Future<Output = Result<Done>> + Send + 'staticA delete, a mark on a table with a deleted column, of the rows at the version, all of them or none.
aio::sql::Guarded
pub struct Guarded<'m, T> { /* private fields */ }An awaited mirror's replace with its min_kept guard; Mirror::min_kept
makes one.
aio::sql::Guarded.replace
pub fn replace<S: AsRef<str>, V: Serialize>(
&self,
scope: &[(S, V)],
rows: &[T],
) -> impl Future<Output = Result<Diff<T>>> + Send + 'staticMirror::replace, which writes nothing when the guard holds it back.
aio::sql::Guarded.begin
pub fn begin<S: AsRef<str>, V: Serialize>(&self, scope: &[(S, V)]) -> Result<Run<T>>Mirror::begin, whose commit writes nothing when the guard holds it back.
aio::sql::Mirror
pub struct Mirror<T> { /* private fields */ }A table that mirrors another system whose calls are awaited, the same
mirror as sql::Mirror. Each call checks and converts its rows when it
is made, so that its future borrows nothing, and writes in its first poll.
aio::sql::Mirror.replace
pub fn replace<S: AsRef<str>, V: Serialize>(
&self,
scope: &[(S, V)],
rows: &[T],
) -> impl Future<Output = Result<Diff<T>>> + Send + 'staticThe scope made what rows lists, as sql::Mirror::replace makes it.
aio::sql::Mirror.min_kept
pub fn min_kept(&self, fraction: f64) -> Guarded<'_, T>replace with its min_kept guard, as sql::Mirror::min_kept guards it.
aio::sql::Mirror.begin
pub fn begin<S: AsRef<str>, V: Serialize>(&self, scope: &[(S, V)]) -> Result<Run<T>>A replace whose list comes in parts, as sql::Mirror::begin
begins one: its table checked now, its parts and commit awaited.
aio::sql::Mirror.upsert
pub fn upsert(&self, rows: &[T]) -> impl Future<Output = Result<Diff<T>>> + Send + 'staticWhat rows adds and changes, as sql::Mirror::upsert writes it.
aio::sql::MirrorBuilder
pub struct MirrorBuilder<T> { /* private fields */ }How an awaited mirror opens, with the blocking builder's options.
aio::sql::MirrorBuilder.key
pub fn key<I: IntoIterator<Item = S>, S: Into<String>>(self, columns: I) -> SelfThe columns that name a row at the source, as sql::MirrorBuilder::key takes them.
aio::sql::MirrorBuilder.gone
pub fn gone(self, mark: impl Into<Mark>) -> SelfThe column that marks what the source dropped, as sql::MirrorBuilder::gone says.
aio::sql::MirrorBuilder.change_on
pub fn change_on<I: IntoIterator<Item = S>, S: Into<String>>(self, columns: I) -> SelfThe columns whose change makes a row changed, as sql::MirrorBuilder::change_on says.
aio::sql::MirrorBuilder.open
pub fn open(self) -> Result<Mirror<T>>Opens the mirror as sql::MirrorBuilder::open does: a plain call,
which reads the table's columns and indexes.
aio::sql::Run
pub struct Run<T> { /* private fields */ }An awaited mirror's replace in parts, the same run as sql::Run.
aio::sql::Run.add
pub fn add(&self, rows: &[T]) -> impl Future<Output = Result<()>> + Send + 'staticGives a part of the list, as sql::Run::add does, checked when
the call is made.
aio::sql::Run.commit
pub fn commit(self) -> impl Future<Output = Result<Diff<T>>> + Send + 'staticMakes the scope what every part listed, as sql::Run::commit
does, its answer decoded on the store's pool.