Permalink
Browse files

feat(entity-manager): Set repository on new entities and allow easier…

… entity registering
  • Loading branch information...
RWOverdijk committed Jan 6, 2016
1 parent f580901 commit 2ae8ad1ad4309363dc58cd8c05715bfdb872b0eb
Showing with 16 additions and 13 deletions.
  1. +16 −13 src/entity-manager.js
@@ -21,14 +21,18 @@ export class EntityManager {
/**
* Register an array of entity references.
*
* @param {Entity[]} entities
* @param {Entity[]|Entity} entities Array or object of entities.
*
* @return {EntityManager}
*/
registerEntities(entities) {
entities.forEach(entity => {
this.registerEntity(entity);
});
for (let reference in entities) {
if (!entities.hasOwnProperty(reference)) {
continue;
}
this.registerEntity(entities[reference]);
}
return this;
}
@@ -49,7 +53,7 @@ export class EntityManager {
/**
* Get a repository instance.
*
* @param {Entity} entity
* @param {Entity|string} entity
*
* @return {Repository}
*
@@ -125,17 +129,16 @@ export class EntityManager {
getEntity(entity) {
let reference = this.resolveEntityReference(entity);
let instance = this.container.get(reference);
let resource = reference.getResource();
if (reference.getResource()) {
return instance.setResource(reference.getResource());
}
if (!resource) {
if (typeof entity !== 'string') {
throw new Error('Unable to find resource for entity.');
}
if (typeof entity !== 'string') {
throw new Error('Unable to find resource for entity.');
resource = entity;
}
instance.setResource(entity);
return instance;
return instance.setResource(resource).setRepository(this.getRepository(resource));
}
}

0 comments on commit 2ae8ad1

Please sign in to comment.