0.0.7 • Published 7 years ago

dbobject v0.0.7

Weekly downloads
17
License
ISC
Repository
github
Last release
7 years ago

dbobject

initialize

you need to initialize this module with calling mysql or postgresql methods

var db = require('dbobject');

// initialize for mysql
db.mysql('database', 'username', 'password', 'host');

// OR initialize for postgresql
db.postgresql('database', 'username', 'password', 'host');

call anywhere

just require dbobject when you need to reach database

var db = require('dbobject')

methods

  • mysql(database, user, pass, host)
  • postgresql(database, user, pass, host)
  • query(sql, options)
  • exec(sql, options)
  • first(sql, options)
  • value(sql, options)
  • values(sql, options)
  • keyValue(sql, options)
  • dbobject(tablename, options)
  • end()

dbobject methods

  • select(query, options)
  • insert(values)
  • update(id, values)
  • delete(id)

Usage

  • sample usage for database queries
var db = require('dbobject')

// initialize
db.mysql('database', 'username', 'password', 'host')

db.query('select * from employee').then(function(result){
    console.log(result)
}).catch(function(err){
    console.log(err)
}).then(function(){
    db.end()
})
  • Sample usage with dbobject (simplified crud functions for api services)
var db = require('dbobject')

db.mysql('database', 'username', 'password', 'host')

var dbo = db.dbobject('tablename')

dbo.search({title: 'manager'}).then(function(result){
    console.log(result)
}).catch(function(err){
    console.log(err)
})
  • Sample nodejs/express module for api services
var express = require('express'),
    router = express.Router(),
    db = require(dbobject),
    dbo = db.dbobject('article')

router.get('/', (req, res, next) => {
	dbo.search(req.query).then(function(result){
		res.json(result)
	}).catch(next)
})

router.get('/:id', (req, res, next) => {
	dbo.get(req.params.id).then(function(result){
		res.json(result)
	}).catch(next)
})

router.post('/', (req, res, next) => {
	dbo.insert(req.body).then(function(result){
		res.json(result)
	}).catch(next)
})

router.put('/:id', (req, res, next) => {
	dbo.update(req.params.id, req.body).then(function(result){
		res.json(result)
	}).catch(next)
})

router.delete('/:id', (req, res, next) => {
	dbo.delete(req.params.id).then(function(result){
		res.json(result)
	}).catch(next)
})

module.exports = router;

ToDo

  • PostgreSQL implementation
0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago

1.0.0

7 years ago