Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow replacing the underlying 'pg'Β #240

@getkey

Description

@getkey

I should elaborate on my use-case. I'm using PostgreSQL with AWS Lambda, and I'm having an issue where when I get a lot of simultaneous requests, connections stay hanging too long (zombie connections).

To solve this, I would like to switch from using the normal pg to this wrapper: https://github.com/MatteoGioioso/serverless-pg

I have first tried naively to alias pg with yarn yarn add pg@npm:serverless-postgres, which didn't work.

ERROR in ./node_modules/pg-cursor/index.js 3:16-55
Module not found: Error: Can't resolve 'pg/lib/utils.js' in '/mnt/data/devel/bae/node_modules/pg-cursor'
 @ ./node_modules/slonik/dist/QueryStream.js 9:36-56
 @ ./node_modules/slonik/dist/connectionMethods/stream.js 9:22-47
 @ ./node_modules/slonik/dist/connectionMethods/index.js 28:15-34
 @ ./node_modules/slonik/dist/binders/bindPool.js 5:28-59
 @ ./node_modules/slonik/dist/factories/createPool.js 6:19-49
 @ ./node_modules/slonik/dist/factories/index.js 10:19-42
 @ ./node_modules/slonik/dist/index.js 14:20-42 16:18-40
 @ ./src/handlers/players/addRole.js 2:0-29 24:41-44

Now I decided to install serverless-pg normally and use Slonik's query builder alongside it. My code looks somewhat like this.

import ServerlessClient from 'serverless-postgres';
import { sql } from 'slonik';

const client = new ServerlessClient();

async function handler(event) {
	await client.connect();
	const query = sql`
SELECT something
FROM somewhere`;
	try {

		res = await client.query(query.sql, query.values);
		res = res.rows[0];
	} catch (err) {
		throw new createError(500, 'Failed to execute query', {
			details: err,
		});
	}
	await client.clean();
        // use res!
}

That works. But sadly, I'm limited to the query builder part of Slonik only. I would love to be able to use connection.one(), connection.transaction(), etc. How would you achieve this, could you point me in the right direction?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions