All Projects → jspizziri → sequelize-connect

jspizziri / sequelize-connect

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to sequelize-connect

Nodejs-Tutorials
Nodejs Tutorial
Stars: ✭ 38 (+2.7%)
Mutual labels:  sequelize, sequelize-orm
sequelize-slugify
Sequelize Slugify is a plugin for the Sequelize ORM that automatically creates and updates unique, URL safe slugs for your database models.
Stars: ✭ 49 (+32.43%)
Mutual labels:  sequelize, sequelize-orm
nest-blog-api
Blog Web API with NestJs, Postgres, and Sequelize ORM
Stars: ✭ 69 (+86.49%)
Mutual labels:  sequelize, sequelize-orm
hapi-sequelizejs
A hapi.js plugin to connect with Sequelize ORM
Stars: ✭ 56 (+51.35%)
Mutual labels:  sequelize, sequelize-orm
seedpress-cms
A headless CMS built in Express for PostgresQL using Sequelize. Generally follows the Wordpress post and term schema.
Stars: ✭ 71 (+91.89%)
Mutual labels:  sequelize, sequelize-orm
react-ant-admin-api
专为 react-ant-admin 开发的后端API,完善的权限路由系统,使用 eggjs、ts、mysql开发
Stars: ✭ 52 (+40.54%)
Mutual labels:  sequelize
chanyeong
👨‍💻 chanyeong's portfolio and blog webpage
Stars: ✭ 39 (+5.41%)
Mutual labels:  sequelize
auth-quickstart
Authentication Quickstart with Express, Passport and Sequelize
Stars: ✭ 49 (+32.43%)
Mutual labels:  sequelize
passport-local-sequelize
passport-local-sequelize is a sequelize plugin for building username and password login with passport
Stars: ✭ 81 (+118.92%)
Mutual labels:  sequelize
pg-search-sequelize
Postgres full-text search in Node.js and Sequelize.
Stars: ✭ 31 (-16.22%)
Mutual labels:  sequelize
doc
Sequelize Documentation
Stars: ✭ 32 (-13.51%)
Mutual labels:  sequelize
wildebeest
Database migration for Sequelize.
Stars: ✭ 13 (-64.86%)
Mutual labels:  sequelize
koa-orm
koa orm using sequelize & sk2 (fork from knex)
Stars: ✭ 62 (+67.57%)
Mutual labels:  sequelize
expresso
expresso ☕ ( expressjs, Sequelize, TypeScript )
Stars: ✭ 111 (+200%)
Mutual labels:  sequelize-orm
serverless-node-sequelize-rds-rest-api
Serverless rest api application with Node.js to perform simple CRUD operation using MYSQL database hosted on AWS RDS with Sequelize ORM
Stars: ✭ 43 (+16.22%)
Mutual labels:  sequelize-orm
2019-21
🙋‍♀️ 🙋‍♂️ 바글바글(Vaagle): 실시간 질의응답 및 투표 공유 서비스
Stars: ✭ 38 (+2.7%)
Mutual labels:  sequelize-orm
node-server-template
This is Node.js server tidy template / boilerplate with Express (with asyncified handlers, custom error handler) framework and MongoDb. The server use ES6 and above. On different branches you can see different techniques' and technologies' usage, such as Kafka, nodemailer, file download... You also can find postman collections.
Stars: ✭ 116 (+213.51%)
Mutual labels:  sequelize
libstack
NodeJS modules to boost productivity
Stars: ✭ 24 (-35.14%)
Mutual labels:  sequelize
react-with-nodejs-and-sequelize
React-Redux application using NodeJS relational database API with Sequelize ORM. Two level CRUD with a main data table (bands) and other that is relationed with it (albums).
Stars: ✭ 30 (-18.92%)
Mutual labels:  sequelize-orm
sequelize-mig
Sequelize migration generator and es6 init tool (Planned)
Stars: ✭ 54 (+45.95%)
Mutual labels:  sequelize

sequelize-connect

Commitizen friendly

Formerly sequelize-singleton.

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

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.

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

var orm = new Connection(
  'test-db',
  'test-user',
  'secret1234',
  {
    dialect: "mysql",
    port:    3306
  }
)
.then(function(instance){
  // Connection is completed
});

It is important to configure the discover array of the set of paths where your models should be discovered.

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

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

Upon the first initialization of the Connection e.g. new Connection(...); 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.

Connection String

You can use a connection string to connect as well:

new Connection(
  'MyConnectionString',
  {
    dialect: "mysql",
    port:    3306
  })
  .then(function(){
    // Connection is completed
  });

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 injected to Connection like so:

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

  return false;
};

new Connection(
  'test-db',
  'test-user',
  'secret1234',
  {
    dialect: "mysql",
    port:    3306
  },
  discover,
  matcher
)

Accessing sequelize

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

// somefile.js

var Connection = require('sequelize-connect');
var orm = new Connection(); // singleton pattern - returns the created instance
var sequelize = orm.sequelize;
var Sequelize = orm.Sequelize;
var models    = orm.models;
var User      = models.User;

If you prefer, rather than waiting for the connection to load before initializing every part of your application, you can wrap the code that has dependencies on your models in a then. e.g.

// app.js
new Connection(
  'MyConnectionString',
  {
    dialect: "mysql",
    port:    3306
  })
// foobar.js

var Promise = require('bluebird');
var Connection = require('sequelize-connect');
var orm = new Connection();

// This will ensure that the connection has been established
// if you load foobar.js before you wait for your initial connection
// to return
var Promise.resolve(orm)
  .then(function(instance) {
    var User = instance.models.Foo;

    /**
     * Get list of users
     * restriction: 'admin'
     */
    var index = function(req, res) {
      Foo.bar()
    };
  })

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
  });

  // class association method
  User.associate = function(models) {
    User.hasMany(models.Task);
  }

  return User;
};

Logging

Logging is optional, but is turned off by default. In order to enable logging, simply inject the logger of your choice:

myLogger = console;
// myLogger = winston;
// myLogger = ...;

new Connection(
  'test-db',
  'test-user',
  'secret1234',
  {
    dialect: "mysql",
    port:    3306
  },
  discover,
  matcher,
  myLogger
)

Your logger must comply with the following interface:

logger.log(level, message);

Contributing

Please read the contributing guidlines

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].