@naturalcycles/mysql-lib 4.12.0 → 4.12.1

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.
@@ -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,22 @@ 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
+ !(v instanceof Date) &&
141
+ typeof v.toSqlString !== 'function') {
142
+ return JSON.stringify(v);
143
+ }
144
+ return v;
130
145
  }
131
146
  function selectTokens(q) {
132
147
  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": "0.0.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.12.1",
42
42
  "description": "MySQL client implementing CommonDB interface",
43
43
  "keywords": [
44
44
  "mysql",
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,28 @@ 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
+ !(v instanceof Date) &&
182
+ typeof v.toSqlString !== 'function'
183
+ ) {
184
+ return JSON.stringify(v)
185
+ }
186
+ return v
166
187
  }
167
188
 
168
189
  function selectTokens(q: DBQuery<any>): string[] {