pglue/mod.ts

73 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

2025-01-12 06:36:15 +11:00
import pg_conn_str from "npm:pg-connection-string@^2.7.0";
2025-01-13 11:00:33 +11:00
import { Pool, PoolOptions, Wire, WireOptions } from "./wire.ts";
2025-01-07 22:12:30 +11:00
export {
2025-01-13 13:54:31 +11:00
Wire,
WireOptions,
2025-01-07 22:12:30 +11:00
WireError,
2025-01-13 13:54:31 +11:00
Pool,
PoolOptions,
2025-01-13 14:13:00 +11:00
PostgresError,
type Postgres,
2025-01-13 13:54:31 +11:00
type WireEvents,
type PoolEvents,
2025-01-07 22:12:30 +11:00
type LogLevel,
2025-01-13 13:54:31 +11:00
type Parameters,
2025-01-07 22:12:30 +11:00
type Transaction,
type Channel,
2025-01-13 13:54:31 +11:00
type ChannelEvents,
type NotificationHandler,
2025-01-07 22:12:30 +11:00
} from "./wire.ts";
export {
type SqlFragment,
type SqlType,
type SqlTypeMap,
SqlTypeError,
2025-01-07 22:12:30 +11:00
sql,
2025-01-13 13:54:31 +11:00
sql_types,
sql_format,
2025-01-07 22:12:30 +11:00
is_sql,
Query,
type Result,
type Row,
type Rows,
type RowStream,
2025-01-07 22:12:30 +11:00
} from "./query.ts";
2025-01-13 14:13:00 +11:00
export default function postgres(
s: string,
options: Partial<PoolOptions> = {}
) {
return new Pool(PoolOptions.parse(parse_conn(s, options), { mode: "strip" }));
}
postgres.connect = connect;
export async function connect(s: string, options: Partial<WireOptions> = {}) {
return await new Wire(
WireOptions.parse(parse_conn(s, options), { mode: "strip" })
).connect();
2025-01-12 06:36:15 +11:00
}
2025-01-07 22:12:30 +11:00
2025-01-12 06:36:15 +11:00
function parse_conn(s: string, options: Partial<WireOptions>) {
2025-01-07 22:12:30 +11:00
const {
host,
port,
user,
password,
database,
2025-01-12 06:36:15 +11:00
ssl: _ssl, // TODO: ssl support
2025-01-07 22:12:30 +11:00
...runtime_params
2025-01-12 06:36:15 +11:00
} = s ? pg_conn_str.parse(s) : {};
2025-01-07 22:12:30 +11:00
2025-01-12 06:36:15 +11:00
return {
...options,
host: options.host ?? host,
port: options.port ?? port,
user: options.user ?? user,
password: options.password ?? password,
database: options.database ?? database,
runtime_params: { ...runtime_params, ...options.runtime_params },
};
2025-01-07 22:12:30 +11:00
}