Use an existing pg.Client connection
See original GitHub issueHow can I get a typeorm Connection
(or EntityManager
) from an existing connection created with the pg
library?
https://github.com/brianc/node-postgres/wiki/Client
For example, I want to do something like:
import * as pg from 'pg';
const client = new pg.Client('postgres://brian:mypassword@localhost:5432/dev');
client.connect(async () => {
// Do some SQL queries with client...
await insertPhotosSql(client);
// Now I want to do some typeorm queries using same connection:
const connection = ???(client);
let allPhotos = await connection.entityManager.find(Photo);
});
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Use an existing pg.Client connection · Issue #5211 - GitHub
I am using a wrapper around the postgres client as well to kill ghost connections for use in serverless functions.
Read more >pg.Client - node-postgres
new Client(config: Config). Every field of the config object is entirely optional. A Client instance will use environment variables for all missing values....
Read more >Node.js - PostgreSQL (pg) : Client has already been ...
Then you just use the exported client, which has already been connected, so it won't reconnect again on each request. Be sure to...
Read more >Connecting to PostgreSQL with Node.js - This Dot Labs
In this article, I'm going to cover how you can use Node.js to connect to and execute queries against a Postgres database.
Read more >15: 20.11. Client Connection Defaults - PostgreSQL
The default value for this parameter is "$user", public . This setting supports shared use of a database (where no users have private...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@pleerock Hi Umed. I want to make another argument about existing connection. I work in a big tech company which, as many other such ones, uses a lot of inner libraries. Right now we try to move little by little to TypeORM, but in an existing project, there are many functions that work with DB.
For example, say we have functions like (without TypeORM)
Then we created
Entity1
via TypeORM but notEntity2
. So as we want to make a smooth transition to the framework, we can’t just use TypeORM in that case because they are 2 different drivers (e.g. pg Client/Pool).I can do a PR with passing in the existing client, at least for
pg
, but not sure whether the core team is open for such changes. Is it?Hi @pleerock, would also be interested in this. Thanks.