Skip to content

blacksmithstudio/blockbase-mysql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Driver Mysql for Blockbase

Compatible with Blockbase Framework

Travis Blockbase

Version

1.0.2

How to install ?

$ npm i --save blockbase-mysql

Then add to your config/{env}.yml the following instructions depending of your system

dbms : mysql
mysql :
    host : localhost
    user : johndoe
    password :
    port : 3306
    database : yourdatabase

mysql driver also supports npm mysql options, and implements connection Pooling

How to use ?

When you configure mysql as your dbms, Blockbase automatically binds the driver to the models with the basic methods. Such as read/save/update/delete etc.

Basic usage
//myController.js
module.exports = (app) => {
    cont Logger = app.drivers.logger
    const User = app.models.user

    return {
        async foo(req, res){

            let user = new User({id: 25})
            try {
              user = await user.read()

              //do stuff..

              res.send(user.expose('public'))
            catch (e) {
                Logger.error('foo', e)
                res.status(500).send({error :e})
            }
        }
    }
}
Standalone

The driver can be called inside your controller/models etc..

const mysql = app.drivers.mysql

let q = `SELECT * FROM users`
try {
  let result = await mysql.execute(q, [])
...
Within a controller :
//myController.js
module.exports = (app) => {
    const mysql = app.drivers.mysql

    return {
        async foo(req, res){
            //Do something with mysql
    ...
Within a model :
//myModel.js
module.exports = (app) => {
    const Model = app.model._model

    return class MyModel extends Model {
        constructor(data){
            super({type: 'user'})
            if(data) this.data = data
        }

        async foo(bar){
            let q = `SELECT * FROM ${this.params.type}`

            try {
                //this.client is binded to mysql
                return await this.client.execute(q, [])
            }
            catch(e) {
                throw e
            }
        }
    }
}

Methods

Blockbase-mysql driver implements the following methods :

  • read : read data from a Blockbase model that has an id
  • save : insert data based on a Blockbase model with model validation
  • update : update data based on a Blockbase model
  • delete : delete an item
  • execute: execute a raw query

Run tests

Blockbase has some unit tests (with Mocha) written run them often !

$ npm test

License

(Licence MIT) Coded by Blacksmith

Free Software, Hell Yeah!

Releases

No releases published

Packages

No packages published