eventsourced
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -10,5 +10,6 @@ const EventEmitter = require('events'); | ||
*/ | ||
const cqrs = Symbol(); | ||
const conf = Symbol(); | ||
const es = Symbol(); | ||
const emitter = Symbol(); | ||
const cqrs = Symbol(); | ||
@@ -57,3 +58,4 @@ /** | ||
class Entity { | ||
constructor(events = [], options = {}) { | ||
constructor(events = [], config = {}) { | ||
this[conf] = config; | ||
this[es] = {}; | ||
@@ -68,6 +70,3 @@ this[es].version = 0; | ||
Entity.commands(this).forEach(command => { | ||
Entity.registerCommand(this, command, this[command]); | ||
}); | ||
Object.assign(this[es].mappings, options.mappings); | ||
Entity.registerCommands(this); | ||
@@ -92,3 +91,3 @@ const proxy = new Proxy(this, traps); | ||
*/ | ||
static commands(entity) { | ||
static getMethodsOf(entity) { | ||
const prototype = Object.getPrototypeOf(entity); | ||
@@ -101,2 +100,24 @@ const commands = Object.getOwnPropertyNames(prototype); | ||
/** | ||
* Get a list of registered commands from an entity instance. | ||
* | ||
* @param {Entity} entity The entity being acted on. | ||
*/ | ||
static getRegisteredCommandsOf(entity) { | ||
return Object.getOwnPropertyNames(entity[cqrs].commands); | ||
} | ||
/** | ||
* Get a list of registered commands from an entity instance. | ||
* | ||
* @param {Entity} entity The entity being acted on. | ||
*/ | ||
static registerCommands(entity) { | ||
Entity.getMethodsOf(entity).forEach(method => { | ||
Entity.registerCommand(entity, method, entity[method]); | ||
}); | ||
Object.assign(entity[es].mappings, entity[conf].mappings); | ||
return Entity.getRegisteredCommandsOf(entity); | ||
} | ||
/** | ||
* Register a command. Here we take a function and register it under the CQRS | ||
@@ -103,0 +124,0 @@ * property in the target using the passed command name. Additionaly, the |
@@ -16,7 +16,2 @@ const Entity = require('.'); | ||
class TestEntity extends Entity { | ||
constructor(events, options) { | ||
super(events, options); | ||
this.name = fixtures.a.name; | ||
this.email = fixtures.a.email; | ||
} | ||
rename(name) { | ||
@@ -42,63 +37,41 @@ this.name = name; | ||
tap.equals(Entity.inspect(a).version, 0, 'Instance version should be 0'); | ||
tap.equals(a.name, fixtures.a.name, `Instance name should be ${fixtures.a.name}`); | ||
tap.equals(Entity.inspect(a).version, 0, 'Version should be 0.'); | ||
a.on('renamed', () => { | ||
tap.pass('Should emit "renamed" event'); | ||
tap.equals(Entity.inspect(a).version, 1, 'Version should be 1.'); | ||
tap.equals(a.name, fixtures.b.name, `Name should be ${fixtures.b.name}.`); | ||
}); | ||
a.rename(fixtures.b.name); | ||
tap.equals(Entity.inspect(a).version, 1, 'Instance version should be 1'); | ||
tap.equals(a.name, fixtures.b.name, `Instance name should be ${fixtures.b.name}`); | ||
a.save(); | ||
tap.equals(Entity.inspect(a).version, 2, 'Instance version should be 2'); | ||
tap.equals(Entity.inspect(a).history.length, 2, 'Instance history should contain 2 entries'); | ||
tap.equals(Entity.inspect(a).version, 2, 'Version should be 2.'); | ||
tap.equals(Entity.inspect(a).history.length, 2, 'History should contain 2 entries.'); | ||
a.touch(); | ||
tap.equals(Entity.inspect(a).version, 2, 'Instance version should be 2'); | ||
tap.equals(Entity.inspect(a).history.length, 2, 'Instance history should contain 2 entries'); | ||
tap.equals(Entity.inspect(a).version, 2, 'Version should be 2.'); | ||
tap.equals(Entity.inspect(a).history.length, 2, 'History should contain 2 entries.'); | ||
tap.same(Entity.snapshot(a), { | ||
name: fixtures.b.name, | ||
email: fixtures.a.email, | ||
foo: 'bar', | ||
}, 'Instance snapshot should...'); | ||
}, 'Snapshot should contain expected data.'); | ||
// tap.test('Instance should emit "saved" event', t => { | ||
// i.on('saved', () => { | ||
// t.equals(Entity.version(i), 1, 'Instance version should be 1'); | ||
// t.equals(Entity.history(i).length, 1, 'Instance history should have one event'); | ||
// // t.same(Entity.snapshot(i), { | ||
// // version: 1, | ||
// // changeset: i, | ||
// // }, 'Instance snapshot should...'); | ||
// t.end(); | ||
// }); | ||
// i.save(); | ||
// }); | ||
// | ||
// tap.test('Instance should emit "renamed" event', t => { | ||
// i.on('renamed', () => { | ||
// t.equals(Entity.version(i), 2, 'Instance version should be 2'); | ||
// t.equals(Entity.history(i).length, 2, 'Instance history should have two events'); | ||
// // t.same(Entity.snapshot(i), { | ||
// // version: 2, | ||
// // changeset: i, | ||
// // }, 'Instance snapshot should...'); | ||
// t.end(); | ||
// }); | ||
// i.rename('Peter'); | ||
// }); | ||
// | ||
// tap.test('Instance should emit "touched" event', t => { | ||
// i.on('touched', () => { | ||
// t.equals(Entity.version(i), 3, 'Instance version should be 3'); | ||
// t.equals(Entity.history(i).length, 3, 'Instance history should have three events'); | ||
// // t.same(Entity.snapshot(i), { | ||
// // version: 3, | ||
// // changeset: i, | ||
// // }, 'Instance snapshot should...'); | ||
// t.end(); | ||
// }); | ||
// i.touch(); | ||
// }); | ||
// | ||
tap.test('Manually registering a command', t => { | ||
Entity.registerCommand(a, 'fix', function cmd() { | ||
this.fixed = true; | ||
}); | ||
const cmdInRegistry = (Entity.getRegisteredCommandsOf(a).indexOf('fix') > -1); | ||
t.ok(cmdInRegistry, 'Successfully registers a command.'); | ||
a.on('fixed', () => { | ||
t.pass('Command fires expected event.'); | ||
t.equals(Entity.inspect(a).version, 3, 'Command increases version.'); | ||
t.equals(a.fixed, true, 'Command changes state.'); | ||
t.end(); | ||
}); | ||
a.fix(); | ||
}); | ||
// tap.test('Registering a command', t => { | ||
@@ -105,0 +78,0 @@ // Entity.command(i, 'fix', function cmd() { |
{ | ||
"name": "eventsourced", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "An Event Sourcing library for Node", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -39,11 +39,2 @@ # Event Sourced | ||
/** | ||
* The constructor is required now but will be removed because it makes no | ||
* sense in an event sourced entity. | ||
*/ | ||
constructor(events, options) { | ||
super(events, options); | ||
this.name = 'Luis'; | ||
this.email = 'lgomez@example.com'; | ||
} | ||
/** | ||
* Commands change state and return undefined or null. | ||
@@ -50,0 +41,0 @@ */ |
15366
329
86