Skip to content

Commit

Permalink
add populate with range and hash key test and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
brandongoode committed May 25, 2017
1 parent b156d0f commit a4275b4
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 19 deletions.
74 changes: 68 additions & 6 deletions docs/_docs/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,83 @@ 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'
}
}
}
*/
});
```
#### 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)
Expand Down Expand Up @@ -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.
If true, the `timestamps` attributes will be updated. Will not do anything if timestamps attribute were not specified. Defaults to true.
85 changes: 72 additions & 13 deletions test/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('Model', function (){
default: 'Mittens'
},
owner: String,
age: {
age: {
type: Number,
required: true
}
Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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});
Expand All @@ -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);
});
});

Expand Down Expand Up @@ -1008,7 +1049,7 @@ describe('Model', function (){
model: 'Cat6',
populate: {
path: 'parent',
model: 'Cat6'
model: 'Cat6'
}
}
});
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit a4275b4

Please sign in to comment.