-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmain.ts
66 lines (55 loc) · 1.98 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { once } from 'node:events';
import repl_factory from 'node:repl';
import { createBaseLogger, createLogger, PowerSyncDatabase, SyncStreamConnectionMethod } from '@powersync/node';
import { exit } from 'node:process';
import { AppSchema, DemoConnector } from './powersync.js';
const main = async () => {
const baseLogger = createBaseLogger();
const logger = createLogger('PowerSyncDemo');
baseLogger.useDefaults({ defaultLevel: logger.WARN });
if (!('BACKEND' in process.env) || !('SYNC_SERVICE' in process.env)) {
console.warn(
'Set the BACKEND and SYNC_SERVICE environment variables to point to a sync service and a running demo backend.'
);
return;
}
const db = new PowerSyncDatabase({
schema: AppSchema,
database: {
dbFilename: 'test.db'
},
logger
});
console.log(await db.get('SELECT powersync_rs_version();'));
await db.connect(new DemoConnector(), { connectionMethod: SyncStreamConnectionMethod.HTTP });
await db.waitForFirstSync();
console.log('First sync complete!');
let hasFirstRow: ((value: any) => void) | null = null;
const firstRow = new Promise((resolve) => (hasFirstRow = resolve));
const watchLists = async () => {
for await (const rows of db.watch('SELECT * FROM lists')) {
if (hasFirstRow) {
hasFirstRow(null);
hasFirstRow = null;
}
console.log('Has todo lists', rows.rows?._array);
}
};
watchLists();
await firstRow;
console.log('Connected to PowerSync. Try updating the lists in the database and see it reflected here.');
console.log("To upload a list here, enter `await add('name of new list');`");
const repl = repl_factory.start();
repl.context.add = async (name: string) => {
await db.execute(
"INSERT INTO lists (id, created_at, name, owner_id) VALUEs (uuid(), datetime('now'), ?, uuid());",
[name]
);
};
await once(repl, 'exit');
console.log('shutting down');
await db.disconnect();
await db.close();
exit(0);
};
await main();