Simply Postgres interface
This commit is contained in:
		
							parent
							
								
									7f3e3b236b
								
							
						
					
					
						commit
						33b5158327
					
				@ -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
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,5 @@
 | 
			
		||||
{
 | 
			
		||||
  "name": "@luaneko/pglue",
 | 
			
		||||
  "version": "0.3.2",
 | 
			
		||||
  "version": "0.3.3",
 | 
			
		||||
  "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 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<Options> = {}) {
 | 
			
		||||
  return new Postgres(Options.parse(parse_conn(s, options), { mode: "strip" }));
 | 
			
		||||
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();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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 },
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export type ChannelEvents = { notify: NotificationHandler };
 | 
			
		||||
export type NotificationHandler = (payload: string, process_id: number) => void;
 | 
			
		||||
export type ChannelEvents = { notify: NotificationHandler };
 | 
			
		||||
export interface Channel
 | 
			
		||||
  extends TypedEmitter<ChannelEvents>,
 | 
			
		||||
    Result,
 | 
			
		||||
@ -507,9 +507,19 @@ 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 Disposable
 | 
			
		||||
  implements Postgres, Disposable
 | 
			
		||||
{
 | 
			
		||||
  readonly #options;
 | 
			
		||||
  readonly #params;
 | 
			
		||||
@ -1626,8 +1636,9 @@ export interface PoolTransaction extends Transaction {
 | 
			
		||||
 | 
			
		||||
export class Pool<V extends PoolEvents = PoolEvents>
 | 
			
		||||
  extends TypedEmitter<V>
 | 
			
		||||
  implements PromiseLike<PoolWire>, Disposable
 | 
			
		||||
  implements Postgres, PromiseLike<PoolWire>, Disposable
 | 
			
		||||
{
 | 
			
		||||
  readonly #options;
 | 
			
		||||
  readonly #acquire;
 | 
			
		||||
  readonly #begin;
 | 
			
		||||
  readonly #close;
 | 
			
		||||
@ -1638,7 +1649,15 @@ export class Pool<V extends PoolEvents = PoolEvents>
 | 
			
		||||
      acquire: this.#acquire,
 | 
			
		||||
      begin: this.#begin,
 | 
			
		||||
      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>;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user