From f5b83bfdfcf4bafa0575c5d4ba58734977f5e55e Mon Sep 17 00:00:00 2001 From: Konsti Wohlwend Date: Tue, 14 May 2024 14:21:29 +0200 Subject: [PATCH] Delete packages/stack-server/src/route-handlers/prisma-handler.tsx --- .../src/route-handlers/prisma-handler.tsx | 162 ------------------ 1 file changed, 162 deletions(-) delete mode 100644 packages/stack-server/src/route-handlers/prisma-handler.tsx diff --git a/packages/stack-server/src/route-handlers/prisma-handler.tsx b/packages/stack-server/src/route-handlers/prisma-handler.tsx deleted file mode 100644 index 5a9c2b8b1..000000000 --- a/packages/stack-server/src/route-handlers/prisma-handler.tsx +++ /dev/null @@ -1,162 +0,0 @@ -import { CrudSchema, CrudTypeOf } from "@stackframe/stack-shared/dist/crud"; -import { CrudHandlers, createCrudHandlers } from "./crud-handler"; -import { SmartRequestAuth } from "./smart-request"; -import { Prisma } from "@prisma/client"; -import { GetResult } from "@prisma/client/runtime/library"; -import { StatusError, throwErr } from "@stackframe/stack-shared/dist/utils/errors"; -import { prismaClient } from "@/prisma-client"; - -type AllPrismaModelNames = Prisma.TypeMap["meta"]["modelProps"]; -type WhereUnique = Prisma.TypeMap["model"][Capitalize]["operations"]["findUniqueOrThrow"]["args"]["where"]; -type WhereMany = Prisma.TypeMap["model"][Capitalize]["operations"]["findMany"]["args"]["where"]; -type Where = WhereUnique & WhereMany; -type Include = Prisma.TypeMap["model"][Capitalize]["operations"]["findMany"]["args"]["include"]; -type BaseFields = Where & Partial>; -type PRead, I extends Include> = [T, W, I];//GetResult]["payload"], { where: W, include: I }, "findUniqueOrThrow">; -type PUpdate = Prisma.TypeMap["model"][Capitalize]["operations"]["update"]["args"]["data"]; -type PCreate = Prisma.TypeMap["model"][Capitalize]["operations"]["create"]["args"]["data"]; - -type A = PRead<"projectUser", {}, {}>; - -type Context = { - params: Record, - auth: SmartRequestAuth | null, -}; - -type AllowedCrudSchema = CrudSchema & { admin: { readSchema: {} } }; -type AllowedCrudType = CrudTypeOf & { Admin: { Read: {} } }; - -type CRead = T extends { Admin: { Read: infer R } } ? R : never; -type CCreate = T extends { Admin: { Create: infer R } } ? R : never; -type CUpdate = T extends { Admin: { Update: infer R } } ? R : never; -type CEitherWrite = CCreate | CUpdate; - -type Options< - T extends AllowedCrudType, - PrismaModelName extends AllPrismaModelNames, - ParamName extends string, - W extends Where, - I extends Include, - B extends BaseFields, -> = - & { - paramNames: ParamName[], - baseFields: (context: Context) => Promise, - where?: (context: Context) => Promise, - whereUnique?: (context: Context) => Promise>, - include: (context: Context) => Promise, - crudToPrisma?: ((crud: CEitherWrite, context: Context) => Promise | Omit, keyof B>>), - prismaToCrud?: (prisma: PRead, context: Context) => Promise>, - fieldMapping?: any, - createNotFoundError?: (context: Context) => Error, - } - & ( - | { - crudToPrisma: {}, - prismaToCrud: {}, - fieldMapping?: void, - } - | { - crudToPrisma: void, - prismaToCrud: void, - fieldMapping: {}, - } - ); - -type CrudHandlersFromCrudType> = CrudHandlers< - | ("Create" extends keyof T["Admin"] ? "Create" : never) - | ("Read" extends keyof T["Admin"] ? "Read" : never) - | ("Read" extends keyof T["Admin"] ? "List" : never) - | ("Update" extends keyof T["Admin"] ? "Update" : never) - | ("Delete" extends keyof T["Admin"] ? "Delete" : never) ->; - -export function createPrismaCrudHandlers< - S extends AllowedCrudSchema, - PrismaModelName extends AllPrismaModelNames, - ParamName extends string, - O extends Options & AllowedCrudType, PrismaModelName, ParamName, any, any, any> ->( - crudSchema: S, - prismaModelName: PrismaModelName, - options: O, -): CrudHandlersFromCrudType> { - const wrapper = (func: (data: any, context: Context, queryBase: any) => Promise): (opts: { params: Record, data?: unknown, auth: SmartRequestAuth | null }) => Promise => { - return async (req) => { - const context: Context = { - params: req.params, - auth: req.auth, - }; - const whereBase = await options.where?.(context); - const includeBase = await options.include(context); - try { - return await func(req.data, context, { where: whereBase, include: includeBase }); - } catch (e) { - if ((e as any)?.code === 'P2025') { - throw (options.createNotFoundError ?? (() => new StatusError(StatusError.NotFound)))(context); - } - throw e; - } - }; - }; - - const prismaToCrud = options.prismaToCrud ?? throwErr("missing prismaToCrud is not yet implemented"); - const crudToPrisma = options.crudToPrisma ?? throwErr("missing crudToPrisma is not yet implemented"); - - return createCrudHandlers(crudSchema, { - paramNames: [], - onRead: wrapper(async (data, context) => { - const prisma = await (prismaClient[prismaModelName].findUniqueOrThrow as any)({ - include: await options.include(context), - where: { - ...await options.baseFields(context), - ...await options.where?.(context), - ...await options.whereUnique?.(context), - }, - }); - return await prismaToCrud(prisma, context); - }), - onList: wrapper(async (data, context) => { - const prisma: any[] = await (prismaClient[prismaModelName].findMany as any)({ - include: await options.include(context), - where: { - ...await options.baseFields(context), - ...await options.where?.(context), - }, - }); - return await Promise.all(prisma.map((p) => prismaToCrud(p, context))); - }), - onCreate: wrapper(async (data, context) => { - const prisma = await (prismaClient[prismaModelName].create as any)({ - include: await options.include(context), - data: { - ...await options.baseFields(context), - ...await crudToPrisma(data, context), - }, - }); - return await prismaToCrud(prisma, context); - }), - onUpdate: wrapper(async (data, context) => { - const prisma = await (prismaClient[prismaModelName].update as any)({ - include: await options.include(context), - where: { - ...await options.baseFields(context), - ...await options.where?.(context), - ...await options.whereUnique?.(context), - }, - data: await crudToPrisma(data, context), - }); - return await prismaToCrud(prisma, context); - }), - onDelete: wrapper(async (data, context) => { - await (prismaClient[prismaModelName].delete as any)({ - include: await options.include(context), - where: { - ...await options.baseFields(context), - ...await options.where?.(context), - ...await options.whereUnique?.(context), - }, - }); - }), - }); -}