#UpdatedAt

8 messages · Page 1 of 1 (latest)

vocal pulsar
#

Does convex have a built in updated_at field that automatically updates anytime the data in the document changes.

north groveBOT
#

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.

somber prairie
#

@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

north groveBOT
#

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:

  1. Add an updatedAt field to each table schema
    Define updatedAt as a v.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
    });
    
  2. Update updatedAt in every mutation that changes a document
    In each mutation that modifies a document, set updatedAt: 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(),
        });
      },
    });
    
  3. (Optional) Enforce updatedAt updates with a helper
    If you want to avoid forgetting to update updatedAt, you can create a helper function to wrap your patch/insert logic and always set updatedAt accordingly.

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 updatedAt in Convex; it must be managed manually.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: