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 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 203 204 205 206 207 208 209 210 | import type { IncludeOption } from '../associations/include.ts';
import type { DataTypeInput } from '../data-types/data-types.ts';
import type { CteFindOptions } from './cte-builder.ts';
import type { Values } from './options.ts';
import type { QueryOptionsBase } from './runtime.ts';
import type { TableSampleOptions } from './source-options.ts';
import type { SelectOptions } from './select-query-builder.ts';
import type { WhereOptions } from './types.ts';
/** One group of a grouped count (Sequelize v6 `GroupedCountResultItem`): the grouped attributes plus the group's row count. */
export type GroupedCountResultItem = {
/** Value of each grouped / projected attribute. */
[key: string]: unknown;
/** Number of rows in the group. */
count: number;
};
/** Result of `findAndCountAll`: one page of rows plus the total count (an array of {@link GroupedCountResultItem} with `group`). */
export type FindAndCountResult<M, C = number> = {
/** Total number of matching rows, ignoring `limit` / `offset`. */
count: C;
/** Rows of the current page. */
rows: M[];
};
/**
* Options of the finders (`findAll`, `findOne`, `findByPk`, ...), without association loading details.
*
* Extends the query options (`transaction`, `logging`, ...) and the SELECT options with finder flags and the PostgreSQL CTE extension. `include` is typed through {@link IncludeOption}.
* A `beforeFind` hook may change these options before the query runs.
*
* @see {@link Model.findAll}
* @see {@link WhereOptions}
*
* @example
* ```ts
* const opts: FindOptions<User> = {
* where: { active: true, age: { [Op.gte]: 18 } },
* attributes: ['id', 'name'],
* order: [['name', 'ASC']],
* limit: 10,
* include: [Post],
* };
* const rows = await User.findAll(opts);
* ```
*/
export type FindOptions<T = Values> = QueryOptionsBase &
Omit<SelectOptions, 'where'> & {
/** Filter rows; see {@link WhereOptions}. All rows when omitted. */
where?: WhereOptions<T>;
/** Return plain row objects instead of model instances.
* @defaultValue false */
raw?: boolean;
/** With `raw`, build nested objects from dotted keys such as `'posts.title'`.
* @defaultValue false */
nest?: boolean;
/** Return a single row instead of an array (set internally by `findOne`).
* @defaultValue false */
plain?: boolean;
/** Throw `EmptyResultError` (or the given `Error`) when no row is found.
* @defaultValue false */
rejectOnEmpty?: boolean | Error;
/** For paranoid models: `false` includes soft-deleted rows.
* @defaultValue true */
paranoid?: boolean;
/** Run `beforeFind` / `afterFind` hooks.
* @defaultValue true */
hooks?: boolean;
/** Eager-load associations through JOINs (or separate queries for `separate` includes). */
include?: IncludeOption;
/** `false` disables the sub-query used for `limit` combined with multi-row includes. */
subQuery?: boolean;
/**
* Alias of the main table in `FROM` instead of the model name, so `literal`s and `col('alias.x')`
* can refer to it (a v6 internal option that v6's typings omit). Ignored when `include` is set.
*/
tableAs?: string;
} & CteFindOptions;
/**
* Options of `findAndCountAll` (Sequelize v6 `FindAndCountOptions`): the finder options plus the
* count options `distinct` and `col`. The count query reuses `where`, `include`, `paranoid`,
* `distinct`, `col`, `group`, `replacements`, `transaction` and `logging`. With `group`, `count` is an array of {@link GroupedCountResultItem} like in v6.
*
* @see {@link Model.findAndCountAll}
*
* @example
* ```ts
* const options: FindAndCountOptions<User> = { where: { active: true }, limit: 10, offset: 20 };
* const { count, rows } = await User.findAndCountAll(options);
* ```
*/
export type FindAndCountOptions<T = Values> = FindOptions<T> &
Readonly<{
/** Count with `count(DISTINCT ...)`.
* @defaultValue false */
distinct?: boolean;
/** Column to count. Default: `*` (the primary key with `distinct`). */
col?: string;
}>;
/**
* Options of `count`.
*
* Includes joins and CTE options; `count` fires `beforeCount` hooks.
*
* @see {@link Model.count}
*
* @example
* ```ts
* const total = await Post.count({ where: { published: true }, distinct: true, col: 'authorId' });
* const opts: CountOptions<Post> = { group: 'authorId' };
* const perAuthor = await Post.count(opts); // [{ authorId, count }, ...]
* ```
*/
export type CountOptions<T = Values> = QueryOptionsBase &
Readonly<{
/** Filter rows; see {@link WhereOptions}. */
where?: WhereOptions<T>;
/** Column to count. Default: `*` (primary key with `distinct`). */
col?: string;
/** Use `count(DISTINCT ...)`.
* @defaultValue false */
distinct?: boolean;
/** Group by attributes; the result becomes an array of `{ count, ...groups }`. */
group?: string | readonly string[];
/** For paranoid models: `false` includes soft-deleted rows.
* @defaultValue true */
paranoid?: boolean;
/** Run `beforeCount` hooks.
* @defaultValue true */
hooks?: boolean;
/** Join associations (rows are counted per joined row unless `distinct`). */
include?: IncludeOption;
/**
* Count only a random sample of the table (`TABLESAMPLE`). PostgreSQL extension.
* @postgrelize
*/
sample?: TableSampleOptions;
}> &
CteFindOptions;
/**
* {@link CountOptions} with a required `group`, which makes {@link Model.count} resolve to one {@link GroupedCountResultItem} per group.
*
* @see {@link Model.count}
*
* @example
* ```ts
* const opts: CountWithOptions<Post> = { group: ['authorId'] };
* const perAuthor = await Post.count(opts); // [{ authorId, count }, ...]
* ```
*/
export type CountWithOptions<T = Values> = CountOptions<T> &
Readonly<{
/** Group by attributes; the result is an array of `{ count, ...groups }`. */
group: string | readonly string[];
}>;
/**
* Options of `aggregate`, `max`, `min` and `sum`.
*
* Same as {@link CountOptions} plus `plain`.
*
* @see {@link Model.aggregate}
*
* @example
* ```ts
* const opts: AggregateOptions<Product> = { where: { active: true }, paranoid: false };
* const highest = await Product.max('price', opts);
* ```
*/
export type AggregateOptions<T = Values> = CountOptions<T> &
Readonly<{
/**
* Return all rows exactly as the driver delivers them (numeric results such as `sum` are
* strings, like in Sequelize v6) instead of one converted value.
*
* @defaultValue `true`
*/
plain?: boolean;
/**
* Data type that the single result is converted to: `INTEGER` / `BIGINT` give a number,
* `FLOAT` / `DECIMAL` a float, `DATE` a `Date`. Defaults to the type of the aggregated
* attribute, or `FLOAT` for `'*'`. Ignored with `plain: false`.
*/
dataType?: DataTypeInput;
}>;
/**
* Options of `findOrCreate` and `findOrBuild`.
*
* Extends {@link FindOptions}; the `where` values are also used when creating the row.
*
* @see {@link Model.findOrCreate}
*
* @example
* ```ts
* const [user, created] = await User.findOrCreate({
* where: { email: 'a@example.com' },
* defaults: { name: 'Ann' },
* });
* ```
*/
export type FindOrCreateOptions<T = Values> = FindOptions<T> &
Readonly<{
/** Values used in addition to the plain equality values of `where` when the row is created. */
defaults?: Partial<T>;
}>;
|