mysql-bundle
=== mysql-bundle is extended from felixge/node-mysql but more easy to use. Besides the basic functions of mysql, it has file template sql and ES5 generator-supporting.
Features
1. File Template Sql
You can put sql in *.sql file under your project (default is in ${project}/mysql-bundle/sql/). The mysql-bundle will load them into memory when startup. Then, you can reference them by their names.
2. ES5 Generator Supporting
This feature can makes you away from complex callbacks and use this module in koa
config
3. All Configs ViaInstallation
$ npm install mysql-bundle
Example
var bundle = ; // get a common client (use callback)var client = bundle; // get a generator-supporting client var coClient = bundle; var userData = nickname: 'Jim Hanks' username: 'Jim0455' age: 21; // callback-like query vio template (name without suffix ".sql" )client;var sql = 'update `user` set `nickname` = :nickname, `username` = :username, `age` = :age'client; // generator-supporting query;
Guide
1. Configure
All configs are under 'mysql-bundle' node of config. For example:
config['mysql-bundle']['config'] is the cluster init config, used by require('mysql').createPoolCluster. The details info is: PoolCluster Option. Every node in config['mysql-bundle']['cluster'] is the connection config. The details info is: Connection options.
2. Sql File
The default template dir is "mysql-bundle/sql", this can be configured by change the value of config['mysql-bundle']['config']['templateDir']. This value is relative to cwd.
3 Template Formate
// 1. array format, this is the same as node-mysqlvar sql = 'update `tab` set a = ?, b = ?, c = ?';client;// when sql content in template file upate_tab.sql under template dirclient; // 2. object/map formatvar sql = 'update `tab` set a = :a, b = :b, ::col_c = :c';var map = a: 'a' b: 'b' c: 'c' col_c: 'c' // the 'col_c' is start '::' not ':', that means , it's a col-name, will be wrap by `...` ;client;// also, this sql can be put into a template file and well-formated. If it in upate_tab_map.sqlclient; // 3. all example above in generator var rows1 = client;var rows2 = client;var rows3 = client;var rows4 = client; // 4. If there is a sub-dir in template dir such as ${template dir}/subdir, // then the sql file under it can be access by 'subdir.${name}'. // For example: upate_tab_map.sql under ${template dir}/subdir: var rows4 = client // This feature is a simple modularized design.