sails-mariadb

1.2.3 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i sails-mariadb

Weekly Downloads

21

Version

1.2.3

License

MIT

Last publish

Collaborators

  • elevista