nobatis

0.0.7 • Public • Published

nobatis

This is extremely experimental stuff

This is a simple "mybatis-like" dao for nodejs.

Features

  • TBW ...

Install

npm install nobatis

or

npm install git@github.com:iolo/node-nobatis.git

How to Get DataSource

  1. prepare configurations:

var config = {
  "dataSource": {
    "driver": "mariasql",
    "host": "localhost",
    "port": 3306,
    "user": "root",
    "password": "",
    "db": "test"
  },
  "queries": {
    "test1.selectAll": "SELECT * FROM test1",
    "test1.select": "SELECT * FROM test1 WHERE id=?",
    "test1.insert": "INSERT INTO test1(name) VALUES(:name)",
    "test1.update": "UPDATE test1 SET name=:name WHERE id=:id",
    "test1.delete": "DELETE FROM test1 WHERE id=?"
  }
};

or you can write configurations to a file(json module). 3. import nobatis module


var nobatis = require('nobatis');
  1. create DataSource with configutaion:

var dataSource = nobatis.createDataSource(config);

or create one with a configuration file(json module):


var dataSource = nobatis.createDataSource(require('./config'));

or get the default one:


var dataSource = nobatis.createDataSource();
  1. now openSession():

var session = null;
try {
  session = dataSource.openSession();
  // use session here …
} finally {
  session && session.close();
}

or withSession():


dataSource.withSession(function (session) {
  // use session here ...
});

How to Execute Queries

  • select multiple rows

session.select('test1.selectAll', [])
.then(function(rows) {
  ...
}).fail(function(err) {
  ...
});
  • select rows with row bounds

session.select('test1.selectAll', [], {offset:2, limit:2})
.then(function(rows) {
  ...
})
.fail(function(err) {
  ...
});
  • select a single row

session.selectOne('test1.select', \[1])
.then(function(row) {
  ...
.fail(function(err) {
  ...
});
  • insert new row

session.insert('test1.insert', {name:'a'})
.then(function(insertId) {
  ...
.fail(function(err) {
  ...
});
  • update row(s)

session.update('test1.update', {id:1, name:'a'})
.then(function(affectedRows) {
  ...
.fail(function(err) {
  ...
});
  • delete row(s)

session.destroy('test1.delete', \[1])
.then(function(affectedRows) {
  ...
.fail(function(err) {
  ...
});

How to Create DAO

  • prepare dao object

var nobatis = require('nobatis');
var dataSource = require('nobatis').createDataSource(config);
var dao = nobatis.createDao(dataSource, {
  table: 'test1',
  primaryKey: 'id',
  primaryKeyGenerated: true,
  defaults: function () {
    return {
      id: 0,
      name: '',
      created: new Date()
    };
  }
});
  • create new object with default attributes

var obj = dao.createNew();
  • create new object with custom attributes

var obj = dao.createNew({name:'foo'});
  • check the object is saved or not

dao.isNew(obj);
  • select an object by primary key

dao.load(pk)
.then(function (obj) {
  ...
.fail(function(err) {
  ...
});
  • insert/update an object

dao.save(obj)
.then(function (affectedRow-or-insertId) {
  ...
.fail(function(err) {
  ...
});
  • insert/update an object and reload it

dao.save(obj, true)
.then(function (obj) {
  ...
.fail(function(err) {
  ...
});
  • delete an object by primary key

dao.destroy(pk)
.then(function (success_or_not) {
  ...
.fail(function(err) {
  ...
});
  • select all rows

dao.all()
.then(function (rows) {
  ...
.progress(function (row) {
  ...
.fail(function(err) {
  ...
});
  • select all rows with bounds

dao.all({offset:10, limit:10})
.then(function (rows, numRows) {
  ...
.progress(function (row) {
  ...
.fail(function(err) {
  ...
});
  • see also [https://github.com/kriskowal/q]

  • TBW

Readme

Keywords

none

Package Sidebar

Install

npm i nobatis

Weekly Downloads

0

Version

0.0.7

License

MIT

Last publish

Collaborators

  • iolo