You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts
+43-2Lines changed: 43 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -114,14 +114,25 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
114
114
this._isReadyPromise=this.initialize();
115
115
}
116
116
117
+
/**
118
+
* Schema used for the local database.
119
+
*/
117
120
getschema(){
118
121
returnthis._schema;
119
122
}
120
123
124
+
/**
125
+
* The underlying database.
126
+
*
127
+
* For the most part, behavior is the same whether querying on the underlying database, or on [AbstractPowerSyncDatabase]. The main difference is in update notifications: the underlying database reports updates to the underlying tables, while AbstractPowerSyncDatabase reports updates to the higher-level views.
128
+
*/
121
129
protectedgetdatabase(){
122
130
returnthis.options.database;
123
131
}
124
132
133
+
/**
134
+
* Whether a connection to the PowerSync service is currently open.
135
+
*/
125
136
getconnected(){
126
137
returnthis.currentStatus?.connected||false;
127
138
}
@@ -163,6 +174,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
163
174
this.iterateListeners((cb)=>cb.initialized?.());
164
175
}
165
176
177
+
/**
178
+
* Replace the schema with a new version. This is for advanced use cases - typically the schema should just be specified once in the constructor.
179
+
*
180
+
* Cannot be used while connected - this should only be called before [AbstractPowerSyncDatabase.connect].
181
+
*/
166
182
asyncupdateSchema(schema: Schema){
167
183
if(this.abortController){
168
184
thrownewError('Cannot update schema while connected');
@@ -226,6 +242,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
226
242
this.watchCrudUploads();
227
243
}
228
244
245
+
/**
246
+
* Close the sync connection.
247
+
*
248
+
* Use [connect] to connect again.
249
+
*/
229
250
asyncdisconnect(){
230
251
this.abortController?.abort();
231
252
this.syncStatusListenerDisposer?.();
@@ -237,6 +258,8 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
237
258
* Use this when logging out.
238
259
* The database can still be queried after this is called, but the tables
239
260
* would be empty.
261
+
*
262
+
* To preserve data in local-only tables, set clearLocal to false.
@@ -466,6 +488,12 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
466
488
});
467
489
}
468
490
491
+
/**
492
+
* Open a read-only transaction.
493
+
* TODO: Up to maxReaders read transactions can run concurrently. After that, read transactions are queued.
494
+
* Read transactions can run concurrently to a write transaction.
495
+
* Changes from any write transaction are not visible to read transactions started before it.
496
+
*/
469
497
asyncreadTransaction<T>(
470
498
callback: (tx: Transaction)=>Promise<T>,
471
499
lockTimeout: number=DEFAULT_LOCK_TIMEOUT_MS
@@ -481,6 +509,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
481
509
);
482
510
}
483
511
512
+
/**
513
+
* Open a read-write transaction.
514
+
* This takes a global lock - only one write transaction can execute against the database at a time.
515
+
* TODO: Statements within the transaction must be done on the provided SqliteWriteContext - attempting statements on the SqliteConnection instance will error.
516
+
*/
484
517
asyncwriteTransaction<T>(
485
518
callback: (tx: Transaction)=>Promise<T>,
486
519
lockTimeout: number=DEFAULT_LOCK_TIMEOUT_MS
@@ -496,6 +529,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
496
529
);
497
530
}
498
531
532
+
/**
533
+
* Execute a read query every time the source tables are modified.
534
+
* TODO: Use throttle to specify the minimum interval between queries.
535
+
* TODO: Source tables are automatically detected using `EXPLAIN QUERY PLAN`.
0 commit comments