prisma-client-procedure-generator
v0.1.15
Published
Prisma Client Procedure Generator
Maintainers
Readme
Updates every time npx prisma generate runs.
Getting started
The example sql code used here is for sqlite
- Install this generator:
npm install -D prisma-client-procedure-generator- Add the generator to the
schema.prisma
generator procedures {
provider = "prisma-client-procedure-generator"
}- Running
npx prisma generateto generate a function from the following .sql file located at/prisma/procedures/createUser.sql
-- @param {String} $1:name The name of the user
-- @param {String} $2:email? The email id of the user
INSERT INTO "user" ("name", "email")
VALUES (:name, :email)generates the following function that can be imported from @prisma/client/procedures
import { Prisma } from "./index.js";
export async function createUser($1, $2) {
const[$name, $email] = [$1, $2];
return await Prisma.sql`
INSERT INTO user ("name", email) VALUES (${$name}, ${$email})
`;
}
// .d.ts file
import * as runtime from "@prisma/client/runtime/library";
/**
* @param name The name of the user
* @param email The email id of the user
*/
export const createUser: (name: string, email: string | null) => runtime.Sql;- Import and use the function to execute custom sql code
The following example shows the usage in a NextJS server action
"use server"
import { PrismaClient } from "@prisma/client"
import { createUser } from "@prisma/client/procedures"
const prisma = new PrismaClient()
export async function createUserAction(formData: FormData) {
const name = formData.get("name")
const email = formData.get("email")
await prisma.$executeRaw(createUser(name, email))
}