-
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
base: main
Are you sure you want to change the base?
feat: neon connector #117
Changes from all commits
443bfdb
8070bb0
2dd0c85
1d3fea0
5250ad9
985dcb8
625d06c
5393de1
8b81371
ed93865
e16c33d
7984b0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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, | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+68
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spreading an array into an object produces unexpected results. The π 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
Suggested change
π€ Prompt for AI Agents |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| async get(...params: Primitive[]) { | ||||||||||||||||||||||||||||
| const res = await this.#query(this.#sql, params); | ||||||||||||||||||||||||||||
| return res[0]; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| 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", | ||
| }), | ||
| }); | ||
| }); |
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.
Fix InternalQuery return type to match neon() actual return type.
The
neon()function with<false, false>parameters returns rows asRecord<string, unknown>[]. TheInternalQuerytype should reflect this more accurately.π§ Proposed fix
π§° Tools
πͺ GitHub Check: tests
[failure] 37-37:
Type '(sql: string, params: Primitive[] | undefined) => Promise<Record<string, any>[] | any[][] | FullQueryResults>' is not assignable to type 'InternalQuery'.
π€ Prompt for AI Agents