Skip to content

Commit

Permalink
Fixes async options when not specified from the schema
Browse files Browse the repository at this point in the history
  • Loading branch information
akofman committed May 7, 2016
1 parent ad712b2 commit c098dc7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 54 deletions.
108 changes: 55 additions & 53 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1990,7 +1990,7 @@ function tests(dbName, dbType) {
singular: 'author',
plural: 'authors',
relations: {
books: {hasMany: {type: 'books', options: {async: false}}}
books: {hasMany: 'books'}
}
},
{
Expand Down

0 comments on commit c098dc7

Please sign in to comment.