DBHouse is a generic database API framework, it makes developer to be able to access any kinds of database via generic API. API was designed like SQL Syntax and easy-use.
* Currently, DBHouse only supports MongoDB with official driver (node-mongodb-native).
Using NPM to install DBHouse module directly:
npm install dbhouse
DBHouse is really easy to use, some topic you might be interested, see below:
### Queries
Peform a simple query and return only one record.
Example
var DBHouse = require('dbhouse');
/* Create connection with MongoDB */
var dbHouse = new DBHouse;
dbHouse.connect('mongodb', { host: 'localhost', port: 27017 }, function() {
/* Create a database operator */
var db = new DBHouse.Database(dbHouse);
db.open('mydb')
.collection('users')
.where({
'$or': [ { name: 'Fred Chien'}, { name: 'Stacy Li' } ]
})
.limit(1)
.query(function(err, data) {
if (err)
throw err;
console.log(data);
});
});
### Object/Relation Mapping(ORM)
DBHouse attempts to implement Object/Relation Mapping(ORM), you can define own database scheme with DBHouse APIs.
Example
var DBHouse = require('dbhouse');
/* Create connection with database server */
var dbHouse = new DBHouse;
/* Define schema */
var Contact = new DBHouse.Schema({
_id: { type: 'UUID' },
name: { type: 'String' },
email: { type: 'String' },
tel: { type: 'String' },
created: { type: 'Date' }
});
dbHouse.connect('mongodb', { host: 'localhost', port: 27017 }, function() {
/* Create a database operator */
var db = new DBHouse.Database(dbHouse);
db.open('mydb')
.collection('contact')
.model(Contact)
.insert({
name: 'Fred Chien',
email: 'fred@mandice.com',
tel: '0926123456',
created: new Date().getTime()
}, function(err, data) {
if (err)
throw err;
console.log(data);
});
});
* DBHouse will generate UUID automatically for "_id" field if you set field type to "UUID".
### Creating Database Indexes
With ORM mechanism of DBHouse, it can just use DBHouse.Index
to create indexes for specific fields in database.
Example
var DBHouse = require('dbhouse');
/* Define schema */
var Contact = new DBHouse.Schema({
_id: { type: 'UUID' },
name: { type: 'String' },
email: { type: 'String' },
tel: { type: 'String' },
created: { type: 'Date' }
});
/* Create Indexes */
var index = new DBHouse.Index([
{ fields: [ 'created' ] },
{ fields: [ 'name', 'created' ] }
]);
/* Create connection with database server */
var dbHouse = new DBHouse;
dbHouse.connect('mongodb', { host: 'localhost', port: 27017 }, function() {
/* Create a database operator */
var db = new DBHouse.Database(dbHouse);
/* Create Index */
db.open('mydb')
.collection('contact')
.model(Contact, index)
.createIndex();
});
Database Operator
### [Database Operator].open(db_name)
Sets specific database as the default (current) database.
Note that DBHouse always attempts to keep connection alive for more queries next time, it means that open() doesn't re-create a new connection every time if connection is still alive.
### [Database Operator].collection(collection_name)
Sets specific collection(table) as current collection(table).
### [Database Operator].table(table_name)
Same function with collection(), it is just another name for developer who is familar with SQL.
### [Database Operator].select(fields)
Select the content of columns(fields) from a database.
Arguments
- fields - Object of fields to include or exclude (not both), {‘a’:1}
### [Database Operator].where(condition)
Select the condition for filtering records.
Example
Find records with specific field:
var DBHouse = require('dbhouse');
// Create connection with MongoDB
var dbHouse = new DBHouse;
dbHouse.connect('mongodb', { host: 'localhost', port: 27017 }, function() {
// Create a database operator
var db = new DBHouse.Database(dbHouse);
db.open('mydb')
.collection('users')
.where({
name: 'Fred Chien'
})
.query(function(err, data) {
if (err)
throw err;
console.log(data);
});
});
Licensed under the MIT License.
Copyright© 2012 Fred Chien <fred@mandice.com>