Skip to content

Commit f198486

Browse files
akofmannolanlawson
authored andcommitted
Fix relations fetches (#50)
1 parent 9cda391 commit f198486

File tree

2 files changed

+85
-14
lines changed

2 files changed

+85
-14
lines changed

lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ exports.setSchema = function (schema) {
397397

398398
function find(type, idOrIds) {
399399
return Promise.resolve().then(function () {
400-
return _find(type, idOrIds, new collections.Map());
400+
return _find(getTypeInfo(type).singular, idOrIds, new collections.Map());
401401
});
402402
}
403403

test/test.js

+84-13
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ function tests(dbName, dbType) {
601601
singular: 'post',
602602
plural: 'posts'
603603
}]);
604-
604+
605605
var attachment;
606606
if (process.browser) {
607607
attachment = blobUtil.createBlob(['Is there life on Mars?']);
@@ -628,7 +628,7 @@ function tests(dbName, dbType) {
628628
post.attachments.foo.content_type.should.equal('text/plain');
629629
});
630630
});
631-
631+
632632
it('When updating the existing document, keeps the attachment', function () {
633633
db.setSchema([{
634634
singular: 'post',
@@ -669,7 +669,7 @@ function tests(dbName, dbType) {
669669
post.attachments.foo.content_type.should.equal('text/plain');
670670
});
671671
});
672-
672+
673673
it('Removes the attachment through removal of attachments field', function () {
674674
db.setSchema([{
675675
singular: 'post',
@@ -1401,6 +1401,77 @@ function tests(dbName, dbType) {
14011401
});
14021402
});
14031403

1404+
it('should fetch all authors even with empty relations', function () {
1405+
db.setSchema([
1406+
{
1407+
singular: 'author',
1408+
plural: 'authors',
1409+
relations: {
1410+
'books': {hasMany: 'book'}
1411+
}
1412+
},
1413+
{
1414+
singular: 'book',
1415+
plural: 'books',
1416+
relations: {
1417+
'authors': {hasMany: 'author'}
1418+
}
1419+
}
1420+
]);
1421+
1422+
return db.rel.save('author', {
1423+
name: 'Stephen King',
1424+
id: 19,
1425+
books: [1]
1426+
}).then(function () {
1427+
return db.rel.save('author', {
1428+
name: 'Peter Straub',
1429+
id: 2,
1430+
books: []
1431+
});
1432+
}).then(function () {
1433+
return db.rel.save('book', {
1434+
title: 'It',
1435+
id: 1,
1436+
authors: [19]
1437+
});
1438+
}).then(function () {
1439+
return db.rel.find('authors');
1440+
}).then(function (res) {
1441+
['authors', 'books'].forEach(function (type) {
1442+
res[type].forEach(function (obj) {
1443+
obj.rev.should.be.a('string');
1444+
delete obj.rev;
1445+
});
1446+
});
1447+
res.should.deep.equal({
1448+
"authors": [
1449+
{
1450+
"name": "Peter Straub",
1451+
"books": [],
1452+
"id": 2
1453+
},
1454+
{
1455+
"name": "Stephen King",
1456+
"books": [
1457+
1
1458+
],
1459+
"id": 19
1460+
}
1461+
],
1462+
"books": [
1463+
{
1464+
"title": "It",
1465+
"authors": [
1466+
19
1467+
],
1468+
"id": 1
1469+
}
1470+
]
1471+
});
1472+
});
1473+
});
1474+
14041475
it('does one-to-many', function () {
14051476
db.setSchema([
14061477
{
@@ -1888,7 +1959,7 @@ function tests(dbName, dbType) {
18881959
]);
18891960
});
18901961
});
1891-
1962+
18921963
it('should pass along options', function () {
18931964

18941965
db.setSchema([{
@@ -1925,7 +1996,7 @@ function tests(dbName, dbType) {
19251996
});
19261997
});
19271998
});
1928-
1999+
19292000
it('should pass along options, including startkey', function () {
19302001

19312002
db.setSchema([{
@@ -1945,9 +2016,9 @@ function tests(dbName, dbType) {
19452016
id: 2
19462017
});
19472018
}).then(function () {
1948-
2019+
19492020
return db.rel.find('post', {
1950-
startkey: 2,
2021+
startkey: 2,
19512022
limit: 1
19522023
});
19532024
}).then(function (res) {
@@ -1965,7 +2036,7 @@ function tests(dbName, dbType) {
19652036
]
19662037
});
19672038
});
1968-
});
2039+
});
19692040

19702041
it('should pass along options, including endkey', function () {
19712042

@@ -2006,7 +2077,7 @@ function tests(dbName, dbType) {
20062077
});
20072078
});
20082079
});
2009-
2080+
20102081
it('should pass along options, including kip', function () {
20112082

20122083
db.setSchema([{
@@ -2026,9 +2097,9 @@ function tests(dbName, dbType) {
20262097
id: 2
20272098
});
20282099
}).then(function () {
2029-
2100+
20302101
return db.rel.find('post', {
2031-
skip: 1,
2102+
skip: 1,
20322103
limit: 1
20332104
});
20342105
}).then(function (res) {
@@ -2046,6 +2117,6 @@ function tests(dbName, dbType) {
20462117
]
20472118
});
20482119
});
2049-
});
2050-
2120+
});
2121+
20512122
}

0 commit comments

Comments
 (0)