Hosna Qasmei
Hosna Qasmeiβ€’10mo ago

_creationTime schema setup

Hi, I am trying to add a updateAt to my schema. Similar to how the default _creationTime is. I attached a screenshot of what I am referring to. What does the schema look like for this? I can't find it in the docs. I see when I hover over it a timestamp shows as well.
No description
35 Replies
erquhart
erquhartβ€’10mo ago
_creationTime is a numeric timestamp with fractional milliseconds, it's designed to be unique across documents within a given table from my understanding. Short answer is the schema type is number, and the dashboard just recognizes the numeric timestamp and formats it as a date. I've implemented updatedAt across my tables and personally just use string datetimes, really comes down to preference.
Hosna Qasmei
Hosna QasmeiOPβ€’10mo ago
Awesome, makes sense, thank you!
FleetAdmiralJakob πŸ—• πŸ—— πŸ—™
is creationTime utc time? And milliseconds since when? is it a UNIX timestamp?
DaReal
DaRealβ€’8mo ago
I also noticed that the _creationTime is a little off by 10mins for me. I have a field also named "last_updated" which i use Date.now() when executing the mutation. When i check the table, the document is inserted in realtime but the "last_updated" and "_creationTime" are different by 10mins. Don't know how that is possible. @erquhart
lee
leeβ€’8mo ago
it's a unix timestamp (milliseconds since 1970-01-01 utc) i don't think there are guarantees on clock skew between server and client. 10 minutes does seem like a lot, so we can look into measuring and bounding server clock skew. If you want the timestamps to match client timestamps, i recommend passing the time in as an argument instead of using Date.now()
DaReal
DaRealβ€’8mo ago
when you say passing it in as an arguement. I dont understand. Do you mean i can use the server time in he document? If that is possible, then it would be easier to keep everything in sync
lee
leeβ€’8mo ago
I mean your mutation can take in an argument which is the current time, calculated on the client by Date.now(), and stored in a document's field Like this
import { mutation } from "./_generated/server";
import { v } from "convex/values";

// Create a new task with the given text
export const createTask = mutation({
args: { text: v.string(), time: v.number() },
handler: async (ctx, args) => {
const newTaskId = await ctx.db.insert("tasks", { text: args.text, time: args.time });
return newTaskId;
},
});
import { mutation } from "./_generated/server";
import { v } from "convex/values";

// Create a new task with the given text
export const createTask = mutation({
args: { text: v.string(), time: v.number() },
handler: async (ctx, args) => {
const newTaskId = await ctx.db.insert("tasks", { text: args.text, time: args.time });
return newTaskId;
},
});
DaReal
DaRealβ€’8mo ago
hmmm But what is the difference between passing it as a value and initiating it in the function? when i initiate it as a function currently, it passes the correct time stamp in the database but the _creationTime field which is auto created when the document is first inserted is the one behind by 10mins
lee
leeβ€’8mo ago
The difference is that Date.now() on the client uses the client machine's clock, while on the server it uses the convex server's clock. And these might be different There is no "correct timestamp". Every computer has its own clock, and the best you can get is pass timestamps between computers. Clock skew is a tricky problem
DaReal
DaRealβ€’8mo ago
hmmm...
Michal Srb
Michal Srbβ€’8mo ago
@DaReal sounds like you're observing a big difference between _creationTime and Date.now() in a mutation. Can you share your code? Are you sure that you don't have another update changing the updatedAt field's value?
FleetAdmiralJakob πŸ—• πŸ—— πŸ—™
I experience the same. I create a value and the timestamp is 1 to 10 minutes old
Michal Srb
Michal Srbβ€’8mo ago
Thanks for the report, we'll have a look. Which instance and mutation name is it?
Michal Srb
Michal Srbβ€’8mo ago
@FleetAdmiralJakob πŸ—• πŸ—— πŸ—™ in your case the difference is between the client clock (which determines the "how many minutes ago" logic), and the _creationTime on the server. We're looking at this as well, but as Lee pointed out you can sync them by passing the +Date.now() value from client to the server (although I wouldn't really advise doing this, since you can trust the client clock even less than the server clock - your computer's clock could be off by minutes or set to any time you like).
FleetAdmiralJakob πŸ—• πŸ—— πŸ—™
ok, hope you find the problem. I don't want to implement a crazy sync solution. If the _creationTime is accurate enough it would be really cool
Michal Srb
Michal Srbβ€’8mo ago
(just in case) Are you sure your computer has time set accurately?
FleetAdmiralJakob πŸ—• πŸ—— πŸ—™
yes, compared it with the time on my phone and synced it
No description
FleetAdmiralJakob πŸ—• πŸ—— πŸ—™
/t/chatio/chat-io/youthful-caribou-100 <- my dev instance @Michal Srb Were you able to find the problem? As you said I do not want to pass the time from the client to the server because of many reasons
DaReal
DaRealβ€’8mo ago
I have checked and i believe the mutation is okay. It seems to be something from the server. Even with a simple mutation following the books mutation guide from the docs, the time is till a bit off by 5-10 mins
ballingt
ballingtβ€’8mo ago
Are you looking at Date objects to calculate the difference? If you're just looking at the numbers, could you be thinking the are seconds instead if milliseconds? 1-10 minutes in seconds divided by equals conceivable latency numbers (60-600ms). Convex timestamps are in milliseconds.
FleetAdmiralJakob πŸ—• πŸ—— πŸ—™
I think Im doing it correctly
ian
ianβ€’7mo ago
One way to measure clock skew is to have a mutation that just does return Date.now() and compare that to your browser's Date.now(). The question seems to be whether the clock difference is between different Convex servers or Convex & the client, right?
FleetAdmiralJakob πŸ—• πŸ—— πŸ—™
as said here my client is synced so it has the correct time the issue perists between different devices so the problem has to be in convex somewhere I will try this one out, wait a sec
ian
ianβ€’7mo ago
Cool, thanks for verifying. And are you still seeing the clock differences consistently, or was it an isolated issue? I've never seen it personally, and not sure if it was some machine issue that has since resolved, or something we can reproduce to isolate the issue
RJ
RJβ€’7mo ago
I happened to test out Scroll the other day (as in ~1 week ago or so?) and saw this skew there as well, of ~9 mins Tested it just now and see a difference of ~3 mins (test yourself at https://scroll.ink, see the "n time ago" label on a new note)
ian
ianβ€’7mo ago
We're looking into this, and have isolated the issue a bit. Stay tuned
FleetAdmiralJakob πŸ—• πŸ—— πŸ—™
Thank you. Since 3 people now have had the issue seems like there has to be systematically something wrong in thr creation process of the timestamps
james
jamesβ€’7mo ago
Apologies for this causing issues. The problem was us using a slightly stale creation time on idle backends where there hadn't been any intervening writes to advance time forward. The time still advanced monotonically but the drift from wallclock time certainly could cause confusion. We just rolled out a fix so that you should always see the latest timestamp.
FleetAdmiralJakob πŸ—• πŸ—— πŸ—™
Cool will check the timestamps soon looks fine now, will update if I see any other problems but for now this should be fixed. thank you so much for the nice support
DaReal
DaRealβ€’7mo ago
Thanks for consistent support Convex Team.

Did you find this page helpful?