Compatible with Blockbase Framework
v1.0.17
$ npm i --save blockbase-postgresql
Then add to your config/{env}.yml the following instructions depending of your system
dbms : postgresql
postgresql :
host : localhost
user : johndoe
password :
port : 5432
database : yourdatabase
Postgresql driver also supports npm pg options, and implements connection Pooling
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.
//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})
}
}
}
}
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, [])
...
//myController.js
module.exports = (app) => {
const mysql = app.drivers.mysql
return {
async foo(req, res){
//Do something with mysql
...
//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
}
}
}
}
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
Blockbase has some unit tests (with Mocha) written run them often !
$ npm test
(Licence MIT) Coded by Blacksmith
Free Software, Hell Yeah!