{"id":"49a9665946f7c63e","repo":"mongodb/node-mongodb-native","slug":"option-pkfactory-must-be-an-object-with-a-createpk","errorCode":null,"errorMessage":"Option pkFactory must be an object with a createPk function, got ${value}","messagePattern":"Option pkFactory must be an object with a createPk function, got (.+?)","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":958,"sourceCode":"    type: 'boolean'\n  },\n  name: {\n    target: 'driverInfo',\n    transform({ values: [value], options }) {\n      return { ...options.driverInfo, name: String(value) };\n    }\n  } as OptionDescriptor,\n  noDelay: {\n    default: true,\n    type: 'boolean'\n  },\n  pkFactory: {\n    default: DEFAULT_PK_FACTORY,\n    transform({ values: [value] }): PkFactory {\n      if (isRecord(value, ['createPk'] as const) && typeof value.createPk === 'function') {\n        return value as PkFactory;\n      }\n      throw new MongoParseError(\n        `Option pkFactory must be an object with a createPk function, got ${value}`\n      );\n    }\n  },\n  promoteBuffers: {\n    type: 'boolean'\n  },\n  promoteLongs: {\n    type: 'boolean'\n  },\n  promoteValues: {\n    type: 'boolean'\n  },\n  useBigInt64: {\n    type: 'boolean'\n  },\n  proxyHost: {\n    type: 'string'","sourceCodeStart":940,"sourceCodeEnd":976,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L940-L976","documentation":"`pkFactory` overrides how `_id` values are generated and must be an object exposing a `createPk` function. The transform (src/connection_string.ts:952-961) requires `isRecord(value, ['createPk'])` AND `typeof value.createPk === 'function'`; otherwise it throws.","triggerScenarios":"`{ pkFactory: {} }` (no createPk); `{ pkFactory: { createPk: 'x' } }` (not a function); `{ pkFactory: () => new ObjectId() }` (function instead of object); `{ pkFactory: null }`.","commonSituations":"Migrating from an older driver where a function was accepted; partial implementation of a custom factory; forgetting to wrap the function in an object.","solutions":["Provide `{ createPk: () => /* ObjectId or value */ }`","Omit pkFactory to use the default ObjectId factory","Ensure createPk is a function, not a method-name string"],"exampleFix":"// before\nnew MongoClient(uri, { pkFactory: () => new ObjectId() });\n// after\nnew MongoClient(uri, { pkFactory: { createPk: () => new ObjectId() } });","handlingStrategy":"type-guard","validationCode":"function isValidPkFactory(v) {\n  return v != null && typeof v === 'object' && typeof v.createPk === 'function';\n}\nif (options.pkFactory && !isValidPkFactory(options.pkFactory)) {\n  throw new TypeError('pkFactory must be { createPk: () => any }');\n}","typeGuard":"function isPkFactory(v) {\n  return !!v && typeof v === 'object' && typeof v.createPk === 'function';\n}","tryCatchPattern":null,"preventionTips":["Wrap the generator function in an object: { createPk: fn }","Omit pkFactory unless you truly need custom _id generation","Do not pass a bare function as pkFactory"],"tags":["configuration","primary-key","options"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}