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.

Move tree db utils to project-build (#1042)

Bogdan Chadkind889159874da
diff --git a/apps/builder/app/routes/rest/patch.ts b/apps/builder/app/routes/rest/patch.ts
index 65b2f2c474..48ab504d74 100644
--- a/apps/builder/app/routes/rest/patch.ts
+++ b/apps/builder/app/routes/rest/patch.ts
@@ -1,6 +1,7 @@
import type { ActionArgs } from "@remix-run/node";
import type { SyncItem } from "immerhin";
import type { Build, Tree } from "@webstudio-is/project-build";
+import * as buildDb from "@webstudio-is/project-build/server";
import type { Project } from "@webstudio-is/project";
import { db as projectDb } from "@webstudio-is/project/server";
import { createContext } from "~/shared/context.server";
@@ -35,9 +36,9 @@ export const action = async ({ request }: ActionArgs) => {
const { namespace, patches } = change;
if (namespace === "root") {
- await projectDb.tree.patch({ treeId, projectId }, patches, context);
+ await buildDb.patchTree({ treeId, projectId }, patches, context);
} else if (namespace === "styleSourceSelections") {
- await projectDb.styleSourceSelections.patch(
+ await buildDb.patchStyleSourceSelections(
{ treeId, projectId },
patches,
context
@@ -51,7 +52,7 @@ export const action = async ({ request }: ActionArgs) => {
} else if (namespace === "styles") {
await projectDb.styles.patch({ buildId, projectId }, patches, context);
} else if (namespace === "props") {
- await projectDb.props.patch({ treeId, projectId }, patches, context);
+ await buildDb.patchProps({ treeId, projectId }, patches, context);
} else if (namespace === "breakpoints") {
await projectDb.breakpoints.patch(
{ buildId, projectId },
diff --git a/apps/builder/app/shared/db/canvas.server.ts b/apps/builder/app/shared/db/canvas.server.ts
index 9ec639ada1..8bbfb810e4 100644
--- a/apps/builder/app/shared/db/canvas.server.ts
+++ b/apps/builder/app/shared/db/canvas.server.ts
@@ -1,6 +1,7 @@
import type { CanvasData, Project } from "@webstudio-is/project";
-import { db as projectDb } from "@webstudio-is/project/server";
+import * as buildDb from "@webstudio-is/project-build/server";
import { utils } from "@webstudio-is/project";
+import { db as projectDb } from "@webstudio-is/project/server";
import { loadByProject } from "@webstudio-is/asset-uploader/server";
import type { AppContext } from "@webstudio-is/trpc-interface/server";
@@ -79,7 +80,7 @@ export const loadCanvasData = async (
}
const [tree, assets] = await Promise.all([
- projectDb.tree.loadById(
+ buildDb.loadTreeById(
{
projectId: props.project.id,
treeId: page.treeId,
diff --git a/packages/project-build/package.json b/packages/project-build/package.json
index c7981990bd..42f8adfce5 100644
--- a/packages/project-build/package.json
+++ b/packages/project-build/package.json
@@ -10,7 +10,8 @@
".": {
"import": "./lib/index.js",
"require": "./lib/cjs/index.cjs"
- }
+ },
+ "./server": "./server.js"
},
"types": "src/index.ts",
"files": [
@@ -28,6 +29,10 @@
"dependencies": {
"@webstudio-is/asset-uploader": "workspace:^",
"@webstudio-is/css-data": "workspace:^",
+ "@webstudio-is/prisma-client": "workspace:^",
+ "@webstudio-is/trpc-interface": "workspace:^",
+ "immer": "^9.0.12",
+ "nanoid": "^3.2.0",
"zod": "^3.19.1"
},
"devDependencies": {
diff --git a/packages/project-build/server.d.ts b/packages/project-build/server.d.ts
new file mode 100644
index 0000000000..ad6dde9958
--- /dev/null
+++ b/packages/project-build/server.d.ts
@@ -0,0 +1 @@
+export * from "./src/index.server";
diff --git a/packages/project-build/server.js b/packages/project-build/server.js
new file mode 100644
index 0000000000..a6fbe7c12c
--- /dev/null
+++ b/packages/project-build/server.js
@@ -0,0 +1 @@
+export * from "./lib/index.server";
diff --git a/packages/project/src/db/props.ts b/packages/project-build/src/db/props.ts
similarity index 91%
rename from packages/project/src/db/props.ts
rename to packages/project-build/src/db/props.ts
index d6503304aa..e52504d199 100644
--- a/packages/project/src/db/props.ts
+++ b/packages/project-build/src/db/props.ts
@@ -1,10 +1,11 @@
-import { type Tree, Props, PropsList } from "@webstudio-is/project-build";
import { applyPatches, type Patch } from "immer";
import { type Project, prisma } from "@webstudio-is/prisma-client";
import {
authorizeProject,
type AppContext,
} from "@webstudio-is/trpc-interface/server";
+import type { Tree } from "../types";
+import { Props, PropsList } from "../schema/props";
export const parseProps = (propsString: string): Props => {
const propsList = PropsList.parse(JSON.parse(propsString));
@@ -16,7 +17,7 @@ export const serializeProps = (props: Props) => {
return JSON.stringify(propsList);
};
-export const patch = async (
+export const patchProps = async (
{ treeId, projectId }: { treeId: Tree["id"]; projectId: Project["id"] },
patches: Array<Patch>,
context: AppContext
diff --git a/packages/project/src/db/style-source-selections.ts b/packages/project-build/src/db/style-source-selections.ts
similarity index 93%
rename from packages/project/src/db/style-source-selections.ts
rename to packages/project-build/src/db/style-source-selections.ts
index c7e1ae53b4..52ef532c2f 100644
--- a/packages/project/src/db/style-source-selections.ts
+++ b/packages/project-build/src/db/style-source-selections.ts
@@ -4,11 +4,11 @@ import {
authorizeProject,
type AppContext,
} from "@webstudio-is/trpc-interface/server";
+import type { Tree } from "../types";
import {
StyleSourceSelectionsList,
StyleSourceSelections,
- type Tree,
-} from "@webstudio-is/project-build";
+} from "../schema/style-source-selections";
export const parseStyleSourceSelections = (
styleSourceSelectionsString: string
@@ -30,7 +30,7 @@ export const serializeStyleSourceSelections = (
return JSON.stringify(styleSourceSelectionsList);
};
-export const patch = async (
+export const patchStyleSourceSelections = async (
{ treeId, projectId }: { treeId: Tree["id"]; projectId: Project["id"] },
patches: Array<Patch>,
context: AppContext
diff --git a/packages/project/src/db/tree.ts b/packages/project-build/src/db/tree.ts
similarity index 87%
rename from packages/project/src/db/tree.ts
rename to packages/project-build/src/db/tree.ts
index 8ddd0321aa..36cac0d4f1 100644
--- a/packages/project/src/db/tree.ts
+++ b/packages/project-build/src/db/tree.ts
@@ -1,19 +1,15 @@
import { applyPatches, type Patch } from "immer";
+import { nanoid } from "nanoid";
import { v4 as uuid } from "uuid";
import type { z } from "zod";
-import {
- type Tree,
- type InstancesItem,
- Instance,
- Instances,
-} from "@webstudio-is/project-build";
import { type Project, type Prisma, prisma } from "@webstudio-is/prisma-client";
-import { utils } from "../index";
-import { parseProps, serializeProps } from "./props";
import {
authorizeProject,
type AppContext,
} from "@webstudio-is/trpc-interface/server";
+import type { Tree } from "../types";
+import { type InstancesItem, Instance, Instances } from "../schema/instances";
+import { parseProps, serializeProps } from "./props";
import {
parseStyleSourceSelections,
serializeStyleSourceSelections,
@@ -39,14 +35,19 @@ const normalizeTree = (instance: Instance, instances: InstancesItem[]) => {
}
};
-export const createTree = ({
+export const createNewTreeData = ({
projectId,
buildId,
}: {
projectId: string;
buildId: string;
}): TreeData => {
- const root = utils.tree.createInstance({ component: "Body" });
+ const root: Instance = {
+ type: "instance",
+ id: nanoid(),
+ component: "Body",
+ children: [],
+ };
const instances: Instances = [];
normalizeTree(root, instances);
@@ -59,7 +60,7 @@ export const createTree = ({
};
};
-export const create = async (
+export const createTree = async (
treeData: TreeData,
client: Prisma.TransactionClient = prisma
) => {
@@ -85,7 +86,7 @@ export const create = async (
});
};
-export const deleteById = async ({
+export const deleteTreeById = async ({
projectId,
treeId,
}: {
@@ -123,7 +124,7 @@ const denormalizeTree = (instances: z.infer<typeof Instances>) => {
return convertTree(instances[0]);
};
-export const loadById = async (
+export const loadTreeById = async (
{ projectId, treeId }: { projectId: Project["id"]; treeId: Tree["id"] },
_context: AppContext,
client: Prisma.TransactionClient = prisma
@@ -154,21 +155,21 @@ export const loadById = async (
};
};
-export const clone = async (
+export const cloneTree = async (
from: { projectId: Project["id"]; treeId: Tree["id"] },
to: { projectId: Project["id"]; buildId: Project["id"] },
context: AppContext,
client: Prisma.TransactionClient = prisma
) => {
- const tree = await loadById(from, context, client);
+ const tree = await loadTreeById(from, context, client);
if (tree === null) {
throw new Error(`Tree ${from.projectId}/${from.treeId} not found`);
}
- return await create({ ...tree, ...to }, client);
+ return await createTree({ ...tree, ...to }, client);
};
-export const patch = async (
+export const patchTree = async (
{ treeId, projectId }: { treeId: Tree["id"]; projectId: Project["id"] },
patches: Array<Patch>,
context: AppContext
@@ -182,7 +183,7 @@ export const patch = async (
throw new Error("You don't have edit access to this project");
}
- const tree = await loadById({ projectId, treeId }, context);
+ const tree = await loadTreeById({ projectId, treeId }, context);
if (tree === null) {
throw new Error(`Tree ${treeId} not found`);
}
diff --git a/packages/project-build/src/index.server.ts b/packages/project-build/src/index.server.ts
new file mode 100644
index 0000000000..c88a4ab250
--- /dev/null
+++ b/packages/project-build/src/index.server.ts
@@ -0,0 +1,3 @@
+export * from "./db/tree";
+export * from "./db/props";
+export * from "./db/style-source-selections";
diff --git a/packages/project/src/db/build.ts b/packages/project/src/db/build.ts
index 0dff665230..bb2de7a165 100644
--- a/packages/project/src/db/build.ts
+++ b/packages/project/src/db/build.ts
@@ -7,6 +7,7 @@ import {
} from "@webstudio-is/prisma-client";
import type { AppContext } from "@webstudio-is/trpc-interface";
import { type Build, type Page, Pages } from "@webstudio-is/project-build";
+import * as buildDb from "@webstudio-is/project-build/server";
import * as db from ".";
import * as pagesUtils from "../shared/pages";
import { parseBreakpoints, serializeBreakpoints } from "./breakpoints";
@@ -112,8 +113,8 @@ export const addPage = async ({
Partial<Omit<Page, "id" | "treeId" | "name" | "path">>;
}) => {
return updatePages({ projectId, buildId }, async (currentPages) => {
- const tree = await db.tree.create(
- db.tree.createTree({ projectId, buildId })
+ const tree = await buildDb.createTree(
+ buildDb.createNewTreeData({ projectId, buildId })
);
return {
@@ -190,7 +191,7 @@ export const deletePage = async ({
throw new Error(`Page with id "${pageId}" not found`);
}
- await db.tree.deleteById({ projectId, treeId: page.treeId });
+ await buildDb.deleteTreeById({ projectId, treeId: page.treeId });
return {
homePage: currentPages.homePage,
@@ -204,8 +205,8 @@ const createPages = async (
_context: AppContext,
client: Prisma.TransactionClient = prisma
) => {
- const tree = await db.tree.create(
- db.tree.createTree({ projectId, buildId }),
+ const tree = await buildDb.createTree(
+ buildDb.createNewTreeData({ projectId, buildId }),
client
);
return Pages.parse({
@@ -227,7 +228,7 @@ const clonePage = async (
context: AppContext,
client: Prisma.TransactionClient = prisma
) => {
- const tree = await db.tree.clone(
+ const tree = await buildDb.cloneTree(
{ projectId: from.projectId, treeId: from.page.treeId },
to,
context,
diff --git a/packages/project/src/db/index.ts b/packages/project/src/db/index.ts
index 8d3b64956b..9c0b38c5be 100644
--- a/packages/project/src/db/index.ts
+++ b/packages/project/src/db/index.ts
@@ -1,18 +1,6 @@
import * as project from "./project";
-import * as tree from "./tree";
import * as styles from "./styles";
import * as styleSources from "./style-sources";
-import * as styleSourceSelections from "./style-source-selections";
-import * as props from "./props";
import * as breakpoints from "./breakpoints";
import * as build from "./build";
-export {
- project,
- tree,
- breakpoints,
- styles,
- styleSources,
- styleSourceSelections,
- props,
- build,
-};
+export { project, breakpoints, styles, styleSources, build };
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 2e796127e6..0f96924be1 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -827,13 +827,21 @@ importers:
specifiers:
'@webstudio-is/asset-uploader': workspace:^
'@webstudio-is/css-data': workspace:^
+ '@webstudio-is/prisma-client': workspace:^
'@webstudio-is/scripts': workspace:^
+ '@webstudio-is/trpc-interface': workspace:^
'@webstudio-is/tsconfig': workspace:^
+ immer: ^9.0.12
+ nanoid: ^3.2.0
typescript: 4.9.5
zod: ^3.19.1
dependencies:
'@webstudio-is/asset-uploader': link:../asset-uploader
'@webstudio-is/css-data': link:../css-data
+ '@webstudio-is/prisma-client': link:../prisma-client
+ '@webstudio-is/trpc-interface': link:../trpc-interface
+ immer: 9.0.15
+ nanoid: 3.3.4
zod: 3.19.1
devDependencies:
'@webstudio-is/scripts': link:../scripts
@@ -13174,10 +13182,6 @@ packages:
resolution: {integrity: sha512-ED3jP8saaweFTjeGX8HQPjeC1YYyZs98jGNZx6IiBvxW7JG5v492kamAQB3m2wop07CvU/RQmzcKr6bgcC5D/Q==}
dev: false
- /diff-sequences/29.3.1:
- resolution: {integrity: sha512-hlM3QR272NXCi4pq+N4Kok4kOp6EsgOM3ZSpJI7Da3UAs+Ttsi8MRmB6trM/lhyzUxGfOgnpkHtgqm5Q/CTcfQ==}
- engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0}
-
/diff-sequences/29.4.2:
resolution: {integrity: sha512-R6P0Y6PrsH3n4hUXxL3nns0rbRk6Q33js3ygJBeEpbzLzgcNuJ61+u0RXasFpTKISw99TxUzFnumSnRLsjhLaw==}
engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0}
@@ -16347,7 +16351,7 @@ packages:
engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0}
dependencies:
chalk: 4.1.2
- diff-sequences: 29.3.1
+ diff-sequences: 29.4.2
jest-get-type: 29.4.2
pretty-format: 29.4.2
@@ -16448,7 +16452,7 @@ packages:
engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0}
dependencies:
chalk: 4.1.2
- jest-diff: 29.3.1
+ jest-diff: 29.4.2
jest-get-type: 29.4.2
pretty-format: 29.4.2