From 7efe8c5c948bf2438639457c83175d0dab8ee4e9 Mon Sep 17 00:00:00 2001 From: "ruben.morim" Date: Thu, 2 May 2024 11:37:03 +0100 Subject: [PATCH 1/5] feat: Add jsonStrings configuration --- lib/connection_config.js | 4 +++- lib/parsers/text_parser.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/connection_config.js b/lib/connection_config.js index a18b30b330..8f32a7a52c 100644 --- a/lib/connection_config.js +++ b/lib/connection_config.js @@ -65,7 +65,8 @@ const validOptions = { idleTimeout: 1, Promise: 1, queueLimit: 1, - waitForConnections: 1 + waitForConnections: 1, + jsonStrings: 1 }; class ConnectionConfig { @@ -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) { diff --git a/lib/parsers/text_parser.js b/lib/parsers/text_parser.js index be89e77123..6bc5eb4287 100644 --- a/lib/parsers/text_parser.js +++ b/lib/parsers/text_parser.js @@ -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()'; From 3180b51cef73bf53b655cba4dd7ac78172d80b54 Mon Sep 17 00:00:00 2001 From: "ruben.morim" Date: Mon, 6 May 2024 09:41:35 +0100 Subject: [PATCH 2/5] feat: Add jsonStrings typings --- typings/mysql/lib/Connection.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/typings/mysql/lib/Connection.d.ts b/typings/mysql/lib/Connection.d.ts index ade871d1b7..96538d4b46 100644 --- a/typings/mysql/lib/Connection.d.ts +++ b/typings/mysql/lib/Connection.d.ts @@ -326,6 +326,8 @@ export interface ConnectionOptions { authPlugins?: { [key: string]: AuthPlugin; }; + + jsonStrings?: boolean; } declare class Connection extends QueryableBase(ExecutableBase(EventEmitter)) { From 9050b6876e2009f424e1618deec98bc1dc0f8a67 Mon Sep 17 00:00:00 2001 From: "ruben.morim" Date: Mon, 6 May 2024 09:41:52 +0100 Subject: [PATCH 3/5] feat: Add jsonStrings to binary_parser --- lib/parsers/binary_parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parsers/binary_parser.js b/lib/parsers/binary_parser.js index 9d53d8c188..81e2ea06e1 100644 --- a/lib/parsers/binary_parser.js +++ b/lib/parsers/binary_parser.js @@ -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 From 5ef2828ce0b6641843f1e12352894ccb693cb0a9 Mon Sep 17 00:00:00 2001 From: "ruben.morim" Date: Mon, 6 May 2024 09:53:08 +0100 Subject: [PATCH 4/5] feat: add comments --- typings/mysql/lib/Connection.d.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/typings/mysql/lib/Connection.d.ts b/typings/mysql/lib/Connection.d.ts index 96538d4b46..2dabd960fc 100644 --- a/typings/mysql/lib/Connection.d.ts +++ b/typings/mysql/lib/Connection.d.ts @@ -327,6 +327,11 @@ export interface ConnectionOptions { [key: string]: AuthPlugin; }; + /** + * Force JSON to be returned as string + * + * (Default: false) + */ jsonStrings?: boolean; } From fac1a87d19b20d92bd480cb04095e9fac5f76f27 Mon Sep 17 00:00:00 2001 From: "ruben.morim" Date: Mon, 6 May 2024 11:49:11 +0100 Subject: [PATCH 5/5] feat: add documentation to known incompatibilities section --- website/docs/documentation/00-index.mdx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/website/docs/documentation/00-index.mdx b/website/docs/documentation/00-index.mdx index a2d3f2c2f5..b249a066a0 100644 --- a/website/docs/documentation/00-index.mdx +++ b/website/docs/documentation/00-index.mdx @@ -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, +} +``` +
## Other Resources