-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use postgres connection pool (#2106)
* refactor: use postgres connection pool * enable modern build * use spread operator for postgres query args * bump server size limit --------- Co-authored-by: Lutz Biedinger <[email protected]>
- Loading branch information
1 parent
54fc84f
commit 0be7a6b
Showing
9 changed files
with
123 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Pool } from 'pg'; | ||
|
||
let pool; | ||
|
||
export default { | ||
config: {}, | ||
|
||
get enabled() { | ||
return this.config.enabled; | ||
}, | ||
|
||
get pool() { | ||
if (!pool) { | ||
pool = new Pool(this.config); | ||
pool.on('error', (err) => { | ||
console.error('PostgreSQL pool error', err); | ||
pool = null; | ||
}); | ||
} | ||
return pool; | ||
}, | ||
|
||
query() { | ||
return this.pool.query(...arguments); | ||
} | ||
}; |
20 changes: 5 additions & 15 deletions
20
packages/portal/src/server-middleware/api/events/trending.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/portal/tests/unit/server-middleware/api/events/pg.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import pg from 'pg'; | ||
import sinon from 'sinon'; | ||
|
||
import pgEvents from '@/server-middleware/api/events/pg'; | ||
|
||
const pgPoolOn = sinon.stub(); | ||
const pgPoolQuery = sinon.stub(); | ||
|
||
describe('@/server-middleware/api/events/pg', () => { | ||
beforeAll(() => { | ||
sinon.replace(pg.Pool.prototype, 'on', pgPoolOn); | ||
sinon.replace(pg.Pool.prototype, 'query', pgPoolQuery); | ||
}); | ||
afterEach(sinon.resetHistory); | ||
afterAll(sinon.resetBehavior); | ||
|
||
describe('pool', () => { | ||
it('creates and returns a pg pool, with error handler', () => { | ||
const pool = pgEvents.pool; | ||
|
||
expect(pool instanceof pg.Pool).toBe(true); | ||
expect(pgPoolOn.calledWith('error', sinon.match.func)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('query', () => { | ||
it('delegates with all args to pg pool', () => { | ||
const sql = 'SELECT * FROM table WHERE type=$1'; | ||
const params = ['like']; | ||
|
||
pgEvents.query(sql, params); | ||
|
||
expect(pgPoolQuery.calledWith(sql, params)).toBe(true); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.