I have a legacy repository module that accepts arguments to create a query. It’s not a great design but it’s what I have to work with right now. The current design doesn’t have any mechanism for creating NOT NULL queries. I’ve created an argument to do so and it works fine for my use but introduces a bug. If someone in the future were to pass a required, not-nullable, column as the argument, the code would break.
schema:
model Table {
id Int
name String?
}
repository.ts is something like this.
export class Repository {
async query (exists: string) {
prisma.table.findMany(
... ,
where = {
[exists]: {
"not": null
}
},
...
}
}
Prisma does not allow this query to be sent if the column does not have an optional null type.
So for example:
repository.query(exists="name") // Works fine.
repository.query(exists="id") // Fails since id is required and cannot be null.
// Argument `not` is missing
This is by design, and I understand the logic of it. However, I obviously dont want to introduce a bug. I’m looking for something that does
if (exists in Prisma.TableNullableColumns) { construct where_clause }
.
I’m open to any other workarounds as well.
I’m very new to typescript so apologies if there’s syntax or formatting mistakes.