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

enable order by ordinal position for query columns #520

Merged
merged 1 commit into from
Jan 21, 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
5 changes: 4 additions & 1 deletion lib/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",' +
Expand Down Expand Up @@ -186,6 +186,9 @@ function mixinDiscovery(MySQL, mysql) {
(table ? ' WHERE table_name=' + mysql.escape(table) : ''),
'table_name, ordinal_position', {});
}
if (options.orderBy) {
samarpanB marked this conversation as resolved.
Show resolved Hide resolved
sql += ' ORDER BY ' + options.orderBy;
}
return sql;
};

Expand Down
26 changes: 26 additions & 0 deletions test/mysql.discover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,32 @@ 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);
}
});
});
});
describe('Discover a named model', function() {
it('should return an array of columns for product', function(done) {
db.discoverModelProperties('product', function(err, models) {
Expand Down