Giter Club home page Giter Club logo

kinesis-events's Introduction

kinesis-events

npm npm David Travis license Beerpay

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 = require('kinesis-events');

// Lambda function handler
exports.handler = async event => {
    // Parse the records
    const result = kinesisEvents.parse(event);
    
    // Check for errors (optional)
    if(result.hasErrors) {
        console.error('There are errors while parsing, ending process...');
        process.exit(1);
    }
    
    result.records.forEach(record => {
        //... iterate through the parsed records
    });
};

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 = require('kinesis-events');

// Advanced usage
const { KinesisEvents } = require('kinesis-events');
const kinesisEvents = new KinesisEvents({
    // options...
});

ParseError ⇐ Error

Custom error that is generated when there is a parsing error.

Kind: global class
Extends: Error

parseError.payload : String

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.

kinesisEvents.options : Object

Options object for KinesisEvents. Allows overridding options after instantiation.

Kind: instance property of KinesisEvents
Example

kinesisEvents.options.transform = function(record, index) {
    // transform record...
    return record;
};

kinesisEvents.ParseError : ParseError

Access to the ParseError class.

Kind: instance property of KinesisEvents
Read only: true

kinesisEvents.parse(records, [json]) ⇒ RecordSet

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.parse(event.Records);

result.records.forEach(record => {
    // do something with each record...
});

RecordSet

A set of parsed records with additional functionality.

Kind: global class

recordSet.records : Array

The records within this record set.

Kind: instance property of RecordSet

recordSet.failed : Array.<ParseError>

List of failed records (ParseError).

Kind: instance property of RecordSet

recordSet.length : Number

The total number of parsed records in the record set.

Kind: instance property of RecordSet
Read only: true

recordSet.hasErrors : Boolean

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 } = require('kinesis-events');
const kinesisEvents = new KinesisEvents({
    transform: (record, index) => {
        if(record.firstName && record.lastName) {
            // example, remove record if data is missing
            return null;
        }
        
        record.someCustomProperty = '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.

kinesis-events's People

Contributors

kyleross avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.