Skip to main content

PowerSyncDatabase

Struct PowerSyncDatabase 

Source
pub struct PowerSyncDatabase { /* private fields */ }

Implementations§

Source§

impl PowerSyncDatabase

An opened database managed by the PowerSync SDK.

To run SQL statements against this database, use Self::reader and Self::writer.

To connect to a PowerSync service, first call [Self::download_actor] and [Self::upload_actor] and have those futures be polled by an async runtime of your choice. They will run as long as the database is active. Then, call Self::connect to establish a connection to the PowerSync service.

Source

pub fn new(env: PowerSyncEnvironment, schema: Schema) -> Self

Source

pub fn async_tasks(&self) -> AsyncDatabaseTasks

Returns a collection of [AsyncDatabaseTasks] that need to be started before connecting this PowerSync database to a PowerSync service.

To start the tasks, see the documentation on [AsyncDatabaseTasks].

By exposing these async tasks instead of starting them automatically, the SDK stays executor-agnostic and is easier to access from C.

Source

pub async fn connect(&self, options: SyncOptions)

Requests the download actor, started with [Self::download_actor], to start establishing a connection to the PowerSync service.

Source

pub async fn disconnect(&self)

If the sync client is currently connected, requests it to disconnect.

Source

pub fn watch_tables<'a, Tables: IntoIterator<Item = impl Into<Cow<'a, str>>>>( &self, emit_initially: bool, tables: Tables, ) -> impl Stream<Item = ()> + 'static

Returns an asynchronous Stream emitting an empty event every time one of the specified tables is written to.

The emit_initially option can be used to control whether the stream should emit as well when polled for the first time. This can be useful to build streams emitting a complete snapshot of results every time a source table is changed.

Source

pub fn watch_statement<T, F, P: Params + Clone + 'static>( &self, sql: String, params: P, read: F, ) -> impl Stream<Item = Result<T, PowerSyncError>> + 'static
where for<'a> F: Fn(&'a mut Statement<'_>, P) -> Result<T, PowerSyncError> + 'static + Clone,

Returns an asynchronous Stream emitting snapshots of a SELECT statement every time source tables are modified.

sql is the SELECT statement to execute and params are parameters to use. The read function obtains the raw prepared statement and a copy of parameters to use, and can run the statements into desired results.

This method is a core building block for reactive applications with PowerSync - since it updates automatically, all writes (regardless of whether they’re local or due to synced writes from your backend) are reflected.

Source

pub fn crud_transactions<'a>( &'a self, ) -> impl Stream<Item = Result<CrudTransaction<'a>, PowerSyncError>> + 'a

Returns a Stream traversing through transactions that have been completed on this database.

Each CrudTransaction contains all local writes made in that transaction, allowing a backend connector to upload them.

Source

pub async fn next_crud_transaction<'a>( &'a self, ) -> Result<Option<CrudTransaction<'a>>, PowerSyncError>

Returns the first transaction that has not been marked as completed.

This is always the first item of Self::crud_transactions, see that method for details.

Source

pub fn status(&self) -> Arc<SyncStatusData>

Returns the current SyncStatusData snapshot reporting the sync state of this database.

Source

pub fn watch_status<'a>( &'a self, ) -> impl Stream<Item = Arc<SyncStatusData>> + 'a

Returns an updating Stream of SyncStatusData events emitting every time the status is changed.

Source

pub fn sync_stream<'a>( &'a self, name: &'a str, parameters: Option<&Value>, ) -> SyncStream<'a>

Creates a SyncStream based on name and optional parameters.

PowerSync will sync data from the requested stream when calling SyncStream::subscribe. After the subscription handle returned by that method is dropped, PowerSync will continue syncing the stream for a specified amount of time as a local cache.

Source

pub async fn reader(&self) -> Result<impl LeasedConnection, PowerSyncError>

Obtains a LeasedConnection that can be used to run read-only queries on this database.

Source

pub async fn writer(&self) -> Result<impl LeasedConnection, PowerSyncError>

Obtains a LeasedConnection allowing reading and writing queries.

Trait Implementations§

Source§

impl Clone for PowerSyncDatabase

Source§

fn clone(&self) -> PowerSyncDatabase

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for PowerSyncDatabase

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, 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,