question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use an existing pg.Client connection

See original GitHub issue

How 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:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sagidMcommented, Jan 29, 2021

@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)

someTransaction(() => {
  createEntity1(params: Entity1Params)
  createEntity2(params: Entity2Params)
})

Then we created Entity1 via TypeORM but not Entity2. 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?

0reactions
leighmancommented, Nov 16, 2018

I want to provide pg.Client as I am monitoring that instance using zipkin and jaeger.

Hi @pleerock, would also be interested in this. Thanks.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found