UNPKG

5.79 kBTypeScriptView Raw
1import { DataType } from './data-types';
2import { Model, ModelCtor, ModelType, WhereOptions, Attributes } from './model';
3import { Optional } from './index';
4
5export type Primitive = 'string' | 'number' | 'boolean';
6
7export type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> };
8
9export interface Inflector {
10 singularize(str: string): string;
11 pluralize(str: string): string;
12}
13
14export function useInflection(inflection: Inflector): void;
15
16export function camelizeIf(string: string, condition?: boolean): string;
17export function underscoredIf(string: string, condition?: boolean): string;
18export function isPrimitive(val: unknown): val is Primitive;
19
20/** Same concept as _.merge, but don't overwrite properties that have already been assigned */
21export function mergeDefaults<T>(a: T, b: Partial<T>): T;
22export function spliceStr(str: string, index: number, count: number, add: string): string;
23export function camelize(str: string): string;
24export function format(arr: string[], dialect: string): string;
25export function formatNamedParameters(sql: string, parameters: {
26 [key: string]: string | number | boolean;
27}, dialect: string): string;
28export function cloneDeep<T>(obj: T, fn?: (el: unknown) => unknown): T;
29
30export interface OptionsForMapping<TAttributes> {
31 attributes?: string[];
32 where?: WhereOptions<TAttributes>;
33}
34
35/** Expand and normalize finder options */
36export function mapFinderOptions<M extends Model, T extends OptionsForMapping<Attributes<M>>>(
37 options: T,
38 model: ModelCtor<M>
39): T;
40
41/* Used to map field names in attributes and where conditions */
42export function mapOptionFieldNames<M extends Model, T extends OptionsForMapping<Attributes<M>>>(
43 options: T, model: ModelCtor<M>
44): T;
45
46export function mapWhereFieldNames(attributes: object, model: ModelType): object;
47/** Used to map field names in values */
48export function mapValueFieldNames(dataValues: object, fields: string[], model: ModelType): object;
49
50export function isColString(value: string): boolean;
51export function canTreatArrayAsAnd(arr: unknown[]): boolean;
52export function combineTableNames(tableName1: string, tableName2: string): string;
53
54export function singularize(s: string): string;
55export function pluralize(s: string): string;
56
57export function toDefaultValue<T>(value: unknown): unknown;
58
59/**
60 * Determine if the default value provided exists and can be described
61 * in a db schema using the DEFAULT directive.
62 *
63 * @param value Any default value.
64 */
65export function defaultValueSchemable(hash: DataType): boolean;
66export function stack(): NodeJS.CallSite[];
67export function now(dialect: string): Date;
68
69// Note: Use the `quoteIdentifier()` and `escape()` methods on the
70// `QueryInterface` instead for more portable code.
71export const TICK_CHAR: string;
72export function addTicks(s: string, tickChar?: string): string;
73export function removeTicks(s: string, tickChar?: string): string;
74
75/**
76 * Wraps a constructor to not need the `new` keyword using a proxy.
77 * Only used for data types.
78 */
79export function classToInvokable<T extends new (...args: any[]) => any>(ctor: T): T & {
80 (...args: ConstructorParameters<T>): T;
81}
82
83export class SequelizeMethod {
84
85}
86
87/*
88 * Utility functions for representing SQL functions, and columns that should be escaped.
89 * Please do not use these functions directly, use Sequelize.fn and Sequelize.col instead.
90 */
91export class Fn extends SequelizeMethod {
92 constructor(fn: string, args: unknown[]);
93 public clone(): this;
94}
95
96export class Col extends SequelizeMethod {
97 public col: string;
98 constructor(col: string);
99}
100
101export class Cast extends SequelizeMethod {
102 public val: unknown;
103 public type: string;
104 constructor(val: unknown, type?: string);
105}
106
107export class Literal extends SequelizeMethod {
108 public val: unknown;
109 constructor(val: unknown);
110}
111
112export class Json extends SequelizeMethod {
113 public conditions: object;
114 public path: string;
115 public value: string | number | boolean;
116 constructor(conditionsOrPath: string | object, value?: string | number | boolean);
117}
118
119export class Where extends SequelizeMethod {
120 public attribute: object;
121 public comparator: string;
122 public logic: string | object;
123 constructor(attr: object, comparator: string, logic: string | object);
124 constructor(attr: object, logic: string | object);
125}
126
127export type AnyFunction = (...args: any[]) => any;
128
129/**
130 * Returns all shallow properties that accept `undefined` or `null`.
131 * Does not include Optional properties, only `undefined` or `null`.
132 *
133 * @example
134 * type UndefinedProps = NullishPropertiesOf<{
135 * id: number | undefined,
136 * createdAt: string | undefined,
137 * firstName: string | null, // nullable properties are included
138 * lastName?: string, // optional properties are not included.
139 * }>;
140 *
141 * // is equal to
142 *
143 * type UndefinedProps = 'id' | 'createdAt' | 'firstName';
144 */
145export type NullishPropertiesOf<T> = {
146 [P in keyof T]-?: undefined extends T[P] ? P
147 : null extends T[P] ? P
148 : never
149}[keyof T];
150
151/**
152 * Makes all shallow properties of an object `optional` if they accept `undefined` or `null` as a value.
153 *
154 * @example
155 * type MyOptionalType = MakeUndefinedOptional<{
156 * id: number | undefined,
157 * firstName: string,
158 * lastName: string | null,
159 * }>;
160 *
161 * // is equal to
162 *
163 * type MyOptionalType = {
164 * // this property is optional.
165 * id?: number | undefined,
166 * firstName: string,
167 * // this property is optional.
168 * lastName?: string | null,
169 * };
170 */
171// 'T extends any' is done to support https://github.com/sequelize/sequelize/issues/14129
172// source: https://stackoverflow.com/questions/51691235/typescript-map-union-type-to-another-union-type
173export type MakeNullishOptional<T extends object> = T extends any ? Optional<T, NullishPropertiesOf<T>> : never;