Schema types: is loose really ok?
Regarding this page on dynamic schemas: https://docs.convex.dev/database/advanced/schema-philosophy
Let's say I have a table called
variables
with fields name
, type
, and value
, and each entry can either be an integer, string, or boolean. Is there any downside to just storing the value
field with different types? For example:
{ name: "foo", type: "int", value: 1234 }
{ name: "bar", type: "string", value: "hello" }
{ name: "baz", type: "bool", value: true }
The link above says that Convex has my back, but wondering if there are performance issues or other gotchas (aside from the fact that I need to check the type in my code).Schema Philosophy | Convex Developer Hub
With Convex there is no need to write any CREATE TABLE statements, or think
2 Replies
I don’t know if any issues you’d run into. You could even have an index over the field with different types. And I’m your schema file you can use v.union to represent them. You could even have a high level union of objects, so when you check the type field, typescript can infer the value type. Let me know if that doesn’t make sense and I’ll show an example.
One thing to be aware of, though it probably wouldn’t come up, is if you end up doing comparisons, you may find that a query for > or < 100 may return non-integer values. I can’t remember the ordering offhand, but all values are strictly ordered. So a query for <100 or >= 100 will return all the values, including strings and bools.
I understand what you mean re: typescript inference. I don't think I need to do any comparisons on these particular values in a query, mostly just lookup by name or ID.
So that second issue should be OK for me.
Thanks!