0.0.26 • Published 10 years ago

dbhouse v0.0.26

Weekly downloads
4
License
-
Repository
github
Last release
10 years ago

DBHouse

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

Installation

Using NPM to install DBHouse module directly:

npm install dbhouse

Quick Examples

DBHouse is really easy to use, some topic you might be interested, see below:


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);
        });
});

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".


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();
});

Documentation

APIs

Database Operator


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.


Sets specific collection(table) as current collection(table).


Same function with collection(), it is just another name for developer who is familar with SQL.


Select the content of columns(fields) from a database.

Arguments

  • fields - Object of fields to include or exclude (not both), {‘a’:1}

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);
        });
});

License

Licensed under the MIT License.

Authors

Copyright© 2012 Fred Chien <fred@mandice.com>

0.0.26

10 years ago

0.0.25

10 years ago

0.0.24

11 years ago

0.0.23

11 years ago

0.0.22

11 years ago

0.0.21

11 years ago

0.0.20

11 years ago

0.0.19

11 years ago

0.0.18

11 years ago

0.0.17

11 years ago

0.0.16

11 years ago

0.0.15

11 years ago

0.0.14

11 years ago

0.0.13

11 years ago

0.0.12

12 years ago

0.0.11

12 years ago

0.0.10

12 years ago

0.0.9

12 years ago

0.0.8

12 years ago

0.0.7

12 years ago

0.0.6

12 years ago

0.0.4

12 years ago

0.0.3

12 years ago

0.0.2

12 years ago

0.0.1

12 years ago