Allow hasOne relation to have a scope option
Signed-off-by: Clark Wang <clark.wangs@gmail.com>
This commit is contained in:
parent
293d904bf0
commit
a9cc1c38df
|
@ -1448,6 +1448,7 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) {
|
|||
keyTo: fk,
|
||||
modelTo: modelTo,
|
||||
properties: params.properties,
|
||||
scope: params.scope,
|
||||
options: params.options,
|
||||
polymorphic: polymorphic
|
||||
});
|
||||
|
|
|
@ -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 () {
|
||||
var Supplier, Account;
|
||||
var supplierId, accountId;
|
||||
|
|
Loading…
Reference in New Issue