kinesis-events
AWS Kinesis event parser and handler for Lambda consumers. Ability to parse kinesis events with error handling and JSON support. Supports Node 8.10+ on AWS Lambda.
Install
npm i --save kinesis-events
Usage
const kinesisEvents = ; // Lambda function handlerexportshandler = async { // Parse the records const result = kinesisEvents; // Check for errors (optional) ifresulthasErrors console; process; resultrecords;};
API Documentation
kinesis-events
KinesisEvents
⏏
kinesisEvents : Instance of the KinesisEvents class which is exported when calling require('kinesis-events')
.
For more advanced usage, you may create a new instance of KinesisEvents (see example below).
Kind: Exported KinesisEvents Instance
Example
const kinesisEvents = ; // Advanced usageconst KinesisEvents = ;const kinesisEvents = // options...;
Error
ParseError ⇐ Custom error that is generated when there is a parsing error.
Kind: global class
Extends: Error
String
parseError.payload : The original data that caused the error.
Kind: instance property of ParseError
KinesisEvents
Kind: global class
new KinesisEvents([options])
Constructor for KinesisEvents.
Param | Type | Default | Description |
---|---|---|---|
[options] | Object |
{} |
Options object to control certain features of KinesisEvents. |
[options.transform(record, index)] | function |
Optional transform function to call for each record. See Transform Function. |
Object
kinesisEvents.options : Options object for KinesisEvents. Allows overridding options after instantiation.
Kind: instance property of KinesisEvents
Example
kinesisEventsoptions { // transform record... return record;};
ParseError
kinesisEvents.ParseError : Access to the ParseError class.
Kind: instance property of KinesisEvents
Read only: true
RecordSet
kinesisEvents.parse(records, [json]) ⇒ Parses records from the incoming Kinesis event.
Kind: instance method of KinesisEvents
Returns: RecordSet
- New instance of RecordSet with the parsed records.
Param | Type | Default | Description |
---|---|---|---|
records | Array |
Event data (records) to parse. | |
[json] | Boolean |
true |
Enable/disable JSON parsing for each event. |
Example
const result = kinesisEvents; resultrecords;
RecordSet
A set of parsed records with additional functionality.
Kind: global class
- RecordSet
- recordSet.records :
Array
- recordSet.failed :
Array.<ParseError>
- recordSet.length :
Number
- recordSet.hasErrors :
Boolean
- recordSet.records :
Array
recordSet.records : The records within this record set.
Kind: instance property of RecordSet
Array.<ParseError>
recordSet.failed : List of failed records (ParseError).
Kind: instance property of RecordSet
Number
recordSet.length : The total number of parsed records in the record set.
Kind: instance property of RecordSet
Read only: true
Boolean
recordSet.hasErrors : Boolean flag if this record set has failed records.
Kind: instance property of RecordSet
Read only: true
Transform Function
New in v3.0.0, there is now an option to pass in a transform function that will allow you to transform the record before it is added to the RecordSet. This allows custom functionality or business logic to be implemented at a higher level.
The transform function takes 2 arguments, record
and index
. The function must return the transformed record in order for it to be added to the RecordSet. If the record is not returned from the function, it will be ignored.
const KinesisEvents = ;const kinesisEvents = { ifrecordfirstName && recordlastName // example, remove record if data is missing return null; recordsomeCustomProperty = 'some custom value'; return record; };
Tests
Tests are written and provided as part of the module. It requires mocha to be installed which is included as a devDependency
. You may run the tests by calling:
$ npm run test
License
MIT License. See License in the repository.