Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 65 additions & 49 deletions packages/database/features/step-definitions/stepdefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@ Given("the database is blank", async () => {
assert.equal(r.error, null);
const r3 = await client.from("group_membership").select("group_id");
assert.equal(r3.error, null);
const groupIds = new Set((r3.data || []).map(({group_id})=>group_id));
const groupIds = new Set((r3.data || []).map(({ group_id }) => group_id));
for (const id of groupIds) {
const ur = await client.auth.admin.deleteUser(id);
assert.equal(ur.error, null);
}
const r2 = await client.from("PlatformAccount").select("dg_account").not('dg_account', 'is', null);
const r2 = await client
.from("PlatformAccount")
.select("dg_account")
.not("dg_account", "is", null);
assert.equal(r2.error, null);
for (const {dg_account} of r2.data || []) {
for (const { dg_account } of r2.data || []) {
const r = await client.auth.admin.deleteUser(dg_account!);
assert.equal(r.error, null);
}
Expand Down Expand Up @@ -296,7 +299,7 @@ Given(
if (typeof spaceId !== "number") assert.fail("spaceId not a number");
const client = await getLoggedinDatabase(spaceId);
const response = await client.rpc("upsert_accounts_in_space", {
space_id_: spaceId, // eslint-disable-line @typescript-eslint/naming-convention
space_id_: spaceId,
accounts,
});
assert.equal(response.error, null);
Expand All @@ -313,7 +316,7 @@ Given(
if (typeof spaceId !== "number") assert.fail("spaceId not a number");
const client = await getLoggedinDatabase(spaceId);
const response = await client.rpc("upsert_documents", {
v_space_id: spaceId, // eslint-disable-line @typescript-eslint/naming-convention
v_space_id: spaceId,
data,
});
assert.equal(response.error, null);
Expand All @@ -332,10 +335,10 @@ Given(
if (typeof userId !== "number") assert.fail("userId not a number");
const client = await getLoggedinDatabase(spaceId);
const response = await client.rpc("upsert_content", {
v_space_id: spaceId, // eslint-disable-line @typescript-eslint/naming-convention
v_space_id: spaceId,
data,
v_creator_id: userId, // eslint-disable-line @typescript-eslint/naming-convention
content_as_document: false, // eslint-disable-line @typescript-eslint/naming-convention
v_creator_id: userId,
content_as_document: false,
});
assert.equal(response.error, null);
},
Expand All @@ -351,7 +354,7 @@ Given(
if (typeof spaceId !== "number") assert.fail("spaceId not a number");
const client = await getLoggedinDatabase(spaceId);
const response = await client.rpc("upsert_concepts", {
v_space_id: spaceId, // eslint-disable-line @typescript-eslint/naming-convention
v_space_id: spaceId,
data,
});
assert.equal(response.error, null);
Expand Down Expand Up @@ -404,47 +407,60 @@ Then("query results should look like this", (table: DataTable) => {
}
});

When("user of space {word} creates group {word}", async (spaceName: string, name: string) => {
const localRefs = (world.localRefs || {}) as LocalRefsType;
const spaceId = localRefs[spaceName];
if (typeof spaceId !== "number") assert.fail("spaceId not a number");
const client = await getLoggedinDatabase(spaceId);
try{
// eslint-disable-next-line @typescript-eslint/naming-convention
const response = await client.functions.invoke<{group_id: string}>("create-group", {body:{name}});
assert.equal(response.error, null);
assert.ok(response.data?.group_id, "create-group response missing group_id");
localRefs[name] = response.data.group_id;
world.localRefs = localRefs;
} catch (error) {
console.error((error as Record<string, any>).actual);
throw error;
}
})
When(
"user of space {word} creates group {word}",
async (spaceName: string, name: string) => {
const localRefs = (world.localRefs || {}) as LocalRefsType;
const spaceId = localRefs[spaceName];
if (typeof spaceId !== "number") assert.fail("spaceId not a number");
const client = await getLoggedinDatabase(spaceId);
try {
const response = await client.functions.invoke<{ group_id: string }>(
"create-group",
{ body: { name } },
);
assert.equal(response.error, null);
assert.ok(
response.data?.group_id,
"create-group response missing group_id",
);
localRefs[name] = response.data.group_id;
world.localRefs = localRefs;
} catch (error) {
console.error((error as Record<string, any>).actual);
throw error;
}
},
);

When("user of space {word} adds space {word} to group {word}",
async (space1Name: string, space2Name:string, groupName: string): Promise<void> =>{
const localRefs = (world.localRefs || {}) as LocalRefsType;
const space1Id = localRefs[space1Name];
const space2Id = localRefs[space2Name];
const groupId = localRefs[groupName];
if (typeof space1Id !== 'number') assert.fail("space1Id not a number");
if (typeof space2Id !== 'number') assert.fail("space2Id not a number");
if (typeof groupId !== 'string') assert.fail("groupId not a string");
const client2 = await getLoggedinDatabase(space2Id);
const r1 = await client2.from("PlatformAccount")
When(
"user of space {word} adds space {word} to group {word}",
async (
space1Name: string,
space2Name: string,
groupName: string,
): Promise<void> => {
const localRefs = (world.localRefs || {}) as LocalRefsType;
const space1Id = localRefs[space1Name];
const space2Id = localRefs[space2Name];
const groupId = localRefs[groupName];
if (typeof space1Id !== "number") assert.fail("space1Id not a number");
if (typeof space2Id !== "number") assert.fail("space2Id not a number");
if (typeof groupId !== "string") assert.fail("groupId not a string");
const client2 = await getLoggedinDatabase(space2Id);
const r1 = await client2
.from("PlatformAccount")
.select("dg_account")
.eq("account_local_id", spaceAnonUserEmail("Roam", space2Id))
.maybeSingle();
assert.equal(r1.error, null);
const memberId = r1.data?.dg_account;
assert.ok(memberId, "memberId not found for space2");
const client1 = await getLoggedinDatabase(space1Id);
const r2 = await client1.from("group_membership").insert({
/* eslint-disable @typescript-eslint/naming-convention */
group_id: groupId,
member_id: memberId
/* eslint-enable @typescript-eslint/naming-convention */
});
assert.equal(r2.error, null);
})
assert.equal(r1.error, null);
const memberId = r1.data?.dg_account;
assert.ok(memberId, "memberId not found for space2");
const client1 = await getLoggedinDatabase(space1Id);
const r2 = await client1.from("group_membership").insert({
group_id: groupId,
member_id: memberId,
});
assert.equal(r2.error, null);
},
);
1 change: 0 additions & 1 deletion packages/database/scripts/createEnv.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { fileURLToPath } from "node:url";
import dotenv from "dotenv";
import { Vercel } from "@vercel/sdk";

// eslint-disable-next-line @typescript-eslint/naming-convention
const __dirname = dirname(fileURLToPath(import.meta.url));
const projectRoot = join(__dirname, "..");
const baseParams: Record<string, string> = {};
Expand Down
2 changes: 0 additions & 2 deletions packages/database/src/dbDotEnv.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,9 @@ export const envContents = () => {
if (!path) {
// Fallback to process.env when running in production environments
const raw = {
/* eslint-disable @typescript-eslint/naming-convention */
SUPABASE_URL: process.env.SUPABASE_URL,
SUPABASE_PUBLISHABLE_KEY: process.env.SUPABASE_PUBLISHABLE_KEY,
NEXT_API_ROOT: process.env.NEXT_API_ROOT,
/* eslint-enable @typescript-eslint/naming-convention */
};
return Object.fromEntries(Object.entries(raw).filter(([, v]) => !!v));
}
Expand Down
1 change: 0 additions & 1 deletion packages/database/src/lib/contextFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export const fetchOrCreateSpaceIndirect = async (
const response = await fetch(baseUrl + "/space", {
method: "POST",
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
"Content-Type": "application/json",
},
body: JSON.stringify(input),
Expand Down
71 changes: 43 additions & 28 deletions packages/database/src/lib/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,70 @@ import type { DGSupabaseClient } from "./client";
const ASSETS_BUCKET_NAME = "assets";

export const addFile = async ({
client, spaceId, sourceLocalId, fname, mimetype, created, lastModified, content
}:{
client: DGSupabaseClient,
spaceId: number,
sourceLocalId: string,
fname: string,
mimetype: string,
created: Date,
lastModified: Date,
content: ArrayBuffer
client,
spaceId,
sourceLocalId,
fname,
mimetype,
created,
lastModified,
content,
}: {
client: DGSupabaseClient;
spaceId: number;
sourceLocalId: string;
fname: string;
mimetype: string;
created: Date;
lastModified: Date;
content: ArrayBuffer;
}): Promise<void> => {
// This assumes the content fits in memory.
const uint8Array = new Uint8Array(content);
const hashBuffer = await crypto.subtle.digest('SHA-256', uint8Array);
const hashBuffer = await crypto.subtle.digest("SHA-256", uint8Array);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashvalue = hashArray.map((h) => h.toString(16).padStart(2, '0')).join('');
const lookForDup = await client.rpc("file_exists",{hashvalue})
const hashvalue = hashArray
.map((h) => h.toString(16).padStart(2, "0"))
.join("");
const lookForDup = await client.rpc("file_exists", { hashvalue });
if (lookForDup.error) throw lookForDup.error;
const exists = lookForDup.data;
if (!exists) {
// we should use upsert here for sync issues, but we get obscure rls errors.
const uploadResult = await client.storage.from(ASSETS_BUCKET_NAME).upload(hashvalue, content, {contentType: mimetype});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (uploadResult.error && String((uploadResult.error as Record<string, any>).statusCode) !== "409")
const uploadResult = await client.storage
.from(ASSETS_BUCKET_NAME)
.upload(hashvalue, content, { contentType: mimetype });
if (
uploadResult.error &&
// eslint-disable-next-line @typescript-eslint/no-explicit-any
String((uploadResult.error as Record<string, any>).statusCode) !== "409"
)
throw uploadResult.error;
}
// not doing an upsert because it does not update on conflict
const frefResult = await client.from("FileReference").insert({
/* eslint-disable @typescript-eslint/naming-convention */
space_id: spaceId,
source_local_id: sourceLocalId,
last_modified: lastModified.toISOString(),
/* eslint-enable @typescript-eslint/naming-convention */
filepath: fname,
filehash: hashvalue,
created: created.toISOString()
created: created.toISOString(),
});

if (frefResult.error) {
if (frefResult.error.code === "23505") {
// 23505 is duplicate key, which means the file is already there, not an error
const updateResult = await client.from("FileReference").update({
// eslint-disable-next-line @typescript-eslint/naming-convention
last_modified: lastModified.toISOString(),
filehash: hashvalue,
created: created.toISOString()
}).eq("source_local_id", sourceLocalId).eq("space_id", spaceId).eq("filepath", fname);
const updateResult = await client
.from("FileReference")
.update({
last_modified: lastModified.toISOString(),
filehash: hashvalue,
created: created.toISOString(),
})
.eq("source_local_id", sourceLocalId)
.eq("space_id", spaceId)
.eq("filepath", fname);
if (updateResult.error) throw updateResult.error;
} else
throw frefResult.error;
} else throw frefResult.error;
}
}
};
Loading
Loading