diff --git a/README.md b/README.md index 3a686fa..b9dfb1e 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,9 @@ The glue for TypeScript to PostgreSQL. ## Installation ```ts -import pglue from "https://git.lua.re/luaneko/pglue/raw/tag/v0.3.2/mod.ts"; +import pglue from "https://git.lua.re/luaneko/pglue/raw/tag/v0.3.3/mod.ts"; // ...or from github: -import pglue from "https://raw.githubusercontent.com/luaneko/pglue/refs/tags/v0.3.2/mod.ts"; +import pglue from "https://raw.githubusercontent.com/luaneko/pglue/refs/tags/v0.3.3/mod.ts"; ``` ## Documentation diff --git a/deno.json b/deno.json index b96c59d..454d45a 100644 --- a/deno.json +++ b/deno.json @@ -1,5 +1,5 @@ { "name": "@luaneko/pglue", - "version": "0.3.2", + "version": "0.3.3", "exports": "./mod.ts" } diff --git a/mod.ts b/mod.ts index e9db931..18289c9 100644 --- a/mod.ts +++ b/mod.ts @@ -1,14 +1,14 @@ import pg_conn_str from "npm:pg-connection-string@^2.7.0"; -import type * as v from "./valita.ts"; import { Pool, PoolOptions, Wire, WireOptions } from "./wire.ts"; export { Wire, WireOptions, WireError, - PostgresError, Pool, PoolOptions, + PostgresError, + type Postgres, type WireEvents, type PoolEvents, type LogLevel, @@ -34,8 +34,19 @@ export { type RowStream, } from "./query.ts"; -export default function postgres(s: string, options: Partial = {}) { - return new Postgres(Options.parse(parse_conn(s, options), { mode: "strip" })); +export default function postgres( + s: string, + options: Partial = {} +) { + return new Pool(PoolOptions.parse(parse_conn(s, options), { mode: "strip" })); +} + +postgres.connect = connect; + +export async function connect(s: string, options: Partial = {}) { + return await new Wire( + WireOptions.parse(parse_conn(s, options), { mode: "strip" }) + ).connect(); } function parse_conn(s: string, options: Partial) { @@ -59,31 +70,3 @@ function parse_conn(s: string, options: Partial) { runtime_params: { ...runtime_params, ...options.runtime_params }, }; } - -postgres.connect = connect; - -export async function connect(s: string, options: Partial = {}) { - return await new Wire( - WireOptions.parse(parse_conn(s, options), { mode: "strip" }) - ).connect(); -} - -export type Options = v.Infer; -export const Options = PoolOptions; - -export class Postgres extends Pool { - readonly #options; - - constructor(options: Options) { - super(options); - this.#options = options; - } - - async connect(options: Partial = {}) { - return await new Wire( - WireOptions.parse({ ...this.#options, ...options }, { mode: "strip" }) - ) - .on("log", (l, c, s) => this.emit("log", l, c, s)) - .connect(); - } -} diff --git a/wire.ts b/wire.ts index ca4746e..d49e1d2 100644 --- a/wire.ts +++ b/wire.ts @@ -495,8 +495,8 @@ export interface Transaction extends Result, AsyncDisposable { rollback(): Promise; } -export type ChannelEvents = { notify: NotificationHandler }; export type NotificationHandler = (payload: string, process_id: number) => void; +export type ChannelEvents = { notify: NotificationHandler }; export interface Channel extends TypedEmitter, Result, @@ -507,9 +507,19 @@ export interface Channel unlisten(): Promise; } +export interface Postgres { + query(sql: SqlFragment): Query; + query(s: TemplateStringsArray, ...xs: unknown[]): Query; + + begin(): Promise; + begin( + f: (pg: Postgres, tx: Transaction) => T | PromiseLike + ): Promise; +} + export class Wire extends TypedEmitter - implements Disposable + implements Postgres, Disposable { readonly #options; readonly #params; @@ -1626,8 +1636,9 @@ export interface PoolTransaction extends Transaction { export class Pool extends TypedEmitter - implements PromiseLike, Disposable + implements Postgres, PromiseLike, Disposable { + readonly #options; readonly #acquire; readonly #begin; readonly #close; @@ -1638,7 +1649,15 @@ export class Pool acquire: this.#acquire, begin: this.#begin, close: this.#close, - } = pool_impl(this, options)); + } = pool_impl(this, (this.#options = options))); + } + + async connect(options: Partial = {}) { + return await new Wire( + WireOptions.parse({ ...this.#options, ...options }, { mode: "strip" }) + ) + .on("log", (l, c, s) => (this as Pool).emit("log", l, c, s)) + .connect(); } get(): Promise;