Skip to content

Uniqueness is not enforced when writing concurrently #51

@xixixao

Description

@xixixao

This pattern of multi-upsert violates uniqueness constraints on the docs:

async function _saveAll(
  ctx: MutationCtx,
  perms: WithoutSystemFields<Doc<"perms">>[],
) {
  return await asyncMap(perms, async (perm) => {
    const existing = await ctx.table("perms").get("id", perm.id);
    if (existing === null) {
      return await ctx.table("perms").insert(perm);
    } else {
      return await existing.replace(perm);
    }
  });
}

This is because the reads happen in concurrent, and then the writes happen. This is not really specific to Ents, but Ents should catch this footgun.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions