All files / src utils-namespace.ts

33.33% Statements 1/3
100% Branches 0/0
0% Functions 0/2
33.33% Lines 1/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                              131x                                                                                                                      
import { Cast, Col, Fn, Json, Literal, SqlExpression, Where } from './expressions/methods.ts';
import type {
  Cast as CastInstance,
  Col as ColInstance,
  Fn as FnInstance,
  Json as JsonInstance,
  Literal as LiteralInstance,
  Where as WhereInstance,
} from './expressions/methods.ts';
import { inflector, useInflection } from './model/inflector.ts';
 
/**
 * The part of Sequelize v6 `Utils` that has a postgrelize counterpart: the expression classes
 * behind `fn`, `col`, `cast`, `literal`, `json` and `where` (`Utils.Fn`, `Utils.Literal`, ... both
 * as values and as types), `SequelizeMethod` (the base class of expressions), `useInflection`,
 * `pluralize` and `singularize`. The other v6 helpers are internal and not provided.
 *
 * @example
 * ```ts
 * const isLiteral = (value: unknown): value is Utils.Literal => value instanceof Utils.Literal;
 * const amount: Utils.Literal = literal('1 + 1');
 * ```
 */
export const Utils = Object.freeze({
  /** Class of `fn()` results. */
  Fn,
  /** Class of `col()` results. */
  Col,
  /** Class of `cast()` results. */
  Cast,
  /** Class of `literal()` results. */
  Literal,
  /** Class of `json()` results. */
  Json,
  /** Class of `where()` results. */
  Where,
  /** Base class of all expressions. */
  SequelizeMethod: SqlExpression,
  /** Replaces the inflection library, see {@link useInflection}. */
  useInflection,
  /** Plural form of a word. */
  pluralize: (word: string): string => inflector.pluralize(word),
  /** Singular form of a word. */
  singularize: (word: string): string => inflector.singularize(word),
});
 
// Types of `Utils`, kept next to the value so `Utils.Fn` works in type positions (like v6's module).
// oxlint-disable-next-line typescript/no-namespace, typescript/no-redeclare -- the v6 `Utils` is both a value and a type namespace.
export declare namespace Utils {
  /** Expression class behind `fn()`. */
  type Fn = FnInstance;
  /** Expression class behind `col()`. */
  type Col = ColInstance;
  /** Expression class behind `cast()`. */
  type Cast = CastInstance;
  /** Expression class behind `literal()`. */
  type Literal = LiteralInstance;
  /** Expression class behind `json()`. */
  type Json = JsonInstance;
  /** Expression class behind `where()`. */
  type Where = WhereInstance;
  /** Base class of expressions (v6 `SequelizeMethod`). */
  type SequelizeMethod = SqlExpression;
  /** Names of primitive types (v6 `Utils.Primitive`). */
  type Primitive = 'string' | 'number' | 'boolean';
  /** Recursively removes `readonly` (v6 `Utils.DeepWriteable`). */
  type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> };
  /** Inflection functions (v6 `Utils.Inflector`). */
  interface Inflector {
    /** Singular form of a word. */
    singularize(str: string): string;
    /** Plural form of a word. */
    pluralize(str: string): string;
  }
  /** Any function (v6 `Utils.AnyFunction`). */
  // oxlint-disable-next-line typescript/no-explicit-any -- v6 declares it this way.
  type AnyFunction = (...args: any[]) => any;
  /** Keys of `T` that accept `null` or `undefined` (v6 `Utils.NullishPropertiesOf`). */
  type NullishPropertiesOf<T> = {
    [P in keyof T]-?: undefined extends T[P] ? P : null extends T[P] ? P : never;
  }[keyof T];
}