Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions data-mapping-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var {QueryEntity} = require('@themost/query');
var {QueryField} = require('@themost/query');
var Q = require('q');
var {hasOwnProperty} = require('./has-own-property');
var {isObjectDeep} = require('./is-object');

class DataMappingExtender {
constructor(mapping) {
Expand Down Expand Up @@ -342,9 +343,20 @@ class DataMappingExtender {
if (_.isNil(childField)) {
return reject('The specified field cannot be found on child model');
}
var childFieldType = thisQueryable.model.context.model(childField.type);
var values = _.intersection(_.map(_.filter(arr, function(x) {
return hasOwnProperty(x, keyField);
}), function (x) { return x[keyField];}));
}), function (x) { return x[keyField];})).map(function(x) {
if (isObjectDeep(x)) {
if (childFieldType) {
return x[childFieldType.primaryKey];
}
throw new Error('The child item is an object but its type cannot determined.');
}
return x;
}).filter(function(x) {
return x != null;
});
if (values.length===0) {
return resolve();
}
Expand Down Expand Up @@ -421,9 +433,20 @@ class DataMappingExtender {
return reject('The specified field cannot be found on parent model');
}
var keyField = parentField.property || parentField.name;
var parentFieldType = thisQueryable.model.context.model(parentField.type);
var values = _.intersection(_.map(_.filter(arr, function(x) {
return hasOwnProperty(x, keyField);
}), function (x) { return x[keyField];}));
}), function (x) { return x[keyField];})).map(function(x) {
if (isObjectDeep(x)) {
if (parentFieldType) {
return x[parentFieldType.primaryKey];
}
throw new Error('The parent item is an object but its type cannot determined.');
}
return x;
}).filter(function(x) {
return x != null;
});
if (values.length===0) {
return resolve();
}
Expand Down Expand Up @@ -528,7 +551,7 @@ class DataMappingExtender {
});
return resolve();
}).catch(function(err) {
return resolve(err);
return reject(err);
});
});
});
Expand Down Expand Up @@ -872,11 +895,22 @@ class DataMappingOptimizedExtender extends DataMappingExtender {
return reject('The specified field cannot be found on parent model');
}
var keyField = parentField.property || parentField.name;
var parentFieldType = thisQueryable.model.context.model(parentField.type);
var values = _.intersection(_.map(_.filter(arr, function(x) {
return hasOwnProperty(x, keyField);
}), function (x) {
return x[keyField];
}));
})).map(function(x) {
if (isObjectDeep(x)) {
if (parentFieldType) {
return x[parentFieldType.primaryKey];
}
throw new Error('The parent item is an object but its type cannot determined.');
}
return x;
}).filter(function(x) {
return x != null;
});;
if (values.length===0) {
return resolve();
}
Expand Down Expand Up @@ -977,7 +1011,7 @@ class DataMappingOptimizedExtender extends DataMappingExtender {
});
return resolve();
}).catch(function(err) {
return resolve(err);
return reject(err);
});
});
});
Expand Down
Loading