PowerSyncDatabase class

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.

Implemented types
Mixed in types

Constructors

PowerSyncDatabase({required Schema schema, required String path, int maxReaders = SqliteDatabase.defaultMaxReaders, Logger? logger, @Deprecated("Use [PowerSyncDatabase.withFactory] instead.") SqliteConnectionSetup? sqliteSetup})
Open a PowerSyncDatabase.
factory
PowerSyncDatabase.withDatabase({required Schema schema, required SqliteDatabase database, Logger? logger})
Open a PowerSyncDatabase on an existing SqliteDatabase.
PowerSyncDatabase.withFactory(PowerSyncOpenFactory openFactory, {required Schema schema, int maxReaders = SqliteDatabase.defaultMaxReaders, Logger? logger})
Open a PowerSyncDatabase with a PowerSyncOpenFactory.
factory

Properties

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.
getter/setter pair
database SqliteDatabase
The underlying database.
final
hashCode int
The hash code for this object.
no setterinherited
logger ↔ Logger
The Logger used by this PowerSyncDatabase.
latefinal
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.
getter/setter pair
statusStream Stream<SyncStatus>
Use this stream to subscribe to connection status updates.
latefinal
updates Stream<UpdateNotification>
Broadcast stream that is notified of any table updates.
latefinaloverride-getter

Methods

close() Future<void>
Close the database, releasing resources.
override
computeWithDatabase<T>(Future<T> compute(Database db)) Future<T>
See SqliteReadContext.computeWithDatabase.
inherited
connect({required PowerSyncBackendConnector connector, Duration crudThrottleTime = const Duration(milliseconds: 10)}) 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).
override
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
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
getCrudBatch({dynamic limit = 100}) Future<CrudBatch?>
Get a batch of crud data to upload.
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.
isolateConnectionFactory() IsolateConnectionFactory
A connection factory that can be passed to different isolates.
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
readLock<T>(Future<T> callback(SqliteReadContext tx), {String? debugContext, Duration? lockTimeout}) Future<T>
Takes a read lock, without starting a transaction.
override
readTransaction<T>(Future<T> callback(SqliteReadContext tx), {Duration? lockTimeout}) Future<T>
Open a read-only transaction.
inherited
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.
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.
inherited
writeLock<T>(Future<T> callback(SqliteWriteContext tx), {String? debugContext, Duration? lockTimeout}) Future<T>
Takes a global lock, without starting a transaction.
override
writeTransaction<T>(Future<T> callback(SqliteWriteContext tx), {Duration? lockTimeout, String? debugContext}) Future<T>
Open a read-write transaction.
override

Operators

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