PowerSyncDatabase class abstract base

A PowerSync managed database.

Use one instance per database file.

Use PowerSyncDatabase.connect to connect to the PowerSync service, to keep the local database in sync with the remote database.

All changes to local tables are automatically recorded, whether connected or not. Once connected, the changes are uploaded.

Inheritance

Constructors

PowerSyncDatabase({required Schema schema, required String path, @Deprecated('This argument is not used, set it on the factory instead') int maxReaders = SqliteOptions.defaultMaxReaders, Logger? logger, SqliteOptions sqliteOptions = powerSyncDefaultSqliteOptions, EncryptionOptions? encryption})
Open a PowerSyncDatabase.
factory
PowerSyncDatabase.withDatabase({required Schema schema, required SqliteDatabase database, Logger? logger, @Deprecated("Use [logger] instead") Logger? loggers})
Open a PowerSyncDatabase on an existing SqliteDatabase.
factory
PowerSyncDatabase.withFactory(SqliteOpenFactory openFactory, {required Schema schema, @Deprecated('This argument is not used, set it on the factory instead') int maxReaders = SqliteOptions.defaultMaxReaders, Logger? logger})
Open a PowerSyncDatabase with a SqliteOpenFactory.
factory

Properties

clientParams Map<String, dynamic>?
getter/setter pair
closed bool
Returns true if the connection is closed
no setteroverride
connected bool
Whether a connection to the PowerSync service is currently open.
no setter
currentStatus SyncStatus
Current connection status.
no setter
database SqliteDatabase
The underlying database.
no setter
hashCode int
The hash code for this object.
no setterinherited
isInitialized Future<void>
no setter
logger → Logger
The Logger used by this PowerSyncDatabase.
no setter
retryDelay Duration
Delay between retrying failed requests. Defaults to 5 seconds. Only has an effect if changed before calling connect.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
schema Schema
Schema used for the local database.
no setter
statusStream Stream<SyncStatus>
Use this stream to subscribe to connection status updates.
no setter
updates Stream<UpdateNotification>
Broadcast stream that is notified of any table updates.
no setteroverride

Methods

abortableReadLock<T>(Future<T> callback(SqliteReadContext tx), {Future<void>? abortTrigger, String? debugContext}) Future<T>
Takes a read lock, without starting a transaction.
override
abortableWriteLock<T>(Future<T> callback(SqliteWriteContext tx), {Future<void>? abortTrigger, String? debugContext}) Future<T>
Takes a global lock, without starting a transaction.
override
close() Future<void>
Close the database, releasing resources.
override
computeWithDatabase<T>(Future<T> compute(CommonDatabase db)) Future<T>
See SqliteReadContext.computeWithDatabase.
inherited
connect({required PowerSyncBackendConnector connector, SyncOptions? options, Duration? crudThrottleTime, Map<String, dynamic>? params}) Future<void>
Connect to the PowerSync service, and keep the databases in sync.
disconnect() Future<void>
Close the sync connection.
disconnectAndClear({bool clearLocal = true}) Future<void>
Disconnect and clear the database.
disconnectedAndClear() Future<void>
execute(String sql, [List<Object?> parameters = const []]) Future<ResultSet>
Execute a write query (INSERT, UPDATE, DELETE) and return the results (if any).
inherited
executeBatch(String sql, List<List<Object?>> parameterSets) Future<void>
Execute a write query (INSERT, UPDATE, DELETE) multiple times with each parameter set. This is more faster than executing separately with each parameter set.
inherited
executeMultiple(String sql) Future<void>
inherited
get(String sql, [List<Object?> parameters = const []]) Future<Row>
Execute a read-only (SELECT) query and return a single result.
inherited
getAll(String sql, [List<Object?> parameters = const []]) Future<ResultSet>
Execute a read-only (SELECT) query and return the results.
inherited
getAutoCommit() Future<bool>
Returns true if auto-commit is enabled. This means the database is not currently in a transaction. This may be true even if a transaction lock is still held, when the transaction has been committed or rolled back.
override
getClientId() Future<String>
Get an unique id for this client. This id is only reset when the database is deleted.
getCrudBatch({int limit = 100}) Future<CrudBatch?>
Get a batch of crud data to upload.
getCrudTransactions() Stream<CrudTransaction>
Returns a stream of completed transactions with local writes against the database.
getNextCrudTransaction() Future<CrudTransaction?>
Get the next recorded transaction to upload.
getOptional(String sql, [List<Object?> parameters = const []]) Future<Row?>
Execute a read-only (SELECT) query and return a single optional result.
inherited
getUploadQueueStats({bool includeSize = false}) Future<UploadQueueStats>
Get upload queue size estimate and count.
initialize() Future<void>
Wait for initialization to complete.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onChange(Iterable<String>? tables, {Duration? throttle = const Duration(milliseconds: 30), bool triggerImmediately = true}) Stream<UpdateNotification>
Create a Stream of changes to any of the specified tables.
inherited
onChangeUnthrottled(Iterable<String>? tables, {bool triggerImmediately = true}) Stream<UpdateNotification>
Like onChange, but without a defaut throttle duration.
inherited
readLock<T>(Future<T> callback(SqliteReadContext tx), {Duration? lockTimeout, String? debugContext}) Future<T>
Takes a read lock, without starting a transaction.
inherited
readTransaction<T>(Future<T> callback(SqliteReadContext tx), {Duration? lockTimeout}) Future<T>
Open a read-only transaction.
override
refreshSchema() Future<void>
Ensures that all connections are aware of the latest schema changes applied (if any). Queries and watch calls can potentially use outdated schema information after a schema update.
override
setStatus(SyncStatus status) → void
syncStream(String name, [Map<String, Object?>? parameters]) SyncStream
Create a SyncStream instance for the given name and parameters.
toString() String
A string representation of this object.
inherited
updateSchema(Schema schema) Future<void>
Replace the schema with a new version. This is for advanced use cases - typically the schema should just be specified once in the constructor.
waitForFirstSync({StreamPriority? priority}) Future<void>
Returns a Future which will resolve once at least one full sync cycle has completed (meaninng that the first consistent checkpoint has been reached across all buckets).
watch(String sql, {List<Object?> parameters = const [], Duration? throttle = const Duration(milliseconds: 30), Iterable<String>? triggerOnTables}) Stream<ResultSet>
Execute a read query every time the source tables are modified.
override
watchUnthrottled(String sql, {List<Object?> parameters = const [], Iterable<String>? triggerOnTables}) Stream<ResultSet>
Like watch, but without a default throttle.
inherited
writeLock<T>(Future<T> callback(SqliteWriteContext tx), {Duration? lockTimeout, String? debugContext}) Future<T>
Takes a global lock, without starting a transaction.
inherited
writeTransaction<T>(Future<T> callback(SqliteWriteContext tx), {Duration? lockTimeout}) Future<T>
Open a read-write transaction.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

openFactory({required String path, SqliteOptions options = const SqliteOptions(), EncryptionOptions? encryption}) SqliteOpenFactory
The default SqliteOpenFactory for PowerSync databases on the current platform.