pub struct Database { /* private fields */ }Expand description
An application’s database: its reads, its writes, its transactions. Clones share the database, which closes with its store.
Implementations§
Source§impl Database
impl Database
Sourcepub fn bucket<V: Value>(&self, name: &str) -> BucketBuilder<V>
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).
Source§impl Database
impl Database
Sourcepub fn queue<V>(&self, name: &str) -> QueueBuilder<V>
pub fn queue<V>(&self, name: &str) -> QueueBuilder<V>
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.
Source§impl Database
impl Database
Sourcepub fn files(&self, name: &str) -> FilesBuilder<'static>
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>(())
})?;Source§impl Database
impl Database
Sourcepub fn search_index<T: DeserializeOwned + 'static>(
&self,
name: &str,
) -> TableIndexBuilder<T>
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()?;Sourcepub fn drop_search_index(&self, name: &str) -> Result<bool>
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.
Source§impl Database
impl Database
Sourcepub fn events(&self, stream: &str) -> EventsBuilder<'static>
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 streamSource§impl Database
impl Database
pub fn name(&self) -> &str
Sourcepub fn all<T: DeserializeOwned>(
&self,
statement: impl Into<Sql>,
) -> Result<Vec<T>>
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.
Sourcepub fn one<T: DeserializeOwned>(
&self,
statement: impl Into<Sql>,
) -> Result<Option<T>>
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.
Sourcepub fn scalar<T: DeserializeOwned>(
&self,
statement: impl Into<Sql>,
) -> Result<T>
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.
Sourcepub fn each<T: DeserializeOwned>(
&self,
statement: impl Into<Sql>,
) -> Result<Each<T>>
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?;
}Sourcepub fn exec(&self, statement: impl Into<Sql>) -> Result<Done>
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.
Sourcepub fn batch<I>(&self, writes: I) -> Result<Vec<Done>>
pub fn batch<I>(&self, writes: I) -> Result<Vec<Done>>
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.
Sourcepub fn tx<T, E: From<Error>>(
&self,
work: impl FnOnce(&Tx<'_>) -> Result<T, E>,
) -> Result<T, E>
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.