How can I use slonik with an existing pg.Client? #482
Unanswered
innermatrix
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Slonik doesn't let you point to your own instance of However, you can specify a constructor to the import { Pool } from "pg";
import { createPool } from "slonik";
const client = pgClientFromSomeLibrary();
// Do whatever you want here to extend the pool behavior including re-using existing pg.Client instance here.
class CustomPool extends Pool {
constructor(config) {
super(config);
}
async connect() {
// Override the connect method to return the existing pg.Client instance;
// UNDERSTAND THE CAVEATS AND IMPLICATIONS FOR DOING THIS SINCE YOU'RE ALWAYS CHECKING OUT
// THE SAME CLIENT INSTANCE. YOU MAY NEED TO CLONE INSTANCES AND RETURN THOSE INSTEAD.
return client;
}
}
const customPool = await createPool("postgres://", {
// Specify the custom pool class here for Slonik to use under the hood
PgPool: CustomPool
}); However, just because you can doesn't mean you should! Doing this will most likely break pool management behavior of Slonik so you should be really REALLY sure you need to override PgPool! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am working with a 3rd party package that sets up a
pg.Client
for me and passes it into my code. I want to useslonik
utilities with thispg.Client
rather than creating a separate db connection. How can I accomplish this? Thanks!Beta Was this translation helpful? Give feedback.
All reactions