Merge pull request #356 from clarkorz/feature/allow-scope-hasone

Allow hasOne relation to have a scope option
This commit is contained in:
Raymond Feng 2014-11-20 08:19:40 -08:00
commit d5dcf0a966
2 changed files with 48 additions and 1 deletions

View File

@ -1448,6 +1448,7 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) {
keyTo: fk, keyTo: fk,
modelTo: modelTo, modelTo: modelTo,
properties: params.properties, properties: params.properties,
scope: params.scope,
options: params.options, options: params.options,
polymorphic: polymorphic polymorphic: polymorphic
}); });

View File

@ -1797,6 +1797,52 @@ describe('relations', function () {
}); });
describe('hasOne with scope', function () {
var Supplier, Account;
var supplierId, accountId;
before(function () {
db = getSchema();
Supplier = db.define('Supplier', {name: String});
Account = db.define('Account', {accountNo: String, supplierName: String, block: Boolean});
Supplier.hasOne(Account, { scope: { where: { block: false } }, properties: { name: 'supplierName' } });
});
it('can be used to query data', function (done) {
db.automigrate(function () {
Supplier.create({name: 'Supplier 1'}, function (e, supplier) {
supplierId = supplier.id;
should.not.exist(e);
should.exist(supplier);
supplier.account.create({accountNo: 'a01'}, function (err, account) {
supplier.account(function (e, act) {
accountId = act.id;
should.not.exist(e);
should.exist(act);
act.should.be.an.instanceOf(Account);
supplier.account().id.should.equal(act.id);
act.supplierName.should.equal(supplier.name);
done();
});
});
});
});
});
it('should find record that match scope', function (done) {
Account.updateAll({ block: true }, function (err) {
Supplier.findById(supplierId, function (err, supplier) {
supplier.account(function (err, account) {
should.not.exists(account);
done();
});
});
});
});
});
describe('hasOne with non standard id', function () { describe('hasOne with non standard id', function () {
var Supplier, Account; var Supplier, Account;
var supplierId, accountId; var supplierId, accountId;