New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature Request] Update @prisma/client
to use the generated types
#223
Comments
I agree this would be useful. The discrepancy between the Prisma types and the zod-prisma-types is proving to be annoying. I am also using custom validators, specifically to handle date serialization like this:
This raises all kinds of "cannot assign Date to string" errors everywhere I run prisma queries, because Prisma is still expecting Date objects. As a result, I end up having to override the inferred types everywhere by using import {prisma} from "#app/utils/db.server.ts"
import type { Collection, MintData } from "#types/generated/index.ts";
export type IFetchedCollection = Collection & { mint_data: MintData };
export async function fetchCollection(address: string) {
const collection = (await prisma.collection.findFirst({
where: { address: address },
include: { mint_data: true },
})) as unknown as IFetchedCollection;
if (collectionBySlug) {
return collectionBySlug;
}
throw new Error("Collection not found");
} I also have a question in this regard. Do I have to create this custom include intersection type (IFetchedCollection), or is that already exported somewhere in my generated types? I know zod-prisma-types exports my "Collection" type like this:
However, is there also a "Collection" type with all possible "joins"? In other words, is there a type consisting of my model intersecting all its potential includes? |
@Stadly thanks for the suggestion, this seems to be a nice extension of the client. I need to look into it and see how much time this would take to implement and how complex it would get. @coindegen you can try to create additional model types with the options createOptionalDefaultValuesTypes, createRelationValuesTypes, createPartialTypes and see if these generated schemas/types will suit your needs. |
that's really helpful, thanks. Just what I needed, there's so many options it's hard to keep track :) |
Have you considered updating
@prisma/client
with the generated types? Then we would automatically get correctly typed models out of the database and get type safety when inserting in the database. We would not get automatic checks that inserted objects adhere to the generated Zod schema, but we would get automatic checks that they adhere to the TypeScript type corresponding to the Zod schema.For the normal validators, I don't think it would make any difference, but for custom validators I think it would be really useful.
Example
Consider the following
schema.prisma
, schema generated by zod-prisma-types, and generated@prisma/client
:I envision that
node_modules/.prisma/client/index.d.ts
was changed to something like this:This would give us type-safety out of the box:
Prisma Json Types Generator uses this approach for typing JSON fields, so that might be a good source for implementation inspiration.
The text was updated successfully, but these errors were encountered: