@naturalcycles/mysql-lib 4.12.0 → 4.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/mysql.db.js CHANGED
@@ -190,7 +190,9 @@ export class MysqlDB extends BaseCommonDB {
190
190
  const rows = rowsInput.map(row => _mapValues(row, (_k, v) => {
191
191
  if (v && typeof v === 'object' && !Buffer.isBuffer(v)) {
192
192
  // This is to avoid implicit Date stringification and mismatch: it gets saved as Date, but loaded as String
193
- _assert(!(v instanceof Date), 'mysql-lib does not support Date values, please stringify them before passing');
193
+ _assert(
194
+ // oxlint-disable-next-line no-restricted-globals -- detects Date values to reject them
195
+ !(v instanceof Date), 'mysql-lib does not support Date values, please stringify them before passing');
194
196
  return JSON.stringify(v);
195
197
  }
196
198
  return v;
@@ -58,7 +58,7 @@ export function insertSQL(table, rows, verb = 'INSERT', logger = consoleLogger)
58
58
  `VALUES\n`,
59
59
  ].join(' ');
60
60
  const valueRows = rows.map(rec => {
61
- return `(` + fields.map(k => mysql.escape(rec[k])).join(',') + `)`;
61
+ return `(` + fields.map(k => mysql.escape(mapValueToMySQL(rec[k]))).join(',') + `)`;
62
62
  });
63
63
  const full = start + valueRows.join(',\n');
64
64
  if (full.length < MAX_PACKET_SIZE)
@@ -126,7 +126,23 @@ export function dbQueryToSQLUpdate(q, patch) {
126
126
  .join(', '),
127
127
  ...whereTokens,
128
128
  ];
129
- return mysql.format(tokens.join(' '), Object.values(patch));
129
+ return mysql.format(tokens.join(' '), Object.values(patch).map(v => mapValueToMySQL(v)));
130
+ }
131
+ /**
132
+ * Objects and arrays are stored as JSON, as mysql.escape would turn an object into `key = value`
133
+ * pairs and an array into a list of values. Buffer, Date and raw SQL (toSqlString) are left to
134
+ * mysql.escape.
135
+ */
136
+ function mapValueToMySQL(v) {
137
+ if (v &&
138
+ typeof v === 'object' &&
139
+ !Buffer.isBuffer(v) &&
140
+ // oxlint-disable-next-line no-restricted-globals -- Date values are left to the mysql driver to escape
141
+ !(v instanceof Date) &&
142
+ typeof v.toSqlString !== 'function') {
143
+ return JSON.stringify(v);
144
+ }
145
+ return v;
130
146
  }
131
147
  function selectTokens(q) {
132
148
  let fields = ['*'];
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "tslib": "^2"
11
11
  },
12
12
  "devDependencies": {
13
- "@naturalcycles/dev-lib": "20.68.0",
13
+ "@naturalcycles/dev-lib": "20.71.0",
14
14
  "@types/node": "^26",
15
15
  "typescript": "^7"
16
16
  },
@@ -38,7 +38,7 @@
38
38
  "engines": {
39
39
  "node": ">=26.12.1"
40
40
  },
41
- "version": "4.12.0",
41
+ "version": "4.13.0",
42
42
  "description": "MySQL client implementing CommonDB interface",
43
43
  "keywords": [
44
44
  "mysql",
package/src/mysql.db.ts CHANGED
@@ -302,6 +302,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
302
302
  if (v && typeof v === 'object' && !Buffer.isBuffer(v)) {
303
303
  // This is to avoid implicit Date stringification and mismatch: it gets saved as Date, but loaded as String
304
304
  _assert(
305
+ // oxlint-disable-next-line no-restricted-globals -- detects Date values to reject them
305
306
  !(v instanceof Date),
306
307
  'mysql-lib does not support Date values, please stringify them before passing',
307
308
  )
package/src/query.util.ts CHANGED
@@ -79,7 +79,7 @@ export function insertSQL(
79
79
  ].join(' ')
80
80
 
81
81
  const valueRows = rows.map(rec => {
82
- return `(` + fields.map(k => mysql.escape(rec[k])).join(',') + `)`
82
+ return `(` + fields.map(k => mysql.escape(mapValueToMySQL(rec[k]))).join(',') + `)`
83
83
  })
84
84
 
85
85
  const full = start + valueRows.join(',\n')
@@ -162,7 +162,29 @@ export function dbQueryToSQLUpdate(q: DBQuery<any>, patch: Record<any, any>): st
162
162
  ...whereTokens,
163
163
  ]
164
164
 
165
- return mysql.format(tokens.join(' '), Object.values(patch))
165
+ return mysql.format(
166
+ tokens.join(' '),
167
+ Object.values(patch).map(v => mapValueToMySQL(v)),
168
+ )
169
+ }
170
+
171
+ /**
172
+ * Objects and arrays are stored as JSON, as mysql.escape would turn an object into `key = value`
173
+ * pairs and an array into a list of values. Buffer, Date and raw SQL (toSqlString) are left to
174
+ * mysql.escape.
175
+ */
176
+ function mapValueToMySQL(v: any): any {
177
+ if (
178
+ v &&
179
+ typeof v === 'object' &&
180
+ !Buffer.isBuffer(v) &&
181
+ // oxlint-disable-next-line no-restricted-globals -- Date values are left to the mysql driver to escape
182
+ !(v instanceof Date) &&
183
+ typeof v.toSqlString !== 'function'
184
+ ) {
185
+ return JSON.stringify(v)
186
+ }
187
+ return v
166
188
  }
167
189
 
168
190
  function selectTokens(q: DBQuery<any>): string[] {