Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sequelize-connect

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sequelize-connect

A simple connection wrapper for the sequelize ORM, making it easier to configure and build models & connections.

  • 1.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20
increased by233.33%
Maintainers
1
Weekly downloads
 
Created
Source

sequelize-connect

sequelize-connect is a simple singleton wrapper for the sequelize ORM, making it easier to configure and build models with Sequelize.

  • Configuring sequelize-connect
  • Custom Matcher
  • Accessing Sequelize
  • Defining Models
  • Logging

Configuring sequelize-connect

NOTE: sequelize-connect must be configured upon app initialization, prior to accessing your models

The sequelize-connect connect() method accepts the same parameters as the Sequelize() object database, username, password, options. It is important to configure the discover array of the set of paths where your models should be discovered.

// app.js
var orm 		= require('sequelize-connect');

orm.discover = [__dirname + '/models'];
orm.connect(
  'test-db',
  'test-user',
  'secret1234',
  {
    dialect: "mysql",
    port:    3306
  })
  .then(function(){
    // Connection is completed
  });

Upon connect() sequelize-connect will ASYNCHRONOUSLY recurse through all of the subfolders located at the provided file paths looking for any files with the naming default convention *.model.js. Connect will return a Promise that is called on it's completion.

Custom matcher

If you prefer to define your own naming convention instead of the default you can create a custom matching function which receives the file name as the parameter returns a boolean indicating if sequelize-connect should attempt to load the file as a model.

This function should be attached to matcher like so:

orm.matcher = function(file){
  if(//some condition or regex here)
    return true;

  return false;
};

Accessing sequelize

After connecting you can access the sequelize instance and models wherever you need!

// somefile.js

var orm       = require('sequelize-connect');
var sequelize = orm.sequelize;
var Sequelize = orm.Sequelize;
var models    = orm.models;
var User      = models.User;

Defining Models

Models are defined as per the suggestion the article here: http://sequelizejs.com/articles/express. All associations are done via the class method associate which is injected with the models object.

// user.model.js

"use strict";

module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define("User", {
    username: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        User.hasMany(models.Task)
      }
    }
  });

  return User;
};

Logging

Logging is done via the winston, the winston object can be accessed at via orm.logger. If you want to control the log level you can do it like so:

orm.logger.level = "debug";

Keywords

FAQs

Package last updated on 25 Oct 2015

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc