Socket
Socket
Sign inDemoInstall

sequelize-singleton

Package Overview
Dependencies
21
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.4 to 0.2.0

lib/dive.js

7

index.js

@@ -1,6 +0,1 @@

module.exports = function(config){
var singleton = require('./lib/singleton');
return singleton.getInstance(config);
};
module.exports = require('./lib/singleton');

@@ -1,52 +0,55 @@

"user strict";
// Singleton Object
var singleton = function(){
var instance
, Sequelize
, sequelize;
"use strict";
function createInstance(config){
var path = require("path");
var Sequelize = require("sequelize");
var dive = require("./dive");
var db = {};
if(!config)
throw "The first time getInstace is called it must be called with configuration.";
Sequelize = require('sequelize');
sequelize = new Sequelize(
config.database,
config.username,
config.password,
config.opts
);
// Connect to MySQL
sequelize
.authenticate()
.complete(function(err) {
if (!!err) {
console.log('Unable to connect to the database:', err);
} else {
console.log('Connection has been established to: \''+config.database+'\' Listening on: \''+config.opts.port+'\'');
}
});
db.Sequelize = Sequelize; // Expose Sequelize
db.models = {}; // Expose models
db.discover = ["/model"]; // Set the default discovery paths
db.logger = true; // Set logger to true
db.matcher = null; // Set matcher to null
// Expose the connection function
db.connect = function(database, username, password, options) {
// Instanciate a new sequelize instance
var sequelize = new db.Sequelize(database, username, password, options);
db.discover.forEach(function(location){
return {
DataTypes: Sequelize,
sequelize: sequelize
};
}
// Construct the path to discover
//var p = path.join(__dirname, '/../', location);
return {
getInstance: function(config){
if(!instance) {
console.log("Creating instance");
instance = createInstance(config);
}
// Recurse through the api directory and collect the models
dive(location, function(err, path) {
console.log("returning instance");
return instance;
if(db.logger)
console.log("Loading Model: "+ path.substring(path.lastIndexOf("/") + 1));
var model = sequelize["import"](path);
if(model)
db.models[model.name] = model;
});
});
// Execute the associate methods for each Model
Object.keys(db.models).forEach(function(modelName) {
if ("associate" in db.models[modelName]) {
db.models[modelName].associate(db.models);
}
};
}();
});
module.exports = singleton;
// Expose the sequelize object
db.sequelize = sequelize;
return true;
};
module.exports = db;
{
"name": "sequelize-singleton",
"version": "0.1.4",
"version": "0.2.0",
"author": "Jacob Spizziri <jacob.spizziri@gmail.com> (https://github.com/jspizziri)",

@@ -5,0 +5,0 @@ "license": "BSD-2-Clause",

@@ -1,36 +0,76 @@

sequelize-singleton is a simple singleton wrapper for the sequelize ORM, making it easier to configure and build models.
# sequelize-singleton
Example Usage:
sequelize-singleton is a simple singleton wrapper for the sequelize ORM, making it easier to configure and build models with Sequelize.
* [Configuring sequelize-singleton](#configuring-sequelize-singleton)
* [Custom Matcher](#custom-matcher)
* [Accessing Sequelize](#accessing-sequelize)
* [Defining Models](#defining-models)
## Configuring sequelize-singleton
The sequelize-singleton ```connect()``` argument accepts the same parameters as the Sequelize() object. It is important to configure the ```discover``` array of the set of paths where your models should be discovered.
```
#app.js
var config = { database: 'db', username: 'user', password: 'password123', opts: {...}};
var orm = require('sequelize-singleton')(config);
// app.js
var orm = require('sequelize-singleton');
orm.sequelize
.authenticate()
.complete(function(err){
if(!!err) {
console.log('Unable to connect to the database:',err);
} else {
console.log('Connection has been established successfully');
}
orm.discover = [__dirname + '/models'];
orm.connect(
'test-db',
'test-user',
'secret1234',
{
dialect: "mysql",
port: 3306
});
```
Upon ```connect()``` sequelize-singleton will SYNCHRONOUSLY recurse through the provided file paths looking for any files with the naming convention ```*.model.js```.
Now you can access the sequelize instance wherever you need:
## Custom matcher
Alternatively you can define a custom matching function which returns a ```boolean``` and attach it to:
```
var orm = require('sequelize-singleton')();
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!
var User = sequelize.define('User', {
username: orm.DataTypes.STRING,
password: orm.DataTypes.STRING
});
```
// 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;
};
```
SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc