@webgap/database-dao

0.1.2 • Public • Published

WebGAP Database DAO

Build Status Test Coverage Code Climate Dependency Status

NPM version NPM downloads

README

This is the WebGAP DatabaseDAO module.
Handles abstraction for multiple database systems.

Dependencies

Handles logging using @webgap/logger.
Loads default configuration using @webgap/configuration.
MongoDB Driver mongodb.
JSON File System @webgap/jsonfs.

Requirements

A configuration file must exist (check @webgap/configuration documentation).

API

Installation

npm install @webgap/database-dao --save

Usage

// using jsondbfs driver
var options = {
  databaseDriver: 'jsondbfs', 
  collectionName: 'UserCollection', 
  connectionString: '/path/to/store/json/files'
};
var UserDAO = new DAO(options);
UserDAO.getClient(function (err, client) {
  client.insert({name: "teste", age: 33}, callback);
  client.createIndex({name: "teste"}, callback);
  client.find({name: "teste"}, callback);
  client.findOne({name: "teste"}, callback);
  client.findAndModify({name: "teste"}, {name: "teste", age: 34},callback);
  client.update({name: "teste"}, {name: "teste1", age: 33},callback);
  client.remove({name: "teste1", age: 33}, callback);
});

// or using mongodb driver
var options = {
  databaseDriver: 'mongodb', 
  collectionName: 'UserCollection', 
  connectionString: 'mongodb://user:pass@host:port'
};
var UserDAO = new DAO(options);
UserDAO.getClient(function (err, client) {
  client.insert({name: "teste", age: 33}, callback);
  client.createIndex({name: "teste"}, callback);
  client.find({name: "teste"}, callback);
  client.findOne({name: "teste"}, callback);
  client.findAndModify({name: "teste"}, {name: "teste", age: 34},callback);
  client.update({name: "teste"}, {name: "teste1", age: 33},callback);
  client.remove({name: "teste1", age: 33}, callback);
});

Options

The object 'options' should contain (at least) the following attributes:

var options = {
  collectionName: 'name'  //the name of the collection
};

Other options include:

var options = {
  databaseDriver: 'jsondbfs', //one of mongodb or jsondbfs
  connectionString: '/tmp' // the path to the database. if jsonfs is a filesystem location otherwise if is mongodb should be a mongo db url: mongodb://user:pass@host:port
};

If no 'options' is provided, the module will try to load the settings from @webgap/configuration module.

DB.DRIVER - Defaults to 'jsondbfs'
DB.CONNECTION_STRING - Defaults to '/tmp/'

License

Apache License, Version 2.0

Package Sidebar

Install

npm i @webgap/database-dao

Weekly Downloads

2

Version

0.1.2

License

Apache-2.0

Last publish

Collaborators

  • webgap