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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@stilt/sequelize

v0.21.0-alpha.21

Published

Sequelize Wrapper for Stilt Framework

Downloads

159

Readme

@stilt/sequelize

Sequelize 4 Adapter for Stilt Framework

Note: You do not have to use this plugin to use Sequelize with the Stilt framework, but this will be much more convenient.

Install

npm install sequelize @stilt/sequelize

Usage

Enable sequelize support like any other Stilt extension.

import Stilt from '@stilt/core';
import StiltSequelize from '@stilt/sequelize';

const app = new Stilt();

app.use(new StiltSequelize({
  models: `**/*.entity.js`,
  databaseUri: 'postgres://ephys@localhost:5432/myblog',
  debug: false,
}));

app.launch();

Configuration Options:

type Config = {
  // The directory containing the various Sequelize Models of your application
  // They will be automatically loaded by this plugin.
  models: string,

  // The URI containing all the information necessary to access the database.
  // format:
  // <dialect>://[username]:[password]@<host>:[port]/<databaseName>
  databaseUri: string,

  // Print Sequelize debug messages.
  debug?: boolean,
};

Declaring your Database Models

Your models must be placed in your "models" folder. Each file can only contain one model which must be the default export.

You can declare your models similarly to how you'd declare them using sequelize-decorators. The @Options, @Attribute and @Attributes decorators are exported directly from that package.

import { Model, DataTypes } from 'sequelize';
import { Options, Attribute } from '@stilt/sequelize';

@Options({
    tableName: 'users'
})
@Attributes({
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
  },
  name: {
    type: DataTypes.STRING(255),
    allowNull: false,
  },
})
export default class User extends Model {}

Note: Unline sequelize-decorators, you do not need to pass an instance of sequelize to your @Options decorator. That part is handled automatically by this plugin.

Model Associations

@stilt/sequelize provides you with a series of decorators that simplify the creation of associations between your models. Your can use the following decorators:

  • @BelongsTo(model, options)
  • @HasMany(model, options)
  • @HasOne(model, options)
  • @BelongsToMany(model, options)

These decorators work exactly like if you were to manually call the corresponding static method on your models with the same parameters:

@HasMany(User)
export default class Team extends Model {}

// == is equivalent to ==

class Team extends Model {}

Team.hasMany(User);

Inverse Associations

One point where the above decorators differ from vanilla Sequelize is that you can specify the parameters of the inverse association directly in the decorator. This greatly reduces duplicated code.

Instead of doing

// create the association User#team
@BelongsTo(Team, {
  as: 'team',
  onDelete: 'CASCADE',
  foreignKey: {
    name: 'team_id',
    allowNull: false,
  },
})
class User extends Model {
}

// create the inverse association, Team#members
Team.hasMany(User, {
  as: 'members',
  onDelete: 'CASCADE',
  foreignKey: {
    name: 'team_id',
    allowNull: false,
  },
});

You can now do

@BelongsTo(Team, {
  // create the association User#team
  as: 'team',
  onDelete: 'CASCADE',
  foreignKey: {
    name: 'team_id',
    allowNull: false,
  },

  // create the inverse association, Team#members
  inverse: {
    type: 'many',
    as: 'members',
  },
})
class User extends Model {
}

Signatures:

@BelongsTo(TargetModel, {
  // <sequelize parameters>

  // Added by Stilt
  inverse: {
    // "many" will create an association using hasMany
    // "one" will create an association using hasOne
    type: 'many' | 'one',

    // "as" of the inverse association
    as: string,

    // only if type = "many" (see .hasMany options for details)
    scope: boolean,

    // only if type = "many" (see .hasMany options for details)
    sourceKey: string,
 },
})

@HasOne(TargetModel, {
  // <sequelize parameters>

  // Added by Stilt
  // if value is a string, it is used for the "as" option of the inverse relation
  inverse: { as: string } | string,
})

@HasMany(TargetModel, {
  // <sequelize parameters>

  // Added by Stilt
  // if value is a string, it is used for the "as" option of the inverse relation
  inverse: { as: string } | string,
})

@BelongsToMany(TargetModel, {
  // <sequelize parameters>

  // These two parameters will be switched in the inverse association.
  foreignKey: string, // see sequelize .belongsToMany decoumentation for details
  otherKey: string,   // see sequelize .belongsToMany decoumentation for details

  // Added by Stilt
  // if value is a string, it is used for the "as" option of the inverse relation
  inverse: { as: string } | string,
})

Associations with the same model

If you need to create an association with the model that is currently being decorated, you can pass a function that returns said model instead of passing the model directly. This way the class has time to initialize before being used in the association.

import { Model } from 'sequelize';
import { Options, Attribute, BelongsTo } from '@stilt/sequelize';

@Options()
@Attributes({
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
  },
  name: {
    type: DataTypes.STRING(255),
    allowNull: false,
  },
})
@BelongsTo(() => Category, { // <- notice how we pass an arrow function that returns the model class.
  as: 'parent',
})
export default class Category extends Model {}

Decorator Naming

All decorators exported by this plugin are available in both camelCase and UpperCamelCase (eg. both @HasMany and @hasMany are exported).

Credit

sequelize-decorators for the @Options, @Attribute and @Attributes decorators which this module exports.