Gathering detailed insights and metrics for nbiladoniga-prisma-generator-omit-extra-fields
Gathering detailed insights and metrics for nbiladoniga-prisma-generator-omit-extra-fields
Gathering detailed insights and metrics for nbiladoniga-prisma-generator-omit-extra-fields
Gathering detailed insights and metrics for nbiladoniga-prisma-generator-omit-extra-fields
npm install nbiladoniga-prisma-generator-omit-extra-fields
Typescript
Module System
Node Version
NPM Version
67.1
Supply Chain
98.4
Quality
78.2
Maintenance
50
Vulnerability
99.6
License
Total Downloads
404
Last Day
1
Last Week
2
Last Month
14
Last Year
404
Minified
Minified + Gzipped
Latest Version
0.3.6
Package Id
nbiladoniga-prisma-generator-omit-extra-fields@0.3.6
Unpacked Size
31.51 kB
Size
9.31 kB
File Count
23
NPM Version
10.2.4
Node Version
20.11.0
Published on
Jan 06, 2025
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
0%
2
Compared to previous week
Last Month
-72.5%
14
Compared to previous month
Last Year
0%
404
Compared to previous year
2
2
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:
1type Person = { 2 first: string, last: string 3} 4 5declare function savePerson(person: Person); 6 7const tooFew = { first: 'Stefan' }; 8const exact = { first: 'Stefan', last: 'Baumgartner' } 9const tooMany = { first: 'Stefan', last: 'Baumgartner', age: 37 } 10 11savePerson(tooFew); // 💥 doesn't work 12savePerson(exact); // ✅ satisfies the contract 13savePerson(tooMany); // ✅ satisfies the contract 14 15// 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:
1 npm i --save-dev prisma-generator-omit-extra-fields
Using yarn:
1 yarn add -D prisma-generator-omit-extra-fields
1 - Add the generator to your Prisma schema
1generator omitExtraFields { 2 provider = "prisma-generator-omit-extra-fields" 3 4 // optional parameter to change directory or name of generated file 5 // if output is specified, it should contain filename of generated file 6 // (output like "../generated" is invalid) 7 output = "../generated/extra-fields.ts" 8}
2- Run npx prisma generate
for the following schema.prisma, or your schema
1enum Role { 2 Admin 3 Default 4} 5 6model User { 7 id Int @id @default(autoincrement()) 8 createdAt DateTime @default(now()) 9 updatedAt DateTime @updatedAt 10 name String 11 email String 12 role Role 13}
3 - Now you're able to use generated functions. For above schema you'll get next functions:
1 polishUser(user); // remove extra fields but has no explicit types 2 polishDefaultUser(user); // remove extra fields and is suitable for "data" field in queries 3 polistPartialUser(user); // remove extra fields and is suitalble for "where" field in queries
Example of their usage:
1const userWithExtraFields = { 2 email: 'test@gmail.com', 3 name: 'test', 4 role: 'Default', 5 a: 1, // extra field 6} as const; 7 8// typescript is satisfied, but prisma will throw error 9// because of extra field 'a: 1' 10prisma.user.create({ data: userWithExtraFields }); 11 12// this one will be fine for prisma 13prisma.user.create({ data: polishUser(userWithExtraFields) }) 14 15// or this one, if you want more excplicit types 16prisma.user.create({ data: polishDefaultUser(userWithExtraFields) }); 17prisma.user.findFirst({ where: polishPartialUser(userWithExtraFields) });
No vulnerabilities found.
No security vulnerabilities found.