sd

sequelize-decorators

Sequelize + Decorators = ❤

Showing:

Popularity

Downloads/wk

250

GitHub Stars

78

Maintenance

Last Commit

3yrs ago

Contributors

5

Package

Dependencies

1

Size (min+gzip)

3.3KB

License

ISC

Type Definitions

Built-In

Tree-Shakeable

No?

Categories

Readme

Sequelize + Decorators = ❤

npm downloads build codecov dependencies node license

A proof of concept for using Sequelize with decorators.

Usage with TypeScript

Installation

npm install --save sequelize sequelize-decorators

Add to your tsconfig.json:

"experimentalDecorators": true,
"emitDecoratorMetadata": true

The second setting lets sequelize-decorators infer the type of attributes from the type declaration.

Example:

import {Sequelize, Model, DataTypes} from 'sequelize'
import {Options, Attribute} from 'sequelize-decorators'

const sequelize = new Sequelize(process.env.DB)

@Options({
    sequelize,
    tableName: 'users'
})
export class User extends Model {

    @Attribute({
        type: DataTypes.STRING,
        primaryKey: true
    })
    public username: string;

    @Attribute(DataTypes.STRING)
    public firstName: string;

    @Attribute() // Type is inferred as DataTypes.STRING
    public lastName: string;

    get fullName(): string {
        return this.firstName + ' ' + this.lastName;
    }

    set fullName(fullName: string) {
        const names = fullName.split(' ');
        this.lastName = names.pop();
        this.firstName = names.join(' ');
    }
}

The @Options decorator is required and must include the sequelize option (the connection to use).

Type inference

TypeScript typeSequelize data type
stringSTRING
numberINTEGER
DateDATE
BufferBLOB

Usage with Babel

Installation

npm install --save sequelize sequelize-decorators
npm install --save-dev babel-plugin-transform-decorators-legacy

Add to your .babelrc:

"plugins": ["transform-decorators-legacy"]

Example:

import {Sequelize, Model, DataTypes} from 'sequelize'
import {Options, Attributes} from 'sequelize-decorators'

const sequelize = new Sequelize(process.env.DB)

@Options({
    sequelize,
    tableName: 'users'
})
@Attributes({
    username: {
        type: DataTypes.STRING,
        primaryKey: true
    },
    lastName: DataTypes.STRING,
    firstName: DataTypes.STRING,
})
export class User extends Model {

    get fullName(): string {
        return this.firstName + ' ' + this.lastName;
    }

    set fullName(fullName: string) {
        const names = fullName.split(' ');
        this.lastName = names.pop();
        this.firstName = names.join(' ');
    }
}

The @Options decorator is required and must include the sequelize option (the connection to use).

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial