All files / src/model model.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 2/2
100% Lines 7/7

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                                131x                                                                                                                   1x                   139583x       131x 262x 3013x 2751x                
import { ModelBase } from './model-base.ts';
import { ModelInstancePersistence } from './model-instance-persistence.ts';
import { ModelInstanceValues } from './model-instance-values.ts';
import { ModelAggregates } from './mixins/model-aggregates.ts';
import { ModelAssociations } from './mixins/model-associations.ts';
import { ModelFinders } from './mixins/model-finders.ts';
import { ModelHooks } from './mixins/model-hooks.ts';
import { ModelPostgres } from './mixins/model-postgres.ts';
import { ModelSchema } from './mixins/model-schema.ts';
import { ModelSchemaBinding } from './mixins/model-schema-binding.ts';
import { ModelScopes } from './mixins/model-scopes.ts';
import { ModelWriters } from './mixins/model-writers.ts';
import type { BuildOptions, Values } from './options.ts';
 
export type { NamedHookAdder } from './mixins/named-hooks.ts';
 
const ModelStatics = ModelPostgres(
  ModelWriters(
    ModelAggregates(
      ModelFinders(
        ModelSchemaBinding(ModelScopes(ModelHooks(ModelSchema(ModelAssociations(ModelBase))))),
      ),
    ),
  ),
);
 
// Instance methods use the class type parameters, which mixins cannot see: they are declared on the
// merged interface below and copied onto the prototype at the end of this file.
// oxlint-disable-next-line typescript/no-unsafe-declaration-merging -- intentional merge.
export interface Model<
  // oxlint-disable-next-line typescript/no-explicit-any -- Sequelize v6 compat: the `any` default lets interface-typed models (no index signature) and untyped `Model` work.
  TAttributes extends {} = any,
  // oxlint-disable-next-line no-unused-vars -- merged declarations need identical type parameters.
  TCreationAttributes extends {} = TAttributes,
>
  extends ModelInstanceValues<TAttributes>, ModelInstancePersistence<TAttributes> {}
 
/**
 * Base class of models, API-compatible with Sequelize v6 `Model`.
 *
 * @typeParam TAttributes - Attribute types (usually `InferAttributes<M>`).
 * @typeParam TCreationAttributes - Attribute types accepted on creation (usually `InferCreationAttributes<M>`).
 * @example
 * ```ts
 * class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
 *   declare id: CreationOptional<number>;
 *   declare email: string;
 * }
 * User.init({ id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, email: DataTypes.STRING }, { sequelize });
 * ```
 */
// oxlint-disable-next-line typescript/no-unsafe-declaration-merging -- see the interface above.
export class Model<
  // oxlint-disable-next-line typescript/no-explicit-any -- Sequelize v6 compat, see the interface above.
  TAttributes extends {} = any,
  TCreationAttributes extends {} = TAttributes,
> extends ModelStatics {
  /** Phantom type of attributes; not set at runtime. */
  declare _attributes: TAttributes;
 
  /** Phantom type of creation attributes; not set at runtime. */
  declare _creationAttributes: TCreationAttributes;
 
  /** Current attribute values. */
  declare dataValues: TAttributes;
 
  /** Values as of the last load/save. */
  declare _previousDataValues: Partial<TAttributes>;
 
  /** True until the instance is saved or when built from a query result. */
  declare isNewRecord: boolean;
 
  /** Model class of the instance. */
  get _model(): typeof Model {
    return this.constructor as typeof Model;
  }
 
  /**
   * Builds an instance without saving it. Prefer `Model.build`.
   *
   * @param values - Attribute values.
   * @param options - Build options.
   */
  constructor(values: Partial<TCreationAttributes> | Values = {}, options: BuildOptions = {}) {
    super(values as Values, options);
  }
}
 
for (const source of [ModelInstanceValues, ModelInstancePersistence]) {
  for (const key of Object.getOwnPropertyNames(source.prototype)) {
    if (key !== 'constructor') {
      Object.defineProperty(
        Model.prototype,
        key,
        Object.getOwnPropertyDescriptor(source.prototype, key) as PropertyDescriptor,
      );
    }
  }
}