0.1.1 • Published 8 years ago
mysql-easy-model v0.1.1
mysql-easy-model
MySQL Easy Model is a MySQL object modeling tool designed to work in an asynchronous environment.
Installation
Install from npm package:
npm install mysql-easy-model
Or install from git:
npm install git://github.com/rudolfoborges/mysql-easy-model.git
Usage
Connecting to MongoDB
First, we need to define a connection.
var mysqlEasyModel = require('mysql-easy-model');
mysqlEasyModel.createConnection({
connectionLimit : 10,
host : 'localhost',
user : 'root',
password : '',
database : 'test'
});
Defining a Model
Models are defined through the name and options object. var User = mysqlEasyModel.model(name, options)
var User = mysqlEasyModel.model('user', {
table: 'user',
fields: ['id', 'name', 'email'],
primary: ['id']
});
Accessing a Model
After define a model, we can access it through the same function. The argument is the name of the your model.
var User = mysqlEasyModel.model('user');
Find
User.find(function(err, users){
if(!err) console.log(users);
}
Find with filter
User.find({id: 1}, function(err, users){
if(!err) console.log(users);
}
Read a model
var user = new User();
user.id = 1;
user.read(function(err){
console.log(err);
if(!err) console.log(user.name);
});
Find with dynamic query
User.query('select * from user where email = ?', ['js@gmail.com'], function(err, users){
if(!err) console.log(users);
});
Create a new Model
var user = new User({name: 'John Smith', email: 'js@gmail.com'});
user.create(function(err, result){
if(!err) console.log('created');
});
Find one and update
User.findOne({email: 'js@gmail.com'}, function(err, user){
if(user){
//Update the name
user.name = 'John Smith';
user.update(function(err, result){
if(!err) console.log('updated');
})
}
});
Immediate update - Update without find
var user = new User({id: 1, name: 'John Smith'});
user.update(function(err, result){
if(!err) console.log('updated');
});
OR
var user = new User();
user.id = 1;
user.name = 'John Smith';
user.update(function(err, result){
if(!err) console.log('updated');
});
Find one and destroy
User.findOne({email: 'js@gmail.com'}, function(err, user){
if(user) user.destroy(function(err, result){
if(!err) console.log('removed');
});
});
Immediate destroy - Destroy without find
var user = new User({id: 1});
user.destroy(function(err, result){
if(!err) console.log('removed');
});
OR
var user = new User();
user.id = 1;
user.destroy(function(err, result){
if(!err) console.log('removed');
});
Examples
Contains runnable sample mysql-easy-model programs.
Bugs
If you'd like to leave feedback, please open an issue on GitHub.
License
mysql-easy-model is released under MIT license.
Credits
mysql-easy-model was created by Rudolfo Borges.