diff --git a/lib/index.js b/lib/index.js index 2f7419a..fe47dc9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -257,68 +257,70 @@ exports.setSchema = function (schema) { foundObjects.get(type).set(JSON.stringify(obj.id), obj); - // fetch all relations var subTasks = []; - Object.keys(typeInfo.relations || {}).forEach(function (field) { - var relationDef = typeInfo.relations[field]; - var relationType = Object.keys(relationDef)[0]; - var relatedType = relationDef[relationType]; - if (typeof relatedType !== 'string') { - var relationOptions = relatedType.options; - var async = idOrIds && idOrIds.async; - if (async || (relationOptions && relationOptions.async && (async === undefined))) { - return; - } - relatedType = relatedType.type; - } - if (relationType === 'belongsTo') { - var relatedId = obj[field]; - if (typeof relatedId !== 'undefined') { - subTasks.push(Promise.resolve().then(function () { - - // short-circuit if it's already in the foundObjects - // else we could get caught in an infinite loop - if (foundObjects.has(relatedType) && - foundObjects.get(relatedType).has(JSON.stringify(relatedId))) { - return; - } - - // signal that we need to fetch it - return { - relatedType: relatedType, - relatedIds: [relatedId] - }; - })); + + if (!idOrIds || !idOrIds.async) { + // fetch all relations + Object.keys(typeInfo.relations || {}).forEach(function (field) { + var relationDef = typeInfo.relations[field]; + var relationType = Object.keys(relationDef)[0]; + var relatedType = relationDef[relationType]; + if (typeof relatedType !== 'string') { + var relationOptions = relatedType.options; + if (relationOptions && relationOptions.async && typeof idOrIds === 'undefined') { + return; + } + relatedType = relatedType.type; } - } else { // hasMany - var relatedIds = extend(true, [], obj[field]); - if (typeof relatedIds !== 'undefined' && relatedIds.length) { - subTasks.push(Promise.resolve().then(function () { - - // filter out all ids that are already in the foundObjects - for (var i = relatedIds.length - 1; i >= 0; i--) { - var relatedId = relatedIds[i]; + if (relationType === 'belongsTo') { + var relatedId = obj[field]; + if (typeof relatedId !== 'undefined') { + subTasks.push(Promise.resolve().then(function () { + + // short-circuit if it's already in the foundObjects + // else we could get caught in an infinite loop if (foundObjects.has(relatedType) && foundObjects.get(relatedType).has(JSON.stringify(relatedId))) { - delete relatedIds[i]; + return; } - } - relatedIds = relatedIds.filter(function (relatedId) { - return typeof relatedId !== 'undefined'; - }); - - // just return the ids and the types. We'll find them all - // in a single bulk operation in order to minimize HTTP requests - if (relatedIds.length) { + + // signal that we need to fetch it return { relatedType: relatedType, - relatedIds: relatedIds + relatedIds: [relatedId] }; - } - })); + })); + } + } else { // hasMany + var relatedIds = extend(true, [], obj[field]); + if (typeof relatedIds !== 'undefined' && relatedIds.length) { + subTasks.push(Promise.resolve().then(function () { + + // filter out all ids that are already in the foundObjects + for (var i = relatedIds.length - 1; i >= 0; i--) { + var relatedId = relatedIds[i]; + if (foundObjects.has(relatedType) && + foundObjects.get(relatedType).has(JSON.stringify(relatedId))) { + delete relatedIds[i]; + } + } + relatedIds = relatedIds.filter(function (relatedId) { + return typeof relatedId !== 'undefined'; + }); + + // just return the ids and the types. We'll find them all + // in a single bulk operation in order to minimize HTTP requests + if (relatedIds.length) { + return { + relatedType: relatedType, + relatedIds: relatedIds + }; + } + })); + } } - } - }); + }); + } return Promise.all(subTasks); }); return Promise.all(tasks); diff --git a/test/test.js b/test/test.js index e60bfcf..079120b 100644 --- a/test/test.js +++ b/test/test.js @@ -1990,7 +1990,7 @@ function tests(dbName, dbType) { singular: 'author', plural: 'authors', relations: { - books: {hasMany: {type: 'books', options: {async: false}}} + books: {hasMany: 'books'} } }, {