{"id":"3385e4b62e8c8153","repo":"sidorares/node-mysql2","slug":"database-connection-config-property-must-be-a-st-3385e4","errorCode":null,"errorMessage":"\"database\" connection config property must be a string","messagePattern":"\"database\" connection config property must be a string","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/packets/handshake_response.js","lineNumber":129,"sourceCode":"      }\n      packet.writeLengthCodedNumber(keysLength);\n      for (k = 0; k < attrNames.length; ++k) {\n        packet.writeLengthCodedString(attrNames[k], encoding);\n        packet.writeLengthCodedString(\n          connectAttributes[attrNames[k]],\n          encoding\n        );\n      }\n    }\n    return packet;\n  }\n\n  toPacket() {\n    if (typeof this.user !== 'string') {\n      throw new Error('\"user\" connection config property must be a string');\n    }\n    if (typeof this.database !== 'string') {\n      throw new Error('\"database\" connection config property must be a string');\n    }\n    // dry run: calculate resulting packet length\n    const p = this.serializeResponse(Packet.MockBuffer());\n    return this.serializeResponse(Buffer.alloc(p.offset));\n  }\n  static fromPacket(packet, serverFlags = 0xffffffff) {\n    const args = {};\n    args.clientFlags = packet.readInt32();\n    function isSet(flag) {\n      return args.clientFlags & serverFlags & ClientConstants[flag];\n    }\n    args.maxPacketSize = packet.readInt32();\n    args.charsetNumber = packet.readInt8();\n    const encoding = CharsetToEncoding[args.charsetNumber];\n    args.encoding = encoding;\n    packet.skip(23);\n    args.user = packet.readNullTerminatedString(encoding);\n    let authTokenLength;","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/sidorares/node-mysql2/blob/5ebe8903d6aea2d8ea1490e11b52491526e50f19/lib/packets/handshake_response.js#L111-L147","documentation":"Thrown by HandshakeResponse.toPacket() when the 'database' connection-config property is present but not a string. Unlike user, database is optional (undefined is allowed and means 'no default database'), but once provided it must be a string because it is serialized into the CONNECT_WITH_DB portion of the handshake packet. The guard at handshake_response.js:128 rejects numbers/objects/booleans.","triggerScenarios":"Passing { database: null } or { database: 0 }, passing a numeric database identifier, or assigning database from a config value that resolved to a non-string. Note: omitting database or setting it to undefined does NOT trigger this error.","commonSituations":"Treating database as a boolean flag (e.g. { database: false }), passing a numeric tenant id instead of the schema name, deserializing config where database becomes null, or copy-paste from a config that used a different shape.","solutions":["Set database to a string schema name: { database: 'myapp' }","Omit database entirely (or set to undefined) if you do not need a default database","Coerce or validate before connect: if (config.database != null && typeof config.database !== 'string') throw ...","Ensure config loaders return a string, not null, for the database key"],"exampleFix":"// before\nconst pool = mysql.createPool({ host: 'localhost', user: 'root', database: null });\n\n// after\nconst pool = mysql.createPool({\n  host: 'localhost',\n  user: 'root',\n  database: process.env.DB_NAME || 'myapp', // string or omit entirely\n});","handlingStrategy":"validation","validationCode":"function assertDatabaseConfig(config) {\n  if (\n    config.database !== undefined &&\n    config.database !== null &&\n    typeof config.database !== 'string'\n  ) {\n    throw new TypeError('mysql2: config.database must be a string or undefined');\n  }\n}","typeGuard":"function isDatabaseConfig(c) {\n  return c == null || c.database === undefined || c.database === null || typeof c.database === 'string';\n}","tryCatchPattern":null,"preventionTips":["Remember database is optional: undefined is valid, but null/number/object is not","When loading config from JSON, coerce missing keys to undefined rather than null","Validate the full config object once at startup with a schema library"],"tags":["config","connection","validation"],"analyzedSha":"5ebe8903d6aea2d8ea1490e11b52491526e50f19","analyzedAt":"2026-08-03T18:58:53.602Z","schemaVersion":2}