Skip to content

Commit

Permalink
feat: add jsonStrings option (#2642)
Browse files Browse the repository at this point in the history
* 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 <ruben.morim@sovos.com>
  • Loading branch information
rubenmorim and ruben.morim authored May 30, 2024
1 parent 05cb8bf commit 9820fe5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const validOptions = {
idleTimeout: 1,
Promise: 1,
queueLimit: 1,
waitForConnections: 1
waitForConnections: 1,
jsonStrings: 1
};

class ConnectionConfig {
Expand Down Expand Up @@ -180,6 +181,7 @@ class ConnectionConfig {
};
this.connectAttributes = { ...defaultConnectAttributes, ...(options.connectAttributes || {})};
this.maxPreparedStatements = options.maxPreparedStatements || 16000;
this.jsonStrings = options.jsonStrings || false;
}

static mergeFlags(default_flags, user_flags) {
Expand Down
2 changes: 1 addition & 1 deletion lib/parsers/binary_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function readCodeFor(field, config, options, fieldNum) {
// Since for JSON columns mysql always returns charset 63 (BINARY),
// we have to handle it according to JSON specs and use "utf8",
// see https://github.com/sidorares/node-mysql2/issues/409
return 'JSON.parse(packet.readLengthCodedString("utf8"));';
return config.jsonStrings ? 'packet.readLengthCodedString("utf8")' : 'JSON.parse(packet.readLengthCodedString("utf8"));';
case Types.LONGLONG:
if (!supportBigNumbers) {
return unsigned
Expand Down
2 changes: 1 addition & 1 deletion lib/parsers/text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function readCodeFor(type, charset, encodingExpr, config, options) {
// Since for JSON columns mysql always returns charset 63 (BINARY),
// we have to handle it according to JSON specs and use "utf8",
// see https://github.com/sidorares/node-mysql2/issues/409
return 'JSON.parse(packet.readLengthCodedString("utf8"))';
return config.jsonStrings ? 'packet.readLengthCodedString("utf8")' : 'JSON.parse(packet.readLengthCodedString("utf8"))';
default:
if (charset === Charsets.BINARY) {
return 'packet.readLengthCodedBuffer()';
Expand Down
7 changes: 7 additions & 0 deletions typings/mysql/lib/Connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,13 @@ export interface ConnectionOptions {
authPlugins?: {
[key: string]: AuthPlugin;
};

/**
* Force JSON to be returned as string
*
* (Default: false)
*/
jsonStrings?: boolean;
}

declare class Connection extends QueryableBase(ExecutableBase(EventEmitter)) {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/documentation/00-index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ Please check these [examples](/docs/examples) for **MySQL2**.
This option could lose precision on the number as Javascript Number is a Float!
:::

- By default, the `JSON` type is always returned parsed into an object. However, you can modify this behavior by specifying the following configuration:

```js
{
jsonStrings: true,
}
```

<hr />

## Other Resources
Expand Down

0 comments on commit 9820fe5

Please sign in to comment.