不要怂,就是干,撸起袖子干!

Commit cae4d8da by Mick Hansen

Merge pull request #5312 from taylorhakes/order-field-alias

Fixed field alias in order clause
2 parents 3f6100e6 313b5006
Showing with 267 additions and 2 deletions
...@@ -115,6 +115,37 @@ var Utils = module.exports = { ...@@ -115,6 +115,37 @@ var Utils = module.exports = {
options.where = Utils.mapWhereFieldNames(options.where, Model); options.where = Utils.mapWhereFieldNames(options.where, Model);
} }
if (Array.isArray(options.order)) {
options.order.forEach(function(oGroup) {
var OrderModel, attr, attrOffset;
if (Array.isArray(oGroup)) {
OrderModel = Model;
// Check if we have ['attr', 'DESC'] or [Model, 'attr', 'DESC']
if (typeof oGroup[oGroup.length - 2] === 'string') {
attrOffset = 2;
// Assume ['attr'], [Model, 'attr'] or [seq.fn('somefn', 1), 'DESC']
} else {
attrOffset = 1;
}
attr = oGroup[oGroup.length - attrOffset];
if (oGroup.length > attrOffset) {
OrderModel = oGroup[oGroup.length - (attrOffset + 1)];
if (OrderModel.model) {
OrderModel = OrderModel.model;
}
}
if (OrderModel.rawAttributes && OrderModel.rawAttributes[attr] && attr !== OrderModel.rawAttributes[attr].field) {
oGroup[oGroup.length - attrOffset] = OrderModel.rawAttributes[attr].field;
}
}
});
}
return options; return options;
}, },
......
...@@ -186,6 +186,241 @@ suite(Support.getTestDialectTeaser('Utils'), function() { ...@@ -186,6 +186,241 @@ suite(Support.getTestDialectTeaser('Utils'), function() {
} }
}); });
}); });
test('string field order', function() {
expect(Utils.mapOptionFieldNames({
order: 'firstName DESC'
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
}
}))).to.eql({
order: 'firstName DESC'
});
});
test('string in array order', function() {
expect(Utils.mapOptionFieldNames({
order: ['firstName DESC']
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
}
}))).to.eql({
order: ['firstName DESC']
});
});
test('single field alias order', function() {
expect(Utils.mapOptionFieldNames({
order: [['firstName', 'DESC']]
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
}
}))).to.eql({
order: [['first_name', 'DESC']]
});
});
test('multi field alias order', function() {
expect(Utils.mapOptionFieldNames({
order: [['firstName', 'DESC'], ['lastName', 'ASC']]
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
},
lastName: {
type: DataTypes.STRING,
field: 'last_name'
}
}))).to.eql({
order: [['first_name', 'DESC'], ['last_name', 'ASC']]
});
});
test('multi field alias no direction order', function() {
expect(Utils.mapOptionFieldNames({
order: [['firstName'], ['lastName']]
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
},
lastName: {
type: DataTypes.STRING,
field: 'last_name'
}
}))).to.eql({
order: [['first_name'], ['last_name']]
});
});
test('field alias to another field order', function() {
expect(Utils.mapOptionFieldNames({
order: [['firstName', 'DESC']]
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'lastName'
},
lastName: {
type: DataTypes.STRING,
field: 'firstName'
}
}))).to.eql({
order: [['lastName', 'DESC']]
});
});
test('multi field no alias order', function() {
expect(Utils.mapOptionFieldNames({
order: [['firstName', 'DESC'], ['lastName', 'ASC']]
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING
},
lastName: {
type: DataTypes.STRING
}
}))).to.eql({
order: [['firstName', 'DESC'], ['lastName', 'ASC']]
});
});
test('multi field alias sub model order', function() {
var Location = this.sequelize.define('Location', {
latLong: {
type: DataTypes.STRING,
field: 'lat_long'
}
});
var Item = this.sequelize.define('Item', {
fontColor: {
type: DataTypes.STRING,
field: 'font_color'
}
});
expect(Utils.mapOptionFieldNames({
order: [[Item, Location, 'latLong', 'DESC'], ['lastName', 'ASC']]
}, this.sequelize.define('User', {
lastName: {
type: DataTypes.STRING
}
}))).to.eql({
order: [[Item, Location, 'lat_long', 'DESC'], ['lastName', 'ASC']]
});
});
test('multi field alias sub model no direction order', function() {
var Location = this.sequelize.define('Location', {
latLong: {
type: DataTypes.STRING,
field: 'lat_long'
}
});
var Item = this.sequelize.define('Item', {
fontColor: {
type: DataTypes.STRING,
field: 'font_color'
}
});
expect(Utils.mapOptionFieldNames({
order: [[Item, Location, 'latLong'], ['lastName', 'ASC']]
}, this.sequelize.define('User', {
lastName: {
type: DataTypes.STRING
}
}))).to.eql({
order: [[Item, Location, 'lat_long'], ['lastName', 'ASC']]
});
});
test('function order', function() {
var fn = this.sequelize.fn('otherfn', 123);
expect(Utils.mapOptionFieldNames({
order: [[fn, 'ASC']]
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING
}
}))).to.eql({
order: [[fn, 'ASC']]
});
});
test('function no direction order', function() {
var fn = this.sequelize.fn('otherfn', 123);
expect(Utils.mapOptionFieldNames({
order: [[fn]]
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING
}
}))).to.eql({
order: [[fn]]
});
});
test('string no direction order', function() {
expect(Utils.mapOptionFieldNames({
order: [['firstName']]
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
}
}))).to.eql({
order: [['first_name']]
});
});
test('model alias order', function() {
var Item = this.sequelize.define('Item', {
fontColor: {
type: DataTypes.STRING,
field: 'font_color'
}
});
expect(Utils.mapOptionFieldNames({
order: [[{ model: Item, as: 'another'}, 'fontColor', 'ASC']]
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING
},
lastName: {
type: DataTypes.STRING
}
}))).to.eql({
order: [[{ model: Item, as: 'another'}, 'font_color', 'ASC']]
});
});
test('model alias no direction order', function() {
var Item = this.sequelize.define('Item', {
fontColor: {
type: DataTypes.STRING,
field: 'font_color'
}
});
expect(Utils.mapOptionFieldNames({
order: [[{ model: Item, as: 'another'}, 'fontColor']]
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING
}
}))).to.eql({
order: [[{ model: Item, as: 'another'}, 'font_color']]
});
});
test('model alias wrong field order', function() {
var Item = this.sequelize.define('Item', {
fontColor: {
type: DataTypes.STRING,
field: 'font_color'
}
});
expect(Utils.mapOptionFieldNames({
order: [[{ model: Item, as: 'another'}, 'firstName', 'ASC']]
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING
}
}))).to.eql({
order: [[{ model: Item, as: 'another'}, 'firstName', 'ASC']]
});
});
}); });
suite('stack', function() { suite('stack', function() {
...@@ -228,4 +463,4 @@ suite(Support.getTestDialectTeaser('Utils'), function() { ...@@ -228,4 +463,4 @@ suite(Support.getTestDialectTeaser('Utils'), function() {
}); });
}); });
}); });
}); });
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!