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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | 131x 131x 4043x 424x 53x 3207x 13x 7x 5x 1x | import { counter, finder } from '../model-operations.ts';
import type {
AnyModel,
Attributes,
CountOptions,
CountWithOptions,
FindAndCountResult,
GroupedCountResultItem,
FindAndCountOptions,
FindOptions,
FindOrCreateOptions,
ModelStatic,
} from '../options.ts';
import type { NonNullFindOptions } from '../v6-aliases.ts';
import type { ModelConstructor } from './model-constructor.ts';
/**
* Read statics of `Model`: `findAll`, `findOne`, `findByPk`, `count`, `findAndCountAll`, `findOrCreate`, `findOrBuild`.
*
* @internal
*/
// oxlint-disable-next-line max-lines-per-function, typescript/explicit-module-boundary-types -- a mixin factory's body is its class (each method is still size-checked); its type can only be inferred.
export const ModelFinders = <TBase extends ModelConstructor>(Base: TBase) => {
return class FindersCapability extends Base {
/**
* Selects all rows matching the options, applying the default scope and, for paranoid models,
* hiding soft-deleted rows. Runs `beforeFind` and `afterFind` hooks unless `hooks: false`.
*
* @param this - The model class (or a scoped model class from {@link Model.scope}).
* @param options - Query options (`where`, `attributes`, `order`, `limit`, `include`, ...).
* @returns Instances, or plain objects with `raw: true`.
* @example
* ```ts
* const posts = await Post.findAll({
* where: { published: true },
* order: [['createdAt', 'DESC']],
* limit: 20,
* include: [User],
* });
* ```
* @see {@link Model.findEach} to stream large result sets.
*/
static findAll<M extends AnyModel>(
this: ModelStatic<M>,
options: FindOptions<Attributes<M>> = {},
): Promise<M[]> {
return finder.findAll(this, options as FindOptions);
}
/**
* Selects the first row matching the options and rejects when none is found.
*
* @param this - The model class (or a scoped model class from {@link Model.scope}).
* @param options - Query options with `rejectOnEmpty`.
* @returns Instance.
* @throws EmptyResultError (or the given error) when nothing is found.
* @example
* ```ts
* const user = await User.findOne({ where: { email: 'ada@example.com' }, rejectOnEmpty: true });
* ```
*/
static findOne<M extends AnyModel>(
this: ModelStatic<M>,
options: NonNullFindOptions<Attributes<M>>,
): Promise<M>;
/**
* Selects the first row matching the options (`LIMIT 1` unless `limit` is given).
*
* @param this - The model class (or a scoped model class from {@link Model.scope}).
* @param options - Query options.
* @returns Instance or `null`.
*/
static findOne<M extends AnyModel>(
this: ModelStatic<M>,
options?: FindOptions<Attributes<M>>,
): Promise<M | null>;
static findOne<M extends AnyModel>(
this: ModelStatic<M>,
options: FindOptions<Attributes<M>> = {},
): Promise<M | null> {
return finder.findOne(this, options as FindOptions);
}
/**
* Selects the row with the given primary key and rejects when none is found.
*
* @param this - The model class (or a scoped model class from {@link Model.scope}).
* @param key - Primary key value.
* @param options - Query options other than `where`, with `rejectOnEmpty`.
* @returns Instance.
* @throws EmptyResultError (or the given error) when nothing is found.
* @example
* ```ts
* const user = await User.findByPk(42, { rejectOnEmpty: true });
* ```
*/
static findByPk<M extends AnyModel>(
this: ModelStatic<M>,
key: unknown,
options: Omit<NonNullFindOptions<Attributes<M>>, 'where'>,
): Promise<M>;
/**
* Selects the row with the given primary key.
*
* @param this - The model class (or a scoped model class from {@link Model.scope}).
* @param key - Primary key value.
* @param options - Query options other than `where`.
* @returns Instance or `null`.
* @example
* ```ts
* const user = await User.findByPk(42, { include: [Profile] });
* ```
*/
static findByPk<M extends AnyModel>(
this: ModelStatic<M>,
key?: unknown,
options?: Omit<FindOptions<Attributes<M>>, 'where'>,
): Promise<M | null>;
static findByPk<M extends AnyModel>(
this: ModelStatic<M>,
key?: unknown,
options: Omit<FindOptions<Attributes<M>>, 'where'> = {},
): Promise<M | null> {
return finder.findByPk(this, key, options as Omit<FindOptions, 'where'>);
}
/**
* Counts rows per group, applying the default scope and the paranoid filter. Runs `beforeCount`
* hooks unless `hooks: false`.
*
* @param this - The model class (or a scoped model class from {@link Model.scope}).
* @param options - Count options with `group`.
* @returns One `{ count, ...groupColumns }` item per group.
* @example
* ```ts
* const rows = await User.count({ group: ['role'] });
* rows.map((r) => r.count);
* ```
*/
static count<M extends AnyModel>(
this: ModelStatic<M>,
options: CountWithOptions<Attributes<M>>,
): Promise<GroupedCountResultItem[]>;
/**
* Counts rows, applying the default scope and the paranoid filter. Runs `beforeCount` hooks
* unless `hooks: false`.
*
* @param this - The model class (or a scoped model class from {@link Model.scope}).
* @param options - Count options (`where`, `col`, `distinct`, `include`).
* @returns The count. Use the `group` overload for per-group counts.
* @example
* ```ts
* const active = await User.count({ where: { active: true } });
* ```
*/
static count<M extends AnyModel>(
this: ModelStatic<M>,
options?: Omit<CountOptions<Attributes<M>>, 'group'>,
): Promise<number>;
static count<M extends AnyModel>(
this: ModelStatic<M>,
options: CountOptions<Attributes<M>> = {},
): Promise<number | GroupedCountResultItem[]> {
return counter.count(this, options as CountOptions);
}
/**
* Runs {@link Model.findAll} and {@link Model.count} with the same filter, so `limit` / `offset`
* paginate the rows while `count` is the total.
*
* @param this - The model class (or a scoped model class from {@link Model.scope}).
* @param options - Query options.
* @returns Total count and the current page of rows.
* @example
* ```ts
* const { count, rows } = await Post.findAndCountAll({ where: { published: true }, limit: 10, offset: 20 });
* ```
*/
static findAndCountAll<M extends AnyModel>(
this: ModelStatic<M>,
options?: Omit<FindAndCountOptions<Attributes<M>>, 'group'>,
): Promise<FindAndCountResult<M>>;
/**
* With `group`, `count` is an array with one item per group (the grouped attributes and the row
* count of the group) instead of a number.
*
* @param this - The model class (or a scoped model class from {@link Model.scope}).
* @param options - Query options including `group`.
* @returns Per-group counts and the current page of rows.
* @example
* ```ts
* const { count, rows } = await Post.findAndCountAll({ group: ['status'] });
* ```
*/
static findAndCountAll<M extends AnyModel>(
this: ModelStatic<M>,
options: Omit<FindAndCountOptions<Attributes<M>>, 'group'> &
Required<Pick<FindAndCountOptions<Attributes<M>>, 'group'>>,
): Promise<FindAndCountResult<M, GroupedCountResultItem[]>>;
static findAndCountAll<M extends AnyModel>(
this: ModelStatic<M>,
options: FindAndCountOptions<Attributes<M>> = {},
): Promise<FindAndCountResult<M, number | GroupedCountResultItem[]>> {
return finder.findAndCountAll(this, options as FindAndCountOptions);
}
/**
* Finds a row, or creates it when none matches. Creation runs inside a transaction (a nested
* one when `options.transaction` is given); when creation fails but the row exists after all
* (a concurrent insert won), that row is returned, otherwise the error is rethrown.
*
* @param this - The model class (or a scoped model class from {@link Model.scope}).
* @param options - `where` to search (its equality values also seed the new row), `defaults` to add on creation.
* @returns `[instance, created]`.
* @example
* ```ts
* const [tag, created] = await Tag.findOrCreate({ where: { name: 'news' }, defaults: { color: 'red' } });
* ```
*/
static findOrCreate<M extends AnyModel>(
this: ModelStatic<M>,
options: FindOrCreateOptions<Attributes<M>>,
): Promise<[M, boolean]> {
return finder.findOrCreate(this, options as FindOrCreateOptions);
}
/**
* Finds a row; when none matches, inserts it and, if that fails with a unique violation (a concurrent
* insert won), reads the row again. Unlike `findOrCreate` the insert is not wrapped in a transaction, so it
* suits code that already handles the race itself. Inside a transaction the insert uses a savepoint, which
* fixes v6 returning an unsaved instance after a conflict.
*
* @param this - The model class (or a scoped model class from {@link Model.scope}).
* @param options - `where` to search (required; its equality values also seed the new row), `defaults` to add on creation.
* @returns `[instance, created]`.
* @throws Error when `where` is missing.
* @example
* ```ts
* const [tag, created] = await Tag.findCreateFind({ where: { name: 'news' }, defaults: { color: 'red' } });
* ```
*/
static findCreateFind<M extends AnyModel>(
this: ModelStatic<M>,
options: FindOrCreateOptions<Attributes<M>>,
): Promise<[M, boolean]> {
return finder.findCreateFind(this, options as FindOrCreateOptions);
}
/**
* Finds a row, or builds a new unsaved instance when none matches; nothing is inserted.
*
* @param this - The model class (or a scoped model class from {@link Model.scope}).
* @param options - `where` to search (its equality values also seed the built instance), `defaults` to add when building.
* @returns `[instance, built]`.
* @example
* ```ts
* const [tag, built] = await Tag.findOrBuild({ where: { name: 'news' } });
* if (built) {
* await tag.save();
* }
* ```
*/
static findOrBuild<M extends AnyModel>(
this: ModelStatic<M>,
options: FindOrCreateOptions<Attributes<M>>,
): Promise<[M, boolean]> {
return finder.findOrBuild(this, options as FindOrCreateOptions);
}
};
};
|