{"record":{"id":"00607af028a60c9b","repo":"denoland/deno","slug":"value-must-be-a-positive-bigint-received-value","errorCode":null,"errorMessage":"Value must be a positive bigint: received ${value}","messagePattern":"Value must be a positive bigint: received (.+?)","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ext/kv/01_db.ts","lineNumber":735,"sourceCode":"      return \"AtomicOperation (empty)\";\n    }\n\n    return `AtomicOperation\\n${ArrayPrototypeJoin(operations, \"\\n\")}`;\n  }\n}\n\nconst MIN_U64 = BigInt(\"0\");\nconst MAX_U64 = BigInt(\"0xffffffffffffffff\");\n\nclass KvU64 {\n  value: bigint;\n\n  constructor(value: bigint) {\n    if (typeof value !== \"bigint\") {\n      throw new TypeError(`Value must be a bigint: received ${typeof value}`);\n    }\n    if (value < MIN_U64) {\n      throw new RangeError(\n        `Value must be a positive bigint: received ${value}`,\n      );\n    }\n    if (value > MAX_U64) {\n      throw new RangeError(\"Value must fit in a 64-bit unsigned integer\");\n    }\n    this.value = value;\n    ObjectFreeze(this);\n  }\n\n  valueOf() {\n    return this.value;\n  }\n\n  toString() {\n    return BigIntPrototypeToString(this.value);\n  }\n","sourceCodeStart":717,"sourceCodeEnd":753,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/kv/01_db.ts#L717-L753","documentation":"KvU64 values are unsigned by definition (MIN_U64 = 0n in the source). A bigint below 0 throws RangeError(`Value must be a positive bigint: received <value>`). The u64 counter domain is add-only: there is no negative accumulator, so a decrement that would cross zero is a domain error by design, not a wrapped value.","triggerScenarios":"`.sum(key, -5n)`; `new Deno.KvU64(-1n)`; feeding a computed delta (current - previous) straight to sum() when the result can be negative.","commonSituations":"Rate/stock/inventory counters that can decrease; porting signed arithmetic from SQL DECIMAL or int columns; diff calculations without a floor at zero.","solutions":["Validate the delta first: if (delta < 0n) handle the negative case explicitly (clamp, floor at 0n, or raise a domain error).","For signed state, read the current value and set() the new signed number instead of using u64 sum(); or store magnitude and sign separately.","Never assume wrap-around: u64 semantics reject rather than underflow."],"exampleFix":"// before\nawait kv.atomic().sum([\"stock\", sku], delta).commit(); // delta < 0n throws\n\n// after\nif (delta < 0n) throw new RangeError(`stock underflow for ${sku}`);\nawait kv.atomic().sum([\"stock\", sku], delta).commit();","handlingStrategy":"validation","validationCode":"function assertU64(v: bigint) {\n  if (v < 0n) throw new RangeError(`negative u64 not allowed: ${v}`);\n  if (v > 0xffffffffffffffffn) throw new RangeError(`u64 overflow: ${v}`);\n}\nassertU64(delta);\nawait kv.atomic().sum(key, delta).commit();","typeGuard":"const isU64Range = (v: bigint): v is bigint => v >= 0n && v <= 0xffffffffffffffffn;","tryCatchPattern":null,"preventionTips":["Clamp or reject negative deltas before .sum() — u64 counters are add-only","For signed state use read + set() instead of u64 sum","Compute deltas as bigint so intermediate arithmetic never goes through Number"],"tags":["deno-kv","bigint","u64","rangeerror","underflow"],"backgroundTag":"value-out-of-range","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}