-
Notifications
You must be signed in to change notification settings - Fork 48
feat: neon connector #117
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
Open
arashsheyda
wants to merge
12
commits into
unjs:main
Choose a base branch
from
arashsheyda:feat/neon-connector
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.
Open
feat: neon connector #117
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
443bfdb
feat: neon connector
arashsheyda 8070bb0
Merge branch 'main' into pr/arashsheyda/117
pi0 2dd0c85
reduce diff
pi0 1d3fea0
update lock
pi0 5250ad9
chore: apply automated updates
autofix-ci[bot] 985dcb8
Merge remote-tracking branch 'origin/main' into feat/neon-connector
arashsheyda 625d06c
chore: apply automated updates
autofix-ci[bot] 5393de1
fix: update test
arashsheyda 8b81371
chore: update package
arashsheyda ed93865
fix: update ConnectorOptions type and improve neon mock implementation
arashsheyda e16c33d
chore: update docs
arashsheyda 7984b0f
Merge branch 'main' into feat/neon-connector
pi0 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
Some comments aren't visible on the classic Files Changed page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { neon } from "@neondatabase/serverless"; | ||
| import type { | ||
| NeonQueryFunction, | ||
| HTTPTransactionOptions, | ||
| } from "@neondatabase/serverless"; | ||
| import type { Connector, Primitive } from "db0"; | ||
| import { BoundableStatement } from "./_internal/statement.ts"; | ||
|
|
||
| export interface ConnectorOptions extends HTTPTransactionOptions<false, false> { | ||
| /** | ||
| * The URL of the Neon Serverless Postgres instance. | ||
| * | ||
| * @required | ||
| */ | ||
| url: string; | ||
| } | ||
|
|
||
| type InternalQuery = (sql: string, params?: Primitive[]) => Promise<unknown[]>; | ||
|
|
||
| export default function neonConnector( | ||
| opts: ConnectorOptions, | ||
| ): Connector<NeonQueryFunction<false, false>> { | ||
| let _connection: NeonQueryFunction<false, false>; | ||
|
|
||
| function getConnection() { | ||
| if (_connection) { | ||
| return _connection; | ||
| } | ||
| const { url, ...transactionOptions } = opts; | ||
| _connection = neon(url, transactionOptions); | ||
| return _connection; | ||
| } | ||
|
|
||
| const query: InternalQuery = async (sql, params) => { | ||
| const connection = getConnection(); | ||
| return connection.query(normalizeParams(sql), params); | ||
| }; | ||
|
|
||
| return { | ||
| name: "neon", | ||
| dialect: "postgresql", | ||
| getInstance: () => getConnection(), | ||
| exec: (sql) => query(sql), | ||
| prepare: (sql) => new StatementWrapper(sql, query), | ||
| }; | ||
| } | ||
|
|
||
| // https://www.postgresql.org/docs/9.3/sql-prepare.html | ||
| function normalizeParams(sql: string) { | ||
| let i = 0; | ||
| return sql.replace(/\?/g, () => `$${++i}`); | ||
| } | ||
|
|
||
| class StatementWrapper extends BoundableStatement<void> { | ||
| #query: InternalQuery; | ||
| #sql: string; | ||
|
|
||
| constructor(sql: string, query: InternalQuery) { | ||
| super(); | ||
| this.#sql = sql; | ||
| this.#query = query; | ||
| } | ||
|
|
||
| async all(...params: Primitive[]) { | ||
| return this.#query(this.#sql, params); | ||
| } | ||
|
|
||
| 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[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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { vi, describe } from "vitest"; | ||
| import { testConnector } from "./_tests"; | ||
|
|
||
| vi.mock("@neondatabase/serverless", async () => { | ||
| const { PGlite } = await import("@electric-sql/pglite"); | ||
| const pglite = await PGlite.create(); | ||
| return { | ||
| neon: () => { | ||
| const queryFn = async (sql: string, params?: unknown[]) => { | ||
| const result = await pglite.query(sql, params); | ||
| return result.rows; | ||
| }; | ||
| queryFn.query = queryFn; | ||
| return queryFn; | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| const { default: neonConnector } = await import("../../src/connectors/neon"); | ||
|
|
||
| describe("connectors: neon (mocked with pglite)", () => { | ||
| testConnector({ | ||
| dialect: "postgresql", | ||
| connector: neonConnector({ | ||
| url: "postgresql://mock@localhost/mock", | ||
| }), | ||
| }); | ||
| }); |
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.
Spreading an array into an object produces unexpected results.
The
resvariable is an array (unknown[]), so spreading it into an object with...reswill produce numeric keys (0, 1, 2...) rather than meaningful properties. Therun()method should only return{ success: boolean }per theBoundableStatementinterface contract.π Proposed fix
async run(...params: Primitive[]) { - const res = await this.#query(this.#sql, params); + await this.#query(this.#sql, params); return { success: true, - ...res, }; }π Committable suggestion
π€ Prompt for AI Agents