My Prisma schema is structured like this:
model Sample {
id String @id @default(cuid())
createdOn DateTime @default(now())
category String
}
category
should STRICTLY belong to one of these options:
const Categories = [
"alphaCategory",
"betaCategory",
"gammaCategory",
...
] as const;
As I am using Prisma with an MSSQL database, I cannot utilize Prisma's enums. To ensure accuracy during inserts, I rely on the satisfies
operator for validity.
category: "betaCategory" satisfies ClassificationType,
This method works adequately well for me, though it's not foolproof. However, issues arise when dealing with select statements because the return type of the category
field remains as string
, which is too generic.
One idea I had was to customize the Prisma client, but my current approach has not yielded the desired outcome:
type ClassificationType = (typeof Categories)[number];
const custom = prisma.$extends({
name: "sample",
query: {
example: {
// narrow down the return type to make the field `category` of type ClassificationType
async findMany({ model, args, operation, query }) {
const queryResult = await query(args);
return queryResult.map((item) => {
return {
...item,
category: item.category as ClassificationType,
};
});
},
},
},
});
However, the return type still remains as string
.