sails-mariadb
MariaDB service for sails.js server
Last updated 3 months ago by elevista .
MIT · Repository · Bugs · Original npm · Tarball
$ cnpm install sails-mariadb 
SYNC missed versions from official npm registry.

Sails MariaDB

MariaDB service for sails.js server

npm package JavaScript Style Guide

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 is params.variableName.
  • ;variableName - same as ?? in node mysql. target value is params.variableName.
  • $variableName - replace $variableName as params.variableName with escaped string in sql string.
  • #variableName - replace #variableName as params.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

Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 9
Dependencies (2)
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |