Eva
Eva2w ago

Why no _updatedTime

I'm curious why _creationTime is a default column for every table but not something like _updatedTime. Finding myself adding updatedAt: Date.now() as an arg in lots of mutations
8 Replies
ballingt
ballingt2w ago
Any automatic updated at usually doesn't end up being trustworthy because things like migrations and incidental updates end up modifying it
Eva
EvaOP2w ago
Ahh, okay. That makes sense!
ballingt
ballingt2w ago
We'd suggest creating your own updatedAt that you update when it makes sense, e.g. not when you make automated changes, only when you make the kind of changes you consider "real" changes but that does lead to the question of how to implement this; I like using helper functions to modify my data, and avoid accessing the tables outside of these "accessor" functions the extreme of this is something ORM-like, a class that always mediates access to the table any kind of accessor layer is nice for changing the data representation later, eg splitting one table into two Or (now that I read more carefully) just what you're doing makes sense! If it's a mutation or read/write helper function that should be considered changing the data, update that time
Eva
EvaOP2w ago
Cool — landing on something similar. An update helper function with uses MutationCtx and is used to wrap most mutations. Do you generally stick these helper functions in your convex/myTable.ts file? Or do you keep them elsewhere? As my Convex site grows I'm trying to figure out how best to keep things organized
ballingt
ballingt2w ago
I've seen several teams use directories for each concern, where they define the schema for some tables and all the relevant functions, then of course the convex/schema.ts has to import all the schemas from the different business domains to build up all the tables Generally I do these in files like you're suggesting and initially the filenames match the tables, but later I might change the tables but I don't change the filename so the filenames end up reflecting the business domain, not the table name
Eva
EvaOP2w ago
I thought the files had to match the table name? Also unclear to me where things are required to live within /convex and what is open to the author to modify — I remember reading about some restrictions but I'm not 100% sure what they were so I've just been adding a .ts file for each table to contain its queries and mutations Organizing by domain would simplify things. Can you nest them in folders?
ballingt
ballingt2w ago
No, filenames have nothing to do with table names. Hm I heard someone else suggest this too, sounds like we should change some examples to use different names. The only requirements for files in the convex/ folder are that schema.ts, http.ts, and crons.ts are special. But besides that no restrictions, you can nest folders, and use any names you want.
Eva
EvaOP2w ago
Oh! Really helpful to know. Not sure why I thought otherwise

Did you find this page helpful?