I am trying to use the generated @prisma/client
together with Supabase Edge functions. When running npx prisma generate
the default location would be in my node_modules
folder, which is a problem since I can't access it when using edge functions. I therefore added the output
property to my prisma.schema
file and the client gets generated in the correct location. My schema.prisma
looks like this:
generator client {
provider = "prisma-client-js"
output = "./../supabase/functions/_shared/prisma-client"
}
datasource db {
provider = "postgresql"
url = "..."
directUrl = "..."
}
model Users {}
I tried importing the client in multiple ways into my edge functions:
// Attempt #1 (according to: https://deno.land/manual@v1.14.2/npm_nodejs/std_node#loading-commonjs-modules):
import { createRequire } from 'https://deno.land/std@0.155.0/node/module.ts'
const require = createRequire(import.meta.url)
const cjsModule = require('../_shared/prisma-client')
/* Error: worker thread panicked TypeError: Cannot read properties of undefined (reading 'timeOrigin')
at https://deno.land/std@0.155.0/node/perf_hooks.ts */
/* Note: I also tried different versions of the std library */
// Attempt #2:
import { PrismaClient } from '../_shared/prisma-client'
/* Error: Unable to load a local module: "file:///C:/Users/.../supabase/functions/_shared/prisma-client".
Please check the file path. */
// Attempt #3:
import { serve } from 'server'
import { PrismaClient } from '../_shared/prisma-client/index.d.ts'
serve((_req: Request) => {
const prisma = new PrismaClient()
})
/* Error: worker thread panicked Uncaught SyntaxError: Missing initializer in const declaration
at file:///home/deno/functions/_shared/prisma-client/index.d.ts:53:11 */
I furthermore tried to convert the module from CommonJS into an ESM module using the cjs-to-es6
npm package. But this also didn't work.
So my question is: Why my attempts failed, and (perhaps more importantly) how do I get it to work?
Taken from https://deno.land/manual@v1.28.3/node/how_to_with_npm/prisma
import { Prisma, PrismaClient } from "../generated/client/deno/edge.ts";
import { config } from "https://deno.land/std@0.163.0/dotenv/mod.ts";
const envVars = await config();
const prisma = new PrismaClient({
datasources: {
db: {
url: envVars.DATABASE_URL,
},
},
});
Note that for Prisma to work in Edge Functions you will need to proxy through something like Prisma Data Platform: https://deno.land/manual@v1.28.3/node/how_to_with_npm/prisma#setup-prisma-data-platform
So you might want to consider using supabase-js
instead as it works in Edge Functions out of the box: https://github.com/supabase/supabase/blob/master/examples/edge-functions/supabase/functions/restful-tasks/index.ts