node package manager

tiny-models-sequelize

What is Tiny-Models ?

  • Simple - Tiny Models it's a module that will help you save a lot of time in your process of creating models for your project
  • Automatic - Tiny-Models will read all the modules you have created in an specific folder in order to create the sequelize model associated.
    Important : The name of the models will be the name of the model file
  • Clear - The definition of the modules is really clear and this is the force of Tiny-Models. The models are finally just a Javascript object which makes the creation of models really simple and clear.

Installation

npm install tiny-models-sequelize

Sample model models/Users.js

This file will give you a taste of what Tiny-Models does. The name of the model, will be : Users

 
var Seq = require('sequelize');
 
//Creating our module 
module.exports = {
 
    model: {
        first_name: {type : Seq.STRING},
        last_name: {type : Seq.STRING},
        mail: {type : Seq.STRING},
        password: {type : Seq.STRING},
    },
 
    options : {
        instanceMethods : {
            validPassword : function(password) {
                var ret = cypter.comparePassword(password, this.password)
                return ret;
            }
        }
    }
 
}
 

Initialize Tiny-Models

Here you learn how to initialize the module. For that you have to give the path where your models are defined and the configuration of the database.

 
var tmS = require('tiny-models-sequelize');
 
tmS.init({
    config_path : path.join(__dirname, 'config', 'database.json'),
    models_path : path.join(__dirname, 'models')
});
 
 

Sample database.json

You can call this file as you like

{
    "development": {
        "database": "YourBDD",
        "dialect": "mysql",
        "host": "127.0.0.1",
        "username": "root",
        "password": ""
    }
}
 

Use a model

When you have defined your model, you will be able to use it really easily, and of course have access to all the sequelize methods.
See Sequelize doc

var tmS = require('tiny-models-sequelize');
 
function findUsers() {
    var Users = tmS.getModel('Users');
 
    Users.findAll()
    .then(function (data) {
        console.log(data);
    })
    .catch(function (err) {
        console.log(err);
    })
}
 

If you have any questions or feedbacks, feel free to contact me at : levacher.alex@gmail.com
This module was developed for Luncher.fr
This module is inspired by a Jeysson Guevara article