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: switch authorization-token to postgrest (#3878)

Bogdan Chadkin7c5fba51446a
diff --git a/packages/authorization-token/package.json b/packages/authorization-token/package.json
index dc94d48f9a..524a0bded2 100644
--- a/packages/authorization-token/package.json
+++ b/packages/authorization-token/package.json
@@ -9,7 +9,7 @@
"typecheck": "tsc"
},
"dependencies": {
- "@webstudio-is/prisma-client": "workspace:*",
+ "@webstudio-is/postrest": "workspace:*",
"@webstudio-is/trpc-interface": "workspace:*",
"zod": "^3.22.4"
},
diff --git a/packages/authorization-token/src/db/authorization-token.ts b/packages/authorization-token/src/db/authorization-token.ts
index 81be8320f9..927bb27dc8 100644
--- a/packages/authorization-token/src/db/authorization-token.ts
+++ b/packages/authorization-token/src/db/authorization-token.ts
@@ -1,16 +1,15 @@
+import type { Database } from "@webstudio-is/postrest/index.server";
import {
- prisma,
- type AuthorizationToken,
- type Project,
-} from "@webstudio-is/prisma-client";
-import {
- authorizeProject,
type AppContext,
+ authorizeProject,
AuthorizationError,
} from "@webstudio-is/trpc-interface/index.server";
+type AuthorizationToken =
+ Database["public"]["Tables"]["AuthorizationToken"]["Row"];
+
export const findMany = async (
- props: { projectId: Project["id"] },
+ props: { projectId: string },
context: AppContext
) => {
// Only owner of the project can list authorization tokens
@@ -25,20 +24,18 @@ export const findMany = async (
);
}
- const dbTokens = await prisma.authorizationToken.findMany({
- where: {
- projectId: props.projectId,
- },
+ const dbTokens = await context.postgrest.client
+ .from("AuthorizationToken")
+ .select()
+ .eq("projectId", props.projectId)
// Stable order
- orderBy: [
- {
- createdAt: "asc",
- },
- { token: "asc" },
- ],
- });
-
- return dbTokens;
+ .order("createdAt", { ascending: true })
+ .order("token", { ascending: true });
+ if (dbTokens.error) {
+ throw dbTokens.error;
+ }
+
+ return dbTokens.data;
};
export const tokenDefaultPermissions = {
@@ -49,28 +46,28 @@ export const tokenDefaultPermissions = {
export type TokenPermissions = typeof tokenDefaultPermissions;
export const getTokenPermissions = async (
- props: { projectId: Project["id"]; token: AuthorizationToken["token"] },
- _context: AppContext
+ props: { projectId: string; token: AuthorizationToken["token"] },
+ context: AppContext
): Promise<TokenPermissions> => {
- const dbToken = await prisma.authorizationToken.findUnique({
- where: {
- token_projectId: {
- projectId: props.projectId,
- token: props.token,
- },
- },
- });
-
- if (dbToken === null) {
+ const dbToken = await context.postgrest.client
+ .from("AuthorizationToken")
+ .select()
+ .eq("projectId", props.projectId)
+ .eq("token", props.token)
+ .maybeSingle();
+ if (dbToken.error) {
+ throw dbToken.error;
+ }
+ if (dbToken.data === null) {
throw new AuthorizationError("Authorization token not found");
}
- switch (dbToken.relation) {
+ switch (dbToken.data.relation) {
// canClone, canCopy permissions can be applied for viewers only
case "viewers":
return {
- canClone: dbToken.canClone,
- canCopy: dbToken.canCopy,
+ canClone: dbToken.data.canClone,
+ canCopy: dbToken.data.canCopy,
};
default:
return tokenDefaultPermissions;
@@ -79,7 +76,7 @@ export const getTokenPermissions = async (
export const create = async (
props: {
- projectId: Project["id"];
+ projectId: string;
relation: AuthorizationToken["relation"];
name: string;
},
@@ -99,20 +96,24 @@ export const create = async (
);
}
- const dbToken = await prisma.authorizationToken.create({
- data: {
+ const dbToken = await context.postgrest.client
+ .from("AuthorizationToken")
+ .insert({
projectId: props.projectId,
relation: props.relation,
token: tokenId,
name: props.name,
- },
- });
+ })
+ .select();
+ if (dbToken.error) {
+ throw dbToken.error;
+ }
- return dbToken;
+ return dbToken.data;
};
export const update = async (
- projectId: Project["id"],
+ projectId: string,
props: Pick<AuthorizationToken, "token" | "relation"> &
Partial<AuthorizationToken>,
context: AppContext
@@ -129,40 +130,41 @@ export const update = async (
);
}
- const previousToken = await prisma.authorizationToken.findUnique({
- where: {
- token_projectId: {
- projectId,
- token: props.token,
- },
- },
- });
-
- if (previousToken === null) {
+ const previousToken = await context.postgrest.client
+ .from("AuthorizationToken")
+ .select()
+ .eq("projectId", projectId)
+ .eq("token", props.token)
+ .maybeSingle();
+ if (previousToken.error) {
+ throw previousToken.error;
+ }
+ if (previousToken.data === null) {
throw new AuthorizationError("Authorization token not found");
}
- const dbToken = await prisma.authorizationToken.update({
- where: {
- token_projectId: {
- projectId: projectId,
- token: props.token,
- },
- },
- data: {
+ const dbToken = await context.postgrest.client
+ .from("AuthorizationToken")
+ .update({
name: props.name,
relation: props.relation,
canClone: props.canClone,
canCopy: props.canCopy,
- },
- });
+ })
+ .eq("projectId", projectId)
+ .eq("token", props.token)
+ .select()
+ .single();
+ if (dbToken.error) {
+ throw dbToken.error;
+ }
- return dbToken;
+ return dbToken.data;
};
export const remove = async (
props: {
- projectId: Project["id"];
+ projectId: string;
token: AuthorizationToken["token"];
},
context: AppContext
@@ -179,14 +181,16 @@ export const remove = async (
);
}
- const dbToken = await prisma.authorizationToken.delete({
- where: {
- token_projectId: {
- projectId: props.projectId,
- token: props.token,
- },
- },
- });
+ const dbToken = await context.postgrest.client
+ .from("AuthorizationToken")
+ .delete()
+ .eq("projectId", props.projectId)
+ .eq("token", props.token)
+ .select()
+ .single();
+ if (dbToken.error) {
+ throw dbToken.error;
+ }
- return dbToken;
+ return dbToken.data;
};
diff --git a/packages/postgrest/package.json b/packages/postgrest/package.json
index 0b7bee5518..c56cb5bb77 100644
--- a/packages/postgrest/package.json
+++ b/packages/postgrest/package.json
@@ -19,7 +19,7 @@
"generate-types": "pnpx supabase gen types --lang=typescript --db-url postgresql://postgres:pass@localhost/webstudio > ./src/__generated__/db-types.ts"
},
"dependencies": {
- "@supabase/postgrest-js": "^1.15.8"
+ "@supabase/postgrest-js": "^1.16.0"
},
"devDependencies": {
"@webstudio-is/tsconfig": "workspace:*",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 9088456633..f730e0c4bf 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -952,9 +952,9 @@ importers:
packages/authorization-token:
dependencies:
- '@webstudio-is/prisma-client':
+ '@webstudio-is/postrest':
specifier: workspace:*
- version: link:../prisma-client
+ version: link:../postgrest
'@webstudio-is/trpc-interface':
specifier: workspace:*
version: link:../trpc-interface
@@ -1606,8 +1606,8 @@ importers:
packages/postgrest:
dependencies:
'@supabase/postgrest-js':
- specifier: ^1.15.8
- version: 1.15.8
+ specifier: ^1.16.0
+ version: 1.16.0
devDependencies:
'@webstudio-is/tsconfig':
specifier: workspace:*
@@ -5137,8 +5137,8 @@ packages:
resolution: {integrity: sha512-1ibVeYUacxWYi9i0cf5efil6adJ9WRyZBLivgjs+AUpewx1F3xPi7gLgaASI2SmIQxPoCEjAsLAzKPgMJVgOUQ==}
engines: {node: 4.x || >=6.0.0}
- '@supabase/[email protected]':
- resolution: {integrity: sha512-YunjXpoQjQ0a0/7vGAvGZA2dlMABXFdVI/8TuVKtlePxyT71sl6ERl6ay1fmIeZcqxiuFQuZw/LXUuStUG9bbg==}
+ '@supabase/[email protected]':
+ resolution: {integrity: sha512-HQkldynOiKQMwD7ZhRsXyt777hE26YQ2UANx7SKux+io4VzMwC5DxwxKZwp7ceI4qAJ28DsuRx2HqCGJfjZTiw==}
resolution: {integrity: sha512-0e4B41q+GnS8TI+SWHQw78ziW6V07Li2TMLS8FwbvKOTIJL0ua1Sat1AI0AFAjHlAePz9b+1VtyJzv1516edJw==}
@@ -13340,7 +13340,7 @@ snapshots:
dependencies:
whatwg-url: 5.0.0
- '@supabase/[email protected]':
+ '@supabase/[email protected]':
dependencies:
'@supabase/node-fetch': 2.6.15