GithubHelp home page GithubHelp logo

node-nobatis's Introduction

nobatis

This is a simple mybatis-like DAO(NOT ORM) for node.js.

Features

  • query mapper and builder
  • TODO: connection management
  • TODO: promise support(kriskowal's Q)
  • TODO: caching
  • TODO: validation
  • TODO: logging
  • TODO: error handling
  • DAO(NOT ORM)

Install

$ npm install nobatis

or

$ npm install [email protected]:iolo/node-nobatis.git

How to Get DataSource

  • 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)

  • import nobatis module

var nobatis = require('nobatis');
  • 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();
  • now you can openSession():
var session = null;
try {
  session = dataSource.openSession();
  // use session here …
} finally {
  session && session.close();
}
  • or work withSession():
dataSource.withSession(function (session) {
  // use session here ...
});
  • work withSession() and promise:
dataSource.withSession(function (session) {
  // use session and return promise ...
  return session.select(...)
    .then(function (select_result) {
      return session.insert(...);
    })
    .then(function (insert_result) {
      return session.update(...);
    })
    .then(function (update_result) {
      return session.destroy(...);
    });
})
.then(function (destroy_result) {
  ...
})
.catch(function (err) {
  ...
});

How to Execute Queries

  • select multiple rows
session.select('test1.selectAll', [])
  .then(function (rows) {
    ...
  })
  .catch(function (err) {
    ...
  });
  • select multiple rows with row bounds
session.select('test1.selectAll', [], {offset:2, limit:2})
  .then(function (rows) {
    ...
  })
  .catch(function (err) {
    ...
  });
  • select a single row
session.selectOne('test1.select', \[1])
  .then(function (row) {
    ...
  .catch(function (err) {
    ...
  });
  • insert new row
session.insert('test1.insert', {name:'a'})
  .then(function (insertId) {
    ...
  .catch(function (err) {
    ...
  });
  • update row(s)
session.update('test1.update', {id:1, name:'a'})
  .then(function (affectedRows) {
    ...
  .catch(function (err) {
    ...
  });
  • delete row(s)
session.destroy('test1.delete', \[1])
  .then(function (affectedRows) {
    ...
  .catch(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) {
    ...
  .catch(function (err) {
    ...
  });
  • insert/update an object
dao.save(obj)
  .then(function (affectedRow-or-insertId) {
    ...
  .catch(function (err) {
    ...
  });
  • insert/update an object and reload it
dao.save(obj, true)
  .then(function (obj) {
    ...
  .catch(function (err) {
    ...
  });
  • delete an object by primary key
dao.destroy(pk)
  .then(function (success_or_not) {
    ...
  .catch(function(err) {
    ...
  });
  • select all rows
dao.all()
  .then(function (rows) {
    ...
  .progress(function (row) {
    ...
  .catch(function(err) {
    ...
  });
  • select all rows with bounds
dao.all({offset:10, limit:10})
  .then(function (rows, numRows) {
    ...
  .progress(function (row) {
    ...
  .catch(function(err) {
    ...
  });

More

node-nobatis's People

Contributors

iolo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.