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

@bedrok/sequelize-factory

v1.1.2

Published

Factory for loading Sequelize models, running migrations and applying plugins

Downloads

14

Readme

Supporter MIT Commitizen friendly maintained semantic-release

Welcome to Sequelize Factory

For those who have already made the choice to adopt Sequelize as the ORM for your NodeJS service, you will quickly realize that getting started may not be so out-of-the-box as you would like.

Defining models, associations and hooks etc. are clearly defined and well documented. Loading those models, establishing the associations (in particular) are not.

For example, you must define all models first before attempting to establish the associations. Associating your model to another that has not yet been previously been loaded will lead to an error. There are many good posts on how to do this, and in fact, it's not that hard. The challenge however is that you must solve this yourself.

If you are building out a microservice pattern with NodeJS and Sequelize, being consistent is key. Hence, Sequelize Factory was introduced.

You will still define your models the same, including associations and hooks etc., but you will use the Factory to initialize them. In addition to associations and hooks however, the Factory supports the ability of plugins, a feature to allow developers to establish other key characteristics and behaviors of a model in a uniform and consistent way. Examples include configuring a model to support Elasticsearch, or auto-generating GraphQL or gRPC endpoints.

One other key piece to the puzzle is executing the migrations. The factory will also take care of this on initialization after all of the definition and plugin capabilities have been applied. This allows your service to start-up in a consistent manner, apply features in a predictable order, followed by executing the migrations.

Now, as a developer, you need only be focused on the amazing features Sequelize has to offer and leave the initialization and assembly of them to the Factory.

Sequelize Factory follows Semantic Versioning and supports Node v16 and above.

Getting started

Installation via NPM

$ npm i @bedrok/sequelize-factory --save

or via YARN

$ yarn add @bedrok/sequelize-factory

Defining your models

As stated above, Sequelize Factory is wrapper to Sequelize that provides additional functionality while simplifying the overall initialization and activation process.

That said, it is opinionated on how you structure your definitions. Specifically, models must use the define method approach and not class extension approach.

Use this method for defining your model;

// MyModel.js
module.exports = (sequelize, DataTypes) => {

  const MyModel = sequelize.define(
    'MyModel', {
      id: DataTypes.STRING(32),
      attrA: DataTypes.STRING(20),
      attrB: DataTypes.STRING(20)
    }, {
      tableName: 'my_models'
    }
  );

  return MyModel;
};