-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsupabase.service.ts
160 lines (139 loc) · 4.68 KB
/
supabase.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
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
import { Injectable } from '@angular/core';
import {
type AuthChangeEvent,
type AuthSession,
createClient,
type Session,
type SupabaseClient,
type User
} from '@supabase/supabase-js';
import { environment } from '../environment';
import { type AbstractPowerSyncDatabase, type CrudEntry, UpdateType, PowerSyncBackendConnector, type PowerSyncCredentials } from '@powersync/web';
/// 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$')
];
@Injectable({
providedIn: 'root'
})
export class SupabaseService implements PowerSyncBackendConnector {
private supabase: SupabaseClient;
_session: AuthSession | null = null;
_userId: string | null = null;
constructor() {
this.supabase = createClient(environment.supabaseUrl, environment.supabaseKey, {
auth: {
persistSession: true
}
});
}
setUserId(userId: string) {
this._userId = userId;
}
async getSession() {
const { data } = await this.supabase.auth.getSession();
if (data.session?.user.id) {
this.setUserId(data.session.user.id);
}
return data.session;
}
setSession(session: AuthSession | null) {
this._session = session;
}
async fetchCredentials() {
const {
data: { session },
error
} = await this.supabase.auth.getSession();
if (!session || error) {
throw new Error(`Could not fetch Supabase credentials: ${error}`);
}
return {
endpoint: environment.powersyncUrl,
token: session.access_token ?? '',
expiresAt: session.expires_at ? new Date(session.expires_at * 1000) : undefined
} satisfies PowerSyncCredentials;
}
authChanges(callback: (event: AuthChangeEvent, session: Session | null) => void) {
return this.supabase.auth.onAuthStateChange(callback);
}
async signIn(email: string, password: string) {
const result = await this.supabase.auth.signInWithPassword({ email, password });
if (result.data.session) {
this.setSession(result.data.session);
}
return !!result.data.session?.access_token;
}
async signUp(email: string, password: string): Promise<User | null> {
const { data, error } = await this.supabase.auth.signUp({
email,
password
});
if (error) {
throw error;
}
return data.user;
}
async signOut() {
await this.supabase.auth.signOut();
}
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 = this.supabase.from(op.table);
let result: any;
switch (op.op) {
case UpdateType.PUT:
const record = { ...op.opData, id: op.id };
result = await table.upsert(record);
break;
case UpdateType.PATCH:
result = await table.update(op.opData).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 Supabase. 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;
}
}
}
}