Decorators and some other features for sequelize
npm install sequelize-typescript


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
- this assumes usage of sequelize@6
- _sequelize-typescript_ requires sequelize
- additional typings as documented here and reflect-metadata
``sh`
npm install --save-dev @types/node @types/validator
npm install sequelize reflect-metadata sequelize-typescript
Your tsconfig.json needs the following flags:
`json`
"target": "es6", // or a more recent ecmascript version
"experimentalDecorators": true,
"emitDecoratorMetadata": true
- SequelizeConfig renamed to SequelizeOptionsmodelPaths
- property renamed to models
The @Scopes and @DefaultScope decorators now take lambda's as options
`ts`
@DefaultScope(() => ({...}))
@Scopes(() => ({...}))
instead of deprecated way:
`ts`
@DefaultScope({...})
@Scopes({...}))
`typescript
import { Table, Column, Model, HasMany } from 'sequelize-typescript';
@Table
class Person extends Model {
@Column
name: string;
@Column
birthday: Date;
@HasMany(() => Hobby)
hobbies: Hobby[];
}
`
`typescript
import { Table, Model } from 'sequelize-typescript';
@Table
class Person extends Model {}
`
`typescript
import { Optional } from 'sequelize';
import { Table, Model } from 'sequelize-typescript';
interface PersonAttributes {
id: number;
name: string;
}
interface PersonCreationAttributes extends Optional
@Table
class Person extends Model
`
The model needs to extend the Model class and has to be annotated with the @Table decorator. All properties that@Column
should appear as a column in the database require the annotation.
See more advanced example here.
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):
`typescript`
@Table({
timestamps: true,
...
})
class Person extends Model {}
#### Table API
| Decorator | Description |
| -------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| @Table | sets options.tableName= and options.modelName= automatically |@Table(options: DefineOptions)
| | sets define options (also sets options.tableName= and options.modelName= 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 hasautoIncrement=true (This behaviour is a native sequelize thing). The id can easily be overridden by marking another@Column({primaryKey: true})
attribute as primary key. So either set or use @PrimaryKey together with @Column.
#### @CreatedAt, @UpdatedAt, @DeletedAt
Annotations to define custom and type safe createdAt, updatedAt and deletedAt attributes:
`typescript
@CreatedAt
creationDate: Date;
@UpdatedAt
updatedOn: Date;
@DeletedAt
deletionDate: Date;
`
| 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' |
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).
`typescript`
@Column
name: string;
If the type cannot or should not be inferred, use:
`typescript
import {DataType} from 'sequelize-typescript';
@Column(DataType.TEXT)
name: string;
`
Or for a more detailed column description, use an object literal
(all attribute options
from sequelize are valid):
`typescript`
@Column({
type: DataType.FLOAT,
comment: 'Some value',
...
})
value: number;
#### 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-typescript_ provides 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 |
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 |
Get/set accessors do work as well
`typescript
@Table
class Person extends Model {
@Column
get name(): string {
return 'My name is ' + this.getDataValue('name');
}
set name(value: string) {
this.setDataValue('name', value);
}
}
`
Except for minor variations _sequelize-typescript_ will work like pure sequelize.
(See sequelize docs)
To make the defined models available, you have to configure a Sequelize instance from sequelize-typescript(!).
`typescript
import { Sequelize } from 'sequelize-typescript';
const sequelize = new Sequelize({
database: 'some_db',
dialect: 'sqlite',
username: 'root',
password: '',
storage: ':memory:',
models: [__dirname + '/models'], // or [Player, Team],
});
`
Before you can use your models you have to tell sequelize where they can be found. So either set models in thesequelize.addModels([Person])
sequelize config or add the required models later on by calling orsequelize.addModels([__dirname + '/models']):
`typescript`
sequelize.addModels([Person]);
sequelize.addModels(['path/to/models']);
`typescript
import {Sequelize} from 'sequelize-typescript';
const sequelize = new Sequelize({
...
models: [__dirname + '/*/.model.ts']
});
// or
sequelize.addModels([__dirname + '/*/.model.ts']);
`
#### Model-path resolving
A model is matched to a file by its filename. E.g.
`typescript`
// File User.ts matches the following exported model.
export class User extends Model {}
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 calledUser, you can match these two by using the following function:
`typescript
import {Sequelize} from 'sequelize-typescript';
const sequelize = new Sequelize({
models: [__dirname + '/models/*/.model.ts']
modelMatch: (filename, member) => {
return filename.substring(0, filename.indexOf('.model')) === member.toLowerCase();
},
});
`
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
`TypeScript
//user.model.ts
import {Table, Column, Model} from 'sequelize-typescript';
export const UserN = 'Not a model';
export const NUser = 'Not a model';
@Table
export class User extends Model {
@Column
nickname: string;
}
`
The modelMatch function will be called three times with the following arguments.
`text`
user.model UserN -> false
user.model NUser -> false
user.model User -> true (User will be added as model)
Another way to match model to file is to make your model the default export.
`TypeScript`
export default class User extends Model {}
> ⚠️ 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.
Instantiation and inserts can be achieved in the good old sequelize way
`typescript
const person = Person.build({ name: 'bob', age: 99 });
person.save();
Person.create({ name: 'bob', age: 99 });
`
but _sequelize-typescript_ also makes it possible to create instances with new:
`typescript`
const person = new Person({ name: 'bob', age: 99 });
person.save();
Finding and updating entries does also work like using native sequelize. So see sequelize
docs for more details.
`typescript
Person.findOne().then((person) => {
person.age = 100;
return person.save();
});
Person.update(
{
name: 'bobby',
},
{ where: { id: 1 } }
).then(() => {});
`
Relations can be described directly in the model by the @HasMany, @HasOne, @BelongsTo, @BelongsToMany@ForeignKey
and annotations.
`typescript
@Table
class Player extends Model {
@Column
name: string;
@Column
num: number;
@ForeignKey(() => Team)
@Column
teamId: number;
@BelongsTo(() => Team)
team: Team;
}
@Table
class Team extends Model {
@Column
name: string;
@HasMany(() => Player)
players: Player[];
}
`
That's all, _sequelize-typescript_ does everything else for you. So when retrieving a team by find
`typescriptPlayer ${player.name}
Team.findOne({ include: [Player] }).then((team) => {
team.players.forEach((player) => console.log());`
});
the players will also be resolved (when passing include: Player to the find options)
`typescript
@Table
class Book extends Model {
@BelongsToMany(() => Author, () => BookAuthor)
authors: Author[];
}
@Table
class Author extends Model {
@BelongsToMany(() => Book, () => BookAuthor)
books: Book[];
}
@Table
class BookAuthor extends Model {
@ForeignKey(() => Book)
@Column
bookId: number;
@ForeignKey(() => Author)
@Column
authorId: number;
}
`
#### Type safe _through_-table instance access
To access the _through_-table instance (instanceOf BookAuthor in the upper example) type safely, the typeAuthor
need to be set up manually. For model it can be achieved like so:
`ts`
@BelongsToMany(() => Book, () => BookAuthor)
books: Array
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)
| 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 explicitly specified values |@BelongsToMany(relatedModelGetter: () => typeof Model, options: AssociationOptionsBelongsToMany)
| | sets SourceModel.belongsToMany(RelatedModel, {through: throughString, ...}) while as is key of annotated property and options are additional association values, including foreignKey and otherKey. |
Note that when using AssociationOptions, certain properties will be overwritten when the association is built, based on reflection metadata or explicit attribute parameters. For example, as will always be the annotated property's name, and through will be the explicitly stated value.
_sequelize-typescript_ resolves the foreign keys by identifying the corresponding class references.
So if you define a model with multiple relations like
`typescript
@Table
class Book extends Model {
@ForeignKey(() => Person)
@Column
authorId: number;
@BelongsTo(() => Person)
author: Person;
@ForeignKey(() => Person)
@Column
proofreaderId: number;
@BelongsTo(() => Person)
proofreader: Person;
}
@Table
class Person extends Model {
@HasMany(() => Book)
writtenBooks: Book[];
@HasMany(() => Book)
proofedBooks: Book[];
}
`
_sequelize-typescript_ cannot know which foreign key to use for which relation. So you have to add the foreign keys
explicitly:
`typescript
// in class "Books":
@BelongsTo(() => Person, 'authorId')
author: Person;
@BelongsTo(() => Person, 'proofreaderId')
proofreader: Person;
// in class "Person":
@HasMany(() => Book, 'authorId')
writtenBooks: Book[];
@HasMany(() => Book, 'proofreaderId')
proofedBooks: Book[];
`
With the creation of a relation, sequelize generates some method on the corresponding
models. So when you create a 1:n relation between ModelA and ModelB, an instance of ModelA willgetModelBs
have the functions , setModelBs, addModelB, removeModelB, hasModelB. These functions still exist with _sequelize-typescript_.getModelB
But TypeScript wont recognize them and will complain if you try to access , setModelB oraddModelB. To make TypeScript happy, the Model.prototype of _sequelize-typescript_ has $set, $get, $add
functions.
`typescript
@Table
class ModelA extends Model {
@HasMany(() => ModelB)
bs: ModelB[];
}
@Table
class ModelB extends Model {
@BelongsTo(() => ModelA)
a: ModelA;
}
`
To use them, pass the property key of the respective relation as the first parameter:
`typescript
const modelA = new ModelA();
modelA
.$set('bs', [
/ instance /
])
.then(/ ... /);
modelA.$add('b' / instance /).then(/ ... /);
modelA.$get('bs').then(/ ... /);
modelA.$count('bs').then(/ ... /);
modelA.$has('bs').then(/ ... /);
modelA.$remove('bs' / instance /).then(/ ... /);
modelA.$create('bs' / value /).then(/ ... /);
`
The @Index annotation can be used without passing any parameters.
`typescript
@Table
class Person extends Model {
@Index // Define an index with default name
@Column
name: string;
@Index // Define another index
@Column
birthday: Date;
}
`
To specify index and index field options, use
an object literal (see indexes define option):
`typescript
@Table
class Person extends Model {
@Index('my-index') // Define a multi-field index on name and birthday
@Column
name: string;
@Index('my-index') // Add birthday as the second field to my-index
@Column
birthday: Date;
@Index({
// index options
name: 'job-index',
parser: 'my-parser',
type: 'UNIQUE',
unique: true,
where: { isEmployee: true },
concurrently: true,
using: 'BTREE',
operator: 'text_pattern_ops',
prefix: 'test-',
// index field options
length: 10,
order: 'ASC',
collate: 'NOCASE',
})
@Column
jobTitle: string;
@Column
isEmployee: boolean;
}
`
#### Index API
| Decorator | Description |
| ---------------------------------------- | --------------------------------------------------------------------------------------------------------- |
| @Index | adds new index on decorated field to options.indexes |@Index(name: string)
| | adds new index or adds the field to an existing index with specified name |@Table(options: IndexDecoratorOptions)
| | sets both index and index field options |
The createIndexDecorator() function can be used to create a decorator for an index with options specified with an object literal supplied as the argument. Fields are added to the index by decorating properties.
`typescript
const SomeIndex = createIndexDecorator();
const JobIndex = createIndexDecorator({
// index options
name: 'job-index',
parser: 'my-parser',
type: 'UNIQUE',
unique: true,
where: { isEmployee: true },
concurrently: true,
using: 'BTREE',
operator: 'text_pattern_ops',
prefix: 'test-',
});
@Table
class Person extends Model {
@SomeIndex // Add name to SomeIndex
@Column
name: string;
@SomeIndex // Add birthday to SomeIndex
@Column
birthday: Date;
@JobIndex({
// index field options
length: 10,
order: 'ASC',
collate: 'NOCASE',
})
@Column
jobTitle: string;
@Column
isEmployee: boolean;
}
`
With sequelize-typescript@1 comes a repository mode. See docs for details.
The repository mode makes it possible to separate static operations like find, create, ... from model definitions.
It also empowers models so that they can be used with multiple sequelize instances.
Enable repository mode by setting repositoryMode flag:
`typescript`
const sequelize = new Sequelize({
repositoryMode: true,
...,
});
Retrieve repository to create instances or perform search operations:
`typescript
const userRepository = sequelize.getRepository(User);
const luke = await userRepository.create({ name: 'Luke Skywalker' });
const luke = await userRepository.findOne({ where: { name: 'luke' } });
`
For now one need to use the repositories within the include options in order to retrieve or create related data:
`typescript
const userRepository = sequelize.getRepository(User);
const addressRepository = sequelize.getRepository(Address);
userRepository.find({ include: [addressRepository] });
userRepository.create({ name: 'Bear' }, { include: [addressRepository] });
`
> ⚠️ This will change in the future: One will be able to refer the model classes instead of the repositories.
Nested scopes and includes in general won't work when using @Scope annotation together with repository mode like:
`typescript`
@Scopes(() => ({
// includes
withAddress: {
include: [() => Address],
},
// nested scopes
withAddressIncludingLatLng: {
include: [() => Address.scope('withLatLng')],
},
}))
@Table
class User extends Model {}
> ⚠️ This will change in the future: Simple includes will be implemented.
Validation options can be set through the @Column annotation, but if you prefer to use separate decorators forvalidate.isEmail=true
validation instead, you can do so by simply adding the validate options _as_ decorators:
So that becomes @IsEmail, validate.equals='value' becomes @Equals('value')
and so on. Please notice that a validator that expects a boolean is translated to an annotation without a parameter.
See sequelize docs
for all validators.
The following validators cannot simply be translated from sequelize validator to an annotation:
| Validator | Annotation |
| ------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| validate.len=[number, number] | @Length({max?: number, min?: number}) |validate[customName: string]
| | For custom validators also use the @Is(...) annotation: Either @Is('custom', (value) => { / ... /}) or with named function @Is(function custom(value) { / ... /}) |
`typescript
const HEX_REGEX = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
@Table
export class Shoe extends Model {
@IsUUID(4)
@PrimaryKey
@Column
id: string;
@Equals('lala')
@Column
readonly key: string;
@Contains('Special')
@Column
special: string;
@Length({ min: 3, max: 15 })
@Column
brand: string;
@IsUrl
@Column
brandUrl: string;
@Is('HexColor', (value) => {
if (!HEX_REGEX.test(value)) {
throw new Error("${value}" is not a hex color value.);
}
})
@Column
primaryColor: string;
@Is(function hexColor(value: string): void {
if (!HEX_REGEX.test(value)) {
throw new Error("${value}" is not a hex color value.);
}
})
@Column
secondaryColor: string;
@Is(HEX_REGEX)
@Column
tertiaryColor: string;
@IsDate
@IsBefore('2017-02-27')
@Column
producedAt: Date;
}
`
Scopes can be defined with annotations as well. The scope options are identical to native
sequelize (See sequelize docs for more details)
`typescript
@DefaultScope(() => ({
attributes: ['id', 'primaryColor', 'secondaryColor', 'producedAt'],
}))
@Scopes(() => ({
full: {
include: [Manufacturer],
},
yellow: {
where: { primaryColor: 'yellow' },
},
}))
@Table
export class ShoeWithScopes extends Model {
@Column
readonly secretKey: string;
@Column
primaryColor: string;
@Column
secondaryColor: string;
@Column
producedAt: Date;
@ForeignKey(() => Manufacturer)
@Column
manufacturerId: number;
@BelongsTo(() => Manufacturer)
manufacturer: Manufacturer;
}
`
Hooks can be attached to your models. All Model-level hooks are supported. See the related unit tests for a summary.
Each hook must be a static method. Multiple hooks can be attached to a single method, and you can define multiple methods for a given hook.
The name of the method cannot be the same as the name of the hook (for example, a @BeforeCreate hook method cannot be named beforeCreate). That’s because Sequelize has pre-defined methods with those names.
`typescript
@Table
export class Person extends Model {
@Column
name: string;
@BeforeUpdate
@BeforeCreate
static makeUpperCase(instance: Person) {
// this will be called when an instance is created or updated
instance.name = instance.name.toLocaleUpperCase();
}
@BeforeCreate
static addUnicorn(instance: Person) {
// this will also be called when an instance is created
instance.name += ' 🦄';
}
}
`
?@ForeignKey(Model) is much easier to read, so why is @ForeignKey(() => Model) so important? When itModel
comes to circular-dependencies (which are in general solved by node for you) can be undefined
when it gets passed to @ForeignKey. With the usage of a function, which returns the actual model, we prevent
this issue.
Unless you are using the repository mode, you won't be able to add one and the same model to multiple
Sequelize instances with differently configured connections. So that one model will only work for one connection.
This is not only good practice regarding design, but also matters for the order
of execution. Since Typescript creates a __metadata("design:type", SomeModel) call due to emitDecoratorMetadataSomeModel
compile option, in some cases is probably not defined(not undefined!) and would throw a ReferenceError.SomeModel
When putting in a separate file, it would look like __metadata("design:type", SomeModel_1.SomeModel),
which does not throw an error.
If you need to minify your code, you need to set tableName and modelNameDefineOptions
in the for @Table annotation. sequelize-typescripttableName
uses the class name as default name for and modelName.class User
When the code is minified the class name will no longer be the originally
defined one (So that will become class b for example).
To contribute you can:
- Open issues and participate in discussion of other issues.
- Fork the project to open up PR's.
- Update the types of Sequelize.
- Anything else constructively helpful.
In order to open a pull request please:
- Create a new branch.
- Run tests locally (npm install && npm run build && npm run cover) and ensure your commits don't break the tests.
- Document your work well with commit messages, a good PR description, comments in code when necessary, etc.
In order to update the types for sequelize please go to the Definitely Typed repo, it would also be a good
idea to open a PR into sequelize so that Sequelize can maintain its own types, but that
might be harder than getting updated types into microsoft's repo. The Typescript team is slowly trying to encourage
npm package maintainers to maintain their own typings, but Microsoft still has dedicated and good people maintaining the DT repo,
accepting PR's and keeping quality high.
Keep in mind sequelize-typescript does not provide typings for sequelize - these are seperate things.
A lot of the types in sequelize-typescript` augment, refer to, or extend what sequelize already has.