A Node.js Event Store client API wrapper using promises
yarn add geteventstore-promise
In your Node.js application:
const EventStore = require('geteventstore-promise');
const EventStore = require('geteventstore-promise');
const client = new EventStore.HTTPClient({
hostname: 'localhost',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
const EventStore = require('geteventstore-promise');
const client = new EventStore.HTTPClient({
protocol: 'https',
hostname: 'localhost',
port: 2113,
validateServer: true, //defaults to `true` when `protocol` is `https`, set to `false` when using self-signed certs
credentials: {
username: 'admin',
password: 'changeit'
}
});
Returns events from a given stream.
The name of the stream to read from.
If specified, the stream will be read starting at event number startPosition, otherwise 0 'head' will start reading from the back of the stream, if direction is specified as 'backward'
The number of events to be read, defaults to 1000, max of 4096
The direction to the read the stream. Can be either 'forward' or 'backward'. Defaults to 'forward'.
Resolve linked events. Defaults to true
Resolve linked events. Options: 'body' and 'rich'. Defaults to body
const EventStore = require('geteventstore-promise');
const client = new EventStore.HTTPClient({
hostname: 'localhost',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
// defaults for getEvents if not specified
const events = await client.getEvents('TestStream', 0, 1000, 'forward')
Returns all events from a given stream.
The name of the stream to read from.
The amount of events to read in each call to Event Store, defaults to 1000,
If specified, the stream will be read starting at event number startPosition, otherwise 0
Resolve linked events. Defaults to true
Resolve linked events. Options: 'body' and 'rich'. Defaults to body
const EventStore = require('geteventstore-promise');
const client = new EventStore.HTTPClient({
hostname: 'localhost',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
const allStreamEvents = await client.getAllStreamEvents('TestStream');
Returns read metadata and events from a given stream.
The name of the stream to read from.
If specified, the stream will be read starting at event number startPosition, otherwise 0 'head' will start reading from the back of the stream, if direction is specified as 'backward'
The number of events to be read, defaults to 1000, max of 4096
Resolve linked events. Defaults to true
Resolve linked events. Options: 'body' and 'rich'. Defaults to body
const EventStore = require('geteventstore-promise');
const client = new EventStore.HTTPClient({
hostname: 'localhost',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
// defaults for readEventsForward if not specified
const readResult = await client.readEventsForward('TestStream', 0, 1000)
Returns read metadata and events from a given stream.
The name of the stream to read from.
If specified, the stream will be read starting at event number startPosition, otherwise 0 'head' will start reading from the back of the stream, if direction is specified as 'backward'
The number of events to be read, defaults to 1000, max of 4096
Resolve linked events. Defaults to true
Resolve linked events. Options: 'body' and 'rich'. Defaults to body
const EventStore = require('geteventstore-promise');
const client = new EventStore.HTTPClient({
hostname: 'localhost',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
// defaults for readEventsBackward if not specified
const readResult = await client.readEventsBackward('TestStream', 0, 1000)
Writes a single event of a specific type to a stream.
The name of the stream to read from.
The type of event to save. Any string value is accepted.
The data to be contained in the event as a JSON object.
Any MetaData to be saved in the event as a JSON object.
Any options to be specified (as documented in GetEvent Store documentation). Default is simply ExpectedVersion = -2.
const EventStore = require('geteventstore-promise');
const { v4: generateEventId } = require('uuid');
const client = new EventStore.HTTPClient({
hostname: 'localhost',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
await client.writeEvent('TestStream-' + generateEventId(), 'TestEventType', { something: '123' });
const events = await client.getEvents(testStream);
Writes an array of Event Store ready events to a stream.
The name of the stream to read from.
The array of Event Store ready events to save.
You can call new EventStore.EventFactory().newEvent('TestType', {something: 123});
to get an Event Store ready event.
Any options to be specified (as documented in GetEvent Store documentation). Default is simply ExpectedVersion = -2.
const EventStore = require('geteventstore-promise');
const { v4: generateEventId } = require('uuid');
const client = new EventStore.HTTPClient({
hostname: 'localhost',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
const events = [new EventStore.EventFactory().newEvent('TestEventType', { something: '456'})];
await client.writeEvents('TestStream-' + generateEventId(), events);
const events = await client.getEvents(testStream);
Check if a stream exists, returns true or false.
The name of the stream to check.
const EventStore = require('geteventstore-promise');
const client = new EventStore.HTTPClient({
hostname: 'localhost',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
const exists = await client.checkStreamExists('ExistingProjectionStreamName');
Deletes a stream, fails the promise if stream does not exist.
The name of the stream to delete.
Hard delete the stream, defaults to false
const EventStore = require('geteventstore-promise');
const client = new EventStore.HTTPClient({
hostname: 'localhost',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
try {
await client.delete('ExistingStreamName');
} catch(err) {
// should only happen if something went wrong or the stream does not exist
console.log(err);
}
Performs Ping command, rejects promise if unsuccessful
const EventStore = require('geteventstore-promise');
const client = new EventStore.HTTPClient({
hostname: 'localhost',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
await client.ping();
Upsert the persistent subscription
The name of the subscription group
The stream name
The mode of the projection to create, defaults to 'continuous'
Tells the subscription to resolve link events.
Start the subscription from the position-th event in the stream.
Tells the backend to measure timings on the clients so statistics will contain histograms of them.
The amount of time the system should try to checkpoint after.
The size of the live buffer (in memory) before resorting to paging.
The size of the read batch when in paging mode.
The number of messages that should be buffered when in paging mode.
The maximum number of messages not checkpointed before forcing a checkpoint.
Sets the number of times a message should be retried before being considered a bad message.
Sets the maximum number of allowed subscribers
Sets the timeout for a client before the message will be retried.
The minimum number of messages to write a checkpoint for.
RoundRobin/DispatchToSingle/Pinned
Get events
The name of the subscription group
The stream name
Number of events to return(defaults to 1)
None, Content, Rich, Body, PrettyBody, TryHarder(defaults to 'Body')
Get specific subscriptions info
The name of the subscription group
The stream name
Get all subscriptions info for a stream
The stream name
Get all subscriptions info
start(projectionName)
The name of the projection
stop(projectionName)
The name of the projection
reset(projectionName)
The name of the projection
remove(projectionName)
The name of the projection
config(projectionName)
The name of the projection
getState(projectionName, options)
The name of the projection
Object, partition
used to specify the partition to query the state with. e.g. { partition: 1 }
getInfo(projectionName, includeConfig)
The name of the projection
Specify if we want to include the projection config in the projection info result set
enableAll()
disableAll()
getAllProjectionsInfo()
assert(projectionName, projectionContent, mode, enabled, checkpointsEnabled, emitEnabled, trackEmittedStreams)
The name of the projection
The content of the projection
The mode of the projection to create, defaults to 'continuous'
Projection enabled by default, defaults to true
Should enable checkpoints, defaults to true for continuous projections and false for onetime projections
Should enable emitting, defaults to false
Should track the emitted streams (tracking emitted streams enables you to delete a projection and all the streams that it has created), defaults to false
Returns the state of the Projection as a JSON object.
The name of the projection to get state of.
The name of the partition to retrieve.
const EventStore = require('geteventstore-promise');
const client = new EventStore.HTTPClient({
hostname: 'localhost',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
const projectionState = await client.projections.getState('TestProjection');
Sends scavenge command to Event Store.
If the promise is fulfilled then the scavenge command has been sent, it does not guarantee that the scavenge will be successful.
const EventStore = require('geteventstore-promise');
const client = new EventStore.HTTPClient({
hostname: 'localhost',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
await client.admin.scavenge();
console.log('Scavenge command sent!');
Sends shutdown command to Event Store.
If the promise is fulfilled then the shutdown command has been sent, it does not guarantee that the shutdown will be successful.
const EventStore = require('geteventstore-promise');
const client = new EventStore.HTTPClient({
hostname: 'localhost',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
await client.admin.shutdown();
console.log('Shutdown command sent!');
Uses the node-eventstore-client
as authored by nicdex
Github: https://github.com/nicdex/node-eventstore-client
const client = new EventStore.TCPClient({
hostname: 'localhost',
port: 1113,
credentials: {
username: 'admin',
password: 'changeit'
},
poolOptions: {
min: 0,
max: 10
}
});
const client = new EventStore.TCPClient({
hostname: 'localhost',
port: 1113,
useSslConnection: true,
validateServer: true, //defaults to `true` when `useSslConnection` is `true`, set to `false` when using self-signed certs
credentials: {
username: 'admin',
password: 'changeit'
},
poolOptions: {
min: 0,
max: 10
}
});
const { v4: generateId } = require('uuid');
const client = new EventStore.TCPClient({
hostname: 'localhost',
port: 1113,
credentials: {
username: 'admin',
password: 'changeit'
},
poolOptions: {
min: 0,
max: 10
},
connectionNameGenerator: () => `APP_NAME_${generateId()}`
});
const client = new EventStore.TCPClient({
gossipSeeds: [
{ hostname: '192.168.0.10', port: 2113 },
{ hostname: '192.168.0.11', port: 2113 },
{ hostname: '192.168.0.12', port: 2113 }
],
credentials: {
username: 'admin',
password: 'changeit'
},
poolOptions: {
min: 0,
max: 10
}
});
const client = new EventStore.TCPClient({
protocol: 'discover',
hostname: 'my.host',
port: 2113,
credentials: {
username: 'admin',
password: 'changeit'
},
poolOptions: {
min: 0,
max: 10
}
});
Close all active connections.
Returns all events from a given stream by Event Types.
The name of the stream to read from.
An array of event types to filter by.
If specified, the stream will be read starting at event number startPosition, otherwise 0
The number of events to be read, defaults to 1000, max of 4096
The direction to the read the stream. Can be either 'forward' or 'backward'. Defaults to 'forward'.
Resolve linked events. Defaults to true
const EventStore = require('geteventstore-promise');
const client = new EventStore.TCPClient({
hostname: 'localhost',
port: 1113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
const eventsByType = await client.getEventsByType('TestStream', ['TestType']);
Returns all events from a given stream.
The name of the stream to read from.
The amount of events to read in each call to Event Store, defaults to 1000,
If specified, the stream will be read starting at event number startPosition, otherwise 0
Resolve linked events. Defaults to true
const EventStore = require('geteventstore-promise');
const client = new EventStore.TCPClient({
hostname: 'localhost',
port: 1113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
const allStreamEvents = await client.getAllStreamEvents('TestStream');
Subscribes to a Stream (live subscription)
The name of the stream to read from.
function
function
Resolve linked events
const EventStore = require('geteventstore-promise');
const client = new EventStore.TCPClient({
hostname: 'localhost',
port: 1113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
function onEventAppeared(subscription, ev) {
processedEventCount++;
return;
};
function onDropped(subscription, reason, error) {
};
await client.subscribeToStream('TestStream', onEventAppeared, onDropped, false);
Subscribes to a Stream from a given event number (Catch up Subscription)
The name of the stream to read from.
The event number to subscribe from
function
function
function
resolveLinkTos - Whether or not to resolve link events
maxLiveQueueSize - The max amount to buffer when processing from live subscription
readBatchSize - The number of events to read per batch when reading history
debug - in debug mode(true/false)
const EventStore = require('geteventstore-promise');
const client = new EventStore.TCPClient({
hostname: 'localhost',
port: 1113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
let processedEventCount = 0;
function onEventAppeared(subscription, ev) {
processedEventCount++;
return;
};
function onLiveProcessingStarted() {
return;
}
function onDropped(subscription, reason, error) {
};
await client.subscribeToStreamFrom('TestStream', 0, onEventAppeared, onLiveProcessingStarted,onDropped);
Returns an events enumerator on which events can be iterated.
The name of the stream to read from.
The direction to the read the stream. Can be either 'forward' or 'backward'. Defaults to 'forward'.
Resolve linked events. Defaults to true
The number of events to read per enumeration.
const EventStore = require('geteventstore-promise');
const client = new EventStore.TCPClient({
hostname: 'localhost',
port: 1113,
credentials: {
username: 'admin',
password: 'changeit'
}
});
const streamName = 'TestStream';
const enumerator = client.eventEnumerator(streamName);
const result = await enumerator.next(20);
//result
// {
// isEndOfStream: true/false,
// events: [ ..., ..., ... ]
// }