npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sequelizejs-decorators

v6.1.7

Published

decorators for sequelize v6* ORM for NodeJS

Downloads

138

Readme

sequelizejs-decorators

Decorators for use with sequelize ORM. Version now follows the Sequelize library version (5.* -> 5.*) from version 5 onward.

Getting Started

$ npm install sequelizejs-decorators sequelize

you will want need to add the following to your tsconfig.json compiler options

"emitDecoratorMetadata": true,
"experimentalDecorators": true

Usage

Player.ts

import {
    Entity, 
    PrimaryGeneratedColumn,
    Index,
    Column,
    CreatedDateColumn,
    UpdatedDateColumn,
    DataType,
    BelongsTo
    } from 'sequelizejs-decorators';
import {Team} from './Team';
import {IncludeAssociation, Model, Instance} from 'sequelize';

@Entity()
export class Player {
    @PrimaryGeneratedColumn()
    public id: number;

    @Column({
        type: DataType.STRING(100),
        allowNull: false
    })
    public name: string;

    @Column({
        type: DataType.INTEGER
    })
    public number: string;

    @CreatedDateColumn()
    public created: Date;

    @UpdatedDateColumn()
    public updated: Date;

    @BelongsTo(() => Team)
    public team: Team;
}

export interface PlayerInstance extends Instance<Player>, Player {
    setTeam(team: Team);
}

export interface PlayerModel extends Model<PlayerInstance, Player> {
    team: IncludeAssociation;
}

Team.ts

import {
    Entity, 
    PrimaryGeneratedColumn,
    Index,
    Column,
    CreatedDateColumn,
    UpdatedDateColumn,
    DataType,
    HasMany,
    ManyToMany
    } from 'sequelizejs-decorators';
import {Player} from './Player';
import {IncludeAssociation, Model, Instance} from 'sequelize';

@Entity()
export class Team {
    @PrimaryGeneratedColumn()
    public id: number;

    @Index({
        unique: true
    })
    @Column({
        type: DataType.STRING(100),
        unique: true,
        allowNull: false
    })
    public name: string;

    @CreatedDateColumn()
    public created: Date;

    @UpdatedDateColumn()
    public updated: Date;

    @HasMany(() => Player)
    public players: Player[];

    @ManyToMany(() => Game, { through: 'TeamGames' })
    public games: Game[];
}

export interface TeamInstance extends Instance<Team>, Team {
    addPlayer(player: Player);
    addGame(game: Game);
}

export interface TeamModel extends Model<TeamInstance, Team> {
    players: IncludeAssociation;
    games: IncludeAssociation;
}

Game.ts

import {
    Entity, 
    PrimaryGeneratedColumn,
    Column,
    CreatedDateColumn,
    UpdatedDateColumn,
    DataType,
    ManyToMany
    } from 'sequelizejs-decorators';
import {Team} from './Team';
import {IncludeAssociation, Model, Instance} from 'sequelize';

@Entity()
export class Game {
    @PrimaryGeneratedColumn()
    public id: number;

    @Column({
        type: DataType.STRING(200),
        allowNull: false
    })
    public location: string;

    @Column({
        type: DataType.DATE
    })
    pubilc time: Date;

    @CreatedDateColumn()
    public created: Date;

    @UpdatedDateColumn()
    public updated: Date;

    @ManyToMany(() => Team, { through: 'TeamGames' })
    public teams: Team[];
}

export interface GameInstance extends Instance<Game>, Game {
    addTeam(team: Team);
}

export interface GameModel extends Model<GameInstance, Game> {
    teams: IncludeAssociation;
}

Database.ts

import { Options, Sequelize } from 'sequelize';
import SequelizeDb = require('sequelize');
import { Player, PlayerModel } from './Player';
import { Team, TeamModel } from './Team';
import { Game, GameModel } from './Game';
import { registerEntities } from 'sequelizejs-decorators';

export class Database {
    private _db: Sequelize;
    private _models: Models;

    /**
     * constructor
     * @param config database config options
     */
    public constructor(config: Options) {
        config.operatorsAliases = {};
        this._db = new SequelizeDb(config);
    }

    public get player(): PlayerModel {
        this.checkIfTableNull(this._models.Player);
        return this._models.Player as PlayerModel;
    }

    public get team(): TeamModel {
        this.checkIfTableNull(this._models.Team);
        return this._models.Team as TeamModel;
    }

    public get game(): GameModel {
        this.checkIfTableNull(this._models.Game);
        return this._models.Game as GameModel;
    }

    /**
     * initiates the database connection
     */
    public connect(): Promise<boolean> {
        return new Promise((resolve, reject) => {
            this._db.authenticate().then(() => {
                this._models = registerEntities(this._db, [
                    Player,
                    Team,
                    Game
                ]);

                this._db.sync().then(() => {
                    resolve(true);
                }).catch((ex: Error) => {
                    reject(ex);
                });
            }).catch((ex: Error) => {
                reject(ex);
            });
        });
    }

    /**
     * closes the database connection
     */
    public close(): Promise<void> {
        return new Promise((resolve, reject) => {
            this._db.close().then(() => {
                resolve();
            }).catch((ex: Error) => {
                reject(ex);
            });
        });
    }

    public async testConnection(): Promise<boolean> {
        await this._db.authenticate().catch(() => false);
        return true;
    }

    private checkIfTableNull(table: any): void {
        if (table == null) { throw new Error('database not initialized'); }
    }
}

index.ts

import { Options, Sequelize } from 'sequelize';
import {Database} from './Database';

let dbOptions = {
    // sequelize options here
} as Options;

let dbInstance: Database = new Database(dbOptions);
dbInstance.connect().then(async () => {
    // now you can use db instance as your data access

    let player = await dbInstance.player.create({
        name: 'Michael Jordan',
        number: 23
    });

    // or

    let team = await dbInstance.team.create({
        name: 'Chicago Bulls',
        players: [
            {
                name: 'Michael Jordan',
                number: 23
            }
        ]
    }, {
        include: [
            { association: dbInstance.team.players }
        ]
    });
});