-
Notifications
You must be signed in to change notification settings - Fork 50
feat: connection pooling (wip) #213
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
Draft
beeequeue
wants to merge
14
commits into
unjs:main
Choose a base branch
from
beeequeue:connection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d3e71ff
fix building on windows
beeequeue 0d05cb7
use `import.meta.dirname` instead of `new URL`
beeequeue 31ab9aa
use pathe instead of replacing slashes
beeequeue c89546b
remove unnecessary character group from regex
beeequeue f61efef
add `supportsPooling`
beeequeue 516b931
add acquireConnection to types
beeequeue 9488b19
implement pg-pool connector using acquireConnection
beeequeue 701e0ad
add tests for pg-pool, fix normalizeParams not being applied everywhere
beeequeue cb7c062
add tests for acquireConnection
beeequeue 9f4ff76
use named exports
beeequeue 0b8965a
extract common postgres util to own file
beeequeue 0bca195
restructure types
beeequeue 1d87645
update pg connector
beeequeue 744fa31
format files
beeequeue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,5 @@ | ||
| // https://www.postgresql.org/docs/9.3/sql-prepare.html | ||
| export function normalizeParams(sql: string): string { | ||
| let i = 0; | ||
| return sql.replace(/\?/g, () => `$${++i}`); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,85 @@ | ||
| import { Pool, type PoolConfig, type QueryResult } from "pg"; | ||
| import type { Connector, Primitive } from "db0"; | ||
| import { normalizeParams } from "./_internal/postgresql.ts"; | ||
| import { BoundableStatement } from "./_internal/statement.ts"; | ||
| import type { ConnectorConnection } from "../types.ts"; | ||
|
|
||
| export type ConnectorOptions = { url: string } | PoolConfig; | ||
|
|
||
| type InternalQuery = ( | ||
| sql: string, | ||
| params?: Primitive[], | ||
| ) => Promise<QueryResult>; | ||
|
|
||
| export default function postgresqlPoolConnector( | ||
| opts: ConnectorOptions, | ||
| ): Connector<Pool> { | ||
| let _pool: undefined | Pool; | ||
| /* | ||
| TODO: decide what behavior to use here, the non-pooling connector connects before | ||
| proceeding with calling functions, while `pg` wants you to run queries | ||
| and let the library handle creating connections via the pool when it needs to. | ||
| should the non-pooling connector do the same? should it just be removed altogether? | ||
| */ | ||
| const getPool = () => { | ||
| _pool ??= new Pool("url" in opts ? { connectionString: opts.url } : opts); | ||
| return _pool; | ||
| }; | ||
|
|
||
| const acquireConnection = async (): Promise<ConnectorConnection> => { | ||
| const client = await getPool().connect(); | ||
| return { | ||
| dialect: "postgresql", | ||
| exec: (sql) => client.query(normalizeParams(sql)), | ||
| prepare: (sql) => new StatementWrapper(sql, client.query.bind(client)), | ||
| dispose: async () => client.release(), | ||
| [Symbol.asyncDispose]: async () => client.release(), | ||
| }; | ||
| }; | ||
|
|
||
| const dispose = async () => { | ||
| await _pool?.end?.(); | ||
| _pool = undefined; | ||
| }; | ||
|
|
||
| return { | ||
| name: "pg-pool", | ||
| dialect: "postgresql", | ||
| supportsPooling: true, | ||
| getInstance: () => getPool(), | ||
| exec: (sql) => getPool().query(normalizeParams(sql)), | ||
| prepare: (sql) => new StatementWrapper(sql, getPool().query.bind(_pool)), | ||
| acquireConnection, | ||
| dispose, | ||
| [Symbol.asyncDispose]: dispose, | ||
| }; | ||
| } | ||
|
|
||
| class StatementWrapper extends BoundableStatement<void> { | ||
| readonly #query: InternalQuery; | ||
| readonly #sql: string; | ||
|
|
||
| constructor(sql: string, query: InternalQuery) { | ||
| super(); | ||
| this.#sql = normalizeParams(sql); | ||
| this.#query = query; | ||
| } | ||
|
|
||
| async all(...params: Primitive[]) { | ||
| const res = await this.#query(this.#sql, params); | ||
| return res.rows; | ||
| } | ||
|
|
||
| async run(...params: Primitive[]) { | ||
| const res = await this.#query(this.#sql, params); | ||
| return { | ||
| success: true, | ||
| ...res, | ||
| }; | ||
| } | ||
|
|
||
| async get(...params: Primitive[]) { | ||
| const res = await this.#query(this.#sql, params); | ||
| return res.rows[0]; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ciorentshould optimally be bundled instead of dependend on, as the used code from it is only around 150B minified, and the install size is 4kBim not sure how to configure this with obuild though, im too used to tsdown :^)