diff --git a/packages/server/src/hocuspocus/extension-supabase/SupabaseHocuspocus.ts b/packages/server/src/hocuspocus/extension-supabase/SupabaseHocuspocus.ts index 0aec5d873..b4365635b 100644 --- a/packages/server/src/hocuspocus/extension-supabase/SupabaseHocuspocus.ts +++ b/packages/server/src/hocuspocus/extension-supabase/SupabaseHocuspocus.ts @@ -81,13 +81,13 @@ export class SupabaseHocuspocus extends Database { .from("documents") .update( { data: "\\x" + data.state.toString("hex") }, // add \x for postgres binary data - { count: "exact" } + { count: "exact" }, ) .eq("nano_id", data.documentName) .select(); if (ret.data?.length !== 1) { throw new Error( - "unexpected: not found when storing " + data.documentName + "unexpected: not found when storing " + data.documentName, ); } }, @@ -164,7 +164,7 @@ export class SupabaseHocuspocus extends Database { socketId: string, documentId: string, event: Y.YEvent[], - tr: Y.Transaction + tr: Y.Transaction, ) => { const supabase = this.supabaseMap.get(socketId); if (!supabase) { @@ -191,9 +191,15 @@ export class SupabaseHocuspocus extends Database { .filter( (r) => r.namespace === ChildReference.namespace && - r.type === ChildReference.type + r.type === ChildReference.type, ) - .map((r) => r.target as string); + .map((r) => { + const target = r.target as string; + if (!target.startsWith("typecell:typecell.org/")) { + console.error("invalid target", target); + } + return target.split("/", 2)[1]; + }); const refsIds = await serviceClient .from("documents") @@ -229,19 +235,20 @@ export class SupabaseHocuspocus extends Database { if (ret.error) { throw new Error( - "error executing supabase request (remove) " + ret.error.message + "error executing supabase request (remove) " + ret.error.message, ); } } if (toAdd.length) { - const ret = await supabase - .from("document_relations") - .insert(toAdd.map((e) => ({ parent_id: documentId, child_id: e }))); + const ret = await supabase.from("document_relations").upsert( + toAdd.map((e) => ({ parent_id: documentId, child_id: e })), + { ignoreDuplicates: true }, + ); if (ret.error) { throw new Error( - "error executing supabase request (add) " + ret.error.message + "error executing supabase request (add) " + ret.error.message, ); } } @@ -259,7 +266,7 @@ export class SupabaseHocuspocus extends Database { data.socketId, documentIdByDocument.get(data.document)!, event, - tr + tr, ); data.document.getMap("refs").observeDeep(refListener); diff --git a/packages/server/src/hocuspocus/extension-supabase/hocuspocus.test.ts b/packages/server/src/hocuspocus/extension-supabase/hocuspocus.test.ts index 85d734099..cf6949d79 100644 --- a/packages/server/src/hocuspocus/extension-supabase/hocuspocus.test.ts +++ b/packages/server/src/hocuspocus/extension-supabase/hocuspocus.test.ts @@ -53,7 +53,7 @@ describe("SupabaseHocuspocus", () => { docId, ydoc, alice.session?.access_token + "$" + alice.session?.refresh_token, - wsProvider + wsProvider, ); ydoc.getMap("mymap").set("hello", "world"); @@ -75,7 +75,7 @@ describe("SupabaseHocuspocus", () => { docId, ydoc2, alice.session?.access_token + "$" + alice.session?.refresh_token, - wsProvider + wsProvider, ); await async.timeout(100); @@ -92,7 +92,7 @@ describe("SupabaseHocuspocus", () => { docId, ydoc, alice.session?.access_token + "$" + alice.session?.refresh_token, - wsProvider + wsProvider, ); ydoc.getMap("mymap").set("hello", "world"); @@ -102,7 +102,7 @@ describe("SupabaseHocuspocus", () => { docId, ydoc2, alice.session?.access_token + "$" + alice.session?.refresh_token, - wsProvider + wsProvider, ); ydoc2.getMap("anothermap").set("hello", "world"); @@ -122,7 +122,7 @@ describe("SupabaseHocuspocus", () => { docId, ydoc, alice.session?.access_token + "$" + alice.session?.refresh_token, - wsProvider + wsProvider, ); ydoc.getMap("mymap").set("hello", "world"); @@ -132,7 +132,7 @@ describe("SupabaseHocuspocus", () => { docId, ydoc2, bob.session?.access_token + "", // TODO - wsProvider + wsProvider, ); ydoc2.getMap("anothermap").set("hello", "world"); await async.timeout(100); @@ -184,7 +184,7 @@ describe("SupabaseHocuspocus", () => { const docB = createDocument(bob.user!.id, "", "no-access"); const retB = await bob.supabase.from("documents").insert(docB).select(); expect(retB.error).toBeNull(); - docBId = retB.data![0].nano_id; + docBId = "typecell:typecell.org/" + retB.data![0].nano_id; docBDbID = retB.data![0].id; }); @@ -197,7 +197,7 @@ describe("SupabaseHocuspocus", () => { docId, ydoc, alice.session?.access_token + "$" + alice.session?.refresh_token, - wsProvider + wsProvider, ); ydoc.getMap("refs").set("fakekey", {