Motivation of this generator - fix typescript and prisma mismatch in case of "exact" types. In typescript, if value has all required fields and some extra fields - typescript will be satisfied:
type Person = {
first: string, last: string
}
declare function savePerson(person: Person);
const tooFew = { first: 'Stefan' };
const exact = { first: 'Stefan', last: 'Baumgartner' }
const tooMany = { first: 'Stefan', last: 'Baumgartner', age: 37 }
savePerson(tooFew); // 💥 doesn't work
savePerson(exact); // ✅ satisfies the contract
savePerson(tooMany); // ✅ satisfies the contract
// example from "https://fettblog.eu/"
But despite typescript satisfaction, prisma will throw error in case of having extra fields being passed.
As a solution - this generator creates utility functions to omit extra fields.
Using npm:
npm i --save-dev prisma-generator-omit-extra-fields
Using yarn:
yarn add -D prisma-generator-omit-extra-fields
1 - Add the generator to your Prisma schema
generator omitExtraFields {
provider = "prisma-generator-omit-extra-fields"
// optional parameter to change directory or name of generated file
// if output is specified, it should contain filename of generated file
// (output like "../generated" is invalid)
output = "../generated/extra-fields.ts"
}
2- Run npx prisma generate
for the following schema.prisma, or your schema
enum Role {
Admin
Default
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
email String
role Role
}
3 - Now you're able to use generated functions. For above schema you'll get next functions:
polishUser(user); // remove extra fields but has no explicit types
polishDefaultUser(user); // remove extra fields and is suitable for "data" field in queries
polistPartialUser(user); // remove extra fields and is suitalble for "where" field in queries
Example of their usage:
const userWithExtraFields = {
email: '[email protected]',
name: 'test',
role: 'Default',
a: 1, // extra field
} as const;
// typescript is satisfied, but prisma will throw error
// because of extra field 'a: 1'
prisma.user.create({ data: userWithExtraFields });
// this one will be fine for prisma
prisma.user.create({ data: polishUser(userWithExtraFields) })
// or this one, if you want more excplicit types
prisma.user.create({ data: polishDefaultUser(userWithExtraFields) });
prisma.user.findFirst({ where: polishPartialUser(userWithExtraFields) });