{"record":{"id":"740539f3cb8d47eb","repo":"denoland/deno","slug":"invalid-mutation-type","errorCode":null,"errorMessage":"Invalid mutation type","messagePattern":"Invalid mutation type","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/kv/01_db.ts","lineNumber":504,"sourceCode":"          }\n          break;\n        case \"set\":\n          if (typeof mutation.expireIn === \"number\") {\n            expireIn = mutation.expireIn;\n          }\n          validateExpireIn(expireIn);\n          /* falls through */\n        case \"sum\":\n        case \"min\":\n        case \"max\":\n          type = mutation.type;\n          if (!ObjectHasOwn(mutation, \"value\")) {\n            throw new TypeError(`Invalid mutation '${type}' without value`);\n          }\n          value = serializeValue(mutation.value);\n          break;\n        default:\n          throw new TypeError(\"Invalid mutation type\");\n      }\n      ArrayPrototypePush(this.#mutations, [key, type, value, expireIn]);\n    }\n    return this;\n  }\n\n  sum(key: Deno.KvKey, n: bigint): this {\n    ArrayPrototypePush(this.#mutations, [\n      key,\n      \"sum\",\n      serializeValue(new KvU64(n)),\n      undefined,\n    ]);\n    return this;\n  }\n\n  min(key: Deno.KvKey, n: bigint): this {\n    ArrayPrototypePush(this.#mutations, [","sourceCodeStart":486,"sourceCodeEnd":522,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/kv/01_db.ts#L486-L522","documentation":"mutate() switches over mutation.type and recognizes only delete, set, sum, min and max; anything else falls through to default and throws TypeError('Invalid mutation type'). There is no 'insert' or 'upsert' — 'set' is the unconditional write, and check() is the separate optimistic-concurrency mechanism.","triggerScenarios":"`mutate({ type: \"insert\", key, value })`, \"upsert\", \"update\", casing mistakes like \"SET\" or \"Delete\", or a type string assembled dynamically from config/data with a typo.","commonSituations":"Porting SQL CRUD vocabulary (insert/update/upsert) to Deno.Kv; constants defined outside TypeScript's checking (plain strings in JSON config); mutation objects typed as any so the union never runs.","solutions":["Use only the five literal types; for upsert semantics use \"set\" (it overwrites unconditionally).","Type mutation arrays as Deno.KvMutation[] — the string-literal union catches typos at compile time.","Keep types in an as-const object: const MUT = { SET: \"set\", DELETE: \"delete\", ... } as const, and reference MUT.* everywhere."],"exampleFix":"// before\nkv.atomic().mutate({ type: \"insert\", key, value }); // Invalid mutation type\n\n// after\nkv.atomic().mutate({ type: \"set\", key, value });","handlingStrategy":"type-guard","validationCode":"const MUTATION_TYPES = new Set([\"delete\", \"set\", \"sum\", \"min\", \"max\"]);\nif (!MUTATION_TYPES.has(m.type)) {\n  throw new TypeError(`unknown mutation type '${m.type}' (allowed: ${[...MUTATION_TYPES].join(\", \")})`);\n}","typeGuard":"const isMutationType = (t: unknown): t is \"delete\" | \"set\" | \"sum\" | \"min\" | \"max\" =>\n  t === \"delete\" || t === \"set\" || t === \"sum\" || t === \"min\" || t === \"max\";","tryCatchPattern":null,"preventionTips":["Remember there is no insert/upsert — use \"set\"","Define mutation type constants as const and reference them instead of raw strings","Type arrays as Deno.KvMutation[] so the literal union catches typos at compile time"],"tags":["deno-kv","atomic","mutate","discriminated-union","validation"],"backgroundTag":"invalid-mutation","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"}