aa-mysql
v1.1.2
Published
A simple and flexible mysql library for node.
Downloads
13
Maintainers
Readme
A simple and flexible MySql library for node.
Installation
$ npm install aa-mysql
Usage
// insert amoa400
conn.table('user').insert({name: 'amoa400'});
// find amoa400
conn.table('user').where({name: 'amoa400'}).find();
// rename amoa400 to cai0715
conn.table('user').where({name: 'amoa400'}).update({name: 'cai0715'});
// delete cai0715
conn.table('user').where({name: 'cai0715'}).delete();
// select users whose id is less than 2 or greater than 3, limit 10
conn.table('user').where({id: [2, '<']}, {id: [3, '>']}, 'OR').limit(10).select();
Example
var aamysql = require('aa-mysql');
// config
aamysql.config({
host: 'localhost',
port: 3306,
user: 'root',
pass: '',
prefix: 'aa_',
db: 'aa-mysql',
connLimit: 20
});
// use single connection to query
// you can also use connection pool, and it is recommended
var conn = aamysql.create();
conn.connect(function(err) {
if (err) {
console.log(err);
return;
}
// select
conn.table('user').select(function(err, res) {
if (err) {
console.log(err);
return;
}
console.log(res);
});
// transaction
conn.transaction([
conn.table('user').option({get: true}).insert({name: 'amoa400'}),
conn.table('user').option({get: true}).insert({id: 'hi', name: 'cai0715'})
// will cause rollback, because id must be number
], function(err, res) {
if (err) {
console.log(err);
return;
}
console.log(res);
});
});