Created
August 22, 2017 20:01
-
-
Save maxbeatty/fb2500277496e798952f9e2ee260179b to your computer and use it in GitHub Desktop.
using node-postgres (`pg`) in AWS Lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import λ from "apex.js"; | |
import { Pool } from "pg"; | |
// connection details inherited from environment | |
const pool = new Pool({ | |
max: 1, | |
min: 0, | |
idleTimeoutMillis: 120000, | |
connectionTimeoutMillis: 10000 | |
}); | |
export default function λ(async (event, context) => { | |
// https://github.com/brianc/node-postgres/issues/930#issuecomment-230362178 | |
context.callbackWaitsForEmptyEventLoop = false; // !important to reuse pool | |
const client = await pool.connect(); | |
try { | |
await client.query("SELECT NOW()"); | |
} finally { | |
// https://github.com/brianc/node-postgres/issues/1180#issuecomment-270589769 | |
client.release(true); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment