All files / src types.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 0/0
100% Lines 3/3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79      2x                               2x                                                                                                               2x      
import { ReaderTaskEither } from "fp-ts/lib/ReaderTaskEither";
import { TaskEither } from "fp-ts/lib/TaskEither";
import * as t from "io-ts";
import { Lens } from "monocle-ts";
import * as pg from "pg";
import {
  PgDriverQueryError,
  PgPoolCheckoutError,
  PgPoolShutdownError,
  PgTransactionRollbackError,
  PgUnhandledConnectionError,
  PgUnhandledPoolError,
} from "./errors";
 
export interface Connection {
  query(config: pg.QueryConfig, context: t.mixed): TaskEither<PgDriverQueryError, QueryResult>;
  release(err?: Error): void;
}
 
export const ConnectionSymbol = Symbol("pg-ts connection");
export type ConnectionSymbol = typeof ConnectionSymbol;
 
export interface ConnectedEnvironment {
  [ConnectionSymbol]: Connection;
}
 
export type ConnectionError<L> = PgPoolCheckoutError | PgUnhandledConnectionError | L;
 
export interface ConnectionPool {
  end(): TaskEither<PgPoolShutdownError, void>;
 
  withConnection<L, A>(
    program: ReaderTaskEither<ConnectedEnvironment, L, A>,
  ): TaskEither<ConnectionError<L>, A>;
 
  withConnectionE<E extends {}, L, A>(
    program: ReaderTaskEither<E & ConnectedEnvironment, L, A>,
  ): ReaderTaskEither<E, ConnectionError<L>, A>;
}
 
export interface ConnectionPoolConfig extends pg.PoolConfig {
  onError: (err: PgUnhandledPoolError) => void;
  parsers?: TypeParsers;
}
 
export type ErrorPredicate<T> = (v: t.mixed) => v is T;
 
export interface QueryResult extends pg.QueryResult {
  rows: t.mixed[];
}
 
export type RowTransformer = (x: t.mixed[]) => t.mixed[];
 
export type TransactionError<L> =
  | PgDriverQueryError
  | PgTransactionRollbackError
  | PgUnhandledConnectionError
  | L;
 
export type TransactionIsolationLevel =
  | "READ UNCOMMITTED"
  | "READ COMMITTED"
  | "REPEATABLE READ"
  | "SERIALIZABLE";
 
export interface TransactionOptions {
  readonly context: t.mixed;
  readonly deferrable: boolean;
  readonly isolation: TransactionIsolationLevel;
  readonly readOnly: boolean;
}
 
export type TypeParser<T> = (val: string) => T;
export type TypeParsers = Record<string, TypeParser<any>>;
 
export const connectionLens = Lens.fromProp<ConnectedEnvironment, ConnectionSymbol>(
  ConnectionSymbol,
);