Skip to content

Commit 95fe95f

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

File tree

2 files changed

+56
-54
lines changed

2 files changed

+56
-54
lines changed

lib/index.js

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -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) &&
302283
foundObjects.get(relatedType).has(JSON.stringify(relatedId))) {
303-
delete relatedIds[i];
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);

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)