Compare commits

..

No commits in common. "master" and "v0.3.1" have entirely different histories.

4 changed files with 39 additions and 51 deletions

View File

@ -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.3/mod.ts";
import pglue from "https://git.lua.re/luaneko/pglue/raw/tag/v0.3.1/mod.ts";
// ...or from github:
import pglue from "https://raw.githubusercontent.com/luaneko/pglue/refs/tags/v0.3.3/mod.ts";
import pglue from "https://raw.githubusercontent.com/luaneko/pglue/refs/tags/v0.3.1/mod.ts";
```
## Documentation

View File

@ -1,5 +1,5 @@
{
"name": "@luaneko/pglue",
"version": "0.3.3",
"version": "0.3.1",
"exports": "./mod.ts"
}

57
mod.ts
View File

@ -1,22 +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,
Pool,
PoolOptions,
PostgresError,
type Postgres,
type WireEvents,
type PoolEvents,
type LogLevel,
type Parameters,
type Transaction,
type Channel,
type ChannelEvents,
type NotificationHandler,
type Parameters,
} from "./wire.ts";
export {
type SqlFragment,
@ -24,8 +16,6 @@ export {
type SqlTypeMap,
SqlTypeError,
sql,
sql_types,
sql_format,
is_sql,
Query,
type Result,
@ -34,19 +24,8 @@ export {
type RowStream,
} from "./query.ts";
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();
export default function postgres(s: string, options: Partial<Options> = {}) {
return new Postgres(Options.parse(parse_conn(s, options), { mode: "strip" }));
}
function parse_conn(s: string, options: Partial<WireOptions>) {
@ -70,3 +49,31 @@ function parse_conn(s: string, options: Partial<WireOptions>) {
runtime_params: { ...runtime_params, ...options.runtime_params },
};
}
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();
}
export type Options = v.Infer<typeof Options>;
export const Options = PoolOptions;
export class Postgres extends Pool {
readonly #options;
constructor(options: Options) {
super(options);
this.#options = options;
}
async connect(options: Partial<WireOptions> = {}) {
return await new Wire(
WireOptions.parse({ ...this.#options, ...options }, { mode: "strip" })
)
.on("log", (l, c, s) => this.emit("log", l, c, s))
.connect();
}
}

27
wire.ts
View File

@ -495,8 +495,8 @@ export interface Transaction extends Result, AsyncDisposable {
rollback(): Promise<Result>;
}
export type NotificationHandler = (payload: string, process_id: number) => void;
export type ChannelEvents = { notify: NotificationHandler };
export type NotificationHandler = (payload: string, process_id: number) => void;
export interface Channel
extends TypedEmitter<ChannelEvents>,
Result,
@ -507,19 +507,9 @@ export interface Channel
unlisten(): Promise<Result>;
}
export interface Postgres {
query<T = Row>(sql: SqlFragment): Query<T>;
query<T = Row>(s: TemplateStringsArray, ...xs: unknown[]): Query<T>;
begin(): Promise<Transaction>;
begin<T>(
f: (pg: Postgres, tx: Transaction) => T | PromiseLike<T>
): Promise<T>;
}
export class Wire<V extends WireEvents = WireEvents>
extends TypedEmitter<V>
implements Postgres, Disposable
implements Disposable
{
readonly #options;
readonly #params;
@ -1636,9 +1626,8 @@ export interface PoolTransaction extends Transaction {
export class Pool<V extends PoolEvents = PoolEvents>
extends TypedEmitter<V>
implements Postgres, PromiseLike<PoolWire>, Disposable
implements PromiseLike<PoolWire>, Disposable
{
readonly #options;
readonly #acquire;
readonly #begin;
readonly #close;
@ -1649,15 +1638,7 @@ export class Pool<V extends PoolEvents = PoolEvents>
acquire: this.#acquire,
begin: this.#begin,
close: this.#close,
} = pool_impl(this, (this.#options = options)));
}
async connect(options: Partial<WireOptions> = {}) {
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();
} = pool_impl(this, options));
}
get(): Promise<PoolWire>;