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

middy-rds

v5.0.0-rc.4

Published

Simple RDS manager for the middy framework

Downloads

292

Readme

Middy RDS Middleware

RDS provides seamless connection with database of your choice.

After initialization your database connection is accessible under:

middy((event, context) => {
  const { sql } = context
})

Mind that if you use knex you will also need driver of your choice (check docs), for PostgreSQL that would be:

yarn add {pg,postgres}
// or
npm install {pg,postgres}

Install

To install this middleware you can use NPM:

npm install --save middy-rds
npm install --save-dev @aws-sdk/rds-signer

Requires: @middy/core:>=4.0.0

Options

  • client (function) (required): client that you want to use when connecting to database of your choice. Designed to be used by knex.js. However, as long as your client is run as client(config), you can use other tools.
  • config (object) (required): configuration object passed as is to client (knex.js recommended), for more details check knex documentation
  • forceConnection (boolean) (default false): After creating the database client call SELECT 1 to for a connection to be opened
  • internalData (object) (optional): Pull values from middy internal storage into config.connection object.
  • cacheKey (string) (default rds): Cache key for the fetched data responses. Must be unique across all middleware.
  • cachePasswordKey (string) (default rds):Cache key for the fetched data response related to the password. Must match the cacheKey for the middleware that stores it.
  • cacheExpiry (number) (default -1): How long fetch data responses should be cached for. -1: cache forever, 0: never cache, n: cache for n ms.

Note:

  • config.connection defaults to:
{
  ssl: {
    rejectUnauthorized: true,
    checkServerIdentity: (host, cert) => {
      const error = tls.checkServerIdentity(host, cert)
      if (error && !cert.subject.CN.endsWith('.rds.amazonaws.com')) {
         return error
      }
    }
  }
}

If your lambda is timing out, likely your database connections are keeping the event loop open. Check out do-not-wait-for-empty-event-loop middleware to resolve this.

Sample usage

Minimal configuration

pg

import rdsMiddleware from 'middy-rds/pg'
import ca from 'middy-rds/ca'

import capturePostgres from 'aws-xray-sdk-postgres'
import pgClient from 'pg'

const pg = capturePostgres(pgClient)

const handler = middy(async (event, context) => {
  const { sql } = context
  const records = await sql.select('*').from('my_table')
  console.log(records)
}).use(
  rdsMiddleware({
    client: pg.Pool,
    config: {
      host: '*.ca-central-1.rds.amazonaws.com',
      user: 'iam_role',
      database: 'postgres',
      application_name: process.env.AWS_LAMBDA_FUNCTION_NAME,
      // loads the cert from process.env.NODE_EXTRA_CA_CERTS, can be set to /var/task/node_modules/middy-rds/certificates/us-east-1.pem
      ssl: { ca: ca() }
    }
  })
)

knex

import rdsMiddleware from 'middy-rds/knex'
import ca from 'middy-rds/certificates/ca-central-1'
import knex from 'knex'

import capturePostgres from 'aws-xray-sdk-postgres'
import pgClient from 'pg'

const pg = capturePostgres(pgClient)

const handler = middy(async (event, context) => {
  const { sql } = context
  const records = await sql.select('*').from('my_table')
  console.log(records)
}).use(
  rdsMiddleware({
    client: knex,
    config: {
      client: 'pg',
      connection: {
        host: '*.ca-central-1.rds.amazonaws.com',
        user: 'iam_role',
        database: 'postgres',
        port: 5432,
        application_name: process.env.AWS_LAMBDA_FUNCTION_NAME,
        // /var/task/node_modules/middy-rds/certificates/ca-central-1.pem ported to a js file for easy import
        ssl: { ca }
      }
    }
  })
)

postgres

import rdsMiddleware from 'middy-rds/postgres'
import ca from 'middy-rds/certificates/ca-central-1'

import postgresClient from 'postgres'

const handler = middy(async (event, context) => {
  const { sql } = context
  const records = await sql`SELECT * FROM my_table`
  console.log(records)
}).use(
  rdsMiddleware({
    client: postgresClient,
    config: {
      host: '*.ca-central-1.rds.amazonaws.com',
      user: 'iam_role',
      database: 'postgres',
      connection: {
        application_name: process.env.AWS_LAMBDA_FUNCTION_NAME
      },
      ssl: {
        ca
      }
    }
  })
)

Using with docker

# https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL.html
ADD https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem /path/to/save/
ENV NODE_EXTRA_CA_CERTS=/path/to/save/global-bundle.pem

Middy documentation and examples

For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.

Contributing

Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.

License

Licensed under MIT License. Copyright (c) 2017-2022 will Farrell and the Middy team.