forest-express
Version:
Official package for all Forest Express Lianas
57 lines (55 loc) • 2.49 kB
JavaScript
;
var _ = require('lodash');
var JSONAPISerializer = require('jsonapi-serializer').Serializer;
var _require = require('@forestadmin/context'),
inject = _require.inject;
// NOTICE: If a modification is made here, don't forget to replicate it in the toolbelt.
function SchemaSerializer() {
var _inject = inject(),
apimapSorter = _inject.apimapSorter;
// WARNING: Attributes declaration order is important for .forestadmin-schema.json format.
// It must be ordered by "importance" to ease the JSON reading for users.
var options = {
id: 'name',
// TODO: Remove nameOld attribute once the lianas versions older than 2.0.0 are minority.
attributes: ['name', 'nameOld', 'icon', 'integration', 'isReadOnly', 'isSearchable', 'isVirtual', 'onlyForRelationships', 'paginationType', 'fields', 'segments', 'actions'],
fields: {
attributes: ['field', 'type', 'column', 'defaultValue', 'enums', 'integration', 'isFilterable', 'isPrimaryKey', 'isReadOnly', 'isRequired', 'isSortable', 'isVirtual', 'reference', 'inverseOf', 'relationship', 'widget', 'validations', 'foreignAndPrimaryKey']
},
validations: {
attributes: ['message', 'type', 'value']
},
actions: {
ref: 'id',
attributes: ['name', 'type', 'baseUrl', 'endpoint', 'httpMethod', 'description', 'submitButtonLabel', 'redirect', 'download', 'fields', 'hooks'],
fields: {
attributes: ['field', 'type', 'defaultValue', 'enums', 'isRequired', 'isReadOnly', 'reference', 'description', 'position', 'widget', 'hook']
},
hooks: {
attributes: ['change', 'load']
}
},
segments: {
ref: 'id',
attributes: ['name']
},
keyForAttribute: 'camelCase'
};
this.options = options;
this.perform = function (collections, meta) {
// NOTICE: Action ids are defined concatenating the collection name and the object name to
// prevent object id conflicts between collections.
_.each(collections, function (collection) {
_.each(collection.actions, function (action) {
action.id = "".concat(collection.name, ".").concat(action.name);
});
_.each(collection.segments, function (segment) {
segment.id = "".concat(collection.name, ".").concat(segment.name);
});
});
options.meta = meta;
var schema = new JSONAPISerializer('collections', collections, options);
return apimapSorter.sort(schema);
};
}
module.exports = SchemaSerializer;