Gathering detailed insights and metrics for prisma-generator-omit-extra-fields-keep-scalar-plus
Gathering detailed insights and metrics for prisma-generator-omit-extra-fields-keep-scalar-plus
Gathering detailed insights and metrics for prisma-generator-omit-extra-fields-keep-scalar-plus
Gathering detailed insights and metrics for prisma-generator-omit-extra-fields-keep-scalar-plus
npm install prisma-generator-omit-extra-fields-keep-scalar-plus
Typescript
Module System
Node Version
NPM Version
67.7
Supply Chain
98.9
Quality
75.6
Maintenance
50
Vulnerability
99.6
License
TypeScript (99.56%)
JavaScript (0.44%)
Total Downloads
1,074
Last Day
2
Last Week
9
Last Month
26
Last Year
217
7 Stars
17 Commits
1 Forks
1 Watchers
1 Branches
2 Contributors
Updated on Nov 18, 2024
Minified
Minified + Gzipped
Latest Version
0.3.12
Package Id
prisma-generator-omit-extra-fields-keep-scalar-plus@0.3.12
Unpacked Size
35.61 kB
Size
10.01 kB
File Count
23
NPM Version
8.19.2
Node Version
16.18.1
Published on
Aug 10, 2023
Cumulative downloads
Total Downloads
Last Day
0%
2
Compared to previous day
Last Week
125%
9
Compared to previous week
Last Month
-13.3%
26
Compared to previous month
Last Year
-74.7%
217
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.