Skip to content

Commit ac800cc

Browse files
authored
Merge pull request #51 from powersync-ja/jsdoc-comments
JSDoc comments for JS
2 parents a88e797 + b4abac5 commit ac800cc

File tree

11 files changed

+142
-25
lines changed

11 files changed

+142
-25
lines changed

packages/powersync-react/src/hooks/usePowerSyncQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { usePowerSync } from './PowerSyncContext';
33

44
/**
55
* A hook to access a single static query.
6-
* For an updated result, use usePowerSyncWatchedQuery instead
6+
* For an updated result, use {@link usePowerSyncWatchedQuery} instead
77
*/
88
export const usePowerSyncQuery = <T = any>(sqlStatement: string, parameters: any[] = []): T[] => {
99
const powerSync = usePowerSync();

packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ import { EventIterator } from 'event-iterator';
2121
import { quoteIdentifier } from '../utils/strings';
2222

2323
export interface DisconnectAndClearOptions {
24+
/** When set to false, data in local-only tables is preserved. */
2425
clearLocal?: boolean;
2526
}
2627

2728
export interface PowerSyncDatabaseOptions {
29+
/** Schema used for the local database. */
2830
schema: Schema;
2931
database: DBAdapter;
3032
/**
@@ -44,6 +46,7 @@ export interface PowerSyncDatabaseOptions {
4446
export 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

packages/powersync-sdk-common/src/client/AbstractPowerSyncOpenFactory.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Schema } from '../db/schema/Schema';
44
import { AbstractPowerSyncDatabase, PowerSyncDatabaseOptions } from './AbstractPowerSyncDatabase';
55

66
export interface PowerSyncOpenFactoryOptions extends Partial<PowerSyncDatabaseOptions> {
7+
/** Schema used for the local database. */
78
schema: Schema;
89
/**
910
* Filename for the database.
@@ -20,6 +21,9 @@ export abstract class AbstractPowerSyncDatabaseOpenFactory {
2021
options.logger = options.logger ?? Logger.get(`PowerSync ${this.options.dbFilename}`);
2122
}
2223

24+
/**
25+
* Schema used for the local database.
26+
*/
2327
get schema() {
2428
return this.options.schema;
2529
}

packages/powersync-sdk-common/src/client/connection/PowerSyncBackendConnector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface PowerSyncBackendConnector {
1616

1717
/** Upload local changes to the app backend.
1818
*
19-
* Use [PowerSyncDatabase.getCrudBatch] to get a batch of changes to upload. See [DevConnector] for an example implementation.
19+
* Use {@link AbstractPowerSyncDatabase.getCrudBatch} to get a batch of changes to upload.
2020
*
2121
* Any thrown errors will result in a retry after the configured wait period (default: 5 seconds).
2222
*/
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import { CrudEntry } from './CrudEntry';
22

3+
/**
4+
* A batch of client-side changes.
5+
*/
36
export class CrudBatch {
47
constructor(
8+
/**
9+
* List of client-side changes.
10+
*/
511
public crud: CrudEntry[],
12+
/**
13+
* true if there are more changes in the local queue.
14+
*/
615
public haveMore: boolean,
16+
/**
17+
* Call to remove the changes from the local queue, once successfully uploaded.
18+
*/
719
public complete: (writeCheckpoint?: string) => Promise<void>
820
) {}
921
}

packages/powersync-sdk-common/src/client/sync/bucket/CrudEntry.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,33 @@ export type CrudEntryOutputJSON = {
3838
data: Record<string, any>;
3939
};
4040

41+
/**
42+
* A single client-side change.
43+
*/
4144
export class CrudEntry {
45+
/**
46+
* Auto-incrementing client-side id.
47+
*/
4248
clientId: number;
49+
/**
50+
* ID of the changed row.
51+
*/
4352
id: string;
53+
/**
54+
* Type of change.
55+
*/
4456
op: UpdateType;
57+
/**
58+
* Data associated with the change.
59+
*/
4560
opData?: Record<string, any>;
61+
/**
62+
* Table that contained the change.
63+
*/
4664
table: string;
65+
/**
66+
* Auto-incrementing transaction id. This is the same for all operations within the same transaction.
67+
*/
4768
transactionId?: number;
4869

4970
static fromRow(dbRow: CrudEntryJSON) {
@@ -67,6 +88,9 @@ export class CrudEntry {
6788
this.transactionId = transactionId;
6889
}
6990

91+
/**
92+
* Converts the change to JSON format.
93+
*/
7094
toJSON(): CrudEntryOutputJSON {
7195
return {
7296
op_id: this.clientId,
@@ -78,6 +102,9 @@ export class CrudEntry {
78102
};
79103
}
80104

105+
/**
106+
* The hash code for this object.
107+
*/
81108
hashCode() {
82109
return hash([this.transactionId, this.clientId, this.op, this.table, this.id, this.opData]);
83110
}

packages/powersync-sdk-common/src/client/sync/bucket/CrudTransaction.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@ import { CrudEntry } from './CrudEntry';
33

44
export class CrudTransaction extends CrudBatch {
55
constructor(
6+
/**
7+
* List of client-side changes.
8+
*/
69
public crud: CrudEntry[],
10+
/**
11+
* Call to remove the changes from the local queue, once successfully uploaded.
12+
*/
713
public complete: (checkpoint?: string) => Promise<void>,
14+
/**
15+
* If null, this contains a list of changes recorded without an explicit transaction associated.
16+
*/
817
public transactionId?: number
918
) {
1019
super(crud, false, complete);

packages/powersync-sdk-common/src/client/sync/bucket/SqliteBucketStorage.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ export class SqliteBucketStorage implements BucketStorageAdapter {
8080

8181
/**
8282
* Mark a bucket for deletion.
83-
*
84-
* @param bucket
8583
*/
8684
private async deleteBucket(bucket: string) {
8785
// Delete a bucket, but allow it to be re-created.

packages/powersync-sdk-common/src/db/DBAdapter.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@ import { BaseListener, BaseObserverInterface } from '../utils/BaseObserver';
1111
*/
1212

1313
/**
14-
* Object returned by SQL Query executions {
15-
* insertId: Represent the auto-generated row id if applicable
16-
* rowsAffected: Number of affected rows if result of a update query
17-
* message: if status === 1, here you will find error description
18-
* rows: if status is undefined or 0 this object will contain the query results
19-
* }
20-
*
21-
* @interface QueryResult
14+
* Object returned by SQL Query executions.
2215
*/
2316
export type QueryResult = {
17+
/** Represents the auto-generated row id if applicable. */
2418
insertId?: number;
19+
/** Number of affected rows if result of a update query. */
2520
rowsAffected: number;
21+
/** if status is undefined or 0 this object will contain the query results */
2622
rows?: {
2723
/** Raw array with all dataset */
2824
_array: any[];
@@ -37,12 +33,16 @@ export type QueryResult = {
3733
};
3834

3935
export interface DBGetUtils {
36+
/** Execute a read-only query and return results. */
4037
getAll<T>(sql: string, parameters?: any[]): Promise<T[]>;
38+
/** Execute a read-only query and return the first result, or null if the ResultSet is empty. */
4139
getOptional<T>(sql: string, parameters?: any[]): Promise<T | null>;
40+
/** Execute a read-only query and return the first result, error if the ResultSet is empty. */
4241
get<T>(sql: string, parameters?: any[]): Promise<T>;
4342
}
4443

4544
export interface LockContext extends DBGetUtils {
45+
/** Execute a statement and optionally return results. */
4646
execute: (query: string, params?: any[] | undefined) => Promise<QueryResult>;
4747
}
4848

@@ -63,6 +63,10 @@ export interface TableUpdateOperation {
6363
opType: RowUpdateType;
6464
rowId: number;
6565
}
66+
67+
/**
68+
* Notification of an update to one or more tables, for the purpose of realtime change notifications.
69+
*/
6670
export interface UpdateNotification extends TableUpdateOperation {
6771
table: string;
6872
}

0 commit comments

Comments
 (0)