#UpdatedAt
8 messages · Page 1 of 1 (latest)
Hi @vocal pulsar. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Based on the knowledge sources provided, Convex doesn't have a built-in updated_at field that automatically updates when document data changes.
While Convex does have an updatedAt property in the UserIdentity interface as shown in one of the documents, this is specifically related to JWT claims for authentication and not a general feature for all documents in the database Interface: UserIdentity.
In the examples provided in the knowledge sources, developers typically implement their own timestamp tracking. For instance, in one of the code examples, we can see a schema definition that includes a manually defined updatedAt field:
bookings: defineTable({
// other fields...
updatedAt: v.number(), // Last updated time of the booking (timestamp)
// other fields...
})
Additionally, in another example, we see an index being created that includes an updatedAt field that developers have added themselves:
mainlist: defineEnt({})
.field('organizationId', v.string())
.field('updatedAt', v.number())
.index('organizationId_updatedAt', ['organizationId', 'updatedAt']),
This suggests that developers need to manually implement and update timestamp fields in their Convex applications rather than relying on an automatically maintained field.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@vocal pulsar Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@north grove how do you recommend we set up updatedAt, across all models? how do we create one equivalent to prisma's updatedAt for consistency
Let me check the knowledge base for you again. One moment! :books:
Convex does not provide a built-in, automatically-updating updatedAt field like Prisma. To achieve consistent updatedAt behavior across all your models, you should:
-
Add an
updatedAtfield to each table schema
DefineupdatedAtas av.number()(timestamp) in every table where you want to track updates.import { defineTable, v } from "convex/schema"; export default defineSchema({ myTable: defineTable({ // ...other fields updatedAt: v.number(), }), // ...other tables }); -
Update
updatedAtin every mutation that changes a document
In each mutation that modifies a document, setupdatedAt: Date.now()when you perform an update or patch. For example:export const updateMyDoc = mutation({ args: { id: v.id("myTable"), ...otherArgs }, handler: async (ctx, args) => { await ctx.db.patch(args.id, { // ...other fields to update updatedAt: Date.now(), }); }, }); -
(Optional) Enforce
updatedAtupdates with a helper
If you want to avoid forgetting to updateupdatedAt, you can create a helper function to wrap your patch/insert logic and always setupdatedAtaccordingly.
This approach is similar to how you would manually manage timestamps in other databases that don’t have automatic timestamp fields. You are responsible for updating updatedAt in every mutation that changes a document. There is no built-in automation for this in Convex at this time Keeping Users in Sync: Building Real-time Collaboration with Convex, Bonus round: npm downloads go brrrr.
Summary:
- Add
updatedAt: v.number()to every table. - Set
updatedAt: Date.now()in every mutation that changes a document. - There is no automatic equivalent to Prisma’s
updatedAtin Convex; it must be managed manually.