NonNullable JSON data type attribute
See original GitHub issueProblem
Columns with data type Json
can technically be nullable, even thought they are defined as not nullable in the prisma schema.
However, it would be very helpful to mark Json
columns as absolutely not nullable via a data type attribute.
This would prevent a lot of tedious logic in larger codebases which try to enforce a strict type-safety.
Example:
To please the typescript compiler that prisma queries really don’t return a null
value (because we enforce that on data write/update operations) for json columns, we have a lot places which look like this:
const entity = await prisma.entity.findMany(...)
return {
...entity,
entries: entity.entries.map((entry) => ({
...entry,
someSubEntity: {
...entry.someSubEntity,
attributes: entry.someSubEntity
.attributes as Attributes, // we need to tell tsc that there are really no null values stored in the json column
},
})),
}
Suggested solution
Introduction of a @jsonNotNullable
data type attribute which ensures that both for write / update operations on the model no null
value is written to the column and that the generated JsonValue
type does not contain ... | null
.
E.g. the generated type would be something like JsonValueNotNullable = string | number | boolean | JsonObject | JsonArray
Alternatives
None.
Additional context
None.
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:8 (5 by maintainers)
Top GitHub Comments
Yes that is how I understood you now, you want to disallow a Json
Null
orPrisma.JsonNull
for a specific field to further clarify what is expected and wanted in a column. The database will not be able to express that constraint (without getting fancy), but Prisma could definitely do that in the types and via validations or similar.I think that sounds like a valid feature request if you can describe it in a way the majority of my colleagues will understand 😄
@aqrln Exactly! 😃