This module extends Prisma ORM. It allows instantiating your own types & logic on top of prisma default.
npm install prisma-model-wrapper
Payloads generation is only required if you plan to use
prisma
with relations. Due to TS & prisma limitations it is not possible to load relations directly, so we need to pre-generate payloads to reach them.
npx prisma-model-wrapper
You can specify both functions & classes as a wrapper.
import {Prisma, PrismaClient } from "@prisma/client";
import {prismaWrapper} from "prisma-model-wrapper";
const prisma = new PrismaClient();
type yourWrapperType = {
validate(): boolean;
}
class UserWrapper{
constructor(ret:yourWrapperType, model: string, client: any){
Object.assign(this, ret);
}
validate() {
return true; //just for example always valid
}
}
const wrappers = {
[Prisma.ModelName.users]: UserWrapper
}
const prismaNew = prismaWrapper(prisma, wrappers);
//... somewhere else
const user = await prismaNew.users.findUnique({where: {id: 1}});
user.validate();
import {Prisma, PrismaClient } from "@prisma/client";
import {prismaWrapper} from "prisma-model-wrapper";
const prisma = new PrismaClient();
type yourWrapperType = {
validate(): boolean;
}
const userWrapper = (ret: yourWrapperType, model: string, client: any): any => {
return Object.assign(ret, {
validate(): {
return true; //just for example always valid
}
});
}
const wrappers = {
[Prisma.ModelName.users]: userWrapper
}
const prismaNew = prismaWrapper(prisma, wrappers);
//... somewhere else
const user = await prismaNew.users.findUnique({where: {id: 1}});
user.validate();
If the type on your wrapper doesn't include the original type, then original will be extended like result = Original & Yours
, otherwise original type will be overwritten
Just define your wrapper with arLikeSave
import {arLikeSave,} from "prisma-model-wrapper";
const wrappers = {
[Prisma.ModelName.users]: arLikeSave
}
//... somewhere else
const user = await prismaNew.users.findUnique({where: {id: 1}});
user.name = 'new name';
await user.save(); //model saved