Simply Postgres interface
This commit is contained in:
parent
7f3e3b236b
commit
33b5158327
@ -14,9 +14,9 @@ The glue for TypeScript to PostgreSQL.
|
|||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```ts
|
```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:
|
// ...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
|
## Documentation
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "@luaneko/pglue",
|
"name": "@luaneko/pglue",
|
||||||
"version": "0.3.2",
|
"version": "0.3.3",
|
||||||
"exports": "./mod.ts"
|
"exports": "./mod.ts"
|
||||||
}
|
}
|
||||||
|
47
mod.ts
47
mod.ts
@ -1,14 +1,14 @@
|
|||||||
import pg_conn_str from "npm:pg-connection-string@^2.7.0";
|
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";
|
import { Pool, PoolOptions, Wire, WireOptions } from "./wire.ts";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
Wire,
|
Wire,
|
||||||
WireOptions,
|
WireOptions,
|
||||||
WireError,
|
WireError,
|
||||||
PostgresError,
|
|
||||||
Pool,
|
Pool,
|
||||||
PoolOptions,
|
PoolOptions,
|
||||||
|
PostgresError,
|
||||||
|
type Postgres,
|
||||||
type WireEvents,
|
type WireEvents,
|
||||||
type PoolEvents,
|
type PoolEvents,
|
||||||
type LogLevel,
|
type LogLevel,
|
||||||
@ -34,8 +34,19 @@ export {
|
|||||||
type RowStream,
|
type RowStream,
|
||||||
} from "./query.ts";
|
} from "./query.ts";
|
||||||
|
|
||||||
export default function postgres(s: string, options: Partial<Options> = {}) {
|
export default function postgres(
|
||||||
return new Postgres(Options.parse(parse_conn(s, options), { mode: "strip" }));
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
function parse_conn(s: string, options: Partial<WireOptions>) {
|
function parse_conn(s: string, options: Partial<WireOptions>) {
|
||||||
@ -59,31 +70,3 @@ function parse_conn(s: string, options: Partial<WireOptions>) {
|
|||||||
runtime_params: { ...runtime_params, ...options.runtime_params },
|
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
27
wire.ts
@ -495,8 +495,8 @@ export interface Transaction extends Result, AsyncDisposable {
|
|||||||
rollback(): Promise<Result>;
|
rollback(): Promise<Result>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ChannelEvents = { notify: NotificationHandler };
|
|
||||||
export type NotificationHandler = (payload: string, process_id: number) => void;
|
export type NotificationHandler = (payload: string, process_id: number) => void;
|
||||||
|
export type ChannelEvents = { notify: NotificationHandler };
|
||||||
export interface Channel
|
export interface Channel
|
||||||
extends TypedEmitter<ChannelEvents>,
|
extends TypedEmitter<ChannelEvents>,
|
||||||
Result,
|
Result,
|
||||||
@ -507,9 +507,19 @@ export interface Channel
|
|||||||
unlisten(): Promise<Result>;
|
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>
|
export class Wire<V extends WireEvents = WireEvents>
|
||||||
extends TypedEmitter<V>
|
extends TypedEmitter<V>
|
||||||
implements Disposable
|
implements Postgres, Disposable
|
||||||
{
|
{
|
||||||
readonly #options;
|
readonly #options;
|
||||||
readonly #params;
|
readonly #params;
|
||||||
@ -1626,8 +1636,9 @@ export interface PoolTransaction extends Transaction {
|
|||||||
|
|
||||||
export class Pool<V extends PoolEvents = PoolEvents>
|
export class Pool<V extends PoolEvents = PoolEvents>
|
||||||
extends TypedEmitter<V>
|
extends TypedEmitter<V>
|
||||||
implements PromiseLike<PoolWire>, Disposable
|
implements Postgres, PromiseLike<PoolWire>, Disposable
|
||||||
{
|
{
|
||||||
|
readonly #options;
|
||||||
readonly #acquire;
|
readonly #acquire;
|
||||||
readonly #begin;
|
readonly #begin;
|
||||||
readonly #close;
|
readonly #close;
|
||||||
@ -1638,7 +1649,15 @@ export class Pool<V extends PoolEvents = PoolEvents>
|
|||||||
acquire: this.#acquire,
|
acquire: this.#acquire,
|
||||||
begin: this.#begin,
|
begin: this.#begin,
|
||||||
close: this.#close,
|
close: this.#close,
|
||||||
} = pool_impl(this, options));
|
} = 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
get(): Promise<PoolWire>;
|
get(): Promise<PoolWire>;
|
||||||
|
Loading…
Reference in New Issue
Block a user