Hello.
I’m using a PostgreSQL-backed Ecto schema with a field :fields, :map, default: %{} to store some custom fields. I’d like to limit the size of this map, to have some control over the storage size of this table.
I am not sure where to add this check in an elegant way:
The map is deserialized by a GraphQL/Absinthe custom scalar deserializer. I could check for size limit here, but I would have to return just :error when it’s exceeded, the same as in case of parse error. To the API user this would not be kind - start showing deserialize error after input size crosses a threshold. This approach would also mean i have to define one limit for all JSONBs, regardless of their use.
In resolver, the input is already parsed into a map. That map ends up in changeset - to check it’s size I would have to serialize it to JSON just to check the length of the string, which seems like a waste of CPU cycles.
3… In the DB there is no way I am aware of to limit the size of JSONB column.
The check_constraint/3 documentation suggests that its role is to convert the database exception into a changeset error. So I suspect your guidance is spot on.
I also didn’t realise that there is migration support for constraints either. Using my example above it could be:
Perfect!
I found some stack overflow where they did this with a TRIGGER, and was discouraged as that looked complex. CONSTRAINT CHECK is the proper solution I was looking for. Thanks!