Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add jsonStrings configuration #2642

Merged
merged 5 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading