lkd-collection

1.0.3 • Public • Published

Build Status Coverage Status Dependency Status devDependency Status

lkd-collection

Implement collections in a lkd stack (Lambda, Kinesis, DynamoDB).

How it works

The library implements two lambda functions: a producer and a consumer.

The producer is invoked by API Gateway, and its job is to publish collection-related events into a Kinesis Stream. The consumer is invoked by Kinesis, and its job is to build a materialized view of the collection into DynamoDB, taking as source the collection-related events created by the producer.

Flow

Application event format

    {
        "data": {},
        "timestamp": 1437918813731,
        "type": "event-type"
    }

The data property can be any JSON document.

Collection events

Insert

API Gateway request body:

    {
        "method": "/collection-name/insert",
        "params": [{
            "elementKey": "elementValue"
        }]
    }

Resulting event sent to kinesis:

    {
        "data": {
            "element": {
                "elementKey": "elementValue"
            }
        },
        "timestamp": 1437918813731,
        "type": "/collection-name/insert"
    }

Resulting document inserted into dynamodb

{
    "id": "someId",
    "elementKey": "elementValue"
}

Remove

API Gateway request body:

    {
        "method": "/collection-name/remove",
        "params": ["elementId"]
    }

Resulting event sent to kinesis:

    {
        "data": {
            "id": "elementId"
        },
        "timestamp": 1437918813731,
        "type": "/collection-name/remove"
    }

Resuling operation on dynamodb

Removal of document with document.id === elementId.

Replace

API Gateway request body:

    {
        "method": "/collection-name/replace",
        "params": ["elementId", {
            "replacedKey": "replacedValue"
        }]
    }

Resulting event sent to kinesis:

    {
        "data": {
            "id": "elementId",
            "element": {
                "replacedKey": "replacedValue"
            }
        },
        "timestamp": 1437918813731,
        "type": "/collection-name/replace"
    }

Resulting document inserted into dynamodb (replaces the existing one)

{
    "id": "someId",
    "replacedKey": "replacedValue"
}

Example usage

/* Lambda function invoked by API Gateway */
import Collection from "lkd-collection";
 
var myCollection = new Collection({
    name: "myCollectionName"
    kinesisStreamName: "myStream"
});
 
export var handler = myCollection.producer;
/* Lambda function invoked by Kinesis */
import Collection from "lkd-collection";
 
var myCollection = new Collection({
    name: "myCollectionName",
    dynamodbTableName: "myTable"
});
 
export var handler = myCollection.consumer;

Readme

Keywords

none

Package Sidebar

Install

npm i lkd-collection

Weekly Downloads

0

Version

1.0.3

License

Apache-2.0

Last publish

Collaborators

  • pscanf