Leveraging Prisma Client alongside Supabase Edge Functions

I encounter an issue when trying to integrate the generated @prisma/client with Supabase Edge functions. Running npx prisma generate places the client in the default location within my node_modules folder, inaccessible for edge function usage. To resolve this, I modified my prisma.schema file by including the output property, ensuring the client is generated in the correct location as shown below:

generator client {
  provider        = "prisma-client-js"
  output          = "./../supabase/functions/_shared/prisma-client"
}

datasource db {
  provider  = "postgresql"
  url       = "..."
  directUrl = "..."
}

model Users {}

I made several attempts to import the client into my edge functions, but encountered errors each time:

// Attempt #1:
import { createRequire } from 'https://deno.land/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e58789bd838783828895d5bcb3bc9bb4bf93">[email protected]</a>/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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="43393535713b3530313b29">[email protected]</a>/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 */

In an effort to address these issues, I attempted to convert the module from CommonJS to ESM using the cjs-to-es6 npm package, but without success.

My query now remains: Why did my attempts fail, and more importantly, how can I make it work?

Answer №1

Extracted from https://deno.land/[email protected]/node/how_to_with_npm/prisma

import { Prisma, PrismaClient } from "../generated/client/deno/edge.ts";
import { config } from "https://deno.land/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92e1e6f6d2a2bca3a4a1bca2">[email protected]</a>/dotenv/mod.ts";

const envVars = await config();

const prisma = new PrismaClient({
  datasources: {
    db: {
      url: envVars.DATABASE_URL,
    },
  },
});

Please note that to utilize Prisma in Edge Functions, you will need to route through a service like Prisma Data Platform: https://deno.land/[email protected]/node/how_to_with_npm/prisma#setup-prisma-data-platform

If this proves difficult, considering using supabase-js as it seamlessly integrates with Edge Functions: https://github.com/supabase/supabase/blob/master/examples/edge-functions/supabase/functions/restful-tasks/index.ts

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Strategies for removing duplicate items from an array of objects

My array consists of objects with the following structure: $scope.SACCodes = [ {'code':'023', 'description':'Spread FTGs', 'group':'footings'}, {'code':'024', 'de ...

Exploring the method of displaying a JSON 2D array through the utilization of getJSON

Is there a way to read 2D JSON data? An example of a JSON file is given below: [ { "name":"Menu1", "permission":"1", "link":"http://naver.com" }, { "name":"Menu2", "permission":"2", "link":"http://daum.net", ...

Testing the unit with a customized header in the interceptor

I've been struggling to execute a unit test within an Angular 6 interceptor, but despite numerous attempts, I keep encountering the following error: Error: Expected one matching request for criteria "Match by function: ", found none. I'm rela ...

Is it possible to utilize JavaScript to access a .php file located in a separate directory?

Although I have been searching all day, I couldn't find a workout that specifically addresses my issue. I'm new to JavaScript and AJAX. Currently, I am exploring a web app called calendar.php located in the directory C:\xampp\htdocs&bs ...

Loading node modules within Vue.js

As I venture into the world of Web Apps, I've taken it upon myself to learn using Vue 2 + Firebase technology. However, I find that explanations tailored for beginners would be incredibly helpful. My current objective is to display a list of buckets ...

The function you are trying to access is not Random-js

Everything was running smoothly, but now there seems to be a glitch. Node version - 10.4 Error: var random = require("random-js")(); ^ TypeError: require(...) is not a function Code: var random = require("random-js")(); ...

Grabbing nested JSON Array data using Node.js

As a beginner in Node.js, I’m attempting to extract data from the JSON below: var data1 = { "_id":"R1::table::A1::order::167::comanda::2", "_rev":"1-ed6df32d3b4df9cc8019e38d655a86f5", "comanda":[ [ { ...

Replacing data in a Node server

I am currently working on a server that temporarily stores files in its memory before uploading them to the database. Below is the code snippet I'm using: uploadImage(file, uid, res) { var fs = require('fs'); mongoose.connect(config ...

Unlocking the potential of Object[value] in JavaScript/jQuery: A guide to extracting its value

I have a table named 'mytable' with the following structure: <tr> <td><input type="checkbox" name="check[]" value="11"></td> <td>11</td> <td>2014-11-06 18:49:26</td> < ...

Is it possible to switch from a dropdown menu to radio buttons?

I am looking to change the dropdown menu in my search section into radio buttons. Currently, when I select something from the dropdown menu, the search fields are altered. Here is the code for the dropdown menu: <select id="qs_category" name="qs_catego ...

How can I create a PDF in Node.js with a combination of pdfkit and fontawesome icons

Running a node server, I decided to enhance my dynamically generated PDF with icons using PDFkit. To achieve this, I first installed fontawesome by running npm install fontawesome and included it in the .js file responsible for creating the PDF like so: va ...

Executing nodejs command within the gocd pipeline

After setting up a new GoCD pipeline, I encountered an issue with running three separate shell script files on different stages. The problem arises when the Go agent is unable to recognize the npm command. Important Note: Although I have npm installed on ...

Delete items within the first 10 minutes of shutting it down

Is there a way to temporarily remove a newsletter element for 10 minutes after closing it on a webpage? The idea is that once the panel is closed, it should stay hidden even if the page is refreshed within that timeframe. I was considering using local stor ...

The NodeJS server encountered an issue when attempting to load the JavaScript modules

Hey everyone. I'm currently using Node.js and Express.js for the back end of my web application, but I keep running into issues with the server trying to bind references in the source code. Here's the structure of my app: src/ - static/ - ...

Ways to create space around Navbar MUI for a more balanced design

Currently, I am working on designing a navigation bar using MUI. My goal is to create a navbar with some space on both sides similar to the one seen on https://i.sstatic.net/lPXyC.png If you take a look at Stackoverflow's navbar, you will notice that ...

Learn how to easily copy the success result from an Ajax call to your clipboard

Is there a way to use an ajax method to retrieve data from a controller and display it in a JQuery Dialog Box? I want to add a button within the dialog box that allows the user to easily copy the data with a single click, instead of having to manually high ...

Node.js application with decoupled Redis client

In my node.js app, I am utilizing Redis for user caching. Once a user logs in, their information is cached and accessed on subsequent requests to determine their access level. This allows the client to receive personalized information and views based on th ...

When the child component's form is marked as dirty, the parent component can access it

I have implemented a feature in my application that notifies users about pending changes on a form before they navigate away. Everything works as expected, but I have a child component with its own form that needs to be accessed by the guard to check if i ...

"Implementing a feature in React JS react-table to dynamically add a new column when a

I'm struggling to add a new column to react-table when a button is clicked. Even after re-rendering the table with a flag, I can't seem to add the new column. Can anyone suggest where I might be going wrong? Here's the link to the executable ...

What error am I making in the Date calculator for the select box using Javascript/jQuery?

$(.dateselboxes) .change( function(){ var y; y=$("#year").val(); var m; m=$("#month").val(); var d; // check for leap year var isLeapYear; if(y%4==0) { if(y%100==0) { if(y%400==0) {isLeapYear=true;} else {isLeapYear=false;} } ...