Is it possible to write to your database directly ...
# orm-help
z
Is it possible to write to your database directly using SQL commands? I'm calling an INSERT INTO from a lambda function, and while it writes, it also breaks Prisma as I presume all the stuff Prisma generates is not generated? In my case I'm inserting into the Users table, which causes Prisma Cloud's data browser to error on pulling "usersConnection"
m
Yeah,
usersConnection
relies on data written into the
_RelayId
table. Unless you really really really need to, I recommend avoiding direct writes to any Prisma-backed DB
Personally, I use
prisma-binding
in my lambda functions just like I do in my resolvers. There are certain operations that Prisma can’t do efficiently yet, but it’s pretty good at writes. I can definitely see you wanting to do direct reads though for some of that sweet sweet fulltext search efficiency.
z
For this situation I only want createUser to be able to be called from the lambda, and not be exposed to the public as part of my graphql server. I didn't realize I could use prisma-binding in this context. Thanks!
m
Yep, but if you use env-vars in your prisma-binding (like it does in the boilerplate), then you’ll need to make sure that you have those super-secret env vars accessible from your lambda too
z
I'll also need the typeDefs file to be available to the lambda. How did you go about syncing that up?
m
I run a simple build script:
Copy code
rimraf dist && tsc
That builds everything into dist (also from the boilerplate) And then I use
sls deploy
to handle packaging and deploying those pre-compiled files to my lambdas
The typedefs get consumed as part of the build process and never actually make it to the lambda server. The lambdas just run pure good old-fashioned JS
z
Copy code
const prisma = new Prisma({
  typeDefs: 'schemas/database.graphql',
  endpoint: '<https://us1.prisma.sh/demo/my-service/dev>',
  secret: 'my-super-secret-secret'
})
I'm referring to the schema here, is that what you're talking about?
m
No, I thought you meant
src/generated/prisma.ts
Hmm, in typescript, that part gets handled for you as part of the codegen. Let me peek at how they do it
Hmmm. They take your whole schema and package it into a single string:
Copy code
const typeDefs = `
...insanely long string...
`;
That’s more lambda-friendly, but obviously not human-friendly
If you’re using webpack or some other build system, I’d try to use a loader to import that file into a string at build time. https://github.com/webpack-contrib/raw-loader
z
So I decided to go with TS as I already had migrated my frontend to it. I'm a bit confused as to how to structure my folders here, though... Right now, according to the boilerplate, my prisma service is a subdirectory of my graphql server. Where should I put my serverless functions so that they can consume the generated files inside of src, or how can I otherwise achieve that? Right now my serverless lives in its own repo.
m
I also put my serverless files in src like this:
Copy code
src/serverless/subscriptions/fooBar/handler.ts
src/serverless/subscriptions/fooBar/query.graphql
Then I build with
yarn build
and I deploy with
sls deploy
(serverless framework). My
serverless.yml
looks like:
Copy code
custom:
  subsDir: dist/serverless/subscriptions

functions:
  fooBar:
    handler: ${self:custom.subsDir}/fooBar/handler.default
    events:
      - http:
          path: fooBar
          method: post