Permalink
Browse files

fix(entitiy.manager): throw when trying to register non-entity

  • Loading branch information...
doktordirk committed Aug 18, 2016
1 parent 00e13e1 commit 46c6bdbe165ef4565b83433dae774a746aec35ef
Showing with 38 additions and 11 deletions.
  1. +5 −1 doc/configuration.md
  2. +16 −10 src/entity-manager.js
  3. +17 −0 test/entity-manager.spec.js
@@ -7,8 +7,12 @@ Aurelia-orm uses [aurelia-api](https://github.com/SpoonX/aurelia-api) to talk to
Upon configuration you can register your entities. Code speaks louder than words, so here's an example:
```js
// Import your entities
// Bulk import your entities:
import * as entities from 'config/entities';
/* Note:
* This might not work with all module loaders as additional entries can be added to 'entities'.
* In this case, either import your entities by name or filter them using Entity.isPrototypeOf(YourEntity) === true .
*/
export function configure(aurelia) {
aurelia.use
@@ -21,35 +21,41 @@ export class EntityManager {
}
/**
* Register an array of entity references.
* Register an array of entity classes.
*
* @param {Entity[]|Entity} entities Array or object of entities.
* @param {function[]|function} Entity classes array or object of Entity constructors.
*
* @return {EntityManager} this
* @chainable
*/
registerEntities(entities) {
for (let reference in entities) {
if (!entities.hasOwnProperty(reference)) {
registerEntities(EntityClasses) {
for (let property in EntityClasses) {
if (!EntityClasses.hasOwnProperty(property)) {
continue;
}
this.registerEntity(entities[reference]);
this.registerEntity(EntityClasses[property]);
}
return this;
}
/**
* Register an Entity reference.
* Register an Entity class.
*
* @param {Entity} entity
* @param {function} EntityClass
*
* @return {EntityManager} this
* @chainable
*/
registerEntity(entity) {
this.entities[OrmMetadata.forTarget(entity).fetch('resource')] = entity;
registerEntity(EntityClass) {
if (!Entity.isPrototypeOf(EntityClass)) {
throw new Error(`Trying to register non-Entity with aurelia-orm.
Are you using 'import *' to load your entities?
<http://aurelia-orm.spoonx.org/configuration.html> `);
}
this.entities[OrmMetadata.forTarget(EntityClass).fetch('resource')] = EntityClass;
return this;
}
@@ -37,6 +37,23 @@ describe('EntityManager', function() {
expect(entityManager.entities).toEqual({'with-resource': WithResource});
});
it('Should throw when register with non-Entity', function() {
let entityManager = new EntityManager(new Container());
class Wrong {}
let failClass = () => entityManager.registerEntity(Wrong);
let failObject = () => entityManager.registerEntity({type: 'not an entity'});
let failBoolean = () => entityManager.registerEntity(true);
let failNumber = () => entityManager.registerEntity(1);
let failString = () => entityManager.registerEntity('string');
expect(failClass).toThrow();
expect(failObject).toThrow();
expect(failBoolean).toThrow();
expect(failNumber).toThrow();
expect(failString).toThrow();
});
});
describe('.getRepository()', function() {

0 comments on commit 46c6bdb

Please sign in to comment.