-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathmain.ts
More file actions
97 lines (84 loc) · 2.76 KB
/
Copy pathmain.ts
File metadata and controls
97 lines (84 loc) · 2.76 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { once } from 'node:events';
import repl_factory from 'node:repl';
import {
createBaseLogger,
createLogger,
PowerSyncDatabase,
SyncClientImplementation,
SyncStreamConnectionMethod
} from '@powersync/node';
import { exit } from 'node:process';
import { AppSchema, DemoConnector } from './powersync.js';
import { enableUncidiDiagnostics } from './UndiciDiagnostics.js';
const main = async () => {
const baseLogger = createBaseLogger();
const logger = createLogger('PowerSyncDemo');
const debug = process.env.POWERSYNC_DEBUG == '1';
baseLogger.useDefaults({ defaultLevel: debug ? logger.TRACE : logger.WARN });
// Enable detailed request/response logging for debugging purposes.
if (debug) {
enableUncidiDiagnostics();
}
if (!('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.WEB_SOCKET,
clientImplementation: SyncClientImplementation.RUST
});
// Example using a proxy agent for more control over the connection:
// const proxyAgent = new (await import('undici')).ProxyAgent({
// uri: 'http://localhost:8080',
// requestTls: {
// ca: '<CA for the service>'
// },
// proxyTls: {
// ca: '<CA for the proxy>'
// }
// });
// await db.connect(new DemoConnector(), {
// connectionMethod: SyncStreamConnectionMethod.WEB_SOCKET,
// dispatcher: proxyAgent
// });
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();