Table of Contents
Introduction
One of the duty of my job is to write web scraper -- scrape data and store to mysql tables. And most of the time, the origin tables are not created by myself. It's inefficient to model these tables like sequelize
do. All I want is having direct access to the tables and being efficient. That's why I develop mysql-composer
.
If you are looking for a more powerful tool, sequelize may be a better choice.
Installation
npm i -S mysql-composer
Apis
mysql-composer
is a simple tool for you to execute sql on mysql database.
const Composer = const mysql = const connection = mysql;const composer = connection
insert
insert(config,callback,inspect)
- config:
table:'tableName' //require data: //require field1:'value1' field2:'value2' ignore:true//optional,defaults to true onDuplicateKeyUpdate:field1:'value1' { return 'field1+1' }//optional
- callback: will be called when sql is executed:
callback(err,result)
- inspect: will be called with generated sql :
inspect(sql)
example:
//insert ignore into user (`name`,`age`) values ('Tom','18')composer
update
update(config,callback,inspect)
-
config:
table:'tableName' //requiredata: //requirefield1:'value1'{return 'field2+1'}where:'id=1' //require,if you need to update all, you should explicitly set 'where' to 1. -
callback: will be called when sql is executed:
callback(err,result)
-
inspect: will be called with generated sql :
inspect(sql)
example:
//update `user` set age='18' where id=1composer
query
query(sql,callback,inspect)
- sql: a valid sql syntax
- callback: will be called when sql is executed:
callback(err,result)
- inspect: will be called with generated sql :
inspect(sql)
example:
//update `user` set age='18' where id=1composer
beginTransaction
beginTransaction(callback)
- callback: will be called when beginTransaction is executed:
callback(err)
example:
composer
rollback
rollback(callback)
- callback: will be called when rollback is executed:
callback(err)
commit
commit(callback)
- callback: will be called when commit is executed:
callback(err)
Promise support
mysql-composer
supports promise, it will be convenient if you like to use mysql-composer
with co
or async/await.
if you do not provide a callback in the apis
above, they will return a promise:
var promise = composerpromise