A PostgreSQL client for Node.js serverless applications.
Note: This library is currently alpha and subject to breaking changes. Please install this dependency using an exact version.
This module is intended for use in node.js serverless applications. It supports node v8 and above. It is simply some boilerplate around the fantastic pg library for serverless apps.
npm install serverless-pg --save-exact
First, setup your client in a dedicated module:
// client.jsimportcreateClientfrom'serverless-pg';constdbClient=createClient({config: {host: process.env.HOST,port: process.env.PORT,user: process.env.USERNAME,password: process.env.PASSWORD,database: process.env.NAME,},onConnect: ()=>{},onClose: ()=>{},beforeQuery: ()=>{},afterQuery: ()=>{},});exportdefaultdbClient;Then, import anywhere in your application and use.
// handler.jsimportdbClientfrom'./client';consthandler=async()=>{// calling query automagically connects to the databaseawaitdbClient.query('SELECT * FROM users');awaitdbClient.transaction([()=>dbClient.query(''),()=>dbClient.query('')]);awaitdbClient.end();};