# @corva/node-generic-lambda

> Generic lambda class for constructing data apps.

Latest version **5.0.0** (published 2020-10-12) · UNLICENSED license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @corva/node-generic-lambda
pnpm add @corva/node-generic-lambda
yarn add @corva/node-generic-lambda
bun add @corva/node-generic-lambda
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2020-10-12 |
| First published | 2020-10-01 |
| Weekly downloads | 0 |
| License | UNLICENSED |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 416.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Corva LLC |
| Maintainers | semykin, dumavit1, jordanambra, villeti, ryandawson, bestfit |
| Keywords | lambda |

## Links

- npm: https://www.npmjs.com/package/@corva/node-generic-lambda
- npm.io page: https://npm.io/package/@corva/node-generic-lambda

## Dependencies (2)

- [@corva/node-logger](https://npm.io/package/@corva/node-logger.md) ^4.2.0
- [@corva/node-api-client](https://npm.io/package/@corva/node-api-client.md) ^4.2.1

## Recent versions

- 5.0.0 (latest) — 2020-10-12
- 4.2.0 — 2020-10-01

## README

# 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

- [node-generic-lambda](#node-generic-lambda)
  - [ToC](#toc)
  - [Requirements](#requirements)
  - [Getting started](#getting-started)
    - [Installation](#installation)
  - [Features](#features)
  - [Workflow](#workflow)
    - [Usage](#usage)
    - [Examples](#examples)

## 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

```plantuml
@startuml
(*) -->[{event}] "preProcess"

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

![workflow diagram](https://www.plantuml.com/plantuml/png/TOun3i8m34LtdyBA1AYz0dJfpeuwHCsFY5IIod4HKiJT2QL2B6oM_Px_xx9QqUKsqUwmvxOzNWhkYFeSsCo2iwGHEHkYuDdqZn6pXXHFXlMAIBmQAWiQBX19idefwrV4bB5ntjQvuT-03VhSYxDg6_xp9xhWwOtrqGrSsr9NTxNv2m00 'Workflow')

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

```plantuml
@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](https://www.plantuml.com/plantuml/png/nP91gi8m48RtFSN0spoXlO6BRiID2q4lC4q6BBP9cPcA47DthQcKWdNRNSZtaVp9FwKeilPja-t94TVcW6rb4RQFee2JCc6RThvoSo7RDsINbJFATQIPQL04vbB8088eQcqYx9WzGoBlzG2n4cKq6g5ZRqZaFz2DdEPWl5EwQzoyjB-EwVYJUz458GUBYZdGS6MokNSVQpebZa-O_Gk4TK9EOUObh9udDRvob9AS7ORw2G00 'Usage diagram')

### Examples

```js
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
  }
}
```

---
_Source: https://npm.io/package/@corva/node-generic-lambda · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
