PowerSyncDatabase
A PowerSync database which provides SQLite functionality which is automatically synced.
Example
export const db = new PowerSyncDatabase({
schema: AppSchema,
database: {
dbFilename: 'example.db'
}
});
Extends
Constructors
new PowerSyncDatabase()
new PowerSyncDatabase(options): PowerSyncDatabase
Parameters
Parameter | Type |
---|---|
options | NodePowerSyncDatabaseOptions |
Returns
Overrides
AbstractPowerSyncDatabase
.constructor
Properties
Property | Modifier | Type | Description | Inherited from |
---|---|---|---|---|
closed | public | boolean | Returns true if the connection is closed. | AbstractPowerSyncDatabase .closed |
currentStatus | public | SyncStatus | Current connection status. | AbstractPowerSyncDatabase .currentStatus |
logger | public | ILogger | - | AbstractPowerSyncDatabase .logger |
ready | public | boolean | - | AbstractPowerSyncDatabase .ready |
sdkVersion | public | string | - | AbstractPowerSyncDatabase .sdkVersion |
triggers | readonly | TriggerManager | Experimental Allows creating SQLite triggers which can be used to track various operations on SQLite tables. | AbstractPowerSyncDatabase .triggers |
Accessors
connected
Get Signature
get connected(): boolean
Whether a connection to the PowerSync service is currently open.
Returns
boolean
Inherited from
AbstractPowerSyncDatabase
.connected
connecting
Get Signature
get connecting(): boolean
Returns
boolean
Inherited from
AbstractPowerSyncDatabase
.connecting
connectionOptions
Get Signature
get connectionOptions():
| null
| InternalConnectionOptions
The resolved connection options used to connect to the PowerSync service.
Returns
| null
| InternalConnectionOptions
The resolved connection options used to connect to the PowerSync service or null if connect()
has not been called.
Inherited from
AbstractPowerSyncDatabase
.connectionOptions
connector
Get Signature
get connector():
| null
| PowerSyncBackendConnector
The connector used to connect to the PowerSync service.
Returns
| null
| PowerSyncBackendConnector
The connector used to connect to the PowerSync service or null if connect()
has not been called.
Inherited from
AbstractPowerSyncDatabase
.connector
database
Get Signature
get database(): DBAdapter
The underlying database.
For the most part, behavior is the same whether querying on the underlying database, or on AbstractPowerSyncDatabase.
Returns
Inherited from
AbstractPowerSyncDatabase
.database
schema
Get Signature
get schema(): Schema<{}>
Schema used for the local database.
Returns
Schema
<{}>
Inherited from
AbstractPowerSyncDatabase
.schema
syncStreamImplementation
Get Signature
get syncStreamImplementation():
| null
| StreamingSyncImplementation
Returns
| null
| StreamingSyncImplementation
Inherited from
AbstractPowerSyncDatabase
.syncStreamImplementation
Methods
_initialize()
_initialize(): Promise<void>
Allows for extended implementations to execute custom initialization logic as part of the total init process
Returns
Promise
<void
>
Overrides
AbstractPowerSyncDatabase
._initialize
close()
close(options?): Promise<void>
Close the database, releasing resources.
Also disconnects any active connection.
Once close is called, this connection cannot be used again - a new one must be constructed.
Parameters
Parameter | Type |
---|---|
options ? | PowerSyncCloseOptions |
Returns
Promise
<void
>
Inherited from
AbstractPowerSyncDatabase
.close
connect()
connect(connector, options?): Promise<void>
Connects to stream of events from the PowerSync instance.
Parameters
Parameter | Type |
---|---|
connector | PowerSyncBackendConnector |
options ? | PowerSyncConnectionOptions & NodeCustomConnectionOptions |
Returns
Promise
<void
>
Overrides
AbstractPowerSyncDatabase
.connect
customQuery()
customQuery<RowType>(query): Query<RowType>
Allows building a WatchedQuery using an existing WatchCompatibleQuery. The watched query will use the provided WatchCompatibleQuery.execute method to query results.
Type Parameters
Type Parameter |
---|
RowType |
Parameters
Parameter | Type |
---|---|
query | WatchCompatibleQuery <RowType []> |
Returns
Query
<RowType
>
Example
// Potentially a query from an ORM like Drizzle
const query = db.select().from(lists);
const watchedTodos = powersync.customQuery(query)
.watch()
// OR use .differentialWatch() for fine-grained watches.
Inherited from
AbstractPowerSyncDatabase
.customQuery
disconnect()
disconnect(): Promise<void>
Close the sync connection.
Use connect to connect again.
Returns
Promise
<void
>
Inherited from
AbstractPowerSyncDatabase
.disconnect
disconnectAndClear()
disconnectAndClear(options?): Promise<void>
Disconnect and clear the database. Use this when logging out. The database can still be queried after this is called, but the tables would be empty.
To preserve data in local-only tables, set clearLocal to false.
Parameters
Parameter | Type |
---|---|
options ? | DisconnectAndClearOptions |
Returns
Promise
<void
>
Inherited from
AbstractPowerSyncDatabase
.disconnectAndClear
dispose()
dispose(): void
Returns
void
Deprecated
Use AbstractPowerSyncDatabase#close instead. Clears all listeners registered by AbstractPowerSyncDatabase#registerListener.
Inherited from
AbstractPowerSyncDatabase
.dispose
execute()
execute(sql, parameters?): Promise<QueryResult>
Execute a SQL write (INSERT/UPDATE/DELETE) query and optionally return results.
Parameters
Parameter | Type | Description |
---|---|---|
sql | string | The SQL query to execute |
parameters ? | any [] | Optional array of parameters to bind to the query |
Returns
Promise
<QueryResult
>
The query result as an object with structured key-value pairs
Inherited from
AbstractPowerSyncDatabase
.execute
executeBatch()
executeBatch(sql, parameters?): Promise<QueryResult>
Execute a write query (INSERT/UPDATE/DELETE) multiple times with each parameter set and optionally return results. This is faster than executing separately with each parameter set.
Parameters
Parameter | Type | Description |
---|---|---|
sql | string | The SQL query to execute |
parameters ? | any [][] | Optional 2D array of parameter sets, where each inner array is a set of parameters for one execution |
Returns
Promise
<QueryResult
>
The query result
Inherited from
AbstractPowerSyncDatabase
.executeBatch
executeRaw()
executeRaw(sql, parameters?): Promise<any[][]>
Execute a SQL write (INSERT/UPDATE/DELETE) query directly on the database without any PowerSync processing. This bypasses certain PowerSync abstractions and is useful for accessing the raw database results.
Parameters
Parameter | Type | Description |
---|---|---|
sql | string | The SQL query to execute |
parameters ? | any [] | Optional array of parameters to bind to the query |
Returns
Promise
<any
[][]>
The raw query result from the underlying database as a nested array of raw values, where each row is represented as an array of column values without field names.
Inherited from
AbstractPowerSyncDatabase
.executeRaw
get()
get<T>(sql, parameters?): Promise<T>
Execute a read-only query and return the first result, error if the ResultSet is empty.
Type Parameters
Type Parameter |
---|
T |
Parameters
Parameter | Type | Description |
---|---|---|
sql | string | The SQL query to execute |
parameters ? | any [] | Optional array of parameters to bind to the query |
Returns
Promise
<T
>
The first result matching the query
Throws
Error if no rows are returned
Inherited from
getAll()
getAll<T>(sql, parameters?): Promise<T[]>
Execute a read-only query and return results.
Type Parameters
Type Parameter |
---|
T |
Parameters
Parameter | Type | Description |
---|---|---|
sql | string | The SQL query to execute |
parameters ? | any [] | Optional array of parameters to bind to the query |
Returns
Promise
<T
[]>
An array of results
Inherited from
AbstractPowerSyncDatabase
.getAll