sequelize-connect
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
- Custom Matcher
- Accessing Sequelize
- Defining Models
- Logging
- Contributing
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.jsvar Connection = ; var orm = 'test-db' 'test-user' 'secret1234' dialect: "mysql" port: 3306 ;
It is important to configure the discover
array of the set of paths where your models should be discovered.
// app.jsvar Connection = ; var discover = __dirname + '/models' ...;var orm = 'test-db' 'test-user' 'secret1234' dialect: "mysql" port: 3306 discover;
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:
'MyConnectionString' dialect: "mysql" port: 3306 ;
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 { if//some condition or regex here) return true; return false;}; '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 = ;var orm = ; // singleton pattern - returns the created instancevar sequelize = ormsequelize;var Sequelize = ormSequelize;var models = ormmodels;var User = modelsUser;
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 'MyConnectionString' dialect: "mysql" port: 3306
// foobar.js var Promise = ;var Connection = ;var orm = ; // This will ensure that the connection has been established// if you load foobar.js before you wait for your initial connection// to returnvar Promise
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 { var User = sequelize; // class association method User { User; } 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 = ...; 'test-db' 'test-user' 'secret1234' dialect: "mysql" port: 3306 discover matcher myLogger
Your logger must comply with the following interface:
logger;
Contributing
Please read the contributing guidlines