From 5275beb76f25d0279b2c4ccfe2af036d8ecbfe4e Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Fri, 13 Oct 2023 19:32:11 +0500 Subject: [PATCH] fix: add order by ordinal position for query columns Signed-off-by: Muhammad Aaqil --- lib/discovery.js | 5 +++- test/mysql.discover.test.js | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/lib/discovery.js b/lib/discovery.js index a9cf6c2ca..f5b4d4350 100644 --- a/lib/discovery.js +++ b/lib/discovery.js @@ -154,7 +154,7 @@ function mixinDiscovery(MySQL, mysql) { * @param table * @returns {String} The sql statement */ - MySQL.prototype.buildQueryColumns = function(schema, table) { + MySQL.prototype.buildQueryColumns = function(schema, table, options = {}) { let sql = null; if (schema) { sql = paginateSQL('SELECT table_schema AS "owner",' + @@ -186,6 +186,9 @@ function mixinDiscovery(MySQL, mysql) { (table ? ' WHERE table_name=' + mysql.escape(table) : ''), 'table_name, ordinal_position', {}); } + if (options.orderBy) { + sql += ' ORDER BY ' + options.orderBy; + } return sql; }; diff --git a/test/mysql.discover.test.js b/test/mysql.discover.test.js index 4fd1f2900..885c7c66a 100644 --- a/test/mysql.discover.test.js +++ b/test/mysql.discover.test.js @@ -126,6 +126,56 @@ describe('Discover models including other users', function() { }); describe('Discover model properties', function() { + describe('Discover model properties in Ordinal Order', function() { + it('should return an array of columns for product in ordinal order', function(done) { + const productProperties = []; + const productPropertiesInOrdinalOrder = [ + 'ID', + 'NAME', + 'AUDIBLE_RANGE', + 'EFFECTIVE_RANGE', + 'ROUNDS', + 'EXTRAS', + 'FIRE_MODES', + ]; + db.discoverModelProperties('PRODUCT', {orderBy: 'ordinal_position'}, function(err, models) { + if (err) { + console.error(err); + done(err); + } else { + models.forEach(function(m) { productProperties.push(m.columnName); }); + productProperties.forEach((prop, index) => { + assert(productPropertiesInOrdinalOrder[index] === prop); + }); + done(null, models); + } + }); + }); + it('should return an array of columns for product in default (alphabatical) order', function(done) { + const productProperties = []; + const productPropertiesInOrdinalOrder = [ + 'AUDIBLE_RANGE', + 'EFFECTIVE_RANGE', + 'EXTRAS', + 'FIRE_MODES', + 'ID', + 'NAME', + 'ROUNDS', + ]; + db.discoverModelProperties('PRODUCT', function(err, models) { + if (err) { + console.error(err); + done(err); + } else { + models.forEach(function(m) { productProperties.push(m.columnName); }); + productProperties.forEach((prop, index) => { + assert(productPropertiesInOrdinalOrder[index] === prop); + }); + done(null, models); + } + }); + }); + }); describe('Discover a named model', function() { it('should return an array of columns for product', function(done) { db.discoverModelProperties('product', function(err, models) {