bookshelf-scopes
Advanced tools
Comparing version 1.2.2 to 1.3.0
{ | ||
"name": "bookshelf-scopes", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "Giving you Rails like scopes in Bookshelf.js.", | ||
@@ -5,0 +5,0 @@ "main": "src/scopes.js", |
@@ -105,2 +105,28 @@ # Bookshelf-Scopes | ||
### Scopes on Relationships | ||
You can also use scopes in the relationships as well. So you could have a model | ||
like this: | ||
```javascript | ||
var TestModel = bookshelf.Model.extend({ | ||
tableName: 'testmodel', | ||
scopes: { | ||
active: function(qb) { | ||
qb.where('active', '=', true); | ||
} | ||
} | ||
}); | ||
``` | ||
And then you can have a model that has many active models above as children like so: | ||
```javascript | ||
var MyModel = bookshelf.Model.extend({ | ||
tableName: 'mymodel', | ||
active_test_models: function() { | ||
return this.hasMany(TestModel1).active(); | ||
} | ||
}); | ||
``` | ||
### Override Initialize | ||
@@ -107,0 +133,0 @@ |
@@ -47,13 +47,47 @@ 'use strict'; | ||
var target = relationship.model || relationship.relatedData.target; | ||
var originalSelectConstraints = relationship.relatedData.selectConstraints; | ||
if (target.prototype.scopes && target.prototype.scopes.default) { | ||
var originalSelectConstraints = relationship.relatedData.selectConstraints; | ||
if (target.prototype.scopes) { | ||
relationship.relatedData.selectConstraints = function(knex, options) { | ||
originalSelectConstraints.apply(this, [knex, options]); | ||
if (!knex.appliedDefault) { | ||
knex.appliedDefault = true; | ||
target.prototype.scopes.default.apply(this, [knex, options]); | ||
originalSelectConstraints.apply(this, arguments); | ||
if (target.prototype.scopes.default) { | ||
if (!knex.appliedDefault) { | ||
knex.appliedDefault = true; | ||
target.prototype.scopes.default.apply(this, [knex, options]); | ||
} | ||
} | ||
}; | ||
Object.keys(target.prototype.scopes).forEach(function(key) { | ||
relationship[key] = function() { | ||
var passedInArguments = _.toArray(arguments); | ||
var currentSelectConstraints = relationship.relatedData.selectConstraints; | ||
relationship.relatedData.selectConstraints = function(knex, options) { | ||
currentSelectConstraints.apply(this, arguments); | ||
passedInArguments.unshift(knex); | ||
target.prototype.scopes[key].apply(this, passedInArguments); | ||
}; | ||
return relationship; | ||
}; | ||
}); | ||
relationship.unscoped = function() { | ||
relationship.relatedData.selectConstraints = function(knex, options) { | ||
var originalDefaultScope; | ||
if(target.prototype.scopes && target.prototype.scopes.default) { | ||
originalDefaultScope = target.prototype.scopes.default; | ||
delete target.prototype.scopes.default; | ||
} | ||
originalSelectConstraints.apply(this, arguments); | ||
if (originalDefaultScope) { | ||
target.prototype.scopes.default = originalDefaultScope; | ||
} | ||
}; | ||
return relationship; | ||
}; | ||
} | ||
return relationship; | ||
@@ -60,0 +94,0 @@ |
@@ -166,2 +166,145 @@ 'use strict'; | ||
}); | ||
it('Can add scopes on related', function() { | ||
var TestModel1 = bookshelf.Model.extend({ | ||
tableName: 'testmodel', | ||
scopes: { | ||
active: function(qb) { | ||
qb.where({'archived': false}); | ||
} | ||
} | ||
}); | ||
var TestRole = bookshelf.Model.extend({ | ||
tableName: 'testrole', | ||
test_models: function() { | ||
return this.hasMany(TestModel1).active(); | ||
} | ||
}); | ||
return Promise.all([ | ||
TestModel1.forge({name: 'test1', testrole_id: 1, archived: true}).save(), | ||
TestModel1.forge({name: 'test2', testrole_id: 1, archived: false}).save(), | ||
TestRole.forge({name: 'Company'}).save() | ||
]).then(function() { | ||
return TestRole.fetchAll({ | ||
withRelated: ['test_models'] | ||
}).then(function(allRoles) { | ||
expect(allRoles.length).to.equal(1); | ||
expect(allRoles.at(0).related('test_models').length).to.equal(1); | ||
}); | ||
}); | ||
}); | ||
it('Can call unscoped on related', function() { | ||
var TestModel1 = bookshelf.Model.extend({ | ||
tableName: 'testmodel', | ||
scopes: { | ||
default: function(qb) { | ||
qb.where({'archived': false}); | ||
} | ||
} | ||
}); | ||
var TestRole = bookshelf.Model.extend({ | ||
tableName: 'testrole', | ||
test_models: function() { | ||
return this.hasMany(TestModel1).unscoped(); | ||
} | ||
}); | ||
return Promise.all([ | ||
TestModel1.forge({name: 'test', testrole_id: 1, archived: true}).save(), | ||
TestModel1.forge({name: 'test2', testrole_id: 2, archived: true}).save(), | ||
TestRole.forge({name: 'Company'}).save(), | ||
TestRole.forge({name: 'Region'}).save() | ||
]).then(function() { | ||
return TestRole.fetchAll({ | ||
withRelated: ['test_models'] | ||
}).then(function(allRoles) { | ||
expect(allRoles.length).to.equal(2); | ||
allRoles.forEach(function(role) { | ||
expect(role.related('test_models').length).to.equal(1); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('Can call unscoped and with scope on related', function() { | ||
var TestModel1 = bookshelf.Model.extend({ | ||
tableName: 'testmodel', | ||
scopes: { | ||
default: function(qb) { | ||
qb.where({'archived': false}); | ||
}, | ||
nameContains: function(qb, partialName) { | ||
qb.where('name', 'LIKE', '%' + partialName + '%'); | ||
} | ||
} | ||
}); | ||
var TestRole = bookshelf.Model.extend({ | ||
tableName: 'testrole', | ||
test_models: function() { | ||
return this.hasMany(TestModel1).unscoped().nameContains('test'); | ||
} | ||
}); | ||
return Promise.all([ | ||
TestModel1.forge({name: 'test', testrole_id: 1, archived: true}).save(), | ||
TestModel1.forge({name: 'test2', testrole_id: 1, archived: true}).save(), | ||
TestModel1.forge({name: 'jt1', testrole_id: 1, archived: false}).save(), | ||
TestRole.forge({name: 'Company'}).save(), | ||
]).then(function() { | ||
return TestRole.fetchAll({ | ||
withRelated: ['test_models'] | ||
}).then(function(allRoles) { | ||
expect(allRoles.length).to.equal(1); | ||
allRoles.forEach(function(role) { | ||
expect(role.related('test_models').length).to.equal(2); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('Can call two scoped on related for same table', function() { | ||
var TestModel1 = bookshelf.Model.extend({ | ||
tableName: 'testmodel', | ||
scopes: { | ||
active: function(qb) { | ||
qb.where({'archived': false}); | ||
}, | ||
unactive: function(qb) { | ||
qb.where({'archived': true}); | ||
} | ||
} | ||
}); | ||
var TestRole = bookshelf.Model.extend({ | ||
tableName: 'testrole', | ||
active_test_models: function() { | ||
return this.hasMany(TestModel1).active(); | ||
}, | ||
unactive_test_models: function() { | ||
return this.hasMany(TestModel1).unactive(); | ||
} | ||
}); | ||
return Promise.all([ | ||
TestModel1.forge({name: 'test1', testrole_id: 1, archived: false}).save(), | ||
TestModel1.forge({name: 'test2', testrole_id: 1, archived: true}).save(), | ||
TestModel1.forge({name: 'test3', testrole_id: 1, archived: true}).save(), | ||
TestRole.forge({name: 'Company'}).save(), | ||
]).then(function() { | ||
return TestRole.fetchAll({ | ||
withRelated: ['active_test_models', 'unactive_test_models'] | ||
}).then(function(allRoles) { | ||
expect(allRoles.length).to.equal(1); | ||
expect(allRoles.at(0).related('active_test_models').length).to.equal(1); | ||
expect(allRoles.at(0).related('unactive_test_models').length).to.equal(2); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
42758
1028
156
0