TS 中文文档 TS 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • 入门教程

    • TypeScript 手册
    • 基础知识
    • 日常类型
    • 类型缩小
    • 更多关于函数
    • 对象类型
    • 从类型创建类型
    • 泛型
    • Keyof 类型运算符
    • Typeof 类型运算符
    • 索引访问类型
    • 条件类型
    • 映射类型
    • 模板字面类型
    • 类
    • 模块
  • 参考手册

  • 项目配置

sequelize-typescript


Build Status

Decorators and some other features for sequelize (v6).

Installation
Model Definition
@Table API
@Column API

Usage
Configuration
globs
Model-path resolving

Model association
One-to-many
Many-to-many
One-to-one
@ForeignKey, @BelongsTo, @HasMany, @HasOne, @BelongsToMany API
Generated getter and setter
Multiple relations of same models

Indexes
@Index API
createIndexDecorator() API

Repository mode
How to enable repository mode?
How to use repository mode?
How to use associations with repository mode?
Limitations of repository mode

Model validation
Scopes
Hooks
Why () => Model?
Recommendations and limitations

Installation


this assumes usage of sequelize@6
sequelize-typescript* requires sequelize
additional typings as documented here and reflect-metadata

  1. ``` shell
  2. npm install --save-dev @types/node @types/validator
  3. npm install sequelize reflect-metadata sequelize-typescript
  4. ```

Your tsconfig.json needs the following flags:

  1. ``` json
  2. "target": "es6", // or a more recent ecmascript version
  3. "experimentalDecorators": true,
  4. "emitDecoratorMetadata": true
  5. ```

Sequelize Options


SequelizeConfig renamed to SequelizeOptions
modelPaths property renamed to models

Scopes Options


The @Scopes and @DefaultScope decorators now take lambda's as options

  1. ``` ts
  2. @DefaultScope(() => ({...}))
  3. @Scopes(() => ({...}))
  4. ```

instead of deprecated way:

  1. ``` ts
  2. @DefaultScope({...})
  3. @Scopes({...}))
  4. ```

Model definition


  1. ``` ts
  2. import { Table, Column, Model, HasMany } from 'sequelize-typescript';

  3. @Table
  4. class Person extends Model {
  5.   @Column
  6.   name: string;

  7.   @Column
  8.   birthday: Date;

  9.   @HasMany(() => Hobby)
  10.   hobbies: Hobby[];
  11. }
  12. ```

Less strict


  1. ``` ts
  2. import { Table, Model } from 'sequelize-typescript';

  3. @Table
  4. class Person extends Model {}
  5. ```

More strict


  1. ``` ts
  2. import { Optional } from 'sequelize';
  3. import { Table, Model } from 'sequelize-typescript';

  4. interface PersonAttributes {
  5.   id: number;
  6.   name: string;
  7. }

  8. interface PersonCreationAttributes extends Optional<PersonAttributes, 'id'> {}

  9. @Table
  10. class Person extends Model<PersonAttributes, PersonCreationAttributes> {}
  11. ```

The model needs to extend the Model class and has to be annotated with the @Table decorator. All properties that should appear as a column in the database require the @Column annotation.

See more advanced example here.

@Table


The @Table annotation can be used without passing any parameters. To specify some more define options, use an object literal (all define options from sequelize are valid):

  1. ``` ts
  2. @Table({
  3.   timestamps: true,
  4.   ...
  5. })
  6. class Person extends Model {}
  7. ```

Table API


Decorator Description
:--- :---
@Table sets options.tableName=<CLASS_NAME> and options.modelName=<CLASS_NAME> automatically
@Table(options: DefineOptions) sets define options (also sets options.tableName=<CLASS_NAME> and options.modelName=<CLASS_NAME> if not already defined by define options)

Primary key


A primary key (id ) will be inherited from base class Model. This primary key is by default an INTEGER and has autoIncrement=true (This behaviour is a native sequelize thing). The id can easily be overridden by marking another attribute as primary key. So either set @Column({primaryKey: true}) or use @PrimaryKey together with @Column.

@CreatedAt, @UpdatedAt, @DeletedAt


Annotations to define custom and type safe createdAt, updatedAt and deletedAt attributes:

  1. ``` ts
  2.   @CreatedAt
  3.   creationDate: Date;

  4.   @UpdatedAt
  5.   updatedOn: Date;

  6.   @DeletedAt
  7.   deletionDate: Date;
  8. ```

Decorator Description
:--- :---
@CreatedAt sets timestamps=true and createdAt='creationDate'
@UpdatedAt sets timestamps=true and updatedAt='updatedOn'
@DeletedAt sets timestamps=true, paranoid=true and deletedAt='deletionDate'

@Column


The @Column annotation can be used without passing any parameters. But therefore it is necessary that the js type can be inferred automatically (see Type inference for details).

  1. ``` ts
  2.   @Column
  3.   name: string;
  4. ```

If the type cannot or should not be inferred, use:

  1. ``` ts
  2. import {DataType} from 'sequelize-typescript';

  3.   @Column(DataType.TEXT)
  4.   name: string;
  5. ```

Or for a more detailed column description, use an object literal (all attribute options from sequelize are valid):

  1. ``` ts
  2.   @Column({
  3.     type: DataType.FLOAT,
  4.     comment: 'Some value',
  5.     ...
  6.   })
  7.   value: number;
  8. ```

Column API


Decorator Description
:--- :---
@Column tries to infer dataType from js type
@Column(dataType: DataType) sets dataType explicitly
@Column(options: AttributeOptions) sets attribute options

Shortcuts


If you're in love with decorators: sequelize-typescriptprovides some more of them. The following decorators can be used together with the @Column annotation to make some attribute options easier available:

Decorator Description Options
:--- :--- :---
@AllowNull(allowNull?: boolean) sets attribute.allowNull (default is true)
@AutoIncrement sets attribute.autoIncrement=true
@Unique(options? UniqueOptions) sets attribute.unique=true UniqueOptions
@Default(value: any) sets attribute.defaultValue to specified value
@PrimaryKey sets attribute.primaryKey=true
@Comment(value: string) sets attribute.comment to specified string
Validate annotations see Model validation

Type inference


The following types can be automatically inferred from javascript type. Others have to be defined explicitly.

Design type Sequelize data type
:--- :---
string STRING
boolean BOOLEAN
number INTEGER
bigint BIGINT
Date DATE
Buffer BLOB

Accessors


Get/set accessors do work as well

  1. ``` ts
  2. @Table
  3. class Person extends Model {
  4.   @Column
  5.   get name(): string {
  6.     return 'My name is ' + this.getDataValue('name');
  7.   }

  8.   set name(value: string) {
  9.     this.setDataValue('name', value);
  10.   }
  11. }
  12. ```

Usage


Except for minor variations sequelize-typescriptwill work like pure sequelize. (See sequelize docs )

Configuration


To make the defined models available, you have to configure a Sequelize instance from sequelize-typescript (!).

  1. ``` ts
  2. import { Sequelize } from 'sequelize-typescript';

  3. const sequelize = new Sequelize({
  4.   database: 'some_db',
  5.   dialect: 'sqlite',
  6.   username: 'root',
  7.   password: '',
  8.   storage: ':memory:',
  9.   models: [__dirname + '/models'], // or [Player, Team],
  10. });
  11. ```

Before you can use your models you have to tell sequelize where they can be found. So either set models in the sequelize config or add the required models later on by calling sequelize.addModels([Person]) or sequelize.addModels([__dirname + '/models']) :

  1. ``` ts
  2. sequelize.addModels([Person]);
  3. sequelize.addModels(['path/to/models']);
  4. ```

globs


  1. ``` ts
  2. import {Sequelize} from 'sequelize-typescript';

  3. const sequelize =  new Sequelize({
  4.         ...
  5.         models: [__dirname + '/**/*.model.ts']
  6. });
  7. // or
  8. sequelize.addModels([__dirname + '/**/*.model.ts']);
  9. ```

Model-path resolving


A model is matched to a file by its filename. E.g.

  1. ``` ts
  2. // File User.ts matches the following exported model.
  3. export class User extends Model {}
  4. ```

This is done by comparison of the filename against all exported members. The matching can be customized by specifying the modelMatch function in the configuration object.

For example, if your models are named user.model.ts, and your class is called User, you can match these two by using the following function:

  1. ``` ts
  2. import {Sequelize} from 'sequelize-typescript';

  3. const sequelize =  new Sequelize({
  4.   models: [__dirname + '/models/**/*.model.ts']
  5.   modelMatch: (filename, member) => {
  6.     return filename.substring(0, filename.indexOf('.model')) === member.toLowerCase();
  7.   },
  8. });
  9. ```

For each file that matches the *.model.ts pattern, the modelMatch function will be called with its exported members. E.g. for the following file

  1. ``` ts
  2. //user.model.ts
  3. import {Table, Column, Model} from 'sequelize-typescript';

  4. export const UserN = 'Not a model';
  5. export const NUser = 'Not a model';

  6. @Table
  7. export class User extends Model {

  8.   @Column
  9.   nickname: string;
  10. }
  11. ```

The modelMatch function will be called three times with the following arguments.

  1. ``` sh
  2. user.model UserN -> false
  3. user.model NUser -> false
  4. user.model User  -> true (User will be added as model)

  5. ```

Another way to match model to file is to make your model the default export.

  1. ``` ts
  2. export default class User extends Model {}
  3. ```

⚠️When using paths to add models, keep in mind that they will be loaded during runtime. This means that the path may differ from development time to execution time. For instance, using .ts extension within paths will only work together with ts-node.


Build and create


Instantiation and inserts can be achieved in the good old sequelize way

  1. ``` ts
  2. const person = Person.build({ name: 'bob', age: 99 });
  3. person.save();

  4. Person.create({ name: 'bob', age: 99 });
  5. ```

but sequelize-typescriptalso makes it possible to create instances with new :

  1. ``` ts
  2. const person = new Person({ name: 'bob', age: 99 });
  3. person.save();
  4. ```

Find and update


Finding and updating entries does also work like using native sequelize. So see sequelize docs for more details.

  1. ``` ts
  2. Person.findOne().then((person) => {
  3.   person.age = 100;
  4.   return person.save();
  5. });

  6. Person.update(
  7.   {
  8.     name: 'bobby',
  9.   },
  10.   { where: { id: 1 } }
  11. ).then(() => {});
  12. ```

Model association


Relations can be described directly in the model by the @HasMany, @HasOne, @BelongsTo, @BelongsToMany and @ForeignKey annotations.

One-to-many


  1. ``` ts
  2. @Table
  3. class Player extends Model {
  4.   @Column
  5.   name: string;

  6.   @Column
  7.   num: number;

  8.   @ForeignKey(() => Team)
  9.   @Column
  10.   teamId: number;

  11.   @BelongsTo(() => Team)
  12.   team: Team;
  13. }

  14. @Table
  15. class Team extends Model {
  16.   @Column
  17.   name: string;

  18.   @HasMany(() => Player)
  19.   players: Player[];
  20. }
  21. ```

That's all, sequelize-typescriptdoes everything else for you. So when retrieving a team by find

  1. ``` ts
  2. Team.findOne({ include: [Player] }).then((team) => {
  3.   team.players.forEach((player) => console.log(`Player ${player.name}`));
  4. });
  5. ```

the players will also be resolved (when passing include: Player to the find options)

Many-to-many


  1. ``` ts
  2. @Table
  3. class Book extends Model {
  4.   @BelongsToMany(() => Author, () => BookAuthor)
  5.   authors: Author[];
  6. }

  7. @Table
  8. class Author extends Model {
  9.   @BelongsToMany(() => Book, () => BookAuthor)
  10.   books: Book[];
  11. }

  12. @Table
  13. class BookAuthor extends Model {
  14.   @ForeignKey(() => Book)
  15.   @Column
  16.   bookId: number;

  17.   @ForeignKey(() => Author)
  18.   @Column
  19.   authorId: number;
  20. }
  21. ```

Type safe through-table instance access


To access the through-table instance (instanceOf BookAuthor in the upper example) type safely, the type need to be set up manually. For Author model it can be achieved like so:

  1. ``` ts
  2.   @BelongsToMany(() => Book, () => BookAuthor)
  3.   books: Array<Book & {BookAuthor: BookAuthor}>;
  4. ```

One-to-one


For one-to-one use @HasOne(...) (foreign key for the relation exists on the other model) and @BelongsTo(...) (foreign key for the relation exists on this model)

@ForeignKey, @BelongsTo, @HasMany, @HasOne, @BelongsToMany API


Decorator Description
:--- :---
@ForeignKey(relatedModelGetter: () => typeof Model) marks property as foreignKey for related class
@BelongsTo(relatedModelGetter: () => typeof Model) sets SourceModel.belongsTo(RelatedModel, ...) while as is key of annotated property and foreignKey is resolved from source class
@BelongsTo(relatedModelGetter: () => typeof Model, foreignKey: string) sets SourceModel.belongsTo(RelatedModel, ...) while as is key of annotated property and foreignKey is explicitly specified value
@BelongsTo(relatedModelGetter: () => typeof Model, options: AssociationOptionsBelongsTo) sets SourceModel.belongsTo(RelatedModel, ...) while as is key of annotated property and options are additional association options
@HasMany(relatedModelGetter: () => typeof Model) sets SourceModel.hasMany(RelatedModel, ...) while as is key of annotated property and foreignKey is resolved from target related class
@HasMany(relatedModelGetter: () => typeof Model, foreignKey: string) sets SourceModel.hasMany(RelatedModel, ...) while as is key of annotated property and foreignKey is explicitly specified value
@HasMany(relatedModelGetter: () => typeof Model, options: AssociationOptionsHasMany) sets SourceModel.hasMany(RelatedModel, ...) while as is key of annotated property and options are additional association options
@HasOne(relatedModelGetter: () => typeof Model) sets SourceModel.hasOne(RelatedModel, ...) while as is key of annotated property and foreignKey is resolved from target related class
@HasOne(relatedModelGetter: () => typeof Model, foreignKey: string) sets SourceModel.hasOne(RelatedModel, ...) while as is key of annotated property and foreignKey is explicitly specified value
@HasOne(relatedModelGetter: () => typeof Model, options: AssociationOptionsHasOne) sets SourceModel.hasOne(RelatedModel, ...) while as is key of annotated property and options are additional association options
@BelongsToMany(relatedModelGetter: () => typeof Model, through: (() => typeof Model)) sets SourceModel.belongsToMany(RelatedModel, {through: ThroughModel, ...}) while as is key of annotated property and foreignKey/otherKey is resolved from through class
@BelongsToMany(relatedModelGetter: () => typeof Model, through: (() => typeof Model), foreignKey: string) sets SourceModel.belongsToMany(RelatedModel, {through: ThroughModel, ...}) while as is key of annotated property, foreignKey is explicitly specified value and otherKey is resolved from through class
@BelongsToMany(relatedModelGetter: () => typeof Model, through: (() => typeof Model), foreignKey: string, otherKey: string) sets SourceModel.belongsToMany(RelatedModel, {through: ThroughModel, ...}) while as is key of annotated property and foreignKey/otherKey are explicitly specified values
@BelongsToMany(relatedModelGetter: () => typeof Model, through: string, foreignKey: string, otherKey: string) sets SourceModel.belongsToMany(RelatedModel, {through: throughString, ...}) while as is key of annotated property and foreignKey/otherKey are explici
Last Updated: 2023-09-03 17:10:52