Skip to content

Commit b3bcf01

Browse files
committed
First pass of porting comments from FLutter into sdk-common
1 parent 0f1d246 commit b3bcf01

File tree

8 files changed

+112
-4
lines changed

8 files changed

+112
-4
lines changed

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,25 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
114114
this._isReadyPromise = this.initialize();
115115
}
116116

117+
/**
118+
* Schema used for the local database.
119+
*/
117120
get schema() {
118121
return this._schema;
119122
}
120123

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+
*/
121129
protected get database() {
122130
return this.options.database;
123131
}
124132

133+
/**
134+
* Whether a connection to the PowerSync service is currently open.
135+
*/
125136
get connected() {
126137
return this.currentStatus?.connected || false;
127138
}
@@ -163,6 +174,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
163174
this.iterateListeners((cb) => cb.initialized?.());
164175
}
165176

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+
*/
166182
async updateSchema(schema: Schema) {
167183
if (this.abortController) {
168184
throw new Error('Cannot update schema while connected');
@@ -226,6 +242,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
226242
this.watchCrudUploads();
227243
}
228244

245+
/**
246+
* Close the sync connection.
247+
*
248+
* Use [connect] to connect again.
249+
*/
229250
async disconnect() {
230251
this.abortController?.abort();
231252
this.syncStatusListenerDisposer?.();
@@ -237,6 +258,8 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
237258
* Use this when logging out.
238259
* The database can still be queried after this is called, but the tables
239260
* would be empty.
261+
*
262+
* To preserve data in local-only tables, set clearLocal to false.
240263
*/
241264
async disconnectAndClear(options = DEFAULT_DISCONNECT_CLEAR_OPTIONS) {
242265
await this.disconnect();
@@ -307,7 +330,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
307330
*
308331
* Returns null if there is no data to upload.
309332
*
310-
* Use this from the [PowerSyncBackendConnector.uploadData]` callback.
333+
* Use this from the [PowerSyncBackendConnector.uploadData] callback.
311334
*
312335
* Once the data have been successfully uploaded, call [CrudBatch.complete] before
313336
* requesting the next batch.
@@ -446,7 +469,6 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
446469

447470
/**
448471
* Takes a read lock, without starting a transaction.
449-
*
450472
* In most cases, [readTransaction] should be used instead.
451473
*/
452474
async readLock<T>(callback: (db: DBAdapter) => Promise<T>) {
@@ -466,6 +488,12 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
466488
});
467489
}
468490

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+
*/
469497
async readTransaction<T>(
470498
callback: (tx: Transaction) => Promise<T>,
471499
lockTimeout: number = DEFAULT_LOCK_TIMEOUT_MS
@@ -481,6 +509,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
481509
);
482510
}
483511

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+
*/
484517
async writeTransaction<T>(
485518
callback: (tx: Transaction) => Promise<T>,
486519
lockTimeout: number = DEFAULT_LOCK_TIMEOUT_MS
@@ -496,6 +529,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
496529
);
497530
}
498531

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`.
536+
*/
499537
async *watch(sql: string, parameters?: any[], options?: SQLWatchOptions): AsyncIterable<QueryResult> {
500538
//Fetch initial data
501539
yield await this.executeReadOnly(sql, parameters);
@@ -582,6 +620,9 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
582620
});
583621
}
584622

623+
/**
624+
* TODO
625+
*/
585626
private async executeReadOnly(sql: string, params: any[]) {
586627
await this.waitForReady();
587628
return this.database.readLock((tx) => tx.execute(sql, params));

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export abstract class AbstractPowerSyncDatabaseOpenFactory {
2020
options.logger = options.logger ?? Logger.get(`PowerSync ${this.options.dbFilename}`);
2121
}
2222

23+
/**
24+
* Schema used for the local database.
25+
*/
2326
get schema() {
2427
return this.options.schema;
2528
}
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+
* TODO
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+
* TODO: Converts the change to JSON format, as required by the dev crud API.
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import { CrudBatch } from './CrudBatch';
22
import { CrudEntry } from './CrudEntry';
33

4+
/**
5+
* TODO
6+
*/
47
export class CrudTransaction extends CrudBatch {
58
constructor(
9+
/**
10+
* List of client-side changes.
11+
*/
612
public crud: CrudEntry[],
13+
/**
14+
* Call to remove the changes from the local queue, once successfully uploaded.
15+
*/
716
public complete: (checkpoint?: string) => Promise<void>,
17+
/**
18+
* Unique transaction id.
19+
*/
820
public transactionId?: number
921
) {
1022
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/crud/SyncStatus.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@ export type SyncStatusOptions = {
1414
export class SyncStatus {
1515
constructor(protected options: SyncStatusOptions) {}
1616

17+
/**
18+
* true if currently connected.
19+
*/
1720
get connected() {
1821
return this.options.connected ?? false;
1922
}
2023

24+
/**
25+
* TODO
26+
*/
2127
get lastSyncedAt() {
2228
return this.options.lastSyncedAt;
2329
}
2430

31+
/**
32+
* TODO
33+
*/
2534
get dataFlowStatus() {
2635
return (
2736
this.options.dataFlow ?? {

packages/powersync-sdk-common/src/db/crud/UploadQueueStatus.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
export class UploadQueueStats {
22
constructor(
3+
/**
4+
* Number of records in the upload queue.
5+
*/
36
public count: number,
7+
/**
8+
* Size of the upload queue in bytes.
9+
*/
410
public size: number = null
511
) {}
612

0 commit comments

Comments
 (0)