brianc / node-pg-pool

A connection pool for node-postgres

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lambda, pg pool Error: TypeError: (0 , _dbConnection2.default) is not a function

elliotrock opened this issue · comments

I am getting this error when running a simple pool test in a serverless environment. Everything works with unit tests, even testing the Lambda promise:

TypeError: (0 , _dbConnection2.default) is not a function

My handler:

import {connection, pool} from '../src/dbConnection'
import lib from '../lib/index'

module.exports.handler = (event, context, callback) => {
  return connection().then(result => {
    callback(null, lib.lambdaProxyResponse(200, { connection: result.rowCount > 0 }))
  }).catch(err => {
    callback(null, lib.lambdaProxyResponse(400, { error: err }))
  })
}

My test shows first a non-pool connection. Then a pool connection

/src/dbConnection.js

import pg from 'pg'
import db from '../lib/db'

var config = {
  user: 'user',
  database: 'db_name',
  password: 'password',
  host: 'path-to-db',
  port: 5432,
  max: 10,
  idleTimeoutMillis: 30000
}

export const connection = () => {
  return new Promise((resolve, reject) => {
    var client = new pg.Client(config)
    client.connect(function (err) {
      if (err) {
        reject(err)
      }
      client.query('SELECT 1', function (err, result) {
        if (err) {
          reject(err)
        }
        resolve(result)
      })
    })
  })
}
export const pool = () => {
  return new Promise((resolve, reject) => {
    db.query('SELECT 1', function (err, result) {
      if (err) {
        reject(err)
      }
      console.log(result)
      resolve(result)
    })
  })
}

../lib/db.js

import pg from 'pg'

var config = {
  user: 'user',
  database: 'db_name',
  password: 'password',
  host: 'path-to-db',
  port: 5432,
  max: 10,
  idleTimeoutMillis: 30000
}

const pool = new pg.Pool(config)

pool.on('error', function (err, client) {
  console.error('idle client error', err.message, err.stack)
})
module.exports.query = function (text, values, callback) {
  // doesn't seem to be connecting in a lambda env.?
  return pool.query(text, values, callback)
}
module.exports.connect = function (callback) {
  return pool.connect(callback)
}

Tests once deploy for both a straight connection test and the pool test fail with that error.

Questions:

  • Is there any permissions I need to set for my lambda functions in the security groups?
  • With my db.js will that pool be run outside of my lambda handler? DO I need to structure it different so it warms up correctly?

Cheers

apologies in one of my tests I forgot to enclose the import of the functions with { } here import {connection, pool} from '../src/dbConnection'

Actually I found this around creating an IAM user role:

http://docs.aws.amazon.com/lambda/latest/dg/vpc-rds-create-iam-role.html