Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Wrap database pool in class for reusability #5494

Merged
merged 2 commits into from Mar 8, 2022

Conversation

nwalters512
Copy link
Contributor

This PR introduces a PostgressPool class that encapsulates our opinionated layer on top of node-postgres. The sqldb module retains the same interface as before - that is, we still expose a default pool. However, this change allows us to potentially have multiple database pools, which we'll take advantage of in the the future to improve how our named locks work.

@nwalters512 nwalters512 marked this pull request as ready for review March 8, 2022 02:16
module.exports = {
...defaultPool,
// Class methods are non-enumerable, so we can't spread them. We'll manually
// attach them to `module.exports`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't know this. Explicit pass-throughs still aren't too bad here.

const sqldb = require('./sql-db');

describe('sqldb', () => {
it('exports the full PostgresPool interface', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice to check this here!

* the fact that we tried to acquire new clients inside of transactions, which
* ultimately lead to a deadlock.
*/
this.alsClient = new AsyncLocalStorage();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we in fact still want the AsyncLocalStorage code here? I'm worried that we might inadvertently rely on it, only to have it break in some complex and impossible-to-debug way. An alternative might be to use AsyncLocalStorage to throw an error if anyone tries to nest a DB call inside a transaction?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until we merge #5489, I want to keep this here. Even then, I think it's still useful, and it mirrors how transactions work in things like Ruby on Rails (no need to manually pipe around a client, which is a footgun). That said, after the other PR, we really shouldn't have any more transactions, so this code should then be unused.

@nwalters512 nwalters512 merged commit 073606f into master Mar 8, 2022
@nwalters512 nwalters512 deleted the refactor/db-pool branch March 8, 2022 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants