npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sequelize-lambda

v1.0.6

Published

A small wrapper for configuring and managing Sequelize in AWS Lambda functions

Downloads

24

Readme

sequelize-lambda

A small wrapper for configuring and managing Sequelize in AWS Lambda functions.

Introduction

When using Sequelize from AWS Lambda, typically with RDS, there are some things to consider. This module provides a simple wrapper to simplify configuring Sequelize in Lambda functions to follow the best practices at https://sequelize.org/docs/v6/other-topics/aws-lambda/, by configuring pool options and managing connections at the start and end of function invocations.

Installation

npm install sequelize-lambda

Usage

  • SequelizeManager.create() wraps new Sequelize() to inject appropriate options for running in AWS Lambda. This also stores the reference to the created Sequelize object
  • SequelizeManager.get() returns any Sequelize instance created by SequelizeManager.create(), or undefined if not yet created
  • SequelizeManager.wrapHandler() wraps a Lambda handler function to perform connection cleanup at the start of the function and closing any pending connections at the end of the function.
  • SequelizeManager.init() and SequelizeManager.close() are used by SequelizeManager.wrapHandler but are exposed if for some reason they need to be called manually.

Since lambda containers may be re-used, it's a good idea to store a Sequelize instance and only create it once. This can either be done at function start, or lazily when the function needs to access sequelize. SequelizeManager handles this for you by keeping a static reference created by SequelizeManager.create(), accessible through SequelizeManager.get().

The following example adds a getSequelize() function that lazily obtains a sequelize instance, and wraps the Lambda handler function to handle connection issues.

  • If the Lambda container is used for the first time, nothing extra will happen at the start of the function invocation; the instance may be created lazily later on.

  • If the Lambda container is re-used, and the sequelize instance was already created in a previous invocation, the connection pool will be reset at start.

  • If an instance is created, the connection pool will be closed at end of function invocation.

  • Once an instance is created, appropriate pool options will be injected, along with an evict parameter derived from the approximate timeout of the Lambda function.

import {Handler} from 'aws-lambda';
import {Sequelize} from 'sequelize';
import SequelizeManager from 'sequelize-lambda';

function getSequelize() {
  // lazily create
  return SequelizeManager.get() || 
    SequelizeManager.create(Sequelize, 'mydatabase', 'myusername', 'mypassword', { ... });
}

const handler: Handler<SomeEvent, SomeResult> = (event, context) => {
  // ...
  
  // Do something with sequelize
  const sequelize = getSequelize();
  
  // ...
  // return some result 
};

// wrapped handler with code added at start end end of function invocation
export default SequelizeManager.wrapHandler(handler);

Note: Due to how TypeScript handles constructor parameters for overloaded constructors, the create function may be problematic to use. As an alternative, the register function is offered, to allow the client to invoke the constructor using the supplied sequelize pool options:

SequelizeManager.register(options => new Sequelize('mydatabase', 'myusername', 'mypassword', options));