sails-mariadb
v1.2.3
Published
MariaDB service for sails.js server
Downloads
87
Readme
Sails MariaDB
MariaDB service for sails.js server
Install
npm i sails-mariadb
Usage
config/connection.js
module.exports.connections = {
//...
MariaDBInfo: {
host: 'localhost',
user: 'root',
password: 'pw',
database: 'db',
multipleStatements: true,
connectionLimit: 10
},
//...
}
or sails.config.MariaDBInfo(higher priority)
This module is based on node mysql. same configuration
In sails controller
const MariaDB = require('sails-mariadb')
MariaDB(function * (conn) {
let rows = yield conn.query(`SELECT * FROM table WHERE id=:id`, {id: 10})
let res = yield conn.query(`INSERT INTO table2 SET foo=:foo`, {foo: 'foo'})
return {rows, affectedRows: res.affectedRows}
}).then(r => console.log(r), e => console.error(e))
This function is based on co. same usage.
In generator function, all queries are in transaction.
And will auto commit transaction if all promises are resolved in function
and auto rollback if there's any promise rejection in function.(only promises with yield)
query result is same as mysql
Connection Object Structure
└── conn
├── beginTransaction : Promise
├── changeUser : Promise
├── commit : Promise
├── destroy
├── on : Promise
├── ping : Promise
├── query : Promise
├── release
├── rollback : Promise
├── prototype : PoolConnection (original connection from mysql module)
└── (prototype) : PoolConnection (original connection from mysql module)
NOTE: If want to call that not own method of this object,
please use 'conn.prototype.foo(...args)'
Query Statement
conn.query(sql, params)
In sql
string
:variableName
- same as?
in node mysql. target value isparams.variableName
.;variableName
- same as??
in node mysql. target value isparams.variableName
.$variableName
- replace$variableName
asparams.variableName
with escaped string in sql string.#variableName
- replace#variableName
asparams.variableName
with raw string in sql string. (unsafe!)
Config
const config = {
useStream: { // default : false
fieldsHandler (fields) {
// handle fields (if exist)
},
rowHandler (row) {
// handle a row
}
}
}
Global Config
const MariaDB = require('sails-mariadb')
MairaDB.config.useStream = {/* ... */}
Instance Config
const MariaDB = require('sails-mariadb')
MariaDB(function * (conn) {
// ...
}, {useStream: {/* ... */}})
License
The MIT License (MIT)
Copyright (c) 2017 Elevista