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

Commit 6cb715f1 by Francisco Cardoso

Add hability to use raw queries in Model scopes

1 parent c59268fd
Showing with 18 additions and 2 deletions
...@@ -593,7 +593,7 @@ module.exports = (function() { ...@@ -593,7 +593,7 @@ module.exports = (function() {
if (!!scope) { if (!!scope) {
_.assign(self.$scope, scope, function scopeCustomizer(objectValue, sourceValue, key) { _.assign(self.$scope, scope, function scopeCustomizer(objectValue, sourceValue, key) {
if (key === 'where') { if (key === 'where') {
return _.assign(objectValue || {}, sourceValue); return Array.isArray(sourceValue) ? sourceValue : _.assign(objectValue || {}, sourceValue);
} else if (key === 'include' && Array.isArray(objectValue) && Array.isArray(sourceValue)) { } else if (key === 'include' && Array.isArray(objectValue) && Array.isArray(sourceValue)) {
return objectValue.concat(sourceValue); return objectValue.concat(sourceValue);
} }
......
...@@ -12,6 +12,11 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -12,6 +12,11 @@ describe(Support.getTestDialectTeaser('Model'), function() {
, Company; , Company;
var scopes = { var scopes = {
complexFunction: function(value) {
return {
where: [(value + ' IN (SELECT foobar FROM some_sql_function(foo.bar))')]
};
},
somethingTrue: { somethingTrue: {
where: { where: {
something: true, something: true,
...@@ -91,7 +96,6 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -91,7 +96,6 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(scoped2.$scope).to.deep.equal(scopes.somethingFalse); expect(scoped2.$scope).to.deep.equal(scopes.somethingFalse);
}); });
it('should work with function scopes', function () { it('should work with function scopes', function () {
expect(Company.scope({method: ['actualValue', 11]}).$scope).to.deep.equal({ expect(Company.scope({method: ['actualValue', 11]}).$scope).to.deep.equal({
where: { where: {
...@@ -128,6 +132,18 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -128,6 +132,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
it('should be able to use raw queries', function () {
expect(Company.scope([{method: ['complexFunction', 'qux']}]).$scope).to.deep.equal({
where: [ 'qux IN (SELECT foobar FROM some_sql_function(foo.bar))' ]
});
});
it('should override the default scope', function () {
expect(Company.scope(['defaultScope', {method: ['complexFunction', 'qux']}]).$scope).to.deep.equal({
where: [ 'qux IN (SELECT foobar FROM some_sql_function(foo.bar))' ]
});
});
it('should emit an error for scopes that dont exist', function() { it('should emit an error for scopes that dont exist', function() {
expect(function () { expect(function () {
Company.scope('doesntexist'); Company.scope('doesntexist');
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!