0

I've a lambda configured through cloudformation sam

  OrganizationPOSTFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: OrganizationPOST
      CodeUri: ../src/
      Handler: organization-post.lambdaHandler
      Events:
        Organization:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGateway
            Path: /organization
            Method: post
    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        Minify: true
        Target: "es2020"
        Sourcemap: true
        EntryPoints:
          - organization-post.ts
       

Right now all files in my lambda are merged into a single file, so inside my lambda there is only the .ts file (Is there any way to avoid this?) inside my lambda there is no prisma folder.

and inside my organization-post.ts I'm trying to use the Prisma Client

//inside my lambdaHandler
  const prisma = new PrismaClient()

//lots of lines
const organization = await prisma.organization.create({
            data : organizationRequest
        })

I have done the migration and the PrismaClient imported from @prisma/client works fine...I get autocomplete and everything seems to be ok

but when I run the project I get this error

'errorMessage': "ENOENT: no such file or directory, open '/var/task/schema.prisma'"

I want to know why the client is trying to access to the schema in that path...also is it possible to change the schema path in my PrismaClient instantiation?

Because everything in my lambda is bundled into a single file I found this solution

How to include static files into the lambda package with AWS SAM esbuild?

seems to be a good option but I'd need pass my schema dynamically like this

import { PrismaClient } from "@prisma/client";
import schema from "./prisma/schema.prisma"

console.log(`schema is ${schema}`) //print a weird name generated in runtime
const prisma = new PrismaClient({schema}) // <= I need an option like this but I can't find anything

============================ I found an option to change the schema folder, in the package.json you set the path...in my case because I only can write to tmp..I set the tmp folder as my prisma path schema

https://github.com/prisma/prisma/issues/3144

package.json

"prisma": { "schema": "/tmp/schema.prisma" }

and I tried to read the schema and write to a file in the /tmp folder (copying directly doesn't work)

so..before initialize my Prisma Client I do something like

const schemaText = fs.readFileSync(schema)
fs.writeFileSync('/tmp/schema.prisma',schemaText)

const prisma = new PrismaClient()

in this case I get this error

'errorType': 'Error', 'errorMessage': '@prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.\nIn case this error is unexpected for you, please report it in https://github.com/prisma/prisma/issues'

and here I'm a bit stuck...I'd appreciate any help on how I can fix this, at the moment I can't use the serverless framework, just cloudformation and I found it a bit difficult to get information on Prisma 2 being deployed with CF

Thanks

clagccs
  • 2,224
  • 2
  • 21
  • 33

0 Answers0