Skip to content

Commit 9820fe5

Browse files
rubenmorimruben.morim
and
ruben.morim
authored
feat: add jsonStrings option (#2642)
* feat: Add jsonStrings configuration * feat: Add jsonStrings typings * feat: Add jsonStrings to binary_parser * feat: add comments * feat: add documentation to known incompatibilities section --------- Co-authored-by: ruben.morim <[email protected]>
1 parent 05cb8bf commit 9820fe5

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

lib/connection_config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ const validOptions = {
6565
idleTimeout: 1,
6666
Promise: 1,
6767
queueLimit: 1,
68-
waitForConnections: 1
68+
waitForConnections: 1,
69+
jsonStrings: 1
6970
};
7071

7172
class ConnectionConfig {
@@ -180,6 +181,7 @@ class ConnectionConfig {
180181
};
181182
this.connectAttributes = { ...defaultConnectAttributes, ...(options.connectAttributes || {})};
182183
this.maxPreparedStatements = options.maxPreparedStatements || 16000;
184+
this.jsonStrings = options.jsonStrings || false;
183185
}
184186

185187
static mergeFlags(default_flags, user_flags) {

lib/parsers/binary_parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function readCodeFor(field, config, options, fieldNum) {
5959
// Since for JSON columns mysql always returns charset 63 (BINARY),
6060
// we have to handle it according to JSON specs and use "utf8",
6161
// see https://github.com/sidorares/node-mysql2/issues/409
62-
return 'JSON.parse(packet.readLengthCodedString("utf8"));';
62+
return config.jsonStrings ? 'packet.readLengthCodedString("utf8")' : 'JSON.parse(packet.readLengthCodedString("utf8"));';
6363
case Types.LONGLONG:
6464
if (!supportBigNumbers) {
6565
return unsigned

lib/parsers/text_parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function readCodeFor(type, charset, encodingExpr, config, options) {
6363
// Since for JSON columns mysql always returns charset 63 (BINARY),
6464
// we have to handle it according to JSON specs and use "utf8",
6565
// see https://github.com/sidorares/node-mysql2/issues/409
66-
return 'JSON.parse(packet.readLengthCodedString("utf8"))';
66+
return config.jsonStrings ? 'packet.readLengthCodedString("utf8")' : 'JSON.parse(packet.readLengthCodedString("utf8"))';
6767
default:
6868
if (charset === Charsets.BINARY) {
6969
return 'packet.readLengthCodedBuffer()';

typings/mysql/lib/Connection.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ export interface ConnectionOptions {
326326
authPlugins?: {
327327
[key: string]: AuthPlugin;
328328
};
329+
330+
/**
331+
* Force JSON to be returned as string
332+
*
333+
* (Default: false)
334+
*/
335+
jsonStrings?: boolean;
329336
}
330337

331338
declare class Connection extends QueryableBase(ExecutableBase(EventEmitter)) {

website/docs/documentation/00-index.mdx

+8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ Please check these [examples](/docs/examples) for **MySQL2**.
5454
This option could lose precision on the number as Javascript Number is a Float!
5555
:::
5656

57+
- By default, the `JSON` type is always returned parsed into an object. However, you can modify this behavior by specifying the following configuration:
58+
59+
```js
60+
{
61+
jsonStrings: true,
62+
}
63+
```
64+
5765
<hr />
5866

5967
## Other Resources

0 commit comments

Comments
 (0)