1.0.50 • Published 4 years ago

@wsuite-team/ws-mysql-easy-model v1.0.50

Weekly downloads
83
License
MIT
Repository
bitbucket
Last release
4 years ago

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 ws-mysql-easy-model

Or install from git:

npm install git://github.com/mayuri-jatakiya/mysql-easy-model.git

Usage

Connecting to MySQL

First, we need to define a connection.

var mysqlEasyModel = require('ws-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

Model.find(selector, callback) Selector and callback are optional.

User.find(function(err, users){
	if(!err) console.log(users);
}

Find with filter

Model.find(selector, callback) Selector and callback are optional.

User.find({id: 1}, function(err, users){
	if(!err) console.log(users);
}

Read a model

model.load(callback) Callback is optional.

var user = new User();
user.id = 1;
user.read(function(err){
  console.log(err);
  if(!err) console.log(user.name);
});

Find with dynamic query

Model.query(sql, params, callback)

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

Update without find

You can set the primary key value and update without finding.

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

Destroy without find

You can set the primary key value and delete without finding

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 MAyuri Jatakiya.

1.0.49

4 years ago

1.0.50

4 years ago

1.0.44

4 years ago

1.0.48

4 years ago

1.0.47

4 years ago

1.0.46

4 years ago

1.0.45

4 years ago

1.0.40

4 years ago

1.0.43

4 years ago

1.0.42

4 years ago

1.0.41

4 years ago

1.0.39

4 years ago

1.0.38

4 years ago

1.0.37

4 years ago

1.0.33

4 years ago

1.0.32

4 years ago

1.0.31

4 years ago

1.0.30

4 years ago

1.0.36

4 years ago

1.0.35

4 years ago

1.0.34

4 years ago

1.0.29

4 years ago

1.0.28

4 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.1

5 years ago