From 123b2558a133aa013d10f87f96c37f098522f020 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Wed, 20 Aug 2014 15:54:47 +0200 Subject: [PATCH] Implemented hasOne update() --- lib/relation-definition.js | 19 +++++++++++++++++++ test/relations.test.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 84ebc44e..f913cc6b 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -1353,6 +1353,8 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) { var relationMethod = relation.related.bind(relation) relationMethod.create = relation.create.bind(relation); relationMethod.build = relation.build.bind(relation); + relationMethod.update = relation.update.bind(relation); + relationMethod.destroy = relation.destroy.bind(relation); relationMethod._targetClass = definition.modelTo.modelName; return relationMethod; } @@ -1416,6 +1418,23 @@ HasOne.prototype.create = function (targetModelData, cb) { }); }; +HasOne.prototype.update = function (targetModelData, cb) { + var definition = this.definition; + this.fetch(function(err, inst) { + if (inst instanceof ModelBaseClass) { + inst.updateAttributes(targetModelData, cb); + } else { + cb(new Error('HasOne relation ' + definition.name + + ' is empty')); + } + }); +}; + +HasOne.prototype.destroy = function (cb) { + + +}; + /** * Create a target model instance * @param {Object} targetModelData The target model data diff --git a/test/relations.test.js b/test/relations.test.js index 871ecb54..b517e236 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -1206,6 +1206,7 @@ describe('relations', function () { describe('hasOne', function () { var Supplier, Account; + var supplierId; before(function () { db = getSchema(); @@ -1220,9 +1221,9 @@ describe('relations', function () { }); it('can be used to query data', function (done) { - // Supplier.hasOne(Account); 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) { @@ -1242,6 +1243,32 @@ describe('relations', function () { it('should set targetClass on scope property', function() { should.equal(Supplier.prototype.account._targetClass, 'Account'); }); + + it('should update the related item on scope', function(done) { + Supplier.findById(supplierId, function(e, supplier) { + should.not.exist(e); + should.exist(supplier); + supplier.account.update({supplierName: 'Supplier A'}, function(err, act) { + should.not.exist(e); + act.supplierName.should.equal('Supplier A'); + done(); + }); + }); + }); + + it('should get the related item on scope', function(done) { + Supplier.findById(supplierId, function(e, supplier) { + should.not.exist(e); + should.exist(supplier); + supplier.account(function(err, act) { + should.not.exist(e); + should.exist(act); + act.supplierName.should.equal('Supplier A'); + done(); + }); + }); + }); + }); describe('hasAndBelongsToMany', function () {