5.0.0 • Published 4 years ago

@corva/node-generic-lambda v5.0.0

Weekly downloads
50
License
UNLICENSED
Repository
-
Last release
4 years ago

node-generic-lambda

Generic lambda class for constructing data apps. It provides base class to build more specific lambdas. This package defines general workflow

ToC

Requirements

  • node.js - >=12.0.0

Getting started

Installation

npm i @corva/node-generic-lambda

Features

  • Defines generic workflow
  • Logs errors

Workflow

Generic lambda has 3 phases:

  • pre-process
  • process
  • post-process
@startuml
(*) -->[{event}] "preProcess"

if "Exception?" then
  -->[true, {error}] "postProcess"
else
  -->[false, {event}] "process"
  -->[{data, error}] "postProcess"
endif
-->[End] (*)
@enduml

workflow diagram

Usually you need to define process function to handle the data. Pre-process is used to modify incoming data in some way. Post-process is used to handle processed data or/and handle and log errors that were thrown during pre-process or process.

Also it expose .run method that is used to launch processing.

Usage

@startuml
GenericLambda <|-- StreamLambda
GenericLambda <|-- ScheduledLambda
GenericLambda <|-- TaskLambda

class GenericLambda {
  {static} Error LambdaError
  {abstract} process({event, context})
  #preProcess({event, context})
  #postProcess({event, context, data, error})
  #run({event,context})
}

class StreamLambda {
  {abstract} process({event, context})
  #preProcess({event, context})
  #postProcess({event, context, data, error})
}

class ScheduledLambda {
  {abstract} process({event, context})
  #preProcess({event, context})
  #postProcess({event, context, data, error})
}

class TaskLambda {
  {abstract} process({event, context})
  #preProcess({event, context})
  #postProcess({event, context, data, error})
}

@enduml

usage diagram

Examples

const { GenericLambda } = require('@corva/node-generic-lambda');

class MyLambda extends GenericLambda {
  constructor({ customOptions, ...options }) {
    super(options);
    this.foo = customOptions.foo;
  }

  async preProcess({ event, context }) {
    await super.preProcess({ event, context });
    // you can modify event
    event.foo = this.foo;
    // and return it, so it will be passed to processor function instead of original
    return event;
    // or return undefined if you want to pass original event
  }

  async postProcess({ event, context, data, error }) {
    await super.postProcess({ event, context });
    if (error) {
      // handle error
      return;
    }
    // do something with data
  }
}