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 | 131x 947x 947x 1x 946x 131x 946x 946x 946x 946x 946x 946x 946x 946x 945x 945x 950x 946x 946x 856x 856x 90x 90x 90x 89x 3x 90x 90x 90x 90x 90x 8x 8x 82x 93x 5x 5x 1x 4x 4x 131x 131x 4x 4x 4x 4x 3x 3x 3x 3x 3x 860x 860x 860x | import type { SqlCompiler } from '../../sql/compile.ts';
import type { AttributeColumns } from '../attribute-columns.ts';
import type { DeleteQueryBuilder } from '../delete-query-builder.ts';
import type { JoinSourceBuilder } from '../join-source-builder.ts';
import type { ModelRegistry } from '../model-registry.ts';
import type { ModelValues } from '../model-values.ts';
import type {
AnyModel,
DestroyOptions,
ModelStatic,
MutableOptions,
RestoreOptions,
} from '../options.ts';
import type { RowMapper } from '../row-mapper.ts';
import type { QueryOptionsBase } from '../runtime.ts';
import type { UpdateQueryBuilder } from '../update-query-builder.ts';
import { ModelOperation, type OperationServices } from './model-operation.ts';
/** Collaborators of {@link DestroyOperation}. */
export type DestroyOperationServices = OperationServices &
Readonly<{
/** Model state and scopes. */
registry: ModelRegistry;
/** Value rules. */
values: ModelValues;
/** SET items, paranoid filter. */
columns: AttributeColumns;
/** UPDATE builder (soft delete, restore). */
updates: UpdateQueryBuilder;
/** DELETE / TRUNCATE builder. */
deletes: DeleteQueryBuilder;
/** `using` relations. */
joins: JoinSourceBuilder;
/** Instances of returned rows. */
rows: RowMapper;
/** SQL compiler. */
compiler: SqlCompiler;
}>;
// One `Model.destroy` call after the bulk hook: the filter and whether rows are soft-deleted.
type DestroyCall = Readonly<{
model: ModelStatic<AnyModel>;
options: DestroyOptions;
where: unknown;
soft: boolean;
}>;
/**
* `Model.destroy` (DELETE, or soft delete of paranoid models), `Model.restore` and
* `Model.truncate`, with bulk and individual hooks.
*/
export class DestroyOperation extends ModelOperation<DestroyOperationServices> {
/**
* `Model.destroy`.
*
* @param model - Model class.
* @param options - Destroy options.
* @returns Number of deleted rows, or deleted instances with `returning: true`.
*/
async destroy<M extends AnyModel>(
model: ModelStatic<M>,
options: DestroyOptions,
): Promise<number | M[]> {
const opts: MutableOptions = {
hooks: true,
individualHooks: false,
force: false,
...this.services.registry.scoped(model, options),
};
if (!opts['where'] && opts['truncate'] !== true) {
throw new Error(
'Missing where or truncate attribute in the options parameter of model.destroy.',
);
}
return (await this.#bulkDestroy(model, opts, options)) as number | M[];
}
@ModelOperation.bulkHooks('beforeBulkDestroy', 'afterBulkDestroy')
async #bulkDestroy(
model: ModelStatic<AnyModel>,
opts: MutableOptions,
options: DestroyOptions,
): Promise<number | AnyModel[]> {
const { definition } = this.services.registry.state(model);
const { columns, lifecycle } = this.services;
const soft = definition.deletedAt !== undefined && opts['force'] !== true;
const where = soft ? columns.paranoid(definition, opts['where'], true) : opts['where'];
const targets = await this.#targets(model, opts, where, options);
await lifecycle.runEach(model, 'beforeDestroy', targets, opts);
const deleted = await this.#delete({ model, options, where, soft });
await lifecycle.runEach(model, 'afterDestroy', targets, opts);
return deleted;
}
async #targets(
model: ModelStatic<AnyModel>,
opts: MutableOptions,
where: unknown,
options: QueryOptionsBase,
): Promise<AnyModel[]> {
return opts['individualHooks'] === true
? model.findAll({
where: where as never,
...this.services.values.queryOptions(options),
paranoid: false,
})
: [];
}
async #delete(call: DestroyCall): Promise<number | AnyModel[]> {
const { model, options } = call;
if (options.truncate === true) {
await model.truncate(options);
return 0;
}
const { runtime } = this.services.registry.state(model);
const node = this.#statement(call);
const result = await runtime.execute(this.services.compiler.compile(node), options);
return options.returning === true
? result.rows.map((row) => this.services.rows.instance(model, row))
: result.rowCount;
}
#statement({ model, options, where, soft }: DestroyCall) {
const { definition, runtime } = this.services.registry.state(model);
const { columns, deletes, joins, updates } = this.services;
const inAsAny = DestroyOperation.#inAsAny(runtime.inAsAnyThreshold);
const returning = options.returning === true ? { returning: true } : {};
if (soft && definition.deletedAt !== undefined) {
const set = columns.setItems(definition, { [definition.deletedAt]: new Date() });
return updates.build(definition, set, where, { ...returning, ...inAsAny });
}
return deletes.build(definition, where, {
...(options.using === undefined ? {} : { using: joins.build(options.using) }),
...returning,
...(options.limit === undefined ? {} : { limit: options.limit }),
...inAsAny,
});
}
static #inAsAny(threshold: number | undefined) {
return threshold === undefined ? {} : { inAsAnyThreshold: threshold };
}
/**
* `Model.restore`: sets `deletedAt = NULL` on matching rows of a paranoid model.
*
* @param model - Model class.
* @param options - Rows to restore and hook options.
* @returns Resolves when done.
* @throws Error when the model is not paranoid.
*/
async restore(model: ModelStatic<AnyModel>, options: RestoreOptions): Promise<void> {
const { definition } = this.services.registry.state(model);
if (definition.deletedAt === undefined) {
throw new Error('Model is not paranoid');
}
const opts: MutableOptions = { hooks: true, individualHooks: false, ...options };
await this.#bulkRestore(model, opts, options, definition.deletedAt);
}
@ModelOperation.bulkHooks('beforeBulkRestore', 'afterBulkRestore')
async #bulkRestore(
model: ModelStatic<AnyModel>,
opts: MutableOptions,
options: RestoreOptions,
deletedAt: string,
): Promise<void> {
const { definition, runtime } = this.services.registry.state(model);
const { columns, compiler, lifecycle, updates } = this.services;
const targets = await this.#targets(model, opts, opts['where'], options);
await lifecycle.runEach(model, 'beforeRestore', targets, opts);
const set = columns.setItems(definition, { [deletedAt]: null });
const inAsAny = DestroyOperation.#inAsAny(runtime.inAsAnyThreshold);
const node = updates.build(definition, set, opts['where'], inAsAny);
await runtime.execute(compiler.compile(node), options);
await lifecycle.runEach(model, 'afterRestore', targets, opts);
}
/**
* `Model.truncate`.
*
* @param model - Model class.
* @param options - `cascade`, `restartIdentity` and query options.
* @returns Resolves when done.
*/
async truncate(
model: object,
options: QueryOptionsBase & Readonly<{ cascade?: boolean; restartIdentity?: boolean }>,
): Promise<void> {
const { definition, runtime } = this.services.registry.state(model);
const { compiler, deletes } = this.services;
await runtime.execute(compiler.compile(deletes.truncate(definition, options)), options);
}
}
|