@@ -21,10 +21,12 @@ import { EventIterator } from 'event-iterator';
2121import { quoteIdentifier } from '../utils/strings' ;
2222
2323export interface DisconnectAndClearOptions {
24+ /** When set to false, data in local-only tables is preserved. */
2425 clearLocal ?: boolean ;
2526}
2627
2728export interface PowerSyncDatabaseOptions {
29+ /** Schema used for the local database. */
2830 schema : Schema ;
2931 database : DBAdapter ;
3032 /**
@@ -44,6 +46,7 @@ export interface PowerSyncDatabaseOptions {
4446export interface SQLWatchOptions {
4547 signal ?: AbortSignal ;
4648 tables ?: string [ ] ;
49+ /** The minimum interval between queries. */
4750 throttleMs ?: number ;
4851 /**
4952 * Allows for watching any SQL table
@@ -114,14 +117,25 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
114117 this . _isReadyPromise = this . initialize ( ) ;
115118 }
116119
120+ /**
121+ * Schema used for the local database.
122+ */
117123 get schema ( ) {
118124 return this . _schema ;
119125 }
120126
127+ /**
128+ * The underlying database.
129+ *
130+ * For the most part, behavior is the same whether querying on the underlying database, or on {@link AbstractPowerSyncDatabase}.
131+ */
121132 protected get database ( ) {
122133 return this . options . database ;
123134 }
124135
136+ /**
137+ * Whether a connection to the PowerSync service is currently open.
138+ */
125139 get connected ( ) {
126140 return this . currentStatus ?. connected || false ;
127141 }
@@ -163,6 +177,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
163177 this . iterateListeners ( ( cb ) => cb . initialized ?.( ) ) ;
164178 }
165179
180+ /**
181+ * Replace the schema with a new version. This is for advanced use cases - typically the schema should just be specified once in the constructor.
182+ *
183+ * Cannot be used while connected - this should only be called before {@link AbstractPowerSyncDatabase.connect}.
184+ */
166185 async updateSchema ( schema : Schema ) {
167186 if ( this . abortController ) {
168187 throw new Error ( 'Cannot update schema while connected' ) ;
@@ -226,6 +245,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
226245 this . watchCrudUploads ( ) ;
227246 }
228247
248+ /**
249+ * Close the sync connection.
250+ *
251+ * Use {@link connect} to connect again.
252+ */
229253 async disconnect ( ) {
230254 this . abortController ?. abort ( ) ;
231255 this . syncStatusListenerDisposer ?.( ) ;
@@ -237,6 +261,8 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
237261 * Use this when logging out.
238262 * The database can still be queried after this is called, but the tables
239263 * would be empty.
264+ *
265+ * To preserve data in local-only tables, set clearLocal to false.
240266 */
241267 async disconnectAndClear ( options = DEFAULT_DISCONNECT_CLEAR_OPTIONS ) {
242268 await this . disconnect ( ) ;
@@ -270,7 +296,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
270296 /*
271297 * Close the database, releasing resources.
272298 *
273- * Also [disconnect]s any active connection.
299+ * Also disconnects any active connection.
274300 *
275301 * Once close is called, this connection cannot be used again - a new one
276302 * must be constructed.
@@ -307,12 +333,12 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
307333 *
308334 * Returns null if there is no data to upload.
309335 *
310- * Use this from the [ PowerSyncBackendConnector.uploadData]` callback.
336+ * Use this from the { @link PowerSyncBackendConnector.uploadData} callback.
311337 *
312- * Once the data have been successfully uploaded, call [ CrudBatch.complete] before
338+ * Once the data have been successfully uploaded, call { @link CrudBatch.complete} before
313339 * requesting the next batch.
314340 *
315- * Use [ limit] to specify the maximum number of updates to return in a single
341+ * Use { @link limit} to specify the maximum number of updates to return in a single
316342 * batch.
317343 *
318344 * This method does include transaction ids in the result, but does not group
@@ -358,12 +384,12 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
358384 *
359385 * Returns null if there is no data to upload.
360386 *
361- * Use this from the [ PowerSyncBackendConnector.uploadData]` callback.
387+ * Use this from the { @link PowerSyncBackendConnector.uploadData} callback.
362388 *
363- * Once the data have been successfully uploaded, call [ CrudTransaction.complete] before
389+ * Once the data have been successfully uploaded, call { @link CrudTransaction.complete} before
364390 * requesting the next transaction.
365391 *
366- * Unlike [ getCrudBatch] , this only returns data from a single transaction at a time.
392+ * Unlike { @link getCrudBatch} , this only returns data from a single transaction at a time.
367393 * All data for the transaction is loaded into memory.
368394 */
369395 async getNextCrudTransaction ( ) : Promise < CrudTransaction > {
@@ -413,15 +439,15 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
413439 }
414440
415441 /**
416- * Execute a statement and optionally return results
442+ * Execute a statement and optionally return results.
417443 */
418444 async execute ( sql : string , parameters ?: any [ ] ) {
419445 await this . waitForReady ( ) ;
420446 return this . database . execute ( sql , parameters ) ;
421447 }
422448
423449 /**
424- * Execute a read-only query and return results
450+ * Execute a read-only query and return results.
425451 */
426452 async getAll < T > ( sql : string , parameters ?: any [ ] ) : Promise < T [ ] > {
427453 await this . waitForReady ( ) ;
@@ -446,8 +472,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
446472
447473 /**
448474 * Takes a read lock, without starting a transaction.
449- *
450- * In most cases, [readTransaction] should be used instead.
475+ * In most cases, {@link readTransaction} should be used instead.
451476 */
452477 async readLock < T > ( callback : ( db : DBAdapter ) => Promise < T > ) {
453478 await this . waitForReady ( ) ;
@@ -456,7 +481,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
456481
457482 /**
458483 * Takes a global lock, without starting a transaction.
459- * In most cases, [ writeTransaction] should be used instead.
484+ * In most cases, { @link writeTransaction} should be used instead.
460485 */
461486 async writeLock < T > ( callback : ( db : DBAdapter ) => Promise < T > ) {
462487 await this . waitForReady ( ) ;
@@ -466,6 +491,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
466491 } ) ;
467492 }
468493
494+ /**
495+ * Open a read-only transaction.
496+ * Read transactions can run concurrently to a write transaction.
497+ * Changes from any write transaction are not visible to read transactions started before it.
498+ */
469499 async readTransaction < T > (
470500 callback : ( tx : Transaction ) => Promise < T > ,
471501 lockTimeout : number = DEFAULT_LOCK_TIMEOUT_MS
@@ -481,6 +511,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
481511 ) ;
482512 }
483513
514+ /**
515+ * Open a read-write transaction.
516+ * This takes a global lock - only one write transaction can execute against the database at a time.
517+ * Statements within the transaction must be done on the provided {@link Transaction} interface.
518+ */
484519 async writeTransaction < T > (
485520 callback : ( tx : Transaction ) => Promise < T > ,
486521 lockTimeout : number = DEFAULT_LOCK_TIMEOUT_MS
@@ -496,6 +531,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
496531 ) ;
497532 }
498533
534+ /**
535+ * Execute a read query every time the source tables are modified.
536+ * Use {@link SQLWatchOptions.throttleMs} to specify the minimum interval between queries.
537+ * Source tables are automatically detected using `EXPLAIN QUERY PLAN`.
538+ */
499539 async * watch ( sql : string , parameters ?: any [ ] , options ?: SQLWatchOptions ) : AsyncIterable < QueryResult > {
500540 //Fetch initial data
501541 yield await this . executeReadOnly ( sql , parameters ) ;
@@ -524,7 +564,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
524564 /**
525565 * Create a Stream of changes to any of the specified tables.
526566 *
527- * This is preferred over [ watch] when multiple queries need to be performed
567+ * This is preferred over { @link watch} when multiple queries need to be performed
528568 * together when data is changed.
529569 *
530570 * Note, do not declare this as `async *onChange` as it will not work in React Native
0 commit comments