Skip to main content

Database

Struct Database 

Source
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

Source

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

Source

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.

Source§

impl Database

Source

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

Source

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()?;
Source

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

Source

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 stream
Source§

impl Database

Source

pub fn name(&self) -> &str

Source

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.

Source

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.

Source

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.

Source

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?;
}
Source

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.

Source

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.

Source

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.

Source§

impl Database

Source

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.

Trait Implementations§

Source§

impl Clone for Database

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Database

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more