Skip to content

Commit c905f5d

Browse files
committed
Use custom domains for uploading base64 data.
1 parent d62d1e7 commit c905f5d

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

demos/yjs-react-supabase-text-collab/database.sql

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11

22
-- tables
3+
4+
-- custom app_base64 type, to allow querying and updating binary data as base64
5+
-- via postgrest/supabase-js
6+
create domain app_base64 as bytea;
7+
8+
-- For query responses
9+
CREATE OR REPLACE FUNCTION json(app_base64) RETURNS json AS $$
10+
select to_json(encode($1, 'base64'));
11+
$$ LANGUAGE SQL IMMUTABLE;
12+
CREATE CAST (app_base64 AS json) WITH FUNCTION json(app_base64) AS IMPLICIT;
13+
14+
-- For uploading
15+
CREATE OR REPLACE FUNCTION app_base64(json) RETURNS public.app_base64 AS $$
16+
-- here we reuse the previous app_uuid(text) function
17+
select decode($1 #>> '{}', 'base64');
18+
$$ LANGUAGE SQL IMMUTABLE;
19+
20+
CREATE CAST (json AS public.app_base64) WITH FUNCTION app_base64(json) AS IMPLICIT;
21+
322
CREATE TABLE documents(
423
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
524
title VARCHAR(255),
@@ -10,7 +29,7 @@ CREATE TABLE document_updates(
1029
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
1130
created_at timestamptz DEFAULT now(),
1231
document_id UUID,
13-
update_data BYTEA
32+
update_data app_base64
1433
);
1534

1635

demos/yjs-react-supabase-text-collab/src/library/powersync/SupabaseConnector.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ export class SupabaseConnector extends BaseObserver<SupabaseConnectorListener> i
9595

9696
if (op.op == UpdateType.PUT && op.table == 'document_updates') {
9797
updateBatch.push({
98-
...record,
99-
id: op.id
98+
id: op.id,
99+
document_id: op.opData!.document_id,
100+
update_data: op.opData!.update_b64
100101
});
101102
continue;
102103
}
@@ -123,7 +124,7 @@ export class SupabaseConnector extends BaseObserver<SupabaseConnectorListener> i
123124

124125
if (updateBatch.length > 0) {
125126
console.log('inserting batch of size', updateBatch.length);
126-
const result = await this.client.rpc('insert_document_updates', { batch: JSON.stringify(updateBatch) });
127+
const result = await this.client.from('document_updates').upsert(updateBatch);
127128
if (result.error) {
128129
console.error(result.error);
129130
result.error.message = `Could not update Supabase. Received error: ${result.error.message}`;

0 commit comments

Comments
 (0)