erquhartE
Convex Community3y ago
12 replies
erquhart

crypto.randomUUID() not working as _id in optimistic update

Following the docs, I'm attempting to use crypto.randomUUID() to generate an id for an optimistic update. This produces the following error:
Types of property '_id' are incompatible.
      Type '`${string}-${string}-${string}-${string}-${string}`' is not assignable to type 'Id<"reserves">'.
        Type '`${string}-${string}-${string}-${string}-${string}`' is not assignable to type '{ __tableName: "reserves"; }'.ts(2322)


Guessing this stopped working when ids became strings in 0.17.0, but that's just a guess.

In case it helps, here's the optimistic update function body:
    const existingReserves = localStore.getQuery(api.budget.getReserves, {
      budgetId,
    })
    if (existingReserves !== undefined) {
      const now = Date.now()
      const newReserve = {
        _id: crypto.randomUUID(),
        _creationTime: now,
        budgetId: args.budgetId,
      }
      localStore.setQuery(api.budget.getReserves, { budgetId }, [
        ...existingReserves,
        newReserve,
      ])
    }
Was this page helpful?