Permalink
Browse files

fix(entity): builds a form friendly schema using the entity's metadata

  • Loading branch information...
bas080
bas080 committed Jun 15, 2016
1 parent acf7ee0 commit e68fdf836d4dc07827c548fc485ceb76e70f8867
Showing with 22 additions and 10 deletions.
  1. +22 −10 src/entity-schema.js
@@ -1,19 +1,31 @@
/**
* Takes an entity and uses it's metadata to generate a form-schema.
* Takes an entity and uses it's metadata to generate a form-schema. Entities
* behave like normal objects in the way one can get and set the values on
* it's properties.
*
* @param {object} entity
* @returns {object[]} the schema with the attributes.
*
* @returns {object[]} a schema consisting out of element objects
*/
export function entitySchema(entity) {
const keys = Object.keys(entity);
let typeMap = entity.getMeta().metadata.types;
let schema = [];
let metadata = entity.getMeta();
let types = metadata.fetch('types') || {};
let associations = metadata.fetch('associations');
let schema = [];

for (let key of Object.keys(entity)) {
if (key === '__validationReporter__') {continue;} /* should be fixed in orm */
let element = {
key: key,
type: types[key]
};

if (associations[key] && associations[key].type === 'collection') {
element.type = 'collection';
element.schema = entitySchema(entityManager.getEntity(key));
}

for (let key of keys) {
schema.push({
key : key,
type: typeMap[key]
});
schema.push(element);
}

return schema;

0 comments on commit e68fdf8

Please sign in to comment.