Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Use projectId as composite id in tables (#932)

Bogdan Chadkinaff014f4d087
diff --git a/apps/designer/app/routes/rest/pages.$projectId.ts b/apps/designer/app/routes/rest/pages.$projectId.ts
index b12f4693b5..0df001592f 100644
--- a/apps/designer/app/routes/rest/pages.$projectId.ts
+++ b/apps/designer/app/routes/rest/pages.$projectId.ts
@@ -101,7 +101,12 @@ const handlePost = async (
}
}
- await db.build.editPage(devBuild.id, id, data);
+ await db.build.editPage({
+ projectId,
+ buildId: devBuild.id,
+ pageId: id,
+ data,
+ });
return { status: "ok" };
};
@@ -124,7 +129,11 @@ const handleDelete = async (
return makeFieldError("id", `Can't delete the home page`);
}
- await db.build.deletePage(devBuild.id, id);
+ await db.build.deletePage({
+ projectId,
+ buildId: devBuild.id,
+ pageId: id,
+ });
return { status: "ok" };
};
diff --git a/apps/designer/app/shared/db/canvas.server.ts b/apps/designer/app/shared/db/canvas.server.ts
index 3f81b217a8..1e308794ec 100644
--- a/apps/designer/app/shared/db/canvas.server.ts
+++ b/apps/designer/app/shared/db/canvas.server.ts
@@ -24,7 +24,7 @@ export const loadCanvasData = async (
}
const [tree, assets] = await Promise.all([
- projectDb.tree.loadById(page.treeId),
+ projectDb.tree.loadById({ projectId: project.id, treeId: page.treeId }),
loadByProject(project.id),
]);
diff --git a/packages/asset-uploader/src/db/create.ts b/packages/asset-uploader/src/db/create.ts
index 3975b0cf17..f72f4e383f 100644
--- a/packages/asset-uploader/src/db/create.ts
+++ b/packages/asset-uploader/src/db/create.ts
@@ -92,7 +92,9 @@ export const createAssetWithLimit = async (
const { id, meta, format, name, location } = asset;
const dbAsset = await prisma.asset.update({
- where: { id: updated.id },
+ where: {
+ id_projectId: { id: updated.id, projectId },
+ },
data: {
id,
location,
@@ -108,7 +110,11 @@ export const createAssetWithLimit = async (
return formatAsset(dbAsset);
} catch (error) {
if (updated) {
- await prisma.asset.delete({ where: { id: updated.id } });
+ await prisma.asset.delete({
+ where: {
+ id_projectId: { id: updated.id, projectId },
+ },
+ });
}
throw error;
diff --git a/packages/eslint-config-custom/index.js b/packages/eslint-config-custom/index.js
index d865706e0d..0e070aadb0 100644
--- a/packages/eslint-config-custom/index.js
+++ b/packages/eslint-config-custom/index.js
@@ -42,7 +42,7 @@ module.exports = {
],
curly: 2,
eqeqeq: ["error", "always", { null: "ignore" }],
- camelcase: 2,
+ camelcase: [2, { properties: "never" }],
radix: 2,
"import/no-internal-modules": [
"error",
diff --git a/packages/prisma-client/prisma/migrations/20230202192437_composite_ids/migration.sql b/packages/prisma-client/prisma/migrations/20230202192437_composite_ids/migration.sql
new file mode 100644
index 0000000000..3105a1a4fc
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20230202192437_composite_ids/migration.sql
@@ -0,0 +1,17 @@
+-- DropForeignKey
+ALTER TABLE "Tree" DROP CONSTRAINT "Tree_buildId_fkey";
+
+-- AlterTable
+ALTER TABLE "Asset" DROP CONSTRAINT "Asset_pkey",
+ADD CONSTRAINT "Asset_pkey" PRIMARY KEY ("id", "projectId");
+
+-- AlterTable
+ALTER TABLE "Build" DROP CONSTRAINT "Build_pkey",
+ADD CONSTRAINT "Build_pkey" PRIMARY KEY ("id", "projectId");
+
+-- AlterTable
+ALTER TABLE "Tree" DROP CONSTRAINT "Tree_pkey",
+ADD CONSTRAINT "Tree_pkey" PRIMARY KEY ("id", "projectId");
+
+-- AddForeignKey
+ALTER TABLE "Tree" ADD CONSTRAINT "Tree_buildId_projectId_fkey" FOREIGN KEY ("buildId", "projectId") REFERENCES "Build"("id", "projectId") ON DELETE RESTRICT ON UPDATE CASCADE;
diff --git a/packages/prisma-client/prisma/schema.prisma b/packages/prisma-client/prisma/schema.prisma
index 1e0b8d22a3..e7b9088ea0 100644
--- a/packages/prisma-client/prisma/schema.prisma
+++ b/packages/prisma-client/prisma/schema.prisma
@@ -28,7 +28,7 @@ enum UploadStatus {
}
model Asset {
- id String @id @default(uuid())
+ id String @default(uuid())
projectId String
format String
size Int
@@ -38,6 +38,8 @@ model Asset {
createdAt DateTime @default(now())
meta String @default("{}")
status UploadStatus @default(UPLOADED)
+
+ @@id([id, projectId])
}
model User {
@@ -65,7 +67,7 @@ model Project {
}
model Build {
- id String @id @default(uuid())
+ id String @default(uuid())
createdAt DateTime @default(now())
pages String
@@ -79,19 +81,23 @@ model Build {
breakpoints String @default("[]")
styles String @default("[]")
styleSources String @default("[]")
+
+ @@id([id, projectId])
}
model Tree {
- id String @id @default(uuid())
+ id String @default(uuid())
project Project @relation(fields: [projectId], references: [id])
projectId String
- build Build @relation(fields: [buildId], references: [id])
+ build Build @relation(fields: [buildId, projectId], references: [id, projectId])
buildId String
root String
instances String @default("[]")
props String @default("[]")
styles String @default("[]")
styleSelections String @default("[]")
+
+ @@id([id, projectId])
}
model Breakpoints {
diff --git a/packages/project/src/db/breakpoints.ts b/packages/project/src/db/breakpoints.ts
index 6fbb7c5863..a58992a842 100644
--- a/packages/project/src/db/breakpoints.ts
+++ b/packages/project/src/db/breakpoints.ts
@@ -37,7 +37,9 @@ export const patch = async (
}
const build = await prisma.build.findUnique({
- where: { id: buildId },
+ where: {
+ id_projectId: { id: buildId, projectId },
+ },
});
if (build === null) {
@@ -49,7 +51,9 @@ export const patch = async (
);
await prisma.build.update({
- where: { id: buildId },
+ where: {
+ id_projectId: { id: buildId, projectId },
+ },
data: { breakpoints: JSON.stringify(patchedBreakpoints) },
});
};
diff --git a/packages/project/src/db/build.ts b/packages/project/src/db/build.ts
index 0299981376..d16ec1e8fd 100644
--- a/packages/project/src/db/build.ts
+++ b/packages/project/src/db/build.ts
@@ -27,13 +27,17 @@ export const parseBuild = async (build: DbBuild): Promise<Build> => {
};
};
-export const loadById = async (id: Build["id"]): Promise<Build> => {
- if (typeof id !== "string") {
- throw new Error("Build ID required");
- }
-
+export const loadById = async ({
+ projectId,
+ buildId,
+}: {
+ projectId: Project["id"];
+ buildId: Build["id"];
+}): Promise<Build> => {
const build = await prisma.build.findUnique({
- where: { id },
+ where: {
+ id_projectId: { projectId, id: buildId },
+ },
});
if (build === null) {
@@ -80,14 +84,18 @@ export async function loadByProjectId(
}
const updatePages = async (
- buildId: Build["id"],
+ { projectId, buildId }: { projectId: Project["id"]; buildId: Build["id"] },
updater: (currentPages: Pages) => Promise<Pages>
) => {
- const build = await loadById(buildId);
+ const build = await loadById({ projectId, buildId });
const updatedPages = Pages.parse(await updater(build.pages));
const updatedBuild = await prisma.build.update({
- where: { id: buildId },
- data: { pages: JSON.stringify(updatedPages) },
+ where: {
+ id_projectId: { projectId, id: buildId },
+ },
+ data: {
+ pages: JSON.stringify(updatedPages),
+ },
});
return parseBuild(updatedBuild);
};
@@ -102,7 +110,7 @@ export const addPage = async ({
data: Pick<Page, "name" | "path"> &
Partial<Omit<Page, "id" | "treeId" | "name" | "path">>;
}) => {
- return updatePages(buildId, async (currentPages) => {
+ return updatePages({ projectId, buildId }, async (currentPages) => {
const tree = await db.tree.create(
db.tree.createTree({ projectId, buildId })
);
@@ -124,12 +132,18 @@ export const addPage = async ({
});
};
-export const editPage = async (
- buildId: Build["id"],
- pageId: Page["id"],
- data: Partial<Omit<Page, "id" | "treeId">>
-) => {
- return updatePages(buildId, async (currentPages) => {
+export const editPage = async ({
+ projectId,
+ buildId,
+ pageId,
+ data,
+}: {
+ projectId: Project["id"];
+ buildId: Build["id"];
+ pageId: Page["id"];
+ data: Partial<Omit<Page, "id" | "treeId">>;
+}) => {
+ return updatePages({ projectId, buildId }, async (currentPages) => {
const currentPage = pagesUtils.findByIdOrPath(currentPages, pageId);
if (currentPage === undefined) {
throw new Error(`Page with id "${pageId}" not found`);
@@ -156,8 +170,16 @@ export const editPage = async (
});
};
-export const deletePage = async (buildId: Build["id"], pageId: Page["id"]) => {
- return updatePages(buildId, async (currentPages) => {
+export const deletePage = async ({
+ projectId,
+ buildId,
+ pageId,
+}: {
+ projectId: Project["id"];
+ buildId: Build["id"];
+ pageId: Page["id"];
+}) => {
+ return updatePages({ projectId, buildId }, async (currentPages) => {
if (pageId === currentPages.homePage.id) {
throw new Error("Cannot delete home page");
}
@@ -167,7 +189,7 @@ export const deletePage = async (buildId: Build["id"], pageId: Page["id"]) => {
throw new Error(`Page with id "${pageId}" not found`);
}
- await db.tree.deleteById(page.treeId);
+ await db.tree.deleteById({ projectId, treeId: page.treeId });
return {
homePage: currentPages.homePage,
@@ -198,24 +220,24 @@ const createPages = async (
};
const clonePage = async (
- source: Page,
+ { projectId, source }: { projectId: Project["id"]; source: Page },
client: Prisma.TransactionClient = prisma
) => {
const treeId = source.treeId;
- const tree = await db.tree.clone(treeId, client);
+ const tree = await db.tree.clone({ projectId, treeId }, client);
return { ...source, id: uuid(), treeId: tree.id };
};
const clonePages = async (
- source: Pages,
+ { projectId, source }: { projectId: Project["id"]; source: Pages },
client: Prisma.TransactionClient = prisma
) => {
const clones = [];
for (const page of source.pages) {
- clones.push(await clonePage(page, client));
+ clones.push(await clonePage({ projectId, source: page }, client));
}
return Pages.parse({
- homePage: await clonePage(source.homePage, client),
+ homePage: await clonePage({ projectId, source: source.homePage }, client),
pages: clones,
});
};
@@ -283,11 +305,11 @@ export async function create(
const pages =
sourceBuild === undefined
? await createPages({ projectId, buildId: build.id }, client)
- : await clonePages(sourceBuild.pages, client);
+ : await clonePages({ projectId, source: sourceBuild.pages }, client);
await client.build.update({
where: {
- id: build.id,
+ id_projectId: { projectId, id: build.id },
},
data: {
pages: JSON.stringify(pages),
diff --git a/packages/project/src/db/props.ts b/packages/project/src/db/props.ts
index 3f6af7b682..858a3eee8b 100644
--- a/packages/project/src/db/props.ts
+++ b/packages/project/src/db/props.ts
@@ -97,7 +97,9 @@ export const patch = async (
}
const tree = await prisma.tree.findUnique({
- where: { id: treeId },
+ where: {
+ id_projectId: { projectId, id: treeId },
+ },
});
if (tree === null) {
return;
@@ -109,6 +111,8 @@ export const patch = async (
data: {
props: serializeProps(patchedProps),
},
- where: { id: treeId },
+ where: {
+ id_projectId: { projectId, id: treeId },
+ },
});
};
diff --git a/packages/project/src/db/style-source-selections.ts b/packages/project/src/db/style-source-selections.ts
index bd66d9d11e..e95a113ba4 100644
--- a/packages/project/src/db/style-source-selections.ts
+++ b/packages/project/src/db/style-source-selections.ts
@@ -22,7 +22,9 @@ export const patch = async (
}
const tree = await prisma.tree.findUnique({
- where: { id: treeId },
+ where: {
+ id_projectId: { projectId, id: treeId },
+ },
});
if (tree === null) {
return;
@@ -40,6 +42,8 @@ export const patch = async (
data: {
styleSelections: JSON.stringify(patchedStyleSourceSelections),
},
- where: { id: treeId },
+ where: {
+ id_projectId: { projectId, id: treeId },
+ },
});
};
diff --git a/packages/project/src/db/style-sources.ts b/packages/project/src/db/style-sources.ts
index fb8b4b18db..ec65464a39 100644
--- a/packages/project/src/db/style-sources.ts
+++ b/packages/project/src/db/style-sources.ts
@@ -22,7 +22,9 @@ export const patch = async (
}
const build = await prisma.build.findUnique({
- where: { id: buildId },
+ where: {
+ id_projectId: { projectId, id: buildId },
+ },
});
if (build === null) {
return;
@@ -38,6 +40,8 @@ export const patch = async (
data: {
styleSources: JSON.stringify(patchedStyleSources),
},
- where: { id: buildId },
+ where: {
+ id_projectId: { projectId, id: buildId },
+ },
});
};
diff --git a/packages/project/src/db/styles.ts b/packages/project/src/db/styles.ts
index 2a1ffb9a28..3510721119 100644
--- a/packages/project/src/db/styles.ts
+++ b/packages/project/src/db/styles.ts
@@ -121,7 +121,9 @@ export const patch = async (
}
const build = await prisma.build.findUnique({
- where: { id: buildId },
+ where: {
+ id_projectId: { projectId, id: buildId },
+ },
});
if (build === null) {
return;
@@ -136,6 +138,8 @@ export const patch = async (
data: {
styles: JSON.stringify(patchedStyles),
},
- where: { id: build.id },
+ where: {
+ id_projectId: { projectId, id: buildId },
+ },
});
};
diff --git a/packages/project/src/db/tree.ts b/packages/project/src/db/tree.ts
index f1db038828..02316b8d35 100644
--- a/packages/project/src/db/tree.ts
+++ b/packages/project/src/db/tree.ts
@@ -84,8 +84,18 @@ export const create = async (
});
};
-export const deleteById = async (treeId: Tree["id"]): Promise<void> => {
- await prisma.tree.delete({ where: { id: treeId } });
+export const deleteById = async ({
+ projectId,
+ treeId,
+}: {
+ projectId: Project["id"];
+ treeId: Tree["id"];
+}): Promise<void> => {
+ await prisma.tree.delete({
+ where: {
+ id_projectId: { projectId, id: treeId },
+ },
+ });
};
const denormalizeTree = (instances: z.infer<typeof Instances>) => {
@@ -113,11 +123,13 @@ const denormalizeTree = (instances: z.infer<typeof Instances>) => {
};
export const loadById = async (
- treeId: Tree["id"],
+ { projectId, treeId }: { projectId: Project["id"]; treeId: Tree["id"] },
client: Prisma.TransactionClient = prisma
): Promise<Tree | null> => {
const tree = await client.tree.findUnique({
- where: { id: treeId },
+ where: {
+ id_projectId: { projectId, id: treeId },
+ },
});
if (tree === null) {
return null;
@@ -141,10 +153,10 @@ export const loadById = async (
};
export const clone = async (
- treeId: Tree["id"],
+ { projectId, treeId }: { projectId: Project["id"]; treeId: Tree["id"] },
client: Prisma.TransactionClient = prisma
) => {
- const tree = await loadById(treeId, client);
+ const tree = await loadById({ projectId, treeId }, client);
if (tree === null) {
throw new Error(`Tree ${treeId} not found`);
}
@@ -165,7 +177,7 @@ export const patch = async (
throw new Error("You don't have edit access to this project");
}
- const tree = await loadById(treeId);
+ const tree = await loadById({ projectId, treeId });
if (tree === null) {
throw new Error(`Tree ${treeId} not found`);
}
@@ -180,6 +192,8 @@ export const patch = async (
root: "",
instances: JSON.stringify(instances),
},
- where: { id: treeId },
+ where: {
+ id_projectId: { projectId, id: treeId },
+ },
});
};