From 2fe54bf7e72a040a95fd13a5ad79235002eb9829 Mon Sep 17 00:00:00 2001 From: Eliel Amora Date: Mon, 24 Jul 2023 16:24:24 -0700 Subject: [PATCH 1/3] copy and freeze in development pass through in production --- lib/dialects/abstract/query.js | 83 ++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 15 deletions(-) diff --git a/lib/dialects/abstract/query.js b/lib/dialects/abstract/query.js index b60995bb31a4..d306f8095c0d 100755 --- a/lib/dialects/abstract/query.js +++ b/lib/dialects/abstract/query.js @@ -6,8 +6,71 @@ const SqlString = require('../../sql-string'); const Dot = require('dottie'); const QueryTypes = require('../../query-types'); +const IN_DEVELOPMENT = process.env.NODE_ENV.toLowerCase() === 'development'; + +function identity(result) { + return result; +} +const processRawRequeryResult = IN_DEVELOPMENT ? processRawRequeryResultInDevelopment : identity; + +function processRawRequeryResultInDevelopment(result) { + return Object.freeze(Object.assign({}, result)); +} + +function processRawRequeryResultWithTransforms(result) { + return Dot.transform(processRawRequeryResult(result)); +} + class AbstractQuery { + constructor(connection, sequelize, options) { + this.uuid = crypto.randomUUID(); + this.connection = connection; + this.instance = options.instance; + this.model = options.model; + this.sequelize = sequelize; + this.options = { + plain: false, + raw: false, + logging: console.debug, + ...options, + }; + this.checkLoggingOption(); + + if (options.rawErrors) { + // The default implementation in AbstractQuery just returns the same + // error object. By overidding this.formatError, this saves every dialect + // having to check for options.rawErrors in their own formatError + // implementations. + this.formatError = AbstractQuery.prototype.formatError; + } + } + + async logWarnings(results) { + const warningResults = await this.run('SHOW WARNINGS'); + const warningMessage = `${this.sequelize.dialect.name} warnings (${this.connection.uuid || 'default'}): `; + const messages = []; + for (const _warningRow of warningResults) { + if (_warningRow === undefined || typeof _warningRow[Symbol.iterator] !== 'function') { + continue; + } + + for (const _warningResult of _warningRow) { + if (Object.hasOwn(_warningResult, 'Message')) { + messages.push(_warningResult.Message); + } else { + for (const _objectKey of _warningResult.keys()) { + messages.push([_objectKey, _warningResult[_objectKey]].join(': ')); + } + } + } + } + + this.sequelize.log(warningMessage + messages.join('; '), this.options); + + return results; + } + /** * rewrite query with parameters * @@ -264,21 +327,11 @@ class AbstractQuery { } // Raw queries if (this.options.raw) { - result = results.map(result => { - let o = {}; - - for (const key in result) { - if (result.hasOwnProperty(key)) { - o[key] = result[key]; - } - } - - if (this.options.nest) { - o = Dot.transform(o); - } - - return o; - }); + const cb = this.options.nest ? processRawRequeryResultWithTransforms : processRawRequeryResult; + result = []; + for (let i = 0; i < results.length; i++) { + result.push(cb(results[i])); + } // Queries with include } else if (this.options.hasJoin === true) { results = AbstractQuery._groupJoinData(results, { From 94d366033ccd28b4c9dbefe0d4a02cf0ae8a99c7 Mon Sep 17 00:00:00 2001 From: Eliel Amora Date: Tue, 25 Jul 2023 10:05:52 -0700 Subject: [PATCH 2/3] remove constructor from a different version --- lib/dialects/abstract/query.js | 48 ---------------------------------- 1 file changed, 48 deletions(-) diff --git a/lib/dialects/abstract/query.js b/lib/dialects/abstract/query.js index d306f8095c0d..3da0778f532f 100755 --- a/lib/dialects/abstract/query.js +++ b/lib/dialects/abstract/query.js @@ -23,54 +23,6 @@ function processRawRequeryResultWithTransforms(result) { class AbstractQuery { - constructor(connection, sequelize, options) { - this.uuid = crypto.randomUUID(); - this.connection = connection; - this.instance = options.instance; - this.model = options.model; - this.sequelize = sequelize; - this.options = { - plain: false, - raw: false, - logging: console.debug, - ...options, - }; - this.checkLoggingOption(); - - if (options.rawErrors) { - // The default implementation in AbstractQuery just returns the same - // error object. By overidding this.formatError, this saves every dialect - // having to check for options.rawErrors in their own formatError - // implementations. - this.formatError = AbstractQuery.prototype.formatError; - } - } - - async logWarnings(results) { - const warningResults = await this.run('SHOW WARNINGS'); - const warningMessage = `${this.sequelize.dialect.name} warnings (${this.connection.uuid || 'default'}): `; - const messages = []; - for (const _warningRow of warningResults) { - if (_warningRow === undefined || typeof _warningRow[Symbol.iterator] !== 'function') { - continue; - } - - for (const _warningResult of _warningRow) { - if (Object.hasOwn(_warningResult, 'Message')) { - messages.push(_warningResult.Message); - } else { - for (const _objectKey of _warningResult.keys()) { - messages.push([_objectKey, _warningResult[_objectKey]].join(': ')); - } - } - } - } - - this.sequelize.log(warningMessage + messages.join('; '), this.options); - - return results; - } - /** * rewrite query with parameters * From ab796f2b7ca57cc917e7188e982768582fa47e5d Mon Sep 17 00:00:00 2001 From: Eliel Amora Date: Tue, 25 Jul 2023 10:18:01 -0700 Subject: [PATCH 3/3] fix undefined node env --- lib/dialects/abstract/query.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/dialects/abstract/query.js b/lib/dialects/abstract/query.js index 3da0778f532f..76be2408c421 100755 --- a/lib/dialects/abstract/query.js +++ b/lib/dialects/abstract/query.js @@ -6,7 +6,8 @@ const SqlString = require('../../sql-string'); const Dot = require('dottie'); const QueryTypes = require('../../query-types'); -const IN_DEVELOPMENT = process.env.NODE_ENV.toLowerCase() === 'development'; +const NODE_ENV = process.env.NODE_ENV?.toLowerCase() ?? ''; +const IN_DEVELOPMENT = NODE_ENV === 'development'; function identity(result) { return result;