Ninety-nine Pitchers of Malt

    @ansaro/lambda-pg

    1.0.3 • Public • Published

    lambda-pg

    Connect to Postgres in AWS Lambda environment

    yarn add @ansaro/lambda-pg
    

    Usage

    Create a pool connection that can be resused across Lambda invocations. You can configure Postgres in 4 ways:

    1. environment variables
    2. connection URI
    3. config object
    4. AWS Secrets Manager

    Environment Variables

    const DB = require("@ansaro/lambda-pg");
     
    const db = new DB();
     
    exports.handle = async function() {
      const pool = await db.init();
     
      const res = await pool.query("SELECT 1");
     
      return res;
    };

    Connection URI

    const DB = require("@ansaro/lambda-pg");
     
    const db = new DB();
     
    exports.handle = async function() {
      const pool = await db.init("postgresql://dbuser:pw0rd@example.tld:3211/mydb");
     
      const res = await pool.query("SELECT 1");
     
      return res;
    };

    Config Object

    const DB = require("@ansaro/lambda-pg");
     
    const db = new DB();
     
    exports.handle = async function() {
      const pool = await db.init({
        user: "dbuser",
        host: "database.server.com",
        database: "mydb",
        password: "secretpassword",
        port: 3211
      });
     
      const res = await pool.query("SELECT 1");
     
      return res;
    };

    AWS Secrets Manager

    • name (string) identifier for AWS Secret Manager
    • transformer (function) transforms secret into config object
    const DB = require("@ansaro/lambda-pg");
     
    function transformer({ username, host, dbname, password, port }) {
      return {
        user: username,
        host,
        database: dbname,
        password,
        port
      };
    }
     
    const db = new DB();
     
    exports.handle = async function() {
      const pool = await db.init({
        name: "your/db/secret",
        transformer
      });
     
      const res = await pool.query("SELECT 1");
     
      return res;
    };

    Keywords

    none

    Install

    npm i @ansaro/lambda-pg

    DownloadsWeekly Downloads

    3

    Version

    1.0.3

    License

    ISC

    Unpacked Size

    10.7 kB

    Total Files

    9

    Last publish

    Collaborators

    • bgoldberg15
    • maxbeatty