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.

link breakpoints to build

Roman Pominoveb96fa061ae0
diff --git a/apps/designer/app/routes/index.tsx b/apps/designer/app/routes/index.tsx
index da2f1a9073..adc23c943b 100644
--- a/apps/designer/app/routes/index.tsx
+++ b/apps/designer/app/routes/index.tsx
@@ -40,7 +40,7 @@ export const loader: LoaderFunction = async ({
const [tree, props, breakpoints] = await Promise.all([
db.tree.loadById(prodBuild.pages.homePage.treeId),
db.props.loadByTreeId(prodBuild.pages.homePage.treeId),
- db.breakpoints.load(prodBuild.pages.homePage.treeId),
+ db.breakpoints.load(prodBuild.id),
]);
if (tree === null) {
throw new Error(`Tree ${prodBuild.pages.homePage.treeId} not found`);
diff --git a/apps/designer/app/routes/rest/breakpoints.$projectId.ts b/apps/designer/app/routes/rest/breakpoints.$projectId.ts
index e4def48b02..a1096aacd2 100644
--- a/apps/designer/app/routes/rest/breakpoints.$projectId.ts
+++ b/apps/designer/app/routes/rest/breakpoints.$projectId.ts
@@ -21,10 +21,10 @@ export const loader: LoaderFunction = async ({
`Project ${params.projectId} needs to be published first`
);
}
- const data = await db.breakpoints.load(prodBuild.pages.homePage.treeId);
+ const data = await db.breakpoints.load(prodBuild.id);
if (data === null) {
throw new Error(
- `Breakpoints not found for project ${params.projectId} and tree ID ${prodBuild.pages.homePage.treeId}`
+ `Breakpoints not found for project ${params.projectId} and build ID ${prodBuild.id}`
);
}
return data.values;
diff --git a/apps/designer/app/routes/rest/pages.$projectId.ts b/apps/designer/app/routes/rest/pages.$projectId.ts
index e4d483986f..7cd8252aa7 100644
--- a/apps/designer/app/routes/rest/pages.$projectId.ts
+++ b/apps/designer/app/routes/rest/pages.$projectId.ts
@@ -21,10 +21,10 @@ export const loader: LoaderFunction = async ({
`Project ${params.projectId} needs to be published first`
);
}
- const data = await db.breakpoints.load(prodBuild.pages.homePage.treeId);
+ const data = await db.breakpoints.load(prodBuild.id);
if (data === null) {
throw new Error(
- `Breakpoints not found for project ${params.projectId} and tree ID ${prodBuild.pages.homePage.treeId}`
+ `Breakpoints not found for project ${params.projectId} and build ID ${prodBuild.id}`
);
}
return data.values;
diff --git a/apps/designer/app/routes/rest/patch.ts b/apps/designer/app/routes/rest/patch.ts
index e6b459f28f..b3f05de21c 100644
--- a/apps/designer/app/routes/rest/patch.ts
+++ b/apps/designer/app/routes/rest/patch.ts
@@ -4,33 +4,32 @@ import { db } from "@webstudio-is/project/index.server";
import { type SyncItem } from "immerhin";
import { type Tree } from "@webstudio-is/react-sdk";
-const updaters = {
- root: db.tree.patchRoot,
- props: db.props.patch,
- breakpoints: db.breakpoints.patch,
-};
-
-type UpdaterKey = keyof typeof updaters;
-
type PatchData = {
transactions: Array<SyncItem>;
treeId: Tree["id"];
- buildId: Build["id"]; // @todo use this to update breakpoints
+ buildId: Build["id"];
};
export const action: ActionFunction = async ({ request }) => {
- const { treeId, transactions }: PatchData = await request.json();
+ const { treeId, buildId, transactions }: PatchData = await request.json();
if (treeId === undefined) return { errors: "Tree id required" };
+ if (buildId === undefined) return { errors: "Build id required" };
// @todo parallelize the updates
// currently not possible because we fetch the entire tree
// and parallelized updates will cause unpredictable side effects
for await (const transaction of transactions) {
for await (const change of transaction.changes) {
const { namespace, patches } = change;
- if (namespace in updaters === false) {
+
+ if (namespace === "root") {
+ await db.tree.patchRoot({ treeId }, patches);
+ } else if (namespace === "props") {
+ await db.props.patch({ treeId }, patches);
+ } else if (namespace === "breakpoints") {
+ await db.breakpoints.patch(buildId, patches);
+ } else {
return { errors: `Unknown namespace "${namespace}"` };
}
- await updaters[namespace as UpdaterKey]({ treeId }, patches);
}
}
return { status: "ok" };
diff --git a/apps/designer/app/shared/db/canvas.server.ts b/apps/designer/app/shared/db/canvas.server.ts
index 635b4683dd..2796ca7a38 100644
--- a/apps/designer/app/shared/db/canvas.server.ts
+++ b/apps/designer/app/shared/db/canvas.server.ts
@@ -18,7 +18,7 @@ const loadData = async (projectId: Project["id"]) => {
const [tree, props, breakpoints] = await Promise.all([
db.tree.loadById(devBuild.pages.homePage.treeId),
db.props.loadByTreeId(devBuild.pages.homePage.treeId),
- db.breakpoints.load(devBuild.pages.homePage.treeId),
+ db.breakpoints.load(devBuild.id),
]);
if (tree === null) {
diff --git a/packages/prisma-client/prisma/migrations/20220915143947_breakpoints-build/migration.sql b/packages/prisma-client/prisma/migrations/20220915143947_breakpoints-build/migration.sql
new file mode 100644
index 0000000000..b38cdce2df
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20220915143947_breakpoints-build/migration.sql
@@ -0,0 +1,2 @@
+-- AlterTable
+ALTER TABLE "Breakpoints" ADD COLUMN "buildId" TEXT;
diff --git a/packages/prisma-client/prisma/migrations/20220915144008_breakpoints-build_data/migration.ts b/packages/prisma-client/prisma/migrations/20220915144008_breakpoints-build_data/migration.ts
new file mode 100644
index 0000000000..710efd9bb1
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20220915144008_breakpoints-build_data/migration.ts
@@ -0,0 +1,53 @@
+import { PrismaClient } from "./client";
+import { z } from "zod";
+
+const PageSchema = z.object({
+ id: z.string(),
+ name: z.string(),
+ path: z.string(),
+ title: z.string(),
+ meta: z.record(z.string(), z.string()),
+ treeId: z.string(),
+});
+
+const PagesSchema = z.object({
+ homePage: PageSchema,
+ pages: z.array(PageSchema),
+});
+
+export default () => {
+ const client = new PrismaClient({
+ // Uncomment to see the queries in console as the migration runs
+ log: ["query", "info", "warn", "error"],
+ });
+ return client.$transaction(async (prisma) => {
+ const builds = await prisma.build.findMany();
+ const breakpoints = await prisma.breakpoints.findMany();
+
+ const buildsParsed = builds.map((build) => ({
+ id: build.id,
+ pages: PagesSchema.parse(JSON.parse(build.pages)),
+ }));
+
+ await Promise.all(
+ breakpoints.map((breakpoint) => {
+ const build = buildsParsed.find(
+ (build) => build.pages.homePage.treeId === breakpoint.treeId
+ );
+
+ if (build === undefined) {
+ throw new Error(
+ `Build not found for breakpoint ${breakpoint.treeId}`
+ );
+ }
+
+ return prisma.breakpoints.update({
+ where: { treeId: breakpoint.treeId },
+ data: {
+ buildId: build.id,
+ },
+ });
+ })
+ );
+ });
+};
diff --git a/packages/prisma-client/prisma/migrations/20220915144008_breakpoints-build_data/schema.prisma b/packages/prisma-client/prisma/migrations/20220915144008_breakpoints-build_data/schema.prisma
new file mode 100644
index 0000000000..425fdd9e70
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20220915144008_breakpoints-build_data/schema.prisma
@@ -0,0 +1,98 @@
+// DO NOT EDIT THIS FILE!
+// This is a copy of your schema.prisma that corresponds to the state of the database
+// when all migrations up until this one are applied.
+// It's used to generate a Prisma Client for the migration.
+
+// This is your Prisma schema file,
+// learn more about it in the docs: https://pris.ly/d/prisma-schema
+
+generator client {
+ provider = "prisma-client-js"
+ previewFeatures = ["interactiveTransactions"]
+ output = "client"
+}
+
+datasource db {
+ provider = "postgres"
+ url = env("DATABASE_URL")
+}
+
+model Team {
+ id String @id @default(uuid())
+ users User[]
+}
+
+enum Location {
+ FS
+ REMOTE
+}
+
+model Asset {
+ id String @id @default(uuid())
+ project Project @relation(fields: [projectId], references: [id])
+ projectId String
+ format String?
+ size Int
+ name String
+ description String?
+ location Location
+ createdAt DateTime @default(now())
+ meta String @default("{}")
+}
+
+model User {
+ id String @id @default(uuid())
+ email String? @unique
+ provider String?
+ image String?
+ username String?
+ createdAt DateTime @default(now())
+ team Team? @relation(fields: [teamId], references: [id])
+ teamId String?
+ projects Project[]
+}
+
+model Project {
+ id String @id @default(uuid())
+ title String
+ domain String @unique
+ user User? @relation(fields: [userId], references: [id])
+ userId String?
+ assets Asset[]
+ build Build[]
+}
+
+model Build {
+ id String @id @default(uuid())
+ createdAt DateTime @default(now())
+ pages String
+
+ project Project @relation(fields: [projectId], references: [id])
+ projectId String
+
+ isDev Boolean // exctly one is true per project
+ isProd Boolean // at most one is true per project (none if not published)
+
+ // breakpoints Breakpoints?
+}
+
+model Tree {
+ id String @id @default(uuid())
+ root String
+}
+
+model InstanceProps {
+ id String @id @default(uuid())
+ instanceId String
+ treeId String
+ props String @default("[]")
+}
+
+model Breakpoints {
+ // build Build @relation(fields: [buildId], references: [id])
+ buildId String? // @id
+ values String @default("[]")
+
+ // to be deleted
+ treeId String @id @default(uuid())
+}
diff --git a/packages/prisma-client/prisma/migrations/20220915145316_breakpoints-build_cleanup/migration.sql b/packages/prisma-client/prisma/migrations/20220915145316_breakpoints-build_cleanup/migration.sql
new file mode 100644
index 0000000000..1025bfe303
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20220915145316_breakpoints-build_cleanup/migration.sql
@@ -0,0 +1,8 @@
+-- AlterTable
+ALTER TABLE "Breakpoints" DROP CONSTRAINT "Breakpoints_pkey",
+DROP COLUMN "treeId",
+ALTER COLUMN "buildId" SET NOT NULL,
+ADD CONSTRAINT "Breakpoints_pkey" PRIMARY KEY ("buildId");
+
+-- AddForeignKey
+ALTER TABLE "Breakpoints" ADD CONSTRAINT "Breakpoints_buildId_fkey" FOREIGN KEY ("buildId") REFERENCES "Build"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
diff --git a/packages/prisma-client/prisma/schema.prisma b/packages/prisma-client/prisma/schema.prisma
index 3933ee3032..31f911498c 100644
--- a/packages/prisma-client/prisma/schema.prisma
+++ b/packages/prisma-client/prisma/schema.prisma
@@ -61,10 +61,14 @@ model Build {
id String @id @default(uuid())
createdAt DateTime @default(now())
pages String
- isDev Boolean // exctly one is true per project
- isProd Boolean // at most one is true per project (none if not published)
- project Project @relation(fields: [projectId], references: [id])
+
+ project Project @relation(fields: [projectId], references: [id])
projectId String
+
+ isDev Boolean // exctly one is true per project
+ isProd Boolean // at most one is true per project (none if not published)
+
+ breakpoints Breakpoints?
}
model Tree {
@@ -80,6 +84,7 @@ model InstanceProps {
}
model Breakpoints {
- treeId String @id @default(uuid())
- values String @default("[]")
+ build Build @relation(fields: [buildId], references: [id])
+ buildId String @id
+ values String @default("[]")
}
diff --git a/packages/project/src/db/breakpoints.ts b/packages/project/src/db/breakpoints.ts
index 191583003e..85ecf05d1b 100644
--- a/packages/project/src/db/breakpoints.ts
+++ b/packages/project/src/db/breakpoints.ts
@@ -1,82 +1,64 @@
import {
initialBreakpoints,
- type Tree,
type Breakpoint,
BreakpointsSchema,
} from "@webstudio-is/react-sdk";
import ObjectId from "bson-objectid";
import { applyPatches, type Patch } from "immer";
-import { prisma, Project } from "@webstudio-is/prisma-client";
+import { type Breakpoints, prisma } from "@webstudio-is/prisma-client";
-export const load = async (treeId?: Tree["id"]) => {
- if (typeof treeId !== "string") {
- throw new Error("Tree ID required");
- }
-
- const breakpoint = await prisma.breakpoints.findUnique({
- where: { treeId },
+export const load = async (buildId: Breakpoints["buildId"]) => {
+ const breakpoints = await prisma.breakpoints.findUnique({
+ where: { buildId },
});
- if (breakpoint === null) {
- throw new Error("Breakpoint not found");
+ if (breakpoints === null) {
+ throw new Error("Breakpoints not found");
}
- const values: Array<Breakpoint> = JSON.parse(breakpoint.values);
+ const values: Array<Breakpoint> = JSON.parse(breakpoints.values);
BreakpointsSchema.parse(values);
return {
- ...breakpoint,
+ ...breakpoints,
values,
};
};
-export const getBreakpointsWithId = () =>
- initialBreakpoints.map((breakpoint) => ({
- ...breakpoint,
- id: ObjectId().toString(),
- }));
+export const createValues = () =>
+ BreakpointsSchema.parse(
+ initialBreakpoints.map((breakpoint) => ({
+ ...breakpoint,
+ id: ObjectId().toString(),
+ }))
+ );
export const create = async (
- treeId: Project["id"],
+ buildId: Breakpoints["buildId"],
values: Array<Breakpoint>
) => {
- BreakpointsSchema.parse(values);
+ const breakpoints = await prisma.breakpoints.create({
+ data: {
+ values: JSON.stringify(values),
+ buildId,
+ },
+ });
- const data = {
- treeId,
- values: JSON.stringify(values),
- };
- await prisma.breakpoints.create({ data });
return {
- ...data,
- breakpoints: values,
+ ...breakpoints,
+ values,
};
};
-export const clone = async ({
- previousTreeId,
- nextTreeId,
-}: {
- previousTreeId: Tree["id"];
- nextTreeId: Tree["id"];
-}) => {
- const breakpoints = await load(previousTreeId);
- if (breakpoints === null) {
- throw new Error(`Didn't find breakpoints with tree id "${previousTreeId}"`);
- }
- await create(nextTreeId, breakpoints.values);
-};
-
export const patch = async (
- { treeId }: { treeId: Tree["id"] },
+ buildId: Breakpoints["buildId"],
patches: Array<Patch>
) => {
- const breakpoints = await load(treeId);
- if (breakpoints === null) return;
+ const breakpoints = await load(buildId);
const nextValues = applyPatches(breakpoints.values, patches);
BreakpointsSchema.parse(nextValues);
await prisma.breakpoints.update({
- where: { treeId },
+ where: { buildId: breakpoints.buildId },
data: { values: JSON.stringify(nextValues) },
});
};
diff --git a/packages/project/src/db/build.ts b/packages/project/src/db/build.ts
index 201f2d5ffd..0cddcc7c20 100644
--- a/packages/project/src/db/build.ts
+++ b/packages/project/src/db/build.ts
@@ -1,4 +1,5 @@
import { prisma, Build as DbBuild } from "@webstudio-is/prisma-client";
+import { type Breakpoint } from "@webstudio-is/react-sdk";
import { v4 as uuid } from "uuid";
import * as db from ".";
import { Build, Pages, PagesSchema } from "./types";
@@ -60,10 +61,10 @@ export async function loadByProjectId(
return parseBuild(build);
}
-const createPages = async () => {
- const breakpoints = db.breakpoints.getBreakpointsWithId();
+const createPages = async (breakpoints: Array<Breakpoint>) => {
+ // const breakpoints = db.breakpoints.getBreakpointsWithId();
const tree = await db.tree.create(db.tree.createRootInstance(breakpoints));
- await db.breakpoints.create(tree.id, breakpoints);
+ // await db.breakpoints.create(tree.id, breakpoints);
return PagesSchema.parse({
homePage: {
id: uuid(),
@@ -84,7 +85,6 @@ const clonePages = async (source: Pages) => {
const tree = await db.tree.clone(treeId);
await db.props.clone({ previousTreeId: treeId, nextTreeId: tree.id });
- await db.breakpoints.clone({ previousTreeId: treeId, nextTreeId: tree.id });
return PagesSchema.parse({
homePage: {
@@ -121,12 +121,16 @@ export async function create(
throw new Error("Dev build already exists");
}
+ const breakpointsValues = sourceBuild
+ ? (await db.breakpoints.load(sourceBuild.id)).values
+ : db.breakpoints.createValues();
+
const pages =
sourceBuild === undefined
- ? await createPages()
+ ? await createPages(breakpointsValues)
: await clonePages(sourceBuild.pages);
- await prisma.build.create({
+ const build = await prisma.build.create({
data: {
projectId,
pages: JSON.stringify(pages),
@@ -135,6 +139,8 @@ export async function create(
},
});
+ await db.breakpoints.create(build.id, breakpointsValues);
+
return;
}
@@ -142,9 +148,11 @@ export async function create(
throw new Error("Source build required");
}
+ const breakpointsValues = (await db.breakpoints.load(sourceBuild.id)).values;
+
const pages = await clonePages(sourceBuild.pages);
- await prisma.$transaction([
+ const [, build] = await prisma.$transaction([
prisma.build.updateMany({
where: { projectId: projectId, isProd: true },
data: { isProd: false },
@@ -158,4 +166,6 @@ export async function create(
},
}),
]);
+
+ await db.breakpoints.create(build.id, breakpointsValues);
}