Created
December 23, 2021 18:37
-
-
Save pantharshit00/91b8532c0a1e9cc8e7715af44c0f8894 to your computer and use it in GitHub Desktop.
Prisma middleware example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PrismaClient } from "@prisma/client"; | |
import slugify from "slugify"; | |
async function main() { | |
const prisma = new PrismaClient(); | |
prisma.$use(async (params, next) => { | |
// Manipulate params here | |
if (params.model == "Project" && params.action == "create") { | |
params.args.data.slug = slugify(params.args.data.name); | |
} | |
const result = await next(params); | |
// See results here | |
return result; | |
}); | |
const data = await prisma.project.create({ | |
data: { name: "My project" }, | |
}); | |
console.log(data); | |
prisma.$disconnect(); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment