@earth-app/collegedb
    Preparing search index...

    Function nextId

    • Generates the next monotonic integer id for a sharded table.

      This replaces the common but broken SELECT COALESCE(MAX(id), 0) + 1 on a single shard: because rows for a generated id land on the shard the new id hashes to, a per-shard MAX never sees rows on the other shards and hands out colliding ids. nextId reads MAX(column) across all shards, then:

      • If a coordinator is configured, it calls the Durable Object's atomic sequence (seeded by that cross-shard max), which is race-free across concurrent callers and isolates. Recommended for production writes.
      • Otherwise it returns max + 1. This is cross-shard-correct but not concurrency-safe on its own; pair it with a unique constraint or a coordinator when multiple writers race.

      Parameters

      • table: string

        Table whose id sequence is being advanced

      • options: NextIdOptions = {}

        Column override and lower bound

      Returns Promise<number>

      The next id to use for an insert

      If CollegeDB is not initialized

      1.2.4

      const id = await nextId('tickets');
      await insertInto(String(id), 'tickets', { id, title, created_at: nowSeconds });