lk-dispatch
v1.1.0
Published
Canonical way to dispatch events to kinesis in the lk-architecture
Downloads
11
Readme
lk-dispatch
Canonical way to dispatch events to kinesis in the lk-architecture.
Usage
import {Kinesis} from "aws-sdk";
import getDispatch from "lk-dispatch";
const kinesis = new Kinesis();
const dispatch = getDispatch({
kinesisClient: kinesis,
kinesisStream: "entrypoint",
producerId: "server@hostname"
});
const eventType = "eventType";
const eventData = {
key: "value"
};
const eventOptions = {
sourceUserId: "userId",
partitionKey: "partitionKey"
};
dispatch(eventType, eventData, eventOptions)
.then(event => {
console.log(`Event ${event.id} inserted in kinesis`);
});
Events in the lk-architecture
An event in the lk-architecture is a json object which conforms to the following json schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string"
},
"timestamp": {
"type": "string",
"format": "date-time"
},
"data": {
"type": "object"
},
"source": {
"type": "object",
"properties": {
"kinesisPartitionKey": {
"type": "string"
},
"producerId": {
"type": "string"
},
"producerId": {
"type": "string"
}
}
}
}
}
Example event:
const event = {
id: "f47ac10b-58cc-4372-a567-0e02b2c3d479",
type: "eventType",
data: {/* whatever */},
timestamp: "2016-01-01T00:00:00.000Z",
source: {
userId: "userId",
producerId: "producerId",
kinesisPartitionKey: "kinesisPartitionKey"
}
};
Api
getDispatch(options)
Get the dispatch function.
Params
options
:kinesisClient
:AWS.Kinesis
instancekinesisStream
: name of the kinesis stream where events will be publishedproducerId
: a token to identify where events come from (will end up inevent.source.producerId
)
Return value
The dispatch function.
dispatch(type, data, options)
The dispatch function, publishes event to the configured kinesis stream.
Params
type
: the type of the eventdata
: the data of the eventoptions
(optional):sourceUserId (optional)
: the id of the user which triggered the operation that generated the eventpartitionKey
(optional): kinesis stream partition where to publish the event
Return value
A Promise that will be resolved with the published event when said event has been successfully published to kinesis.