You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is important to understand the difference in how a numeric values are represented both in a JSON string and the corresponding plain JavaScript object. The JSON specification does not enforce any limitation with regards to the size and precision of a numeric value. Traditionally, all numeric values in a JSON string are converted to a corresponding JavaScript `number` when the string is parsed as a plain JavaScript object (`JSON.parse()`). This means that an integer lower than `Number.MIN_SAFE_INTEGER` or higher than `Number.MAX_SAFE_INTEGER` will lose precision on the type conversion. Additionally, decimal numbers will also lose precision depending on the total number of digits and the size of the fractional part.
390
+
391
+
Given this limitation, the connector uses a 3rd-party JSON parser that allows to specify the type into which a given numeric value will be converted. In this case, by default, when a numeric value risks loosing precision, it is converted into a JavaScript `string`.
392
+
393
+
For integer values in particular, an application can customize this behaviour by selecting from one of the following alternatives instead:
394
+
395
+
- all integers are preseved as a `string`
396
+
- all integers are preserved as a `BigInt`
397
+
- only unsafe integers are preserved as a `BigInt`
398
+
399
+
Selecting the appropriate strategy for handling unsafe integers in a result set can be done via a corresponding connection option.
400
+
401
+
Assuming a collection `c` within an existing schema `s` that contains the following document:
402
+
403
+
```json
404
+
{
405
+
"safeNegative":-123,
406
+
"safePositive":123,
407
+
"unsafeNegative":-9223372036854775808,
408
+
"unsafePositive":18446744073709551615
409
+
}
410
+
```
411
+
412
+
**Convert all integers in the result set to a JavaScript `string`**
Statements created and executed by an application that operate on JSON fields containing unsafe numeric values can also be specified with a JavaScript `string` or `BigInt` (if it is an integer, as depicted in the examples above). This is possible not only on placeholder assignments using the `bind()` method, but also on every other method and API used as a CRUD statement building block, like in the following examples:
Collection indexes are ordinary MySQL indexes on virtual columns that extract data from JSON document. To create an index, both the index name and the index definition are required.
It is important to understand how MySQL column types are translated to JavaScript/Node.js native types. One case worth a special mention is the fact every possible `number` higher than 2^53 - 1 (the maximum safest integer in JavaScript) or lower than -2^53 + 1 (the minimum safest integer in JavaScript) will be preserved as a `string` in order to avoid loosing precision. The following table depicts a non-comprehensive and possible mapping between data types.
292
-
293
-
| MySQL | JavaScript/Node.js |
294
-
|-------------------|-----------------------|
295
-
| `INTEGER` | `Number` |
296
-
| `INT` | `Number` |
297
-
| `SMALLINT` | `Number` |
298
-
| `TINYINT` | `Number` |
299
-
| `MEDIUMINT` | `Number` |
300
-
| `BIGINT` | `Number` or `String` |
301
-
| `DECIMAL` | `Number` or `String` |
302
-
| `NUMERIC` | `Number` or `String` |
303
-
| `FLOAT` | `Number` |
304
-
| `DOUBLE` | `Number` |
305
-
| `BIT` | `String` |
306
-
| `DATE` | `Date` |
307
-
| `DATETIME` | `Date` |
308
-
| `TIMESTAMP` | `Number` |
309
-
| `TIME` | `String` |
310
-
| `CHAR` | `String` |
311
-
| `VARCHAR` | `String` |
312
-
| `BINARY` | `String` |
313
-
| `VARBINARY` | `String` |
314
-
| `BLOB` | `String` |
315
-
| `TEXT` | `String` |
316
-
| `ENUM` | `String` |
317
-
| `SET` | `Array` |
318
-
| `JSON` | `Object` |
319
-
| `SPATIAL` | `Buffer` |
291
+
It is important to understand how MySQL column types are translated to JavaScript/Node.js native types. One case worth a special mention is the fact that, by default, every possible `number` higher than 2^53 - 1 (the maximum safest integer in JavaScript) or lower than -2^53 + 1 (the minimum safest integer in JavaScript) will be preserved as a `string` in order to avoid loosing precision. However, an application can customize this behaviour by selecting from one of the following alternatives instead:
292
+
293
+
- all integers are preseved as a `string`
294
+
- all integers are preserved as a `BigInt`
295
+
- only unsafe integers are preserved as a `BigInt`
296
+
297
+
Selecting the appropriate strategy for handling unsafe integers in a result set can be done via a corresponding connection option.
298
+
299
+
**Convert all integers in the result set to a JavaScript `string`**
Statements created and executed by an application that operate with `BIGINT`, `DECIMAL` or `NUMERIC` can also be specified with a JavaScript `string` or `BigInt` (if it is an integer). This is possible on every method and API used as a CRUD statement building block, and additionally, on SQL placeholder assignments, like in the following examples.
388
+
389
+
Assuming a table `t` within an existing schema `s` created with:
0 commit comments