-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
58 lines (45 loc) · 1.45 KB
/
index.js
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
import { column, Schema, Table, PowerSyncDatabase } from '@powersync/web';
import Logger from 'js-logger';
Logger.useDefaults();
/**
* A placeholder connector which doesn't do anything.
* This is just used to verify that the sync workers can be loaded
* when connecting.
*/
class DummyConnector {
async fetchCredentials() {
return {
endpoint: '',
token: ''
};
}
async uploadData(database) {}
}
const customers = new Table({ name: column.text })
export const AppSchema = new Schema({ customers });
let PowerSync;
const openDatabase = async () => {
PowerSync = new PowerSyncDatabase({
schema: AppSchema,
database: { dbFilename: 'test.sqlite' }
});
await PowerSync.init();
// Run local statements.
await PowerSync.execute('INSERT INTO customers(id, name) VALUES(uuid(), ?)', ['Fred']);
const result = await PowerSync.getAll('SELECT * FROM customers');
console.log('contents of customers: ', result);
console.log(
`Attempting to connect in order to verify web workers are correctly loaded.
This doesn't use any actual network credentials.
Network errors will be shown: these can be ignored.`
);
/**
* Try and connect, this will setup shared sync workers
* This will fail due to not having a valid endpoint,
* but it will try - which is all that matters.
*/
await PowerSync.connect(new DummyConnector());
};
document.addEventListener('DOMContentLoaded', (event) => {
openDatabase();
});