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

Commit 2ab64b3b by Mick Hansen

fix(field): provide support for $or/$and and fields, closes #3153

1 parent e3bc9ce9
...@@ -25,7 +25,7 @@ endif ...@@ -25,7 +25,7 @@ endif
# Unit tests # Unit tests
test-unit: test-unit:
./node_modules/mocha/bin/mocha --globals setImmediate,clearImmediate --ui tdd --check-leaks --colors -t 10000 --reporter $(REPORTER) ./test/unit/**/*.js ./node_modules/mocha/bin/mocha --globals setImmediate,clearImmediate --ui tdd --check-leaks --colors -t 10000 --reporter $(REPORTER) ./test/unit/*.js ./test/unit/**/*.js
test-unit-all: test-unit-sqlite test-unit-mysql test-unit-postgres test-unit-postgres-native test-unit-mariadb test-unit-mssql test-unit-all: test-unit-sqlite test-unit-mysql test-unit-postgres test-unit-postgres-native test-unit-mariadb test-unit-mssql
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
- [BUG] Support for plain strings, ints and bools on JSON insert - [BUG] Support for plain strings, ints and bools on JSON insert
- [BUG] Fixed regression where `{$in: []}` would result in `IN ()` rather than `IN (NULL)` [#3105](https://github.com/sequelize/sequelize/issues/3105) [#3132](https://github.com/sequelize/sequelize/issues/3132) - [BUG] Fixed regression where `{$in: []}` would result in `IN ()` rather than `IN (NULL)` [#3105](https://github.com/sequelize/sequelize/issues/3105) [#3132](https://github.com/sequelize/sequelize/issues/3132)
- [BUG] Fixed bug where 2 x `belongsToMany` with `foreignKey` but no `otherKey` defined would result in 3 keys instead of 2. [#2991](https://github.com/sequelize/sequelize/issues/2991) - [BUG] Fixed bug where 2 x `belongsToMany` with `foreignKey` but no `otherKey` defined would result in 3 keys instead of 2. [#2991](https://github.com/sequelize/sequelize/issues/2991)
- [BUG] Fixed regression with `where: sequelize.json()`, [#3138](https://github.com/sequelize/sequelize/issues/3138) - [BUG] Fixed regression with `where: sequelize.json()` [#3138](https://github.com/sequelize/sequelize/issues/3138)
- [BUG] Fixed support for `field` with `$or`/`$and` [#3153](https://github.com/sequelize/sequelize/issues/3153)
# 2.0.2 # 2.0.2
- [BUG] Fixed regression with `DataTypes.ARRAY(DataTypes.STRING(length))` [#3106](https://github.com/sequelize/sequelize/issues/3106) - [BUG] Fixed regression with `DataTypes.ARRAY(DataTypes.STRING(length))` [#3106](https://github.com/sequelize/sequelize/issues/3106)
......
...@@ -6,6 +6,7 @@ var util = require('util') ...@@ -6,6 +6,7 @@ var util = require('util')
, lodash = require('lodash') , lodash = require('lodash')
, ParameterValidator = require('./utils/parameter-validator') , ParameterValidator = require('./utils/parameter-validator')
, inflection = require('inflection') , inflection = require('inflection')
, _ = require('lodash')
, uuid = require('node-uuid'); , uuid = require('node-uuid');
var Utils = module.exports = { var Utils = module.exports = {
...@@ -201,6 +202,20 @@ var Utils = module.exports = { ...@@ -201,6 +202,20 @@ var Utils = module.exports = {
attributes[rawAttribute.field] = attributes[attribute]; attributes[rawAttribute.field] = attributes[attribute];
delete attributes[attribute]; delete attributes[attribute];
} }
if (_.isPlainObject(attributes[attribute])) {
attributes[attribute] = Utils.mapOptionFieldNames({
where: attributes[attribute]
}, Model).where;
}
if (Array.isArray(attributes[attribute])) {
attributes[attribute] = attributes[attribute].map(function (where) {
return Utils.mapOptionFieldNames({
where: where
}, Model).where;
});
}
} }
} }
} }
......
'use strict';
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/support')
, DataTypes = require(__dirname + '/../../lib/data-types')
, util = require('util')
, expectsql = Support.expectsql
, current = Support.sequelize
, sql = current.dialect.QueryGenerator
, Utils = require(__dirname + '/../../lib/utils');
// Notice: [] will be replaced by dialect specific tick/quote character when there is not dialect specific expectation but only a default expectation
suite(Support.getTestDialectTeaser('Utils'), function() {
suite('mapOptionFieldNames', function () {
test('plain where', function () {
expect(Utils.mapOptionFieldNames({
where: {
firstName: 'Paul',
lastName: 'Atreides'
}
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
},
lastName: {
type: DataTypes.STRING,
field: 'last_name'
}
}))).to.eql({
where: {
first_name: 'Paul',
last_name: 'Atreides'
}
});
});
test('$or where', function () {
expect(Utils.mapOptionFieldNames({
where: {
$or: {
firstName: 'Paul',
lastName: 'Atreides'
}
}
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
},
lastName: {
type: DataTypes.STRING,
field: 'last_name'
}
}))).to.eql({
where: {
$or: {
first_name: 'Paul',
last_name: 'Atreides'
}
}
});
});
test('$or[] where', function () {
expect(Utils.mapOptionFieldNames({
where: {
$or: [
{firstName: 'Paul'},
{lastName: 'Atreides'}
]
}
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
},
lastName: {
type: DataTypes.STRING,
field: 'last_name'
}
}))).to.eql({
where: {
$or: [
{first_name: 'Paul'},
{last_name: 'Atreides'}
]
}
});
});
test('$and where', function () {
expect(Utils.mapOptionFieldNames({
where: {
$and: {
firstName: 'Paul',
lastName: 'Atreides'
}
}
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
},
lastName: {
type: DataTypes.STRING,
field: 'last_name'
}
}))).to.eql({
where: {
$and: {
first_name: 'Paul',
last_name: 'Atreides'
}
}
});
});
});
});
\ 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!