|
| 1 | +import 'dotenv/config' |
| 2 | +import { |
| 3 | + column, |
| 4 | + Schema, |
| 5 | + Table, |
| 6 | + PowerSyncDatabase, |
| 7 | +} from "@powersync/node"; |
| 8 | + |
| 9 | +export class Connector { |
| 10 | + async fetchCredentials() { |
| 11 | + const endpoint = process.env.POWERSYNC_URL; |
| 12 | + if (!endpoint) { |
| 13 | + return null; |
| 14 | + } |
| 15 | + const token = await fetch(`${process.env.BACKEND_URL}/api/auth/token`) |
| 16 | + .then((response) => response.json()) |
| 17 | + .then((data) => data.token as string); |
| 18 | + return { endpoint, token }; |
| 19 | + } |
| 20 | + async uploadData(database: any) { } |
| 21 | +}; |
| 22 | + |
| 23 | + |
| 24 | +const schema = new Schema({ |
| 25 | + lists: new Table({ |
| 26 | + name: column.text, |
| 27 | + created_at: column.text, |
| 28 | + owner_id: column.text, |
| 29 | + }), |
| 30 | + todos: new Table({ |
| 31 | + description: column.text, |
| 32 | + list_id: column.text, |
| 33 | + completed: column.integer, |
| 34 | + }), |
| 35 | +}); |
| 36 | + |
| 37 | +const powerSync = new PowerSyncDatabase({ |
| 38 | + schema, |
| 39 | + database: { dbFilename: "example.sqlite" }, |
| 40 | +}); |
| 41 | + |
| 42 | +await powerSync.connect(new Connector(), { |
| 43 | + params: { |
| 44 | + test: "value" |
| 45 | + } |
| 46 | +}); |
| 47 | +await powerSync.init(); |
| 48 | +await powerSync.waitForFirstSync(); |
| 49 | + |
| 50 | + |
| 51 | +const lists = await powerSync.getAll(`SELECT * FROM lists`); |
| 52 | +console.log("lists: ", lists); |
| 53 | + |
| 54 | +const todos = await powerSync.getAll(`SELECT * FROM todos`); |
| 55 | +console.log("todos: ", todos); |
| 56 | + |
| 57 | +await powerSync.disconnect(); |
| 58 | +console.log("Disconnected and cleared the database."); |
| 59 | + |
| 60 | +process.exit(0); |
0 commit comments