-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathpowersync.ts
More file actions
226 lines (197 loc) · 6.3 KB
/
powersync.ts
File metadata and controls
226 lines (197 loc) · 6.3 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import {
AbstractPowerSyncDatabase,
BaseObserver,
LogLevel,
PowerSyncBackendConnector,
type PowerSyncCredentials,
PowerSyncDatabase,
createBaseLogger,
CrudEntry,
UpdateType,
} from "@powersync/web";
import { client } from "@/lib/auth";
import {
wrapPowerSyncWithDrizzle,
DrizzleAppSchema,
} from "@powersync/drizzle-driver";
import { drizzleRelations, drizzleSchema } from "./powersync-schema";
/// Postgres Response codes that we cannot recover from by retrying.
const FATAL_RESPONSE_CODES = [
// Class 22 — Data Exception
// Examples include data type mismatch.
new RegExp("^22...$"),
// Class 23 — Integrity Constraint Violation.
// Examples include NOT NULL, FOREIGN KEY and UNIQUE violations.
new RegExp("^23...$"),
// INSUFFICIENT PRIVILEGE - typically a row-level security violation
new RegExp("^42501$"),
];
export const powersyncLogger = createBaseLogger();
powersyncLogger.useDefaults();
powersyncLogger.setLevel(LogLevel.DEBUG);
// Type for the session returned by client.auth.getSession()
export type NeonSession = {
user: { id: string; name: string; email: string };
session: { token: string };
};
export type NeonConnectorListener = {
initialized: () => void;
sessionStarted: (session: NeonSession) => void;
};
const SESSION_STORAGE_KEY = "neon_session";
export class NeonConnector
extends BaseObserver<NeonConnectorListener>
implements PowerSyncBackendConnector
{
ready: boolean = false;
currentSession: NeonSession | null = null;
constructor() {
super();
// Load cached session from localStorage on construction
this.loadCachedSession();
}
private loadCachedSession() {
try {
const cached = localStorage.getItem(SESSION_STORAGE_KEY);
if (cached) {
this.currentSession = JSON.parse(cached);
}
} catch (error) {
console.debug("Could not load cached session:", error);
}
}
private persistSession(session: NeonSession | null) {
try {
if (session) {
localStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(session));
} else {
localStorage.removeItem(SESSION_STORAGE_KEY);
}
} catch (error) {
console.debug("Could not persist session:", error);
}
}
async init() {
if (this.ready) return;
try {
const sessionResponse = await client.auth.getSession();
this.updateSession(sessionResponse.data ?? null);
} catch (error) {
console.debug(
"Could not fetch session during init (most likely offline), using cached session:",
error,
);
}
this.ready = true;
this.iterateListeners((cb) => cb.initialized?.());
}
updateSession(session: NeonSession | null) {
this.currentSession = session;
this.persistSession(session);
if (session) {
this.iterateListeners((cb) => cb.sessionStarted?.(session));
}
}
async fetchCredentials() {
// Refresh session to get a fresh token (handles token expiry)
try {
const sessionResponse = await client.auth.getSession();
if (sessionResponse.data) {
this.currentSession = sessionResponse.data;
this.persistSession(sessionResponse.data);
}
} catch (error) {
// Network error - use cached session if available (offline mode)
console.debug(
"Could not refresh session (most likely offline), using cached:",
error,
);
}
if (!this.currentSession) {
throw new Error("Could not fetch Neon credentials.");
}
return {
endpoint: import.meta.env.VITE_POWERSYNC_URL,
token: this.currentSession.session.token ?? "",
} satisfies PowerSyncCredentials;
}
async uploadData(database: AbstractPowerSyncDatabase): Promise<void> {
const transaction = await database.getNextCrudTransaction();
if (!transaction) {
return;
}
let lastOp: CrudEntry | null = null;
try {
// Note: If transactional consistency is important, use database functions
// or edge functions to process the entire transaction in a single call.
for (const op of transaction.crud) {
lastOp = op;
const table = client.from(op.table as any);
let result: any;
switch (op.op) {
case UpdateType.PUT:
const record = { ...(op.opData as any), id: op.id };
result = await table.upsert(record as any);
break;
case UpdateType.PATCH:
result = await table.update(op.opData as any).eq("id", op.id);
break;
case UpdateType.DELETE:
result = await table.delete().eq("id", op.id);
break;
}
if (result.error) {
console.error(result.error);
result.error.message = `Could not update Neon. Received error: ${result.error.message}`;
throw result.error;
}
}
await transaction.complete();
} catch (ex: any) {
console.debug(ex);
if (
typeof ex.code == "string" &&
FATAL_RESPONSE_CODES.some((regex) => regex.test(ex.code))
) {
/**
* Instead of blocking the queue with these errors,
* discard the (rest of the) transaction.
*
* Note that these errors typically indicate a bug in the application.
* If protecting against data loss is important, save the failing records
* elsewhere instead of discarding, and/or notify the user.
*/
console.error("Data upload error - discarding:", lastOp, ex);
await transaction.complete();
} else {
// Error may be retryable - e.g. network error or temporary server error.
// Throwing an error here causes this call to be retried after a delay.
throw ex;
}
}
}
}
export const neonConnector = new NeonConnector();
export const AppSchema = new DrizzleAppSchema(drizzleSchema);
export const powersync = new PowerSyncDatabase({
schema: AppSchema,
database: {
dbFilename: "powersync.db",
},
});
export const powersyncDrizzle = wrapPowerSyncWithDrizzle(powersync, {
relations: drizzleRelations,
});
let isInitialized = false;
export async function connectPowerSync() {
if (isInitialized) {
return;
}
await powersync.connect(neonConnector);
isInitialized = true;
console.log("powersync connected");
}
export async function disconnectPowerSync() {
await powersync.disconnect();
isInitialized = false;
}