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.

refactor: migrate asset uploader to postgrest (#3805)

Bogdan Chadkin75266822f455
diff --git a/apps/builder/app/routes/rest.assets.$name.tsx b/apps/builder/app/routes/rest.assets.$name.tsx
index 20e444f245..f8da9cd00e 100644
--- a/apps/builder/app/routes/rest.assets.$name.tsx
+++ b/apps/builder/app/routes/rest.assets.$name.tsx
@@ -1,9 +1,10 @@
+import { z } from "zod";
import type { ActionFunctionArgs } from "@remix-run/server-runtime";
import type { Asset } from "@webstudio-is/sdk";
import { uploadFile } from "@webstudio-is/asset-uploader/index.server";
import type { ActionData } from "~/builder/shared/assets";
import { createAssetClient } from "~/shared/asset-client";
-import { z } from "zod";
+import { createContext } from "~/shared/context.server";
const UrlBody = z.object({
url: z.string(),
@@ -50,7 +51,13 @@ export const action = async (
body = imageRequest.body;
}
- const asset = await uploadFile(params.name, body, createAssetClient());
+ const context = await createContext(request);
+ const asset = await uploadFile(
+ params.name,
+ body,
+ createAssetClient(),
+ context
+ );
return {
uploadedAssets: [asset],
};
diff --git a/packages/asset-uploader/package.json b/packages/asset-uploader/package.json
index f07b41311c..fd765231af 100644
--- a/packages/asset-uploader/package.json
+++ b/packages/asset-uploader/package.json
@@ -13,7 +13,6 @@
"@aws-crypto/sha256-js": "^5.2.0",
"@smithy/signature-v4": "^2.3.0",
"@webstudio-is/fonts": "workspace:*",
- "@webstudio-is/prisma-client": "workspace:*",
"@webstudio-is/sdk": "workspace:*",
"@webstudio-is/trpc-interface": "workspace:*",
"fontkit": "^2.0.2",
diff --git a/packages/asset-uploader/src/db/load.ts b/packages/asset-uploader/src/db/load.ts
index 70ebe9913f..6fa6893392 100644
--- a/packages/asset-uploader/src/db/load.ts
+++ b/packages/asset-uploader/src/db/load.ts
@@ -1,4 +1,3 @@
-import { prisma, type Project } from "@webstudio-is/prisma-client";
import {
authorizeProject,
type AppContext,
@@ -8,7 +7,7 @@ import type { Asset } from "@webstudio-is/sdk";
import { formatAsset } from "../utils/format-asset";
export const loadAssetsByProject = async (
- projectId: Project["id"],
+ projectId: string,
context: AppContext,
{ skipPermissionsCheck = false }: { skipPermissionsCheck?: boolean } = {}
): Promise<Asset[]> => {
@@ -25,26 +24,26 @@ export const loadAssetsByProject = async (
);
}
- const assets = await prisma.asset.findMany({
- select: {
- file: true,
- id: true,
- projectId: true,
- },
- where: {
- projectId,
- file: { status: "UPLOADED" },
- },
- orderBy: {
- file: { createdAt: "desc" },
- },
- });
+ const assets = await context.postgrest.client
+ .from("Asset")
+ // use inner to filter out assets without file
+ // when file is not uploaded
+ .select(
+ `
+ assetId:id,
+ projectId,
+ file:File!inner (*)
+ `
+ )
+ .eq("projectId", projectId)
+ .eq("file.status", "UPLOADED");
- return assets.map((asset) =>
- formatAsset({
- assetId: asset.id,
- projectId: asset.projectId,
- file: asset.file,
- })
- );
+ const result: Asset[] = [];
+ for (const { assetId, projectId, file } of assets.data ?? []) {
+ if (file) {
+ result.push(formatAsset({ assetId, projectId, file }));
+ }
+ }
+
+ return result;
};
diff --git a/packages/asset-uploader/src/delete.ts b/packages/asset-uploader/src/delete.ts
index 1fea509a37..375aa46160 100644
--- a/packages/asset-uploader/src/delete.ts
+++ b/packages/asset-uploader/src/delete.ts
@@ -1,4 +1,3 @@
-import { prisma, type Project } from "@webstudio-is/prisma-client";
import {
authorizeProject,
type AppContext,
@@ -9,7 +8,7 @@ import type { Asset } from "@webstudio-is/sdk";
export const deleteAssets = async (
props: {
ids: Array<Asset["id"]>;
- projectId: Project["id"];
+ projectId: string;
},
context: AppContext
) => {
@@ -24,50 +23,48 @@ export const deleteAssets = async (
);
}
- const assets = await prisma.asset.findMany({
- select: {
- file: true,
- id: true,
- projectId: true,
- name: true,
- },
- where: { id: { in: props.ids }, projectId: props.projectId },
- });
+ const assets = await context.postgrest.client
+ .from("Asset")
+ .select(
+ `
+ id,
+ projectId,
+ name,
+ file:File!inner (*)
+ `
+ )
+ .in("id", props.ids)
+ .eq("projectId", props.projectId);
- if (assets.length === 0) {
+ if ((assets.data ?? []).length === 0) {
throw new Error("Assets not found");
}
- await prisma.project.updateMany({
- where: {
- id: props.projectId,
- previewImageAssetId: {
- in: props.ids,
- },
- },
- data: {
- previewImageAssetId: null,
- },
- });
+ await context.postgrest.client
+ .from("Project")
+ .update({ previewImageAssetId: null })
+ .eq("id", props.projectId)
+ .in("previewImageAssetId", props.ids);
- await prisma.asset.deleteMany({
- where: { id: { in: props.ids }, projectId: props.projectId },
- });
+ await context.postgrest.client
+ .from("Asset")
+ .delete()
+ .in("id", props.ids)
+ .eq("projectId", props.projectId);
// find unused files
- const unusedFileNames = new Set(assets.map((asset) => asset.name));
- const assetsByStillUsedFileName = await prisma.asset.findMany({
- where: { name: { in: Array.from(unusedFileNames) } },
- });
- for (const asset of assetsByStillUsedFileName) {
+ const unusedFileNames = new Set(assets.data?.map((asset) => asset.name));
+ const assetsByStillUsedFileName = await context.postgrest.client
+ .from("Asset")
+ .select("name")
+ .in("name", Array.from(unusedFileNames));
+ for (const asset of assetsByStillUsedFileName.data ?? []) {
unusedFileNames.delete(asset.name);
}
// delete unused files
- await prisma.file.updateMany({
- where: { name: { in: Array.from(unusedFileNames) } },
- data: {
- isDeleted: true,
- },
- });
+ await context.postgrest.client
+ .from("File")
+ .update({ isDeleted: true })
+ .in("name", Array.from(unusedFileNames));
};
diff --git a/packages/asset-uploader/src/patch.ts b/packages/asset-uploader/src/patch.ts
index d94e0bdfca..79d4fbe64a 100644
--- a/packages/asset-uploader/src/patch.ts
+++ b/packages/asset-uploader/src/patch.ts
@@ -1,5 +1,4 @@
import { type Patch, applyPatches } from "immer";
-import { type Project, prisma } from "@webstudio-is/prisma-client";
import {
type AppContext,
authorizeProject,
@@ -13,7 +12,7 @@ import { loadAssetsByProject } from "./db/load";
* update patches are ignored
*/
export const patchAssets = async (
- { projectId }: { projectId: Project["id"] },
+ { projectId }: { projectId: string },
patches: Array<Patch>,
context: AppContext
) => {
@@ -51,29 +50,31 @@ export const patchAssets = async (
}
}
if (addedAssets.length !== 0) {
- const files = await prisma.file.findMany({
- where: { name: { in: addedAssets.map((asset) => asset.name) } },
- });
+ const files = await context.postgrest.client
+ .from("File")
+ .select()
+ .in(
+ "name",
+ addedAssets.map((asset) => asset.name)
+ );
- const fileNames = new Set(files.map((file) => file.name));
+ const fileNames = new Set(files.data?.map((file) => file.name));
// restore file when undo is triggered
- await prisma.file.updateMany({
- where: { name: { in: Array.from(fileNames) } },
- data: {
- isDeleted: false,
- },
- });
+ await context.postgrest.client
+ .from("File")
+ .update({ isDeleted: false })
+ .in("name", Array.from(fileNames));
- await prisma.asset.createMany({
- data: addedAssets
+ await context.postgrest.client.from("Asset").insert(
+ addedAssets
// making sure corresponding file exist before creating an asset that references it
.filter((asset) => fileNames.has(asset.name))
.map((asset) => ({
id: asset.id,
projectId,
name: asset.name,
- })),
- });
+ }))
+ );
}
};
diff --git a/packages/asset-uploader/src/upload.ts b/packages/asset-uploader/src/upload.ts
index 1502d3f0a5..f726ae7e60 100644
--- a/packages/asset-uploader/src/upload.ts
+++ b/packages/asset-uploader/src/upload.ts
@@ -1,4 +1,3 @@
-import { prisma } from "@webstudio-is/prisma-client";
import {
type AppContext,
authorizeProject,
@@ -39,23 +38,25 @@ export const createUploadName = async (
* than UPLOADING_STALE_TIMEOUT milliseconds ago
**/
- const count = await prisma.file.count({
- where: {
- OR: [
- {
- status: "UPLOADED",
- isDeleted: false,
- uploaderProjectId: projectId,
- },
- {
- status: "UPLOADING",
- isDeleted: false,
- uploaderProjectId: projectId,
- createdAt: { gt: new Date(Date.now() - UPLOADING_STALE_TIMEOUT) },
- },
- ],
- },
- });
+ const uploadedCount = await context.postgrest.client
+ .from("File")
+ .select("*", { count: "exact", head: true })
+ .eq("isDeleted", false)
+ .eq("uploaderProjectId", projectId)
+ .eq("status", "UPLOADED");
+
+ const uploadingCount = await context.postgrest.client
+ .from("File")
+ .select("*", { count: "exact", head: true })
+ .eq("isDeleted", false)
+ .eq("uploaderProjectId", projectId)
+ .eq("status", "UPLOADING")
+ .gt(
+ "createdAt",
+ new Date(Date.now() - UPLOADING_STALE_TIMEOUT).toISOString()
+ );
+
+ const count = (uploadedCount.count ?? 0) + (uploadingCount.count ?? 0);
if (count >= maxAssetsPerProject) {
/**
@@ -79,15 +80,13 @@ export const createUploadName = async (
**/
const name = getUniqueFilename(sanitizeS3Key(filename));
- await prisma.file.create({
- data: {
- name,
- status: "UPLOADING",
- // store content type in related field
- format: type,
- size: 0,
- uploaderProjectId: projectId,
- },
+ await context.postgrest.client.from("File").insert({
+ name,
+ status: "UPLOADING",
+ // store content type in related field
+ format: type,
+ size: 0,
+ uploaderProjectId: projectId,
});
return name;
};
@@ -95,49 +94,52 @@ export const createUploadName = async (
export const uploadFile = async (
name: string,
data: ReadableStream<Uint8Array>,
- client: AssetClient
+ client: AssetClient,
+ context: AppContext
) => {
- const file = await prisma.file.findFirst({
- where: {
- name,
- status: "UPLOADING",
- createdAt: { gt: new Date(Date.now() - UPLOADING_STALE_TIMEOUT) },
- },
- });
- if (file === null) {
+ let file = await context.postgrest.client
+ .from("File")
+ .select("*")
+ .eq("name", name)
+ .eq("status", "UPLOADING")
+ .gt(
+ "createdAt",
+ new Date(Date.now() - UPLOADING_STALE_TIMEOUT).toISOString()
+ )
+ .single();
+ if (file.data === null) {
throw Error("File already uploaded or url is expired");
}
try {
const assetData = await client.uploadFile(
name,
- file.format,
+ file.data.format,
// global web streams types do not define ReadableStream as async iterable
data as unknown as AsyncIterable<Uint8Array>
);
const { meta, format, size } = assetData;
- const dbFile = await prisma.file.update({
- where: {
- name,
- },
- data: {
+ file = await context.postgrest.client
+ .from("File")
+ .update({
size,
format,
meta: JSON.stringify(meta),
status: "UPLOADED",
- },
- });
+ })
+ .eq("name", name)
+ .select()
+ .single();
+ if (file.data === null) {
+ throw Error("File not found");
+ }
return formatAsset({
assetId: "",
- projectId: dbFile.uploaderProjectId as string,
- file: dbFile,
+ projectId: file.data.uploaderProjectId as string,
+ file: file.data,
});
} catch (error) {
- await prisma.file.delete({
- where: {
- name,
- },
- });
+ await context.postgrest.client.from("File").delete().eq("name", name);
throw error;
}
diff --git a/packages/asset-uploader/src/utils/format-asset.ts b/packages/asset-uploader/src/utils/format-asset.ts
index 9dcdac976f..39219fb9f4 100644
--- a/packages/asset-uploader/src/utils/format-asset.ts
+++ b/packages/asset-uploader/src/utils/format-asset.ts
@@ -1,4 +1,3 @@
-import type { File as DbFile } from "@webstudio-is/prisma-client";
import { type FontFormat, FontMeta, FONT_FORMATS } from "@webstudio-is/fonts";
import { type Asset, ImageMeta } from "@webstudio-is/sdk";
@@ -9,7 +8,14 @@ export const formatAsset = ({
}: {
assetId: string;
projectId: string;
- file: DbFile;
+ file: {
+ name: string;
+ format: string;
+ description: string | null;
+ size: number;
+ createdAt: string;
+ meta: string;
+ };
}): Asset => {
const isFont = FONT_FORMATS.has(file.format as FontFormat);
@@ -21,7 +27,7 @@ export const formatAsset = ({
projectId,
size: file.size,
type: "font",
- createdAt: file.createdAt.toISOString(),
+ createdAt: file.createdAt,
format: file.format as FontFormat,
meta: FontMeta.parse(JSON.parse(file.meta)),
};
@@ -35,7 +41,7 @@ export const formatAsset = ({
size: file.size,
type: "image",
format: file.format,
- createdAt: file.createdAt.toISOString(),
+ createdAt: file.createdAt,
meta: ImageMeta.parse(JSON.parse(file.meta)),
};
};
diff --git a/packages/postgrest/jest.config.js b/packages/postgrest/jest.config.js
deleted file mode 100644
index e7fc137533..0000000000
--- a/packages/postgrest/jest.config.js
+++ /dev/null
@@ -1 +0,0 @@
-export { default } from "@webstudio-is/jest-config";
diff --git a/packages/postgrest/package.json b/packages/postgrest/package.json
index ee12fc8d6b..0b7bee5518 100644
--- a/packages/postgrest/package.json
+++ b/packages/postgrest/package.json
@@ -23,7 +23,6 @@
},
"devDependencies": {
"@webstudio-is/tsconfig": "workspace:*",
- "type-fest": "^4.3.1",
"typescript": "5.5.2"
}
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 2ad9724afe..61ff31329b 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -906,9 +906,6 @@ importers:
'@webstudio-is/fonts':
specifier: workspace:*
version: link:../fonts
- '@webstudio-is/prisma-client':
- specifier: workspace:*
- version: link:../prisma-client
'@webstudio-is/sdk':
specifier: workspace:*
version: link:../sdk
@@ -1645,9 +1642,6 @@ importers:
'@webstudio-is/tsconfig':
specifier: workspace:*
version: link:../tsconfig
- type-fest:
- specifier: ^4.3.1
- version: 4.3.1
typescript:
specifier: 5.5.2
version: 5.5.2