-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
217 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
POSTGRESQL_URL=postgresql://<user>:@localhost:5432/sql0 |
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 |
---|---|---|
|
@@ -10,3 +10,4 @@ tmp | |
__* | ||
.data | ||
.tmp | ||
.env |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,65 @@ | ||
import { Client, ClientConfig, Query } from "pg"; | ||
|
||
import type { Connector, Statement } from "../types"; | ||
|
||
export type ConnectorOptions = { url: string } | ClientConfig; | ||
|
||
export default function sqliteConnector(opts: ConnectorOptions) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
let _client: undefined | Client | Promise<Client>; | ||
function getClient() { | ||
if (_client) { | ||
return _client; | ||
} | ||
const client = new Client("url" in opts ? opts.url : opts); | ||
_client = client.connect().then(() => { | ||
_client = client; | ||
return _client; | ||
}); | ||
return _client; | ||
} | ||
|
||
async function query(sql: string, params?: unknown[]) { | ||
const client = await getClient(); | ||
return client.query(normalizeParams(sql), params); | ||
} | ||
|
||
return <Connector>{ | ||
name: "postgresql", | ||
exec(sql: string) { | ||
return query(sql); | ||
}, | ||
prepare(sql: string) { | ||
const stmt = <Statement>{ | ||
_sql: sql, | ||
_params: [], | ||
bind(...params) { | ||
if (params.length > 0) { | ||
this._params = params; | ||
} | ||
return stmt; | ||
}, | ||
all(...params) { | ||
return query(this._sql, params || this._params).then((r) => r.rows); | ||
}, | ||
run(...params) { | ||
return query(this._sql, params || this._params).then((r) => ({ | ||
result: r, | ||
})); | ||
}, | ||
get(...params) { | ||
// TODO: Append limit? | ||
return query(this._sql, params || this._params).then( | ||
(r) => r.rows[0] | ||
); | ||
}, | ||
}; | ||
return stmt; | ||
}, | ||
}; | ||
} | ||
|
||
// https://www.postgresql.org/docs/9.3/sql-prepare.html | ||
function normalizeParams(sql: string) { | ||
let i = 0; | ||
return sql.replace(/\?/g, () => `$${++i}`); | ||
} |
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,14 @@ | ||
import { describe } from "vitest"; | ||
import connector from "../../src/connectors/postgresql"; | ||
import { testConnector } from "./_tests"; | ||
|
||
describe.runIf(process.env.POSTGRESQL_URL)( | ||
"connectors: postgresql.test", | ||
() => { | ||
testConnector({ | ||
connector: connector({ | ||
url: process.env.POSTGRESQL_URL!, | ||
}), | ||
}); | ||
} | ||
); |
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,7 @@ | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
setupFiles: ["dotenv/config"], | ||
}, | ||
}); |
I think this function name needs an update :)