From a4275b493a15990b9bd09468133f1e0843c7f6f2 Mon Sep 17 00:00:00 2001 From: Brandon Goode Date: Thu, 25 May 2017 09:08:27 -0400 Subject: [PATCH] add populate with range and hash key test and doc --- docs/_docs/model.md | 74 +++++++++++++++++++++++++++++++++++---- test/Model.js | 85 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 140 insertions(+), 19 deletions(-) diff --git a/docs/_docs/model.md b/docs/_docs/model.md index 40f353e62..ee26b1849 100644 --- a/docs/_docs/model.md +++ b/docs/_docs/model.md @@ -172,14 +172,14 @@ Dog.get(3) then(function(dog) { console.log(dog); /* - { - id: 3, + { + id: 3, name: 'Fox', parent: { - id: 2, - name: 'Rex', + id: 2, + name: 'Rex', parent: { - id: 1, + id: 1, name: 'Odie' } } @@ -187,6 +187,68 @@ Dog.get(3) */ }); ``` +#### Populate with range and hash key + +If the object to populate has both a range and hash key, you must store both in the attribute. + +```js +const CatWithOwner = dynamoose.model('CatWithOwner', + { + id: { + type: Number + }, + name: { + type: String + }, + owner: { + name: String, + address: String + } + } +); + +const Owner = dynamoose.model('Owner', + { + name: { + type: String, + hashKey: true + }, + address: { + type: String, + rangeKey: true + }, + phoneNumber: String + } +); + +var owner = new Owner({ + name: 'Owner', + address: '123 A Street', + phoneNumber: '2345551212' +}); + +var kittenWithOwner = new CatWithOwner({ + id: 100, + name: 'Owned', + owner: { + name: owner.name, + address: owner.address + } +}); + +CatWithOwner.get(100) +.then(function(cat) { + should.not.exist(cat.owner.phoneNumber); + return cat.populate({ + path: 'owner', + model: 'Owner' + }); +}) +.then(function(catWithOwnerPopulated) { + ... +}); +``` + ### Model.batchGet(keys, options, callback) @@ -294,4 +356,4 @@ If true, required attributes will be filled with their default values on update **updateTimestamps**: boolean -If true, the `timestamps` attributes will be updated. Will not do anything if timestamps attribute were not specified. Defaults to true. \ No newline at end of file +If true, the `timestamps` attributes will be updated. Will not do anything if timestamps attribute were not specified. Defaults to true. diff --git a/test/Model.js b/test/Model.js index 58dcfcee2..cf90c191e 100644 --- a/test/Model.js +++ b/test/Model.js @@ -12,7 +12,7 @@ dynamoose.local(); var should = require('should'); -var Cat, Cat2, Cat3, Cat4, Cat5, Cat6, Cat7; +var Cat, Cat2, Cat3, Cat4, Cat5, Cat6, Cat7, CatWithOwner, Owner; describe('Model', function (){ this.timeout(15000); @@ -77,7 +77,7 @@ describe('Model', function (){ default: 'Mittens' }, owner: String, - age: { + age: { type: Number, required: true } @@ -145,6 +145,35 @@ describe('Model', function (){ parent: Number } ); + + CatWithOwner = dynamoose.model('CatWithOwner', + { + id: { + type: Number + }, + name: { + type: String + }, + owner: { + name: String, + address: String + } + } + ); + + Owner = dynamoose.model('Owner', + { + name: { + type: String, + hashKey: true + }, + address: { + type: String, + rangeKey: true + }, + phoneNumber: String + } + ); done(); }); @@ -954,6 +983,12 @@ describe('Model', function (){ describe('Model.populate', function (){ before(function (done) { var kittenWithParents = new Cat6({id: 1, name: 'One'}); + var owner = new Owner({name: 'Owner', address: '123 A Street', phoneNumber: '2345551212'}); + var kittenWithOwner = new CatWithOwner({ + id: 100, + name: 'Owned', + owner: {name: owner.name, address: owner.address} + }); kittenWithParents.save() .then(function(kitten) { var kittenWithParents = new Cat6({id: 2, name: 'Two', parent: kitten.id}); @@ -977,7 +1012,13 @@ describe('Model', function (){ }) .then(function(kitten) { var kittenWithParents = new Cat7({id: 2, name: 'Two', parent: kitten.id}); - return kittenWithParents.save(done); + return kittenWithParents.save(); + }) + .then(function() { + return owner.save(); + }) + .then(function() { + kittenWithOwner.save(done); }); }); @@ -1008,7 +1049,7 @@ describe('Model', function (){ model: 'Cat6', populate: { path: 'parent', - model: 'Cat6' + model: 'Cat6' } } }); @@ -1028,6 +1069,24 @@ describe('Model', function (){ }); }); + + it('Should populate with range & hash key', function (done) { + CatWithOwner.get(100) + .then(function(cat) { + should.not.exist(cat.owner.phoneNumber); + return cat.populate({ + path: 'owner', + model: 'test-Owner' + }); + }) + .then(function(cat) { + should.exist(cat.owner); + cat.owner.name.should.eql('Owner'); + cat.owner.phoneNumber.should.eql('2345551212'); + done(); + }); + }); + it('Populating without the model definition', function (done) { Cat6.get(4) .then(function(cat) { @@ -1127,7 +1186,7 @@ describe('Model', function (){ for (var i=0 ; i<10 ; ++i) { cats.push(new Cat({id: 10+i, name: 'Tom_'+i})); } - + Cat.batchPut(cats, function (err, result) { should.not.exist(err); should.exist(result); @@ -1199,7 +1258,7 @@ describe('Model', function (){ for (var i=0 ; i<10 ; ++i) { cats.push(new Cat2({ownerId: 10+i})); } - + Cat2.batchPut(cats, function (err, result) { should.exist(err); should.not.exist(result); @@ -1213,7 +1272,7 @@ describe('Model', function (){ for (var i=0 ; i<10 ; ++i) { cats.push(new Cat({id: 20+i, name: 'Tom_'+i})); } - + Cat.batchPut(cats, function (err, result) { should.not.exist(err); should.exist(result); @@ -1231,7 +1290,7 @@ describe('Model', function (){ for (var i=0 ; i<10 ; ++i) { delete cats[i].name; } - + Cat.batchGet(cats, function (err3, result3) { should.not.exist(err3); should.exist(result3); @@ -1248,7 +1307,7 @@ describe('Model', function (){ for (var i=0 ; i<10 ; ++i) { cats.push(new Cat2({ownerId: 20+i, name: 'Tom_'+i})); } - + Cat2.batchPut(cats, function (err, result) { should.not.exist(err); should.exist(result); @@ -1279,7 +1338,7 @@ describe('Model', function (){ for (var i=0 ; i<10 ; ++i) { cats.push(new Cat2({ownerId: 20+i, name: 'Tom_'+i})); } - + Cat2.batchPut(cats, function (err, result) { should.not.exist(err); should.exist(result); @@ -1304,7 +1363,7 @@ describe('Model', function (){ for (var i=0 ; i<10 ; ++i) { cats.push(new Cat({id: 30+i, name: 'Tom_'+i})); } - + Cat.batchPut(cats, function (err, result) { should.not.exist(err); should.exist(result); @@ -1330,7 +1389,7 @@ describe('Model', function (){ for (var i=0 ; i<10 ; ++i) { cats.push(new Cat2({ownerId: 30+i, name: 'Tom_'+i})); } - + Cat2.batchPut(cats, function (err, result) { should.not.exist(err); should.exist(result); @@ -1356,7 +1415,7 @@ describe('Model', function (){ for (var i=0 ; i<10 ; ++i) { cats.push(new Cat2({ownerId: 30+i, name: 'Tom_'+i})); } - + Cat2.batchPut(cats, function (err, result) { should.not.exist(err); should.exist(result);