Skip to content

Commit a90245b

Browse files
committed
Adds async option for find method - #19
1 parent 2eacfe2 commit a90245b

File tree

3 files changed

+142
-2
lines changed

3 files changed

+142
-2
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ Result:
284284
}
285285
```
286286

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

289289
`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.
290290

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

324+
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.
325+
324326
### db.rel.del(type, object)
325327

326328
Deletes the given object. Returns a Promise.

lib/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ exports.setSchema = function (schema) {
265265
var relatedType = relationDef[relationType];
266266
if (typeof relatedType !== 'string') {
267267
var relationOptions = relatedType.options;
268-
if (relationOptions && relationOptions.async) {
268+
var async = idOrIds && idOrIds.async;
269+
if (async || (relationOptions && relationOptions.async && (async === undefined))) {
269270
return;
270271
}
271272
relatedType = relatedType.type;

test/test.js

+137
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,83 @@ function tests(dbName, dbType) {
18471847
});
18481848
});
18491849
});
1850+
1851+
it('does sideload if async option is force to false', function () {
1852+
db.setSchema([
1853+
{
1854+
singular: 'author',
1855+
plural: 'authors',
1856+
relations: {
1857+
books: {hasMany: {type: 'books', options: {async: true}}}
1858+
}
1859+
},
1860+
{
1861+
singular: 'book',
1862+
plural: 'books',
1863+
relations: {
1864+
author: {belongsTo: {type: 'author', options: {async: false}}}
1865+
}
1866+
}
1867+
]);
1868+
1869+
return db.rel.save('author', {
1870+
name: 'Stephen King',
1871+
id: 19,
1872+
books: [1, 2, 3]
1873+
}).then(function () {
1874+
return db.rel.save('book', {
1875+
id: 1,
1876+
title: 'The Gunslinger'
1877+
});
1878+
}).then(function () {
1879+
return db.rel.save('book', {
1880+
id: 2,
1881+
title: 'The Drawing of the Three'
1882+
});
1883+
}).then(function () {
1884+
return db.rel.save('book', {
1885+
id: 3,
1886+
title: 'The Wastelands'
1887+
});
1888+
}).then(function () {
1889+
return db.rel.find('author', {async: false});
1890+
}).then(function (res) {
1891+
['authors', 'books'].forEach(function (type) {
1892+
res[type].forEach(function (obj) {
1893+
obj.rev.should.be.a('string');
1894+
delete obj.rev;
1895+
});
1896+
});
1897+
res.should.deep.equal({
1898+
"authors": [
1899+
{
1900+
"name": "Stephen King",
1901+
"books": [
1902+
1,
1903+
2,
1904+
3
1905+
],
1906+
"id": 19
1907+
}
1908+
],
1909+
"books": [
1910+
{
1911+
"title": "The Gunslinger",
1912+
"id": 1
1913+
},
1914+
{
1915+
"title": "The Drawing of the Three",
1916+
"id": 2
1917+
},
1918+
{
1919+
"title": "The Wastelands",
1920+
"id": 3
1921+
}
1922+
]
1923+
});
1924+
});
1925+
});
1926+
18501927
it('does not sideload if async option is true', function () {
18511928
db.setSchema([
18521929
{
@@ -1907,6 +1984,66 @@ function tests(dbName, dbType) {
19071984
});
19081985
});
19091986

1987+
it('does not sideload if async option is force to true', function () {
1988+
db.setSchema([
1989+
{
1990+
singular: 'author',
1991+
plural: 'authors',
1992+
relations: {
1993+
books: {hasMany: {type: 'books', options: {async: false}}}
1994+
}
1995+
},
1996+
{
1997+
singular: 'book',
1998+
plural: 'books',
1999+
relations: {
2000+
author: {belongsTo: {type: 'author', options: {async: true}}}
2001+
}
2002+
}
2003+
]);
2004+
2005+
return db.rel.save('author', {
2006+
name: 'Stephen King',
2007+
id: 19,
2008+
books: [1, 2, 3]
2009+
}).then(function () {
2010+
return db.rel.save('book', {
2011+
id: 1,
2012+
title: 'The Gunslinger'
2013+
});
2014+
}).then(function () {
2015+
return db.rel.save('book', {
2016+
id: 2,
2017+
title: 'The Drawing of the Three'
2018+
});
2019+
}).then(function () {
2020+
return db.rel.save('book', {
2021+
id: 3,
2022+
title: 'The Wastelands'
2023+
});
2024+
}).then(function () {
2025+
return db.rel.find('author', {async: true});
2026+
}).then(function (res) {
2027+
res['authors'].forEach(function (obj) {
2028+
obj.rev.should.be.a('string');
2029+
delete obj.rev;
2030+
});
2031+
res.should.deep.equal({
2032+
"authors": [
2033+
{
2034+
"name": "Stephen King",
2035+
"books": [
2036+
1,
2037+
2,
2038+
3
2039+
],
2040+
"id": 19
2041+
}
2042+
]
2043+
});
2044+
});
2045+
});
2046+
19102047
it('fromRawDoc works with changes', function () {
19112048
db.setSchema([
19122049
{

0 commit comments

Comments
 (0)