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

Commit 80503f1f by Mick Hansen

feat(model/attributes): map field names in simple wheres

1 parent 54b685ea
Showing with 37 additions and 11 deletions
......@@ -675,7 +675,7 @@ module.exports = (function() {
if (options.attributes === undefined) {
options.attributes = Object.keys(this.rawAttributes);
}
options.attributes = mapFieldNames.call(this, options.attributes, this)
mapFieldNames.call(this, options, this)
options = paranoidClause.call(this, options)
......@@ -778,7 +778,7 @@ module.exports = (function() {
if (options.attributes === undefined) {
options.attributes = Object.keys(this.rawAttributes);
}
options.attributes = mapFieldNames.call(this, options.attributes, this)
mapFieldNames.call(this, options, this)
options = paranoidClause.call(this, options)
if (options.limit === undefined) {
......@@ -1235,7 +1235,7 @@ module.exports = (function() {
}
}
});
// Insert all records at once
return self.QueryInterface.bulkInsert(self.getTableName(), records, options, self).then(runAfterCreate)
} else {
......@@ -1548,15 +1548,25 @@ module.exports = (function() {
this.__sql = sql.setDialect(dialect === 'mariadb' ? 'mysql' : dialect)
}
var mapFieldNames = function(attributes, Model) {
attributes = attributes.map(function (attr) {
if (Model.rawAttributes[attr] && Model.rawAttributes[attr].field) {
return [Model.rawAttributes[attr].field, attr];
}
return attr;
});
var mapFieldNames = function(options, Model) {
if (options.attributes) {
options.attributes = options.attributes.map(function (attr) {
if (Model.rawAttributes[attr] && Model.rawAttributes[attr].field) {
return [Model.rawAttributes[attr].field, attr];
}
return attr;
});
}
return attributes
if (options.where) {
for (var attr in options.where) {
if (Model.rawAttributes[attr] && Model.rawAttributes[attr].field) {
options.where[Model.rawAttributes[attr].field] = options.where[attr];
delete options.where[attr];
}
}
}
return options
}
// private
......
......@@ -63,6 +63,22 @@ describe(Support.getTestDialectTeaser("Model"), function () {
})
});
it('should work with a simple where', function () {
var self = this;
return this.User.create({
name: 'Foobar'
}).then(function () {
return self.User.find({
where: {
name: 'Foobar'
}
});
}).then(function (user) {
expect(user).to.be.ok
})
});
it('should work with bulkCreate and findAll', function () {
var self = this;
return this.User.bulkCreate([{
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!