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 | 131x 131x 262x 131x 439x 131x 131x 1x 3x 1x 3x 3x 131x 258x 258x 258x 257x 428x 258x 428x 258x 257x 131x 131x 34x 5x 31x 1x 259x 1x 258x 258x 2x 2x 2x 1x 1x 1x 1x 3x | import { ModelRegistry } from '../model/model-registry.ts';
import { syncHooks } from '../model/model-services.ts';
import type { QueryOptionsBase } from '../model/runtime.ts';
import {
SchemaSynchronizer,
type DropOptions,
type SyncOptions,
} from '../query-interface/schema-synchronizer.ts';
import type { SequelizeBase } from './sequelize-base.ts';
import type { SequelizeConstructor } from './sequelize-constructor.ts';
const registry = new ModelRegistry();
const synchronizer = (sequelize: SequelizeBase): SchemaSynchronizer =>
new SchemaSynchronizer(sequelize.internals.runtime, sequelize.getQueryInterface());
const definitions = (sequelize: SequelizeBase): ReturnType<ModelRegistry['definition']>[] =>
Object.values(sequelize.models).map((model) => registry.definition(model));
const CYCLIC_TRUNCATE =
'Sequelize#truncate: Some of your models have cyclic references (foreign keys). You need to use the "cascade" option to be able to delete rows from models that have cyclic references.';
type TruncateOptions = QueryOptionsBase &
Readonly<{ cascade?: boolean; restartIdentity?: boolean }>;
const truncations = (
sequelize: SequelizeBase,
ordered: readonly ReturnType<ModelRegistry['definition']>[],
options: TruncateOptions,
): (() => Promise<void>)[] => {
const byDefinition = new Map(
Object.values(sequelize.models).map((model) => [registry.definition(model), model]),
);
return ordered.map(
(definition) => () =>
(
byDefinition.get(definition) as unknown as {
truncate: (o: TruncateOptions) => Promise<void>;
}
).truncate(options),
);
};
const runSync = async (sequelize: SequelizeBase, options: SyncOptions): Promise<void> => {
const hooks = options.hooks !== false;
const hookOptions = { ...options, hooks };
if (hooks) {
await sequelize.internals.hooks.run('beforeBulkSync', sequelize, hookOptions);
}
const models = new Map(Object.values(sequelize.models).map((m) => [registry.definition(m), m]));
await synchronizer(sequelize).syncAll(definitions(sequelize), options, (definition, run) =>
syncHooks.around(models.get(definition) ?? {}, hookOptions, run),
);
if (hooks) {
await sequelize.internals.hooks.run('afterBulkSync', sequelize, hookOptions);
}
};
/**
* Schema methods of `Sequelize`: schema management, `sync` and `drop`.
*
* @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 SequelizeSchema = <TBase extends SequelizeConstructor>(Base: TBase) =>
class SchemaCapability extends Base {
/**
* Runs `CREATE SCHEMA IF NOT EXISTS` for a schema.
*
* @param schema - Schema name.
* @param options - Query options.
*
* @example
* ```ts
* await sequelize.createSchema('tenant_1');
* ```
*/
async createSchema(schema: string, options?: QueryOptionsBase): Promise<void> {
await this.getQueryInterface().createSchema(schema, options);
}
/**
* Lists the schemas that belong to the application (without `public`, `information_schema` and `pg_*`).
*
* @param options - Query options.
* @returns Schema names.
*
* @example
* ```ts
* const schemas = await sequelize.showAllSchemas(); // e.g. ['tenant_1']
* ```
*/
async showAllSchemas(options?: QueryOptionsBase): Promise<string[]> {
return await this.getQueryInterface().showAllSchemas(options);
}
/**
* Runs `DROP SCHEMA IF EXISTS ... CASCADE`, removing the schema and every object in it.
*
* @param schema - Schema name.
* @param options - Query options.
*
* @example
* ```ts
* await sequelize.dropSchema('tenant_1');
* ```
*/
async dropSchema(schema: string, options?: QueryOptionsBase): Promise<void> {
await this.getQueryInterface().dropSchema(schema, options);
}
/**
* Drops every schema returned by {@link Sequelize.showAllSchemas} with `CASCADE`. Destructive.
*
* @param options - Query options.
*
* @example
* ```ts
* await sequelize.dropAllSchemas();
* ```
*/
async dropAllSchemas(options?: QueryOptionsBase): Promise<void> {
await this.getQueryInterface().dropAllSchemas(options);
}
/**
* Creates (or alters / re-creates) the tables of all defined models in foreign-key order. Reference cycles use deferred foreign keys.
*
* @param options - `force`, `alter`, `match` and query options.
* @returns This instance.
* @throws Error when `options.match` does not match the database name.
* @see {@link Model.sync}
*
* @example
* ```ts
* await sequelize.sync({ alter: { drop: false } });
* await sequelize.sync({ force: true, match: /_test$/ });
* ```
*/
async sync(options: SyncOptions = {}): Promise<this> {
if (options.match !== undefined && !options.match.test(this.config.database ?? '')) {
throw new Error(
`Database "${this.config.database ?? ''}" does not match sync match parameter "${String(options.match)}"`,
);
}
await runSync(this, options);
return this;
}
/**
* Drops the tables of all defined models in reverse foreign-key order. Destructive.
*
* @param options - `cascade` and query options.
* @returns Resolves when done.
*
* @example
* ```ts
* await sequelize.drop({ cascade: true });
* ```
*/
async drop(options: DropOptions = {}): Promise<void> {
await synchronizer(this).dropAll(definitions(this), options);
}
/**
* Empties the tables of all defined models (`TRUNCATE`), tables that reference others first. With
* `cascade` the tables are truncated one after the other, otherwise all at once. Destructive.
*
* @param options - `cascade`, `restartIdentity` and query options.
* @returns Resolves when done.
* @throws Error when foreign keys form a cycle and `cascade` is not set.
*
* @example
* ```ts
* await sequelize.truncate({ cascade: true });
* ```
*/
async truncate(options: TruncateOptions = {}): Promise<void> {
const { models, cyclic } = synchronizer(this).truncationOrder(definitions(this));
if (cyclic && options.cascade !== true) {
throw new Error(CYCLIC_TRUNCATE);
}
const truncates = truncations(this, models, options);
if (options.cascade === true) {
for (const truncate of truncates) {
await truncate();
}
} else E{
await Promise.all(truncates.map((truncate) => truncate()));
}
}
};
|