diff --git a/src/config/database.js b/src/config/database.js index a04b941..3c11106 100644 --- a/src/config/database.js +++ b/src/config/database.js @@ -5,6 +5,19 @@ let db; const requiredConfig = ['DB_HOST', 'DB_PORT', 'DB_NAME', 'DB_USER', 'DB_PASSWORD']; +// MySQL (strict mode) rejects ISO date strings with a time zone, e.g. "2026-10-19T06:55:53.409Z", in DATETIME +// columns. The models and the content ingest service write such strings, so they are turned into plain local +// date-times here. The server and MySQL both run on China time, which is how the existing rows are stored. +const ISO_DATETIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/; +const pad = (value) => String(value).padStart(2, '0'); +const toMySqlParam = (value) => { + if (typeof value !== 'string' || !ISO_DATETIME.test(value)) return value; + const date = new Date(value); + if (Number.isNaN(date.getTime())) return value; + return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` + + `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`; +}; + class MySqlStatement { constructor(connection, sql) { this.connection = connection; @@ -12,7 +25,7 @@ class MySqlStatement { } all(...params) { - const result = this.connection.query(this.sql, params); + const result = this.connection.query(this.sql, params.map(toMySqlParam)); return Array.isArray(result) ? result : []; } @@ -22,7 +35,7 @@ class MySqlStatement { } run(...params) { - const result = this.connection.query(this.sql, params) || {}; + const result = this.connection.query(this.sql, params.map(toMySqlParam)) || {}; return { changes: result.affectedRows || 0, lastInsertRowid: result.insertId || 0, @@ -83,4 +96,4 @@ const closeDb = () => { } }; -module.exports = { getDb, closeDb }; +module.exports = { getDb, closeDb, toMySqlParam };