Coder Social home page Coder Social logo

jacoblassen / sequelize-singleton Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jspizziri/sequelize-singleton

0.0 2.0 0.0 301 KB

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

JavaScript 100.00%

sequelize-singleton's Introduction

sequelize-singleton

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

Configuring sequelize-singleton

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

The sequelize-singleton 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-singleton');

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

Upon connect() sequelize-singleton will SYNCHRONOUSLY recurse through all of the subfolders located at the provided file paths looking for any files with the naming default convention *.model.js.

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-singleton 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

Now you can access the sequelize instance and models wherever you need!

// somefile.js

var orm       = require('sequelize-singleton');
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 can be turned off by setting orm.logger = false. Additionally, a custom logging function can be defined, which is passed a parameter log. E.g.

orm.logger = function(log){
  //do some sweet logging stuff here
}

sequelize-singleton's People

Contributors

jspizziri avatar hachichaud avatar

Watchers

James Cloos avatar Jacob Lassen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.