As your application grows, you may find the need to group related logic together. We suggest either wrapping a model in a class or extending Prisma Client model object.

Wrap a Prisma Model in a Class

In the example below, you'll see how you can wrap the user model in the Prisma Client within a Users class.

import { PrismaClient, User } from '@prisma/client'
type Signup = {
email: string
firstName: string
lastName: string
}
class Users {
constructor(private readonly prismaUser: PrismaClient['user']) {}
// Signup a new user
async signup(data: Signup): Promise<User> {
// do some custom validation...
return this.prismaUser.create({ data })
}
}
async function main() {
const prisma = new PrismaClient()
const users = new Users(prisma.user)
const user = await users.signup({
email: 'alice@prisma.io',
firstName: 'Alice',
lastName: 'Prisma',
})
}

With this new Users class, you can define custom functions like signup:

Note that in the example above, you're only exposing a signup method from Prisma Client. The Prisma Client is hidden within the Users class, so you're no longer be able to call methods like findMany and upsert.

This approach works well when you have a large application and you want to intentionally limit what your models can do.

Extending Prisma Client

But what if you don't want to hide existing functionality but still want to group custom functions together? In this case, you can use Object.assign to extend Prisma Client without limiting its functionality:

import { PrismaClient, User } from '@prisma/client'
type Signup = {
email: string
firstName: string
lastName: string
}
function Users(prismaUser: PrismaClient['user']) {
return Object.assign(prismaUser, {
/**
* Signup the first user and create a new team of one. Return the User with
* a full name and without a password
*/
async signup(data: Signup): Promise<User> {
return prismaUser.create({ data })
},
})
}
async function main() {
const prisma = new PrismaClient()
const users = Users(prisma.user)
const user = await users.signup({
email: 'alice@prisma.io',
firstName: 'Alice',
lastName: 'Prisma',
})
const numUsers = await users.count()
console.log(user, numUsers)
}

Now you can use your custom signup method alongside count, updateMany, groupBy and all of the other wonderful methods that Prisma Client provides. Best of all, it's all type-safe!

Edit this page on GitHub