Skip to content

Commit afd62ab

Browse files
committed
Reformat example
1 parent 7c0c6c7 commit afd62ab

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

demos/example-node/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## NodeJS Demo
2+
3+
This demonstrates a small NodeJS client opening a database and connecting PowerSync.
4+
5+
To get started, set up a PowerSync service instance with the default rules for the todolist examples.
6+
Create a development token, and use it to implement `DemoConnector.fetchCredentials` in `main.ts`.
7+
8+
Then, use `pnpm run run` to open a database, connect to PowerSync, wait for a first sync, and run a simple query.

demos/example-node/src/main.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
1-
import { column, PowerSyncDatabase, Schema, Table } from "@powersync/node";
1+
import {
2+
AbstractPowerSyncDatabase,
3+
column,
4+
PowerSyncBackendConnector,
5+
PowerSyncDatabase,
6+
Schema,
7+
Table
8+
} from '@powersync/node';
9+
import Logger from 'js-logger';
210

311
const main = async () => {
4-
const db = new PowerSyncDatabase({
5-
schema: AppSchema,
6-
database: {
7-
dbFilename: 'test.db',
8-
},
9-
});
10-
console.log(await db.get('SELECT powersync_rs_version();'));
12+
const db = new PowerSyncDatabase({
13+
schema: AppSchema,
14+
database: {
15+
dbFilename: 'test.db'
16+
},
17+
logger: Logger
18+
});
19+
console.log(await db.get('SELECT powersync_rs_version();'));
20+
db.registerListener({
21+
statusChanged: (status) => {
22+
console.log('Sync status', status);
23+
}
24+
});
25+
26+
await db.connect(new DemoConnector());
27+
28+
await db.waitForFirstSync();
29+
console.log(await db.getAll('SELECT * FROM lists;'));
1130
};
1231

32+
class DemoConnector implements PowerSyncBackendConnector {
33+
async fetchCredentials() {
34+
return {
35+
endpoint: '', // todo
36+
token: '' // todo
37+
};
38+
}
39+
40+
async uploadData(database: AbstractPowerSyncDatabase) {
41+
throw 'not implemented: DemoConnector.uploadData';
42+
}
43+
}
44+
1345
export const LIST_TABLE = 'lists';
1446
export const TODO_TABLE = 'todos';
1547

0 commit comments

Comments
 (0)