Skip to content

Commit eec4d84

Browse files
authored
Merge pull request #66 from powersync-ja/improved-documentation
Improve Readmes and code commenting
2 parents a17e14d + aee0eaf commit eec4d84

File tree

10 files changed

+88
-113
lines changed

10 files changed

+88
-113
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
<a href="https://www.powersync.com" target="_blank"><img src="https://github.com/powersync-ja/.github/assets/19345049/602bafa0-41ce-4cee-a432-56848c278722"/></a>
33
</p>
44

5-
PowerSync is a service and set of SDKs that keeps Postgres databases in sync with on-device SQLite databases.
5+
_Bad connectivity is everywhere, and we're tired of it. PowerSync is on a mission to help developers write offline-first real-time reactive apps._
6+
7+
[PowerSync](https://powersync.com) is a service and set of SDKs that keeps Postgres databases in sync with on-device SQLite databases.
68

79
# PowerSync JavaScript SDKs
810

@@ -31,7 +33,7 @@ PowerSync is a service and set of SDKs that keeps Postgres databases in sync wit
3133

3234
## Demo Apps / Example Projects
3335

34-
Demo applications are located in the [`demos/`](./demos/) directory.
36+
Demo applications are located in the [`demos/`](./demos/) directory. Also see our [Demo Apps / Example Projects](https://docs.powersync.com/resources/demo-apps-example-projects) gallery which lists all projects by the backend and client-side framework they use.
3537

3638
- [demos/nextjs-supabase-todolist](./demos/nextjs-supabase-todolist/): A Next.js to-do list example app using the PowerSync Web SDK and a Supabase backend.
3739
- [demos/yjs-nextjs-supabase-text-collab](./demos/yjs-nextjs-supabase-text-collab/README.md): A Next.js real-time text editing collaboration example app powered by [Yjs](https://github.com/yjs/yjs) CRDTs and [Tiptap](https://tiptap.dev/), using the PowerSync Web SDK and a Supabase backend.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# PowerSync SDK common JS
22

3-
This package contains pure TypeScript common functionality for the PowerSync SDK.
3+
This package contains a TypeScript implementation of a PowerSync database connector and streaming sync bucket implementation, which is used in the following SDKs:
4+
- [packages/powersync-sdk-react-native](./packages/powersync-sdk-react-native/README.md)
5+
- [packages/powersync-sdk-web](./packages/powersync-sdk-web/README.md)

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,15 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
9191
*/
9292
protected static transactionMutex: Mutex = new Mutex();
9393

94+
/**
95+
* Returns true if the connection is closed.
96+
*/
9497
closed: boolean;
9598
ready: boolean;
9699

100+
/**
101+
* Current connection status.
102+
*/
97103
currentStatus?: SyncStatus;
98104
syncStreamImplementation?: AbstractStreamingSyncImplementation;
99105
sdkVersion: string;
@@ -202,7 +208,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
202208
}
203209

204210
/**
205-
* Queues a CRUD upload when internal CRUD tables have been updated
211+
* Queues a CRUD upload when internal CRUD tables have been updated.
206212
*/
207213
protected async watchCrudUploads() {
208214
for await (const event of this.onChange({
@@ -223,7 +229,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
223229
}
224230

225231
/**
226-
* Connects to stream of events from PowerSync instance
232+
* Connects to stream of events from the PowerSync instance.
227233
*/
228234
async connect(connector: PowerSyncBackendConnector) {
229235
// close connection if one is open
@@ -293,7 +299,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
293299
});
294300
}
295301

296-
/*
302+
/**
297303
* Close the database, releasing resources.
298304
*
299305
* Also disconnects any active connection.
@@ -432,7 +438,8 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
432438
}
433439

434440
/**
435-
* Execute a statement and optionally return results.
441+
* Execute a write (INSERT/UPDATE/DELETE) query
442+
* and optionally return results.
436443
*/
437444
async execute(sql: string, parameters?: any[]) {
438445
await this.waitForReady();
@@ -615,6 +622,9 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
615622
});
616623
}
617624

625+
/**
626+
* @ignore
627+
*/
618628
private async executeReadOnly(sql: string, params: any[]) {
619629
await this.waitForReady();
620630
return this.database.readLock((tx) => tx.execute(sql, params));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { PowerSyncCredentials } from './PowerSyncCredentials';
22
import type { AbstractPowerSyncDatabase } from '../AbstractPowerSyncDatabase';
33

44
export interface PowerSyncBackendConnector {
5-
/** Get credentials for PowerSync.
5+
/** Allows the PowerSync client to retrieve an authentication token from your backend
6+
* which is used to authenticate against the PowerSync service.
67
*
78
* This should always fetch a fresh set of credentials - don't use cached
89
* values.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ import hash from 'object-hash';
77
*/
88
export type OpId = string;
99

10+
/**
11+
* Type of local change.
12+
*/
1013
export enum UpdateType {
14+
/** Insert or replace existing row. All non-null columns are included in the data. Generated by INSERT statements. */
1115
PUT = 'PUT',
16+
/** Update existing row. Contains the id, and value of each changed column. Generated by UPDATE statements. */
1217
PATCH = 'PATCH',
18+
/** Delete existing row. Contains the id. Generated by DELETE statements. */
1319
DELETE = 'DELETE'
1420
}
1521

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export enum OpTypeEnum {
77

88
export type OpTypeJSON = string;
99

10+
/**
11+
* Used internally for sync buckets.
12+
*/
1013
export class OpType {
1114
static fromJSON(jsonValue: OpTypeJSON) {
1215
return new OpType(OpTypeEnum[jsonValue]);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ export interface DBGetUtils {
4242
}
4343

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

4949
export interface Transaction extends LockContext {
50+
/** Commit multiple changes to the local DB using the Transaction context. */
5051
commit: () => Promise<QueryResult>;
52+
/** Roll back multiple attempted changes using the Transaction context. */
5153
rollback: () => Promise<QueryResult>;
5254
}
5355

packages/powersync-sdk-common/src/utils/BaseObserver.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export class BaseObserver<T extends BaseListener = BaseListener> implements Base
1515
this.listeners = {};
1616
}
1717

18+
/**
19+
* Register a listener for updates to the PowerSync client.
20+
*/
1821
registerListener(listener: Partial<T>): () => void {
1922
const id = v4();
2023
this.listeners[id] = listener;

packages/powersync-sdk-react-native/README.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
<p align="center">
2+
<a href="https://www.powersync.com" target="_blank"><img src="https://github.com/powersync-ja/.github/assets/19345049/602bafa0-41ce-4cee-a432-56848c278722"/></a>
3+
</p>
4+
15
# PowerSync SDK for React Native
26

3-
[PowerSync](https://powersync.co) is a service and set of SDKs that keeps Postgres databases in sync with on-device SQLite databases. See a summary of features [here](https://docs.powersync.co/client-sdk-references/react-native-and-expo).
7+
[PowerSync](https://powersync.com) is a service and set of SDKs that keeps Postgres databases in sync with on-device SQLite databases.
8+
9+
This package (`packages/powersync-sdk-react-native`) is the PowerSync SDK for React Native clients. It is an extension of `packages/powersync-sdk-common`.
10+
11+
See a summary of features [here](https://docs.powersync.co/client-sdk-references/react-native-and-expo).
412

513
# Installation
614

@@ -82,14 +90,32 @@ module.exports = function (api) {
8290

8391
# Native Projects
8492

85-
This package uses native libraries. Create native Android and iOS projects (if not created already) with
93+
This package uses native libraries. Create native Android and iOS projects (if not created already) by running:
8694

8795
```bash
8896
npx expo run:android
8997
# OR
9098
npx expo run:ios
9199
```
92100

93-
# Learn More
101+
# Getting Started
102+
103+
Our [SDK reference](https://docs.powersync.com/client-sdk-references/react-native-and-expo) contains everything you need to know to get started implementing PowerSync in your project.
104+
105+
# Changelog
106+
107+
A changelog for this SDK is available [here](https://releases.powersync.com/announcements/react-native-client-sdk).
108+
109+
# API Reference
110+
111+
The full API reference for this SDK can be found [here](https://powersync-ja.github.io/powersync-js/react-native-sdk).
112+
113+
# Examples
114+
115+
For example projects built with PowerSync and React Native, see our [Demo Apps / Example Projects](https://docs.powersync.com/resources/demo-apps-example-projects#react-native-and-expo) gallery. Most of these projects can also be found in the [`demos/`](../demos/) directory.
116+
117+
# Found a bug or need help?
94118

95-
Refer to our [full documentation](https://docs.powersync.com/client-sdk-references/react-native-and-expo) to learn more.
119+
* Join our [Discord server](https://discord.gg/powersync) where you can browse topics from our community, ask questions, share feedback, or just say hello :)
120+
* Please open a [GitHub issue](https://github.com/powersync-ja/powersync-js/issues) when you come across a bug.
121+
* Have feedback or an idea? [Submit an idea](https://roadmap.powersync.com/tabs/5-roadmap/submit-idea) via our public roadmap or [schedule a chat](https://calendly.com/powersync-product/powersync-chat) with someone from our product team.

packages/powersync-sdk-web/README.md

Lines changed: 20 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
<p align="center">
2+
<a href="https://www.powersync.com" target="_blank"><img src="https://github.com/powersync-ja/.github/assets/19345049/602bafa0-41ce-4cee-a432-56848c278722"/></a>
3+
</p>
4+
15
# PowerSync SDK for Web
26

3-
[PowerSync](https://powersync.co) is a service and set of SDKs that keeps Postgres databases in sync with on-device SQLite databases.
7+
[PowerSync](https://powersync.com) is a service and set of SDKs that keeps Postgres databases in sync with on-device SQLite databases.
8+
9+
This package (`packages/powersync-sdk-web`) is the PowerSync SDK for JavaScript Web clients. It is an extension of `packages/powersync-sdk-common`.
10+
11+
See a summary of features [here](https://docs.powersync.com/client-sdk-references/js-web).
412

513
## Beta Release
614

@@ -24,114 +32,26 @@ Install it in your app with:
2432
npm install @journeyapps/wa-sqlite
2533
```
2634

27-
## Logging
28-
29-
This package uses [js-logger](https://www.npmjs.com/package/js-logger) for logging.
30-
31-
Enable JS Logger with your logging interface of choice or use the default `console`
32-
33-
```JavaScript
34-
import Logger from 'js-logger';
35-
36-
// Log messages will be written to the window's console.
37-
Logger.useDefaults();
38-
```
39-
40-
Enable verbose output in the developer tools for detailed logs.
41-
42-
The WASQLite DB Adapter opens SQLite connections inside a shared webworker. This worker can be inspected in Chrome by accessing
43-
44-
```
45-
chrome://inspect/#workers
46-
```
47-
4835
# Getting Started
4936

50-
See our [Docs](https://docs.powersync.co/usage/installation/client-side-setup) for detailed instructions.
51-
52-
```JavaScript
53-
import {
54-
Column,
55-
ColumnType,
56-
WASQLitePowerSyncDatabaseOpenFactory,
57-
Schema,
58-
Table
59-
} from '@journeyapps/powersync-sdk-web';
60-
61-
export const AppSchema = new Schema([
62-
new Table({ name: 'customers', columns: [new Column({ name: 'name', type: ColumnType.TEXT })] })
63-
]);
64-
65-
let PowerSync;
66-
67-
export const openDatabase = async () => {
68-
PowerSync = new WASQLitePowerSyncDatabaseOpenFactory({
69-
schema: AppSchema,
70-
dbFilename: 'test.sqlite',
71-
flags: {
72-
// This is disabled once CSR+SSR functionality is verified to be working correctly
73-
disableSSRWarning: true,
74-
}}).getInstance();
75-
76-
await PowerSync.init();
77-
78-
// Run local statements.
79-
await PowerSync.execute('INSERT INTO customers(id, name) VALUES(uuid(), ?)', ['Fred']);
80-
};
81-
82-
class Connector {
83-
async fetchCredentials() {
84-
// TODO logic to fetch a session
85-
return {
86-
endpoint: '[The PowerSync instance URL]',
87-
token: 'An authentication token',
88-
expiresAt: 'When the token expires',
89-
};
90-
}
91-
92-
async uploadData(database) {
93-
// Upload local changes to backend, see docs for example
94-
}
95-
}
96-
97-
export const connectPowerSync = async () => {
98-
const connector = new Connector(); // Which was declared above
99-
await PowerSync.connect(connector);
100-
};
37+
Our [full SDK reference](https://docs.powersync.com/client-sdk-references/js-web) contains everything you need to know to get started implementing PowerSync in your project.
10138

102-
```
103-
104-
React hooks are available in the [@journeyapps/powersync-react](https://www.npmjs.com/package/@journeyapps/powersync-react) package
39+
# Changelog
10540

106-
## Multiple Tab Support
41+
A changelog for this SDK is available [here](https://releases.powersync.com/announcements/powersync-js-web-client-sdk).
10742

108-
Using PowerSync between multiple tabs is supported on some web browsers. Multiple tab support relies on shared web workers for DB and sync streaming operations. When enabled shared web workers named `shared-sync-[dbFileName]` and `shared-DB-worker-[dbFileName]` will be created.
43+
# API Reference
10944

110-
The shared database worker will ensure writes to the DB will instantly be available between tabs.
45+
The full API reference for this SDK can be found [here](https://powersync-ja.github.io/powersync-js/web-sdk).
11146

112-
The shared sync worker will co-ordinate for one active tab to connect to the PowerSync instance and share the latest sync state between tabs.
47+
# Examples
11348

114-
Currently using the SDK in multiple tabs without enabling the `enableMultiTabs` flag will spawn a standard web worker per tab for DB operations. These workers are safe to operate on the DB concurrently, however changes from one tab may not update watches on other tabs. Only one tab can sync from the PowerSync instance at a time. The sync status will not be shared between tabs, only the oldest tab will connect and display the latest sync status.
49+
For example projects built with PowerSync on Web, see our [Demo Apps / Example Projects](https://docs.powersync.com/resources/demo-apps-example-projects#js-web) gallery. Most of these projects can also be found in the [`demos/`](../demos/) directory.
11550

116-
Multiple tab support is not currently available on Android or Safari.
51+
# Found a bug or need help?
11752

118-
Support is enabled by default if available. This can be disabled as below:
119-
120-
```Javascript
121-
PowerSync = new WASQLitePowerSyncDatabaseOpenFactory({
122-
schema: AppSchema,
123-
dbFilename: 'test.sqlite',
124-
flags: {
125-
// This is disabled once CSR+SSR functionality is verified to be working correctly
126-
disableSSRWarning: true,
127-
/**
128-
* Multiple tab support is enabled by default if available. This can be disabled by
129-
* setting this flag to false.
130-
*/
131-
enableMultiTabs: false
132-
}}).getInstance();
133-
```
53+
* Join our [Discord server](https://discord.gg/powersync) where you can browse topics from our community, ask questions, share feedback, or just say hello :)
54+
* Please open a [GitHub issue](https://github.com/powersync-ja/powersync-js/issues) when you come across a bug.
55+
* Have feedback or an idea? [Submit an idea](https://roadmap.powersync.com/tabs/5-roadmap/submit-idea) via our public roadmap or [schedule a chat](https://calendly.com/powersync-product/powersync-chat) with someone from our product team.
13456

135-
## Demo Apps
13657

137-
See the [list of demo apps](https://github.com/powersync-ja/powersync-web-sdk/?tab=readme-ov-file#demos) in the repo README.

0 commit comments

Comments
 (0)