forest-express
Version:
Official package for all Forest Express Lianas
39 lines (37 loc) • 1.47 kB
JavaScript
;
var P = require('bluebird');
function isArray(object) {
return object && Array.isArray(object);
}
module.exports = {
schemas: {},
_concat: function _concat(left, right) {
var leftArray = left || [];
var rightArray = right || [];
return leftArray.concat(rightArray);
},
perform: function perform(implementation, integrator, models, opts) {
var _this = this;
return P.each(models, function (model) {
return implementation.SchemaAdapter(model, opts).then(function (schema) {
integrator.defineFields(model, schema);
integrator.defineSegments(model, schema);
schema.isSearchable = true;
return schema;
}).then(function (schema) {
var modelName = implementation.getModelName(model);
if (_this.schemas[modelName]) {
var currentSchema = _this.schemas[modelName];
schema.fields = _this._concat(schema.fields, currentSchema.fields);
schema.actions = _this._concat(schema.actions, currentSchema.actions);
schema.segments = _this._concat(schema.segments, currentSchema.segments);
// Set this value only if searchFields property as been declared somewhere.
if (isArray(schema.searchFields) || isArray(currentSchema.searchFields)) {
schema.searchFields = _this._concat(schema.searchFields, currentSchema.searchFields);
}
}
_this.schemas[modelName] = schema;
});
});
}
};