-
Notifications
You must be signed in to change notification settings - Fork 30
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
feat: neon connector #117
base: main
Are you sure you want to change the base?
feat: neon connector #117
Changes from all commits
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,59 @@ | ||
import { neon, HTTPTransactionOptions, NeonQueryFunction } from '@neondatabase/serverless'; | ||
|
||
import type { Connector, Statement } from "../types"; | ||
|
||
export interface ConnectorOptions extends HTTPTransactionOptions<undefined, undefined> { | ||
/** | ||
* The URL of the Neon Serverless Postgres instance. | ||
* | ||
* @required | ||
*/ | ||
url: string; | ||
} | ||
|
||
export default function neonConnector(opts: ConnectorOptions) { | ||
let _connection: NeonQueryFunction<undefined, undefined>; | ||
const getConnection = async () => { | ||
if (_connection) { | ||
return _connection; | ||
} | ||
|
||
const { url, ...transactionOptions } = opts; | ||
|
||
_connection = neon(url, { | ||
...transactionOptions, | ||
}) | ||
|
||
return _connection; | ||
}; | ||
|
||
return <Connector>{ | ||
name: "neon", | ||
dialect: "postgresql", | ||
exec(sql: string) { | ||
return getConnection().then((c) => c(sql)); | ||
}, | ||
prepare(sql: string) { | ||
const stmt = <Statement>{ | ||
_sql: sql, | ||
_params: [], | ||
bind(...params) { | ||
if (params.length > 0) { | ||
this._params = params; | ||
} | ||
return stmt; | ||
}, | ||
all(...params) { | ||
return getConnection().then((c) => c(this._sql, params || this._params)); | ||
}, | ||
run(...params) { | ||
return getConnection().then((c) => c(this._sql, params || this._params)); | ||
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.
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. Can you confirm what is production behavior of neon currently? Ideally if it returns additional info, it seems to be correct. 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. I'm so sorry, I've missed your comments π₯² sure that sounds good |
||
}, | ||
get(...params) { | ||
return getConnection().then((c) => c(this._sql, params || this._params)).then((res) => res[0]); | ||
}, | ||
}; | ||
return stmt; | ||
}, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { describe } from "vitest"; | ||
import connector from "../../src/connectors/neon"; | ||
import { testConnector } from "./_tests"; | ||
|
||
describe.runIf(process.env.POSTGRESQL_URL)( | ||
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. I wish we could somehow mock |
||
"connectors: neon.test", | ||
() => { | ||
testConnector({ | ||
dialect: "postgresql", | ||
connector: connector({ | ||
url: process.env.POSTGRESQL_URL!, | ||
}), | ||
}); | ||
}, | ||
); |
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.
This seems can be non async right?
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.
true!