Skip to content

Commit b4607f6

Browse files
committed
Adopt OPFS file system
1 parent b3205e2 commit b4607f6

File tree

3 files changed

+17
-71
lines changed

3 files changed

+17
-71
lines changed

demos/react-supabase-todolist/src/components/providers/SystemProvider.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AppSchema } from '@/library/powersync/AppSchema';
33
import { SupabaseConnector } from '@/library/powersync/SupabaseConnector';
44
import { CircularProgress } from '@mui/material';
55
import { PowerSyncContext } from '@powersync/react';
6-
import { createBaseLogger, LogLevel, PowerSyncDatabase } from '@powersync/web';
6+
import { createBaseLogger, LogLevel, PowerSyncDatabase, WASQLiteOpenFactory, WASQLiteVFS } from '@powersync/web';
77
import React, { Suspense } from 'react';
88
import { NavigationPanelContextProvider } from '../navigation/NavigationPanelContext';
99

@@ -12,9 +12,16 @@ export const useSupabase = () => React.useContext(SupabaseContext);
1212

1313
export const db = new PowerSyncDatabase({
1414
schema: AppSchema,
15-
database: {
16-
dbFilename: 'example.db'
17-
}
15+
database: new WASQLiteOpenFactory({
16+
dbFilename: 'example.db',
17+
vfs: WASQLiteVFS.OPFSCoopSyncVFS,
18+
flags: {
19+
enableMultiTabs: typeof SharedWorker !== 'undefined'
20+
}
21+
}),
22+
flags: {
23+
enableMultiTabs: typeof SharedWorker !== 'undefined'
24+
}
1825
});
1926

2027
export const SystemProvider = ({ children }: { children: React.ReactNode }) => {

demos/react-supabase-todolist/src/library/powersync/SupabaseConnector.ts

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -86,78 +86,17 @@ export class SupabaseConnector extends BaseObserver<SupabaseConnectorListener> i
8686
}
8787

8888
async fetchCredentials() {
89-
const {
90-
data: { session },
91-
error
92-
} = await this.client.auth.getSession();
93-
94-
if (!session || error) {
95-
throw new Error(`Could not fetch Supabase credentials: ${error}`);
96-
}
97-
98-
console.debug('session expires at', session.expires_at);
89+
const response = await fetch('http://localhost:6060/api/auth/token');
90+
const { token } = await response.json();
9991

10092
return {
10193
endpoint: this.config.powersyncUrl,
102-
token: session.access_token ?? ''
94+
token,
10395
} satisfies PowerSyncCredentials;
10496
}
10597

106-
async uploadData(database: AbstractPowerSyncDatabase): Promise<void> {
107-
const transaction = await database.getNextCrudTransaction();
108-
109-
if (!transaction) {
110-
return;
111-
}
112-
113-
let lastOp: CrudEntry | null = null;
114-
try {
115-
// Note: If transactional consistency is important, use database functions
116-
// or edge functions to process the entire transaction in a single call.
117-
for (const op of transaction.crud) {
118-
lastOp = op;
119-
const table = this.client.from(op.table);
120-
let result: any;
121-
switch (op.op) {
122-
case UpdateType.PUT:
123-
const record = { ...op.opData, id: op.id };
124-
result = await table.upsert(record);
125-
break;
126-
case UpdateType.PATCH:
127-
result = await table.update(op.opData).eq('id', op.id);
128-
break;
129-
case UpdateType.DELETE:
130-
result = await table.delete().eq('id', op.id);
131-
break;
132-
}
133-
134-
if (result.error) {
135-
console.error(result.error);
136-
result.error.message = `Could not update Supabase. Received error: ${result.error.message}`;
137-
throw result.error;
138-
}
139-
}
140-
141-
await transaction.complete();
142-
} catch (ex: any) {
143-
console.debug(ex);
144-
if (typeof ex.code == 'string' && FATAL_RESPONSE_CODES.some((regex) => regex.test(ex.code))) {
145-
/**
146-
* Instead of blocking the queue with these errors,
147-
* discard the (rest of the) transaction.
148-
*
149-
* Note that these errors typically indicate a bug in the application.
150-
* If protecting against data loss is important, save the failing records
151-
* elsewhere instead of discarding, and/or notify the user.
152-
*/
153-
console.error('Data upload error - discarding:', lastOp, ex);
154-
await transaction.complete();
155-
} else {
156-
// Error may be retryable - e.g. network error or temporary server error.
157-
// Throwing an error here causes this call to be retried after a delay.
158-
throw ex;
159-
}
160-
}
98+
async uploadData(): Promise<void> {
99+
throw 'unsupported';
161100
}
162101

163102
updateSession(session: Session | null) {

packages/common/rollup.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default (commandLineArgs) => {
2727
// Used by can-ndjson-stream
2828
TextDecoder: ['text-encoding', 'TextDecoder']
2929
}),
30-
terser()
30+
// terser()
3131
],
3232
// This makes life easier
3333
external: [

0 commit comments

Comments
 (0)