Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
fix(entitiy.manager): throw when trying to register non-entity
Browse files Browse the repository at this point in the history
  • Loading branch information
doktordirk committed Aug 18, 2016
1 parent 00e13e1 commit 46c6bdb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
6 changes: 5 additions & 1 deletion doc/configuration.md
Expand Up @@ -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: Upon configuration you can register your entities. Code speaks louder than words, so here's an example:


```js ```js
// Import your entities // Bulk import your entities:
import * as entities from 'config/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) { export function configure(aurelia) {
aurelia.use aurelia.use
Expand Down
26 changes: 16 additions & 10 deletions src/entity-manager.js
Expand Up @@ -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 * @return {EntityManager} this
* @chainable * @chainable
*/ */
registerEntities(entities) { registerEntities(EntityClasses) {
for (let reference in entities) { for (let property in EntityClasses) {
if (!entities.hasOwnProperty(reference)) { if (!EntityClasses.hasOwnProperty(property)) {
continue; continue;
} }


this.registerEntity(entities[reference]); this.registerEntity(EntityClasses[property]);
} }


return this; return this;
} }


/** /**
* Register an Entity reference. * Register an Entity class.
* *
* @param {Entity} entity * @param {function} EntityClass
* *
* @return {EntityManager} this * @return {EntityManager} this
* @chainable * @chainable
*/ */
registerEntity(entity) { registerEntity(EntityClass) {
this.entities[OrmMetadata.forTarget(entity).fetch('resource')] = entity; 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; return this;
} }
Expand Down
17 changes: 17 additions & 0 deletions test/entity-manager.spec.js
Expand Up @@ -37,6 +37,23 @@ describe('EntityManager', function() {


expect(entityManager.entities).toEqual({'with-resource': WithResource}); 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() { describe('.getRepository()', function() {
Expand Down

0 comments on commit 46c6bdb

Please sign in to comment.