Skip to content

Commit f4dc155

Browse files
committed
Fixes async options when not specified from the schema
1 parent ad712b2 commit f4dc155

File tree

2 files changed

+92
-90
lines changed

2 files changed

+92
-90
lines changed

lib/index.js

Lines changed: 91 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ exports.setSchema = function (schema) {
104104
});
105105

106106
/**
107-
* Transform a relational object into a PouchDB doc.
108-
*/
107+
* Transform a relational object into a PouchDB doc.
108+
*/
109109
function toRawDoc(typeInfo, obj) {
110110
obj = extend(true, {}, obj);
111111
var doc = {};
@@ -155,8 +155,8 @@ exports.setSchema = function (schema) {
155155
}
156156

157157
/**
158-
* Transform a PouchDB doc into a relational object.
159-
*/
158+
* Transform a PouchDB doc into a relational object.
159+
*/
160160
function fromRawDoc(pouchDoc) {
161161
var obj = pouchDoc.data;
162162
obj.id = deserialize(pouchDoc._id);
@@ -241,7 +241,7 @@ exports.setSchema = function (schema) {
241241
opts.skip = idOrIds.skip;
242242
}
243243
} else {
244-
// find by single id
244+
// find by single id
245245
opts.key = serialize(typeInfo.documentType, idOrIds);
246246
}
247247

@@ -257,68 +257,70 @@ exports.setSchema = function (schema) {
257257

258258
foundObjects.get(type).set(JSON.stringify(obj.id), obj);
259259

260-
// fetch all relations
261260
var subTasks = [];
262-
Object.keys(typeInfo.relations || {}).forEach(function (field) {
263-
var relationDef = typeInfo.relations[field];
264-
var relationType = Object.keys(relationDef)[0];
265-
var relatedType = relationDef[relationType];
266-
if (typeof relatedType !== 'string') {
267-
var relationOptions = relatedType.options;
268-
var async = idOrIds && idOrIds.async;
269-
if (async || (relationOptions && relationOptions.async && (async === undefined))) {
270-
return;
271-
}
272-
relatedType = relatedType.type;
273-
}
274-
if (relationType === 'belongsTo') {
275-
var relatedId = obj[field];
276-
if (typeof relatedId !== 'undefined') {
277-
subTasks.push(Promise.resolve().then(function () {
278-
279-
// short-circuit if it's already in the foundObjects
280-
// else we could get caught in an infinite loop
281-
if (foundObjects.has(relatedType) &&
282-
foundObjects.get(relatedType).has(JSON.stringify(relatedId))) {
283-
return;
284-
}
285-
286-
// signal that we need to fetch it
287-
return {
288-
relatedType: relatedType,
289-
relatedIds: [relatedId]
290-
};
291-
}));
261+
262+
if (!idOrIds || !idOrIds.async) {
263+
// fetch all relations
264+
Object.keys(typeInfo.relations || {}).forEach(function (field) {
265+
var relationDef = typeInfo.relations[field];
266+
var relationType = Object.keys(relationDef)[0];
267+
var relatedType = relationDef[relationType];
268+
if (typeof relatedType !== 'string') {
269+
var relationOptions = relatedType.options;
270+
if (relationOptions && relationOptions.async && typeof idOrIds === 'undefined') {
271+
return;
272+
}
273+
relatedType = relatedType.type;
292274
}
293-
} else { // hasMany
294-
var relatedIds = extend(true, [], obj[field]);
295-
if (typeof relatedIds !== 'undefined' && relatedIds.length) {
296-
subTasks.push(Promise.resolve().then(function () {
297-
298-
// filter out all ids that are already in the foundObjects
299-
for (var i = relatedIds.length - 1; i >= 0; i--) {
300-
var relatedId = relatedIds[i];
275+
if (relationType === 'belongsTo') {
276+
var relatedId = obj[field];
277+
if (typeof relatedId !== 'undefined') {
278+
subTasks.push(Promise.resolve().then(function () {
279+
280+
// short-circuit if it's already in the foundObjects
281+
// else we could get caught in an infinite loop
301282
if (foundObjects.has(relatedType) &&
302-
foundObjects.get(relatedType).has(JSON.stringify(relatedId))) {
303-
delete relatedIds[i];
283+
foundObjects.get(relatedType).has(JSON.stringify(relatedId))) {
284+
return;
304285
}
305-
}
306-
relatedIds = relatedIds.filter(function (relatedId) {
307-
return typeof relatedId !== 'undefined';
308-
});
309-
310-
// just return the ids and the types. We'll find them all
311-
// in a single bulk operation in order to minimize HTTP requests
312-
if (relatedIds.length) {
286+
287+
// signal that we need to fetch it
313288
return {
314289
relatedType: relatedType,
315-
relatedIds: relatedIds
290+
relatedIds: [relatedId]
316291
};
317-
}
318-
}));
292+
}));
293+
}
294+
} else { // hasMany
295+
var relatedIds = extend(true, [], obj[field]);
296+
if (typeof relatedIds !== 'undefined' && relatedIds.length) {
297+
subTasks.push(Promise.resolve().then(function () {
298+
299+
// filter out all ids that are already in the foundObjects
300+
for (var i = relatedIds.length - 1; i >= 0; i--) {
301+
var relatedId = relatedIds[i];
302+
if (foundObjects.has(relatedType) &&
303+
foundObjects.get(relatedType).has(JSON.stringify(relatedId))) {
304+
delete relatedIds[i];
305+
}
306+
}
307+
relatedIds = relatedIds.filter(function (relatedId) {
308+
return typeof relatedId !== 'undefined';
309+
});
310+
311+
// just return the ids and the types. We'll find them all
312+
// in a single bulk operation in order to minimize HTTP requests
313+
if (relatedIds.length) {
314+
return {
315+
relatedType: relatedType,
316+
relatedIds: relatedIds
317+
};
318+
}
319+
}));
320+
}
319321
}
320-
}
321-
});
322+
});
323+
}
322324
return Promise.all(subTasks);
323325
});
324326
return Promise.all(tasks);
@@ -331,7 +333,7 @@ exports.setSchema = function (schema) {
331333
return;
332334
}
333335
typesToIds[fetchTask.relatedType] =
334-
(typesToIds[fetchTask.relatedType] || []).concat(fetchTask.relatedIds);
336+
(typesToIds[fetchTask.relatedType] || []).concat(fetchTask.relatedIds);
335337
});
336338
});
337339

@@ -417,41 +419,41 @@ exports.setSchema = function (schema) {
417419
if (!defaultType) {
418420
var matchingSchemaTypes = schema.filter(
419421
function (schemaType) { return schemaType.documentType === type; });
420-
if (matchingSchemaTypes.length > 0) {
421-
type = matchingSchemaTypes[0].singular;
422+
if (matchingSchemaTypes.length > 0) {
423+
type = matchingSchemaTypes[0].singular;
424+
}
422425
}
426+
427+
return {
428+
type: type,
429+
id: relId
430+
};
423431
}
424432

425-
return {
426-
type: type,
427-
id: relId
428-
};
429-
}
433+
function makeDocID(obj) {
434+
var type = obj.type;
430435

431-
function makeDocID(obj) {
432-
var type = obj.type;
436+
var typeInfo = keysToSchemas.get(type);
437+
if (typeInfo) {
438+
type = typeInfo.documentType;
439+
}
433440

434-
var typeInfo = keysToSchemas.get(type);
435-
if (typeInfo) {
436-
type = typeInfo.documentType;
441+
return serialize(type, obj.id);
437442
}
438443

439-
return serialize(type, obj.id);
440-
}
441-
442-
db.rel = {
443-
save: save,
444-
find: find,
445-
del: del,
446-
getAttachment: getAttachment,
447-
putAttachment: putAttachment,
448-
removeAttachment: removeAttachment,
449-
parseDocID: parseDocID,
450-
makeDocID: makeDocID
444+
db.rel = {
445+
save: save,
446+
find: find,
447+
del: del,
448+
getAttachment: getAttachment,
449+
putAttachment: putAttachment,
450+
removeAttachment: removeAttachment,
451+
parseDocID: parseDocID,
452+
makeDocID: makeDocID
453+
};
451454
};
452-
};
453455

454-
/* istanbul ignore next */
455-
if (typeof window !== 'undefined' && window.PouchDB) {
456-
window.PouchDB.plugin(exports);
457-
}
456+
/* istanbul ignore next */
457+
if (typeof window !== 'undefined' && window.PouchDB) {
458+
window.PouchDB.plugin(exports);
459+
}

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1990,7 +1990,7 @@ function tests(dbName, dbType) {
19901990
singular: 'author',
19911991
plural: 'authors',
19921992
relations: {
1993-
books: {hasMany: {type: 'books', options: {async: false}}}
1993+
books: {hasMany: 'books'}
19941994
}
19951995
},
19961996
{

0 commit comments

Comments
 (0)