-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathpowersync.service.ts
94 lines (85 loc) · 2.3 KB
/
powersync.service.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
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
import { Injectable } from '@angular/core';
import {
AbstractPowerSyncDatabase,
Column,
ColumnType,
Index,
IndexedColumn,
PowerSyncBackendConnector,
PowerSyncDatabase,
Schema,
Table,
WASQLiteOpenFactory,
WASQLiteVFS
} from '@powersync/web';
export interface ListRecord {
id: string;
name: string;
created_at: string;
owner_id?: string;
}
export interface TodoRecord {
id: string;
created_at: string;
completed: boolean;
description: string;
completed_at?: string;
created_by: string;
completed_by?: string;
list_id: string;
}
export const LISTS_TABLE = 'lists';
export const TODOS_TABLE = 'todos';
export const AppSchema = new Schema([
new Table({
name: TODOS_TABLE,
columns: [
new Column({ name: 'list_id', type: ColumnType.TEXT }),
new Column({ name: 'created_at', type: ColumnType.TEXT }),
new Column({ name: 'completed_at', type: ColumnType.TEXT }),
new Column({ name: 'description', type: ColumnType.TEXT }),
new Column({ name: 'completed', type: ColumnType.INTEGER }),
new Column({ name: 'created_by', type: ColumnType.TEXT }),
new Column({ name: 'completed_by', type: ColumnType.TEXT })
],
indexes: [new Index({ name: 'list', columns: [new IndexedColumn({ name: 'list_id' })] })]
}),
new Table({
name: LISTS_TABLE,
columns: [
new Column({ name: 'created_at', type: ColumnType.TEXT }),
new Column({ name: 'name', type: ColumnType.TEXT }),
new Column({ name: 'owner_id', type: ColumnType.TEXT })
]
})
]);
@Injectable({
providedIn: 'root'
})
export class PowerSyncService {
db: AbstractPowerSyncDatabase;
constructor() {
const factory = new WASQLiteOpenFactory({
dbFilename: 'test.db',
vfs: WASQLiteVFS.OPFSCoopSyncVFS,
// Specify the path to the worker script
worker: 'assets/@powersync/worker/WASQLiteDB.umd.js'
});
this.db = new PowerSyncDatabase({
schema: AppSchema,
database: factory,
sync: {
// Specify the path to the worker script
worker: 'assets/@powersync/worker/SharedSyncImplementation.umd.js'
}
});
}
setupPowerSync = async (connector: PowerSyncBackendConnector) => {
try {
await this.db.init();
await this.db.connect(connector);
} catch (e) {
console.log(e);
}
};
}