|
| 1 | +import { column, Schema, Table, PowerSyncDatabase, WASQLiteVFS } from '@powersync/web'; |
| 2 | +import Logger from 'js-logger'; |
| 3 | + |
| 4 | +Logger.useDefaults(); |
| 5 | + |
| 6 | +/** |
| 7 | + * A placeholder connector which doesn't do anything. |
| 8 | + * This is just used to verify that the sync workers can be loaded |
| 9 | + * when connecting. |
| 10 | + */ |
| 11 | +class DummyConnector { |
| 12 | + async fetchCredentials() { |
| 13 | + return { |
| 14 | + endpoint: '', |
| 15 | + token: '' |
| 16 | + }; |
| 17 | + } |
| 18 | + |
| 19 | + async uploadData(database) {} |
| 20 | +} |
| 21 | + |
| 22 | +const customers = new Table({ name: column.text }); |
| 23 | + |
| 24 | +export const AppSchema = new Schema({ customers }); |
| 25 | + |
| 26 | +let PowerSync; |
| 27 | + |
| 28 | +const openDatabase = async (encryptionKey) => { |
| 29 | + PowerSync = new PowerSyncDatabase({ |
| 30 | + schema: AppSchema, |
| 31 | + database: { dbFilename: 'example-encryption.db' }, |
| 32 | + encryptionKey: encryptionKey |
| 33 | + }); |
| 34 | + |
| 35 | + await PowerSync.init(); |
| 36 | + |
| 37 | + // Run local statements. |
| 38 | + await PowerSync.execute('INSERT INTO customers(id, name) VALUES(uuid(), ?)', ['Fred']); |
| 39 | + |
| 40 | + const result = await PowerSync.getAll('SELECT * FROM customers'); |
| 41 | + console.log('contents of customers: ', result); |
| 42 | + |
| 43 | + console.log( |
| 44 | + `Attempting to connect in order to verify web workers are correctly loaded. |
| 45 | + This doesn't use any actual network credentials. |
| 46 | + Network errors will be shown: these can be ignored.` |
| 47 | + ); |
| 48 | + |
| 49 | + /** |
| 50 | + * Try and connect, this will setup shared sync workers |
| 51 | + * This will fail due to not having a valid endpoint, |
| 52 | + * but it will try - which is all that matters. |
| 53 | + */ |
| 54 | + await PowerSync.connect(new DummyConnector()); |
| 55 | +}; |
| 56 | + |
| 57 | +document.addEventListener('DOMContentLoaded', async (event) => { |
| 58 | + let encryptionKey = ''; |
| 59 | + |
| 60 | + while (!encryptionKey) { |
| 61 | + encryptionKey = prompt('Enter encryption key (non-empty string):'); |
| 62 | + } |
| 63 | + |
| 64 | + openDatabase(encryptionKey); |
| 65 | +}); |
0 commit comments