Skip to content

Commit

Permalink
Adds async option for find method - #19
Browse files Browse the repository at this point in the history
  • Loading branch information
akofman committed May 4, 2016
1 parent 2eacfe2 commit a90245b
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ Result:
}
```

If an `id` isn't found, it's simply not returned. Notice that above, there is no object with an `id` of `3`.
If an `id` isn't found, it's simply not returned. Notice that above, there is no object with an `id` of `3`.

`find` results are always returned ordered by id. The order of your `ids` array will not necessarily be reflected in the returned array of objects.

Expand Down Expand Up @@ -321,6 +321,8 @@ The following options based on the options for [PouchDB batch fetch](http://pouc
* `limit`: Maximum number of documents to return.
* `skip`: Number of docs to skip before returning (warning: poor performance on IndexedDB/LevelDB!).

Also it is possible to add an async option `{async: true|false}` in order to force to sideload or not dependent objects. Please refer to the [Async relationships](#async-relationships) chapter for more details.

### db.rel.del(type, object)

Deletes the given object. Returns a Promise.
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ exports.setSchema = function (schema) {
var relatedType = relationDef[relationType];
if (typeof relatedType !== 'string') {
var relationOptions = relatedType.options;
if (relationOptions && relationOptions.async) {
var async = idOrIds && idOrIds.async;
if (async || (relationOptions && relationOptions.async && (async === undefined))) {
return;
}
relatedType = relatedType.type;
Expand Down
137 changes: 137 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,83 @@ function tests(dbName, dbType) {
});
});
});

it('does sideload if async option is force to false', function () {
db.setSchema([
{
singular: 'author',
plural: 'authors',
relations: {
books: {hasMany: {type: 'books', options: {async: true}}}
}
},
{
singular: 'book',
plural: 'books',
relations: {
author: {belongsTo: {type: 'author', options: {async: false}}}
}
}
]);

return db.rel.save('author', {
name: 'Stephen King',
id: 19,
books: [1, 2, 3]
}).then(function () {
return db.rel.save('book', {
id: 1,
title: 'The Gunslinger'
});
}).then(function () {
return db.rel.save('book', {
id: 2,
title: 'The Drawing of the Three'
});
}).then(function () {
return db.rel.save('book', {
id: 3,
title: 'The Wastelands'
});
}).then(function () {
return db.rel.find('author', {async: false});
}).then(function (res) {
['authors', 'books'].forEach(function (type) {
res[type].forEach(function (obj) {
obj.rev.should.be.a('string');
delete obj.rev;
});
});
res.should.deep.equal({
"authors": [
{
"name": "Stephen King",
"books": [
1,
2,
3
],
"id": 19
}
],
"books": [
{
"title": "The Gunslinger",
"id": 1
},
{
"title": "The Drawing of the Three",
"id": 2
},
{
"title": "The Wastelands",
"id": 3
}
]
});
});
});

it('does not sideload if async option is true', function () {
db.setSchema([
{
Expand Down Expand Up @@ -1907,6 +1984,66 @@ function tests(dbName, dbType) {
});
});

it('does not sideload if async option is force to true', function () {
db.setSchema([
{
singular: 'author',
plural: 'authors',
relations: {
books: {hasMany: {type: 'books', options: {async: false}}}
}
},
{
singular: 'book',
plural: 'books',
relations: {
author: {belongsTo: {type: 'author', options: {async: true}}}
}
}
]);

return db.rel.save('author', {
name: 'Stephen King',
id: 19,
books: [1, 2, 3]
}).then(function () {
return db.rel.save('book', {
id: 1,
title: 'The Gunslinger'
});
}).then(function () {
return db.rel.save('book', {
id: 2,
title: 'The Drawing of the Three'
});
}).then(function () {
return db.rel.save('book', {
id: 3,
title: 'The Wastelands'
});
}).then(function () {
return db.rel.find('author', {async: true});
}).then(function (res) {
res['authors'].forEach(function (obj) {
obj.rev.should.be.a('string');
delete obj.rev;
});
res.should.deep.equal({
"authors": [
{
"name": "Stephen King",
"books": [
1,
2,
3
],
"id": 19
}
]
});
});
});

it('fromRawDoc works with changes', function () {
db.setSchema([
{
Expand Down

0 comments on commit a90245b

Please sign in to comment.