All files / src query.ts

100% Statements 86/86
0% Branches 0/4
100% Functions 40/40
100% Lines 77/77
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 2032x 2x 2x 2x 2x 2x 2x 2x 2x   2x           2x             2x   85x 85x   73x 3x 7x 2x   2x 3x   2x 67x   2x 2x   2x 2x   2x 4x               2x   2x         7x 7x 7x   7x 7x   7x               7x     2x       67x 67x 67x       67x 67x     65x       67x     2x         4x 4x 4x   4x 4x   2x         2x   2x       4x     2x         3x 3x 3x       3x 3x       2x     2x         2x       3x     2x         4x 4x   4x       4x 4x         3x   3x 3x           4x     2x               2x              
import { head } from "fp-ts/lib/Array";
import { either, fromPredicate, right } from "fp-ts/lib/Either";
import { constant, identity, or, Predicate } from "fp-ts/lib/function";
import { NonEmptyArray } from "fp-ts/lib/NonEmptyArray";
import { Option, option } from "fp-ts/lib/Option";
import { ask, fromTaskEither, ReaderTaskEither } from "fp-ts/lib/ReaderTaskEither";
import { fromEither } from "fp-ts/lib/TaskEither";
import { sequence } from "fp-ts/lib/Traversable";
import * as t from "io-ts";
import { QueryConfig } from "pg";
import {
  makeRowValidationError,
  PgDriverQueryError,
  PgRowCountError,
  PgRowValidationError,
} from "./errors";
import {
  ConnectedEnvironment,
  Connection,
  connectionLens,
  QueryResult,
  RowTransformer,
} from "./types";
import { defaultCamelCaser } from "./utils/camelify";
 
const executeQuery = (query: QueryConfig, context: t.mixed) => (connection: Connection) =>
  connection.query(query, context);
 
const isNoneResult: Predicate<QueryResult> = ({ rows }) => rows.length === 0;
const isNonEmptyResult: Predicate<QueryResult> = ({ rows }) => rows.length > 0;
const isOneResult: Predicate<QueryResult> = ({ rows }) => rows.length === 1;
const isOneOrNoneResult: Predicate<QueryResult> = or(isNoneResult, isOneResult);
 
const expectedAtLeastOneErrorFailure = (query: QueryConfig, context: t.mixed) =>
  new PgRowCountError(query, ">= 1", "0", context);
 
const expectedNoneFoundSomeErrorFailure = (query: QueryConfig, context: t.mixed) =>
  new PgRowCountError(query, "0", ">= 1", context);
 
const expectedOneFoundManyErrorFailure = (query: QueryConfig, context: t.mixed) =>
  new PgRowCountError(query, "1", "> 1", context);
 
const expectedOneFoundNoneErrorFailure = (query: QueryConfig, context: t.mixed) =>
  new PgRowCountError(query, "1", "0", context);
 
const expectedOneOrNoneErrorFailure = (query: QueryConfig, context: t.mixed) =>
  new PgRowCountError(query, "0 or 1", "> 1", context);
 
export type QueryAnyError = PgDriverQueryError | PgRowValidationError;
export type QueryNoneError = PgDriverQueryError | PgRowCountError;
export type QueryOneError = PgDriverQueryError | PgRowValidationError | PgRowCountError;
export type QueryOneOrMoreError = PgDriverQueryError | PgRowValidationError | PgRowCountError;
export type QueryOneOrNoneError = PgDriverQueryError | PgRowValidationError | PgRowCountError;
 
const sequenceEO = sequence(either, option);
 
const queryAny = (transformer: RowTransformer = identity) => <A = any>(
  type: t.Type<A, any, t.mixed>,
  query: QueryConfig,
  context?: t.mixed,
): ReaderTaskEither<ConnectedEnvironment, QueryAnyError, A[]> =>
  ask<ConnectedEnvironment, QueryAnyError>().chain(environment => {
    const connection = connectionLens.get(environment);
    const queryResultTE = executeQuery(query, context)(connection).mapLeft<QueryAnyError>(identity);
 
    const decodedQueryResultTE = queryResultTE.chain(({ rows }) => {
      const transformedRows = transformer(rows);
 
      return fromEither(
        t
          .array(type)
          .decode(transformedRows)
          .mapLeft(makeRowValidationError(type, transformedRows, context)),
      );
    });
 
    return fromTaskEither(decodedQueryResultTE);
  });
 
const queryNone = (
  query: QueryConfig,
  context?: t.mixed,
): ReaderTaskEither<ConnectedEnvironment, QueryNoneError, void> =>
  ask<ConnectedEnvironment, QueryNoneError>().chain(environment => {
    const connection = connectionLens.get(environment);
    const queryResultTE = executeQuery(query, context)(connection).mapLeft<QueryNoneError>(
      identity,
    );
 
    const decodedQueryResultTE = queryResultTE.chain(result =>
      fromEither(
        fromPredicate(isNoneResult, constant(expectedNoneFoundSomeErrorFailure(query, context)))(
          result,
        ).map<void>(() => void 0),
      ),
    );
 
    return fromTaskEither(decodedQueryResultTE);
  });
 
const queryOne = (transformer: RowTransformer = identity) => <A = any>(
  type: t.Type<A, any, t.mixed>,
  query: QueryConfig,
  context?: t.mixed,
): ReaderTaskEither<ConnectedEnvironment, QueryOneError, A> =>
  ask<ConnectedEnvironment, QueryOneError>().chain(environment => {
    const connection = connectionLens.get(environment);
    const queryResultTE = executeQuery(query, context)(connection).mapLeft<QueryOneError>(identity);
 
    const decodedQueryResultTE = queryResultTE.chain(result =>
      fromEither(
        fromPredicate(isOneResult, r =>
          fromPredicate(isNoneResult, identity)(r).fold(
            constant(expectedOneFoundManyErrorFailure(query, context)),
            constant(expectedOneFoundNoneErrorFailure(query, context)),
          ),
        )(result)
          .map(({ rows }) => transformer(rows)[0])
          .mapLeft<QueryOneError>(identity)
          .chain(row => type.decode(row).mapLeft(makeRowValidationError(type, row, context))),
      ),
    );
 
    return fromTaskEither(decodedQueryResultTE);
  });
 
const queryOneOrMore = (transformer: RowTransformer = identity) => <A = any>(
  type: t.Type<A, any, t.mixed>,
  query: QueryConfig,
  context?: t.mixed,
): ReaderTaskEither<ConnectedEnvironment, QueryOneOrMoreError, NonEmptyArray<A>> =>
  ask<ConnectedEnvironment, QueryOneOrMoreError>().chain(environment => {
    const connection = connectionLens.get(environment);
    const queryResultTE = executeQuery(query, context)(connection).mapLeft<QueryOneOrMoreError>(
      identity,
    );
 
    const decodedQueryResultTE = queryResultTE.chain(result =>
      fromEither(
        fromPredicate(isNonEmptyResult, constant(expectedAtLeastOneErrorFailure(query, context)))(
          result,
        )
          .map(({ rows }) => transformer(rows))
          .mapLeft<QueryOneOrMoreError>(identity)
          .chain(rows =>
            t
              .array(type)
              .decode(rows)
              .mapLeft(makeRowValidationError(type, rows, context)),
          )
          .map(rows => new NonEmptyArray(rows[0], rows.slice(1))),
      ),
    );
 
    return fromTaskEither(decodedQueryResultTE);
  });
 
const queryOneOrNone = (transformer: RowTransformer = identity) => <A = any>(
  type: t.Type<A, any, t.mixed>,
  query: QueryConfig,
  context?: t.mixed,
): ReaderTaskEither<ConnectedEnvironment, QueryOneOrNoneError, Option<A>> =>
  ask<ConnectedEnvironment, QueryOneOrNoneError>().chain(environment => {
    const connection = connectionLens.get(environment);
 
    const queryResultTE = executeQuery(query, context)(connection).mapLeft<QueryOneOrNoneError>(
      identity,
    );
 
    const decodedQueryResultTE = queryResultTE.chain(result =>
      fromEither(
        fromPredicate(isOneOrNoneResult, constant(expectedOneOrNoneErrorFailure(query, context)))(
          result,
        )
          .mapLeft<QueryOneOrNoneError>(identity)
          .map(({ rows }) => transformer(rows))
          .chain(rows =>
            right<t.Errors, Option<t.mixed>>(head(rows))
              .chain(rowO => sequenceEO(rowO.map(row => type.decode(row))))
              .mapLeft(makeRowValidationError(type, rows, context)),
          ),
      ),
    );
 
    return fromTaskEither(decodedQueryResultTE);
  });
 
export const configurableQueries = {
  queryAny,
  queryNone,
  queryOne,
  queryOneOrMore,
  queryOneOrNone,
};
 
export const camelCasedQueries = {
  queryAny: queryAny(defaultCamelCaser),
  queryNone,
  queryOne: queryOne(defaultCamelCaser),
  queryOneOrMore: queryOneOrMore(defaultCamelCaser),
  queryOneOrNone: queryOneOrNone(defaultCamelCaser),
};