
Security News
US Government Forces Anthropic to Pull Claude Fable Days After Launch
Anthropic says the directive cited national security concerns over a narrow jailbreak, but offered no specific technical details.
events-sourcing
Advanced tools
Event sourcing library. Just a workaround to play with event sourcing and learn more about it.
Event sourcing library.
This library allow you to create projection object that result from a list of events. You can also revert events and time travels at any point in time.
To compute your events you need to attach reducer for each event types. Your reducer must be pure and return an object that reflect the update that you want to apply to your projection.
optional array of events. Default value: [].optional object. Default value: { sequence: 0, values:{} }optional array. Default value: []const evsc = require('events-sourcing');
const state = { sequence: 0, balance: 0 };
const events = [
{
// Event1
sequence: 1 // Beware if you try to add a sequence number which is not the current state sequence + 1 this will throw an error.
type: 'add:money',
payload: { amount: 10 },
},
];
const reducers = [{
event: 'add:money',
reducer: (payload, state) => {
return { balance: state.balance + payload.amount };
},
}];
const projection = evsc.createProjection(events, state, reducers);
projection.sequence(); // => 1
projection.values(); // => { balance: 10 }
projection.events(); // => [ Event1 ]
This will return a projection object which is the results of all events that compose an entity. A projection will expose these functions :
addReducer(eventType, reducer): Will attach a reducer function to an event type.addEvent(eventType, payload): Will add a new event to the projection and refresh his state. Throw an error if no reducer is found.goTo(n): Go to the entity at the time of the event n.revert(n): Revert n events on the projection.apply(n): Apply n next events on the projection.sequence(): Return the current sequence number of the projection.values(): Return the current state values.events(): Return the events list of the projection.reducers(): Return the reducers object map.This method will attach a reducer function to an event type.
You can replace a reducer by a new version but in this case you will need to replay all events to get the state computed with your new reducer implementation
const evsc = require('events-sourcing');
const projection = evsc.createProjection([], { balance: 0 });
projection.addReducer('add:money', (payload, state) => {
return {
balance: state.balance + payload.amount,
};
});
projection.addEvent('add:money', { amount: 10 });
projection.values(); // { balance: 10 }
projection.sequence(); // 1
This method will create an event push it in the projection events array and apply it to the projection.
const evsc = require('events-sourcing');
const events = [event1, event2, event3, event4];
const projection = evsc.createProjection(events, state); // state is optional
projection.sequence(); // => 4
projection.values(); // => State of the projection after event4.
projection.events(); // => [ event1, event2, event3, event4 ]
projection.addEvent('user:updated', { firstName: 'John' });
projection.sequence(); // => 5
projection.values(); // => { firstName: 'John', ...rest }
projection.events(); // => [ event1, event2, event3, event4, event5 ]
This method will revert or apply events until matching the targetSequence
const evsc = require('events-sourcing');
const events = [event1, event2, event3, event4];
const projection = evsc.createProjection(events, state);
projection.goTo(2);
projection.sequence(); // => 2
projection.values(); // => State of the projection after event sequence 2.
This method will apply n events from the projection
const evsc = require('events-sourcing');
const events = [event1, event2, event3, event4];
const projection = evsc.createProjection(events, state);
projection.goTo(2);
projection.sequence(); // 2
projection.apply(2); // Will reapply the event 3 and 4
projection.sequence(); // => 4
This method will revert n events from the projection
const evsc = require('events-sourcing');
const events = [event1, event2, event3, event4];
const projection = evsc.createProjection(events, state);
projection.revert(2); // Will revert event 3 and 4
projection.sequence(); // => 2
projection.values(); // => State of the projection after event sequence 2.
error.code === 'APPLY_EVENT_OUT_OF_SEQUENCE'To remove a key from a state make you reducer return this key undefined.
projection.values(); // => { firstName: 'John', lastName: 'Snow' }
projection.addReducer('user:remove:firstname', () => {
return {
firstname: undefined,
};
});
projection.addEvent('user:remove:firstname', {});
projection.values(); // => { lastName: 'Snow' }
evsc support nested objects. It use deepmerge for that.
projection.values(); // => { user : { firstName: 'John' } }
projection.addEvent('user:updated', { user: { lastName: 'Snow' } });
projection.values(); // => { user: { firstName: 'John', lastName: 'Snow' } }
FAQs
Event sourcing library. Just a workaround to play with event sourcing and learn more about it.
The npm package events-sourcing receives a total of 4 weekly downloads. As such, events-sourcing popularity was classified as not popular.
We found that events-sourcing demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Anthropic says the directive cited national security concerns over a narrow jailbreak, but offered no specific technical details.

Security News
A network of 152 Chrome live wallpaper extensions hid ad tracking and made extension-driven traffic look like Google search clicks.

Company News
Socket’s first CISO brings deep experience securing high-growth SaaS companies as open source supply chain threats accelerate.