{"record":{"id":"68f7cd0c50a01d84","repo":"sidorares/node-mysql2","slug":"database-connection-config-property-must-be-a-st","errorCode":null,"errorMessage":"\"database\" connection config property must be a string","messagePattern":"\"database\" connection config property must be a string","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/packets/change_user.js","lineNumber":89,"sourceCode":"      }\n      packet.writeLengthCodedNumber(keysLength);\n      for (let 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.serializeToBuffer(Packet.MockBuffer());\n    return this.serializeToBuffer(Buffer.allocUnsafe(p.offset));\n  }\n}\n\nmodule.exports = ChangeUser;\n","sourceCodeStart":71,"sourceCodeEnd":98,"githubUrl":"https://github.com/sidorares/node-mysql2/blob/8b1f829d3706404ab372cf97bd77ebcf86578d97/lib/packets/change_user.js#L71-L98","documentation":"ChangeUser.toPacket (lib/packets/change_user.js:88-90) requires the database field to be a string before serializing it null-terminated into COM_CHANGE_USER. A non-string database corrupts the wire format, so the driver throws.","triggerScenarios":"connection.changeUser({ database: null }) (the constructor coerces falsy to '' but an explicit non-string truthy value reaches here); passing a number or object as database; reusing a config whose database is undefined-coerced to a non-string.","commonSituations":"Tenant switching with database: tenantId (number) instead of tenantId.toString(); config building that defaults database to an object on missing key.","solutions":["Coerce: database: String(opts.database || '').","Validate typeof before the changeUser call.","Ensure the tenant selector returns a string identifier."],"exampleFix":"// before\nconnection.changeUser({ database: tenant.id }); // number\n\n// after\nconnection.changeUser({ database: tenant.dbName }); // string","handlingStrategy":"validation","validationCode":"function assertChangeUserOpts(opts) {\n  if (typeof opts.database !== 'string') throw new TypeError('changeUser: database must be string');\n}","typeGuard":"const isDatabaseString = (opts) => typeof (opts && opts.database) === 'string';","tryCatchPattern":null,"preventionTips":["Coerce database identifiers with String(...) at the tenant-selection boundary.","Validate before calling changeUser in multi-tenant flows."],"tags":["change-user","connection-config","validation","serialization"],"backgroundTag":null,"analyzedSha":"8b1f829d3706404ab372cf97bd77ebcf86578d97","analyzedAt":"2026-08-11T02:54:28.964Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}