🚀 DAY 3 OF LAUNCH WEEK: Introducing Webhook Events for Pull Request Scans.Learn more
Socket
Book a DemoInstallSign in
Socket

sequelize-decorators

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sequelize-decorators

Sequelize + Decorators = ❤

latest
Source
npmnpm
Version
1.0.2
Version published
Maintainers
1
Created
Source

Sequelize + Decorators = ❤

Version Downloads Build Status codecov Dependency Status 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).

FAQs

Package last updated on 02 Nov 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts