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

sequelize-fn

v1.0.4

Published

Simple function to fully setup sequelize instances

Downloads

3

Readme

sequelize-fn

Build Status Coverage Status

A simple package to setup sequelize in a very straightforward way.

About

This library can be used to completely configure sequelize in a simple function call, on this call its created the instance, models are imported and other things can be done.

Motivation

This library arises after identifying some deficiencies in the approach taked by sequelize-cli to automate the creation of sequelize instance and import the models over it.

When we use the sequelize-cli to generate our models, it generates an index.js file in the models folder which contains the logic necessary to create the sequelize instance and import the models.

One of the disadvantages of it's approach is that it restricts us to having only one instance of sequelize in our application, so if 2 or more are needed it would be necessary to do the entire process manually,with sequelize-fn to do this it would only takes be 2 function calls, which gives the user more flexibility.

If the application need N sequelize instance sequelize-fn can be use N times with differents configurations.

Another disadvantage of the method used by sequelize-cli is that it does not make the flow of configuring the ORM composable, so if this behavior is desired it is necessary to implement some mechanism by the user to achieve the composability.

sequelize-fn as it is a function admits the composition in a much more natural way being able to implement the different processes of our application following a much more declarative approach, for example:

const seqFn = require("sequelize-fn");

const setupSequelize = seqFn;
const setupExpress = sequelize => {
  // logic to bind sequelize inside of some express application
};

const initApp = compose(setupExpress, setupSequelize);

initApp(config);

Another improvement in the process of configuring sequelize, is that the cli provides us with an eager approach, so in case a lazy behavior is required it is necessary to implement manually.

sequelize-fn supports an eager approach but it can be configurable to behave lazily in case it is needed, just by passing the lazy property equal to true in the sequelize-fn configuration object it will return a function that when executed is that the instance will be created.

Instalation

This is a Node.js module available through the npm registry.

$ npm install sequelize-fn

Usage

const seqFn = require("sequelize-fn");
const config = {
  dataBaseUri: "mysql://user:pass@host/test",
  modelsDir: "/app/models"
};

// this a fully configured sequelize instance ready to be used
const sequelize = seqFn(config);

dataBaseUri and modelsDir are mandatory in the config object, if not the library will throw and error.

Configuration

  • dataBaseUri: String used to establish connection with the database , it must be in the following format dialect://user:pass@host:[port]/database.

  • modelsDir: Absolute path for directory where models are located.

  • options: Options object used as argument in sequelize constructor you can consult the full sequelize documentation.

  • lazy: Boolean flag to indicate the lazy behavior of the function, when this it's true the call to seqFn not return a sequelize instance, but a function that when executed is that the flow for setup sequelize run.

Executing hook function before return sequelize instance

If a new operation is needed on the sequelize instance, we can pass as second argument of our sequelize-fn function, that argument is a callback that receives the instance created as the first parameter, inside of it we can make many kind of thins over the instance.

Example defining relationships between imported models:

const seqFn = require("sequelize-fn");

const config = {
  dataBaseUri: "mysql://user:pass@host/test",
  modelsDir: "/app/models"
};

const bindAssociations = sequelize => {
  const { Players, Teams } = sequelize.models;

  Players.belongsTo(Teams, {
    foreignKey: {
      name: "teamId",
      allowNull: false
    }
  });
};

const sequelize = seqFn(config, bindAssociations);

Note how the sequelize instance is not returned in the bindAssociations function, it's returned automatically.