SQLX
Database driver with extended features like mysql changelog/oplog, connection auto release.
Table of Contents generated with DocToc
Demo
async/await
// initconst client = sqlxconst DB_CONFIG = type: 'mongodb' config: url: 'mongodb://localhost:27017/test?maxPoolSize=30' extend: { let table = args0 if table !== 'test' throw 'Invalid table!' thisconstructorprototypefind } const USER_CONFIG = user: 'root' actions: '*'clientconst conn = client // catch errortry await conn catch err // usagelet doc = fields_1: 'hello'let result = await connassertassert result = await connassert
callback
the demo is in the following documents.
Usage
database driver with extended features.
database list
- mysql
- mongodb
- redis
- define custom function to use any database or service
feature list
- changelog/oplog
- auto release timeout connection
- read-write access control
api support
Method | MySQL | MongoDB | Redis |
---|---|---|---|
select | √ | √ | √ |
selectEx | √ | × | × |
insert | √ | √ | √ |
update | √ | √ | √ |
delete | √ | √ | √ |
find | × | √ | × |
aggregate | × | √ | × |
count | × | √ | × |
findOneAndUpdate | × | √ | × |
interface
overall
const sqlx = const client = sqlx // client.define(table, config_or_interface)clientclientclientclientclient // match all other tables // for changelogvar operator_info = user: '101,23' actions: // action whitelist 'select' 'update' const conn = client // callbackconn // Promise/async/awaitlet resulttry result = await conn console // rows console // info catch err throw err
action whitelist: operator_info.actions
operator_infoactions = '*' // allow alloperator_infoactions = 'select' // allow: select
config_or_interface
var config1 = type: 'mysql' config: host: '1.1.1.1' database: 'db1' user: 'root' password: '' var config2 = type: 'mysql' config: host: '2.2.2.2' database: 'db2' user: 'root' password: '' var config2 = type: 'redis' config: host: '127.0.0.1' port: '6379' const InterfaceOne = // All methods are optional // Do NOT change definition of any method { // 1. 'initialize' is called whenever client.define is called // 2. 'this' is created whenever 'initialize' is called // 3. 'this' is shared between all methods of InterfaceOne this_client = this_client } { } { } { } { } { } {} const InterfaceTwo = { const request = var p = url: 'http://example.com/table/insert' method: 'post' json: true body: field: fields query: where }
method
// callbackconn conn conn conn conn conn conn conn conn conn // Promise/async/awaitlet resulttry result = await conn console // rows console // info catch err throw err// other functions also works.
where
where is mongo-like JSON object, examples:
// a == 1 || b == 2 $or: a:1 b:2
extend
mysql
let config_with_extend = type: 'mysql' config: host: '1.1.1.1' database: 'db1' user: 'root' password: '' extend: { if sets === undefined return thisconstructorprototypeinsert }
mongodb
let config_with_extend = type: 'mongodb' config: url: 'mongodb://localhost:27017/test?maxPoolSize=30' extend: { if where === undefined throw 'find some error before call sqlx' thisconstructorprototypefind }
Development
git clone https://github.com/yinrong/node-sqlx.git
cd node-sqlx
npm i
npm test