mirror of
https://github.com/zhigang1992/deployd.git
synced 2026-05-22 20:37:59 +08:00
added socket session support
This commit is contained in:
104
lib/db.js
104
lib/db.js
@@ -1,7 +1,8 @@
|
||||
var db = module.exports = {}
|
||||
, util = require('util')
|
||||
, EventEmitter = require('events').EventEmitter
|
||||
, mongodb = require('mongodb');
|
||||
, mongodb = require('mongodb')
|
||||
, uuid = require('./util/uuid');
|
||||
|
||||
/**
|
||||
* Create a new database connection with the given options. You can start making
|
||||
@@ -71,7 +72,7 @@ Db.prototype.open = function (fn) {
|
||||
var self = this
|
||||
, mdb = new mongodb.Db(this.options.name, new mongodb.Server(this.options.host, this.options.port));
|
||||
|
||||
self.connecting = true;
|
||||
self.connecting = true;
|
||||
self._mdb = mdb;
|
||||
mdb.open(function (err) {
|
||||
self.connecting = false;
|
||||
@@ -80,6 +81,17 @@ Db.prototype.open = function (fn) {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop the underlying database.
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Db.prototype.drop = function (fn) {
|
||||
this._mdb.dropDatabase(fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new database store (eg. a collection).
|
||||
*
|
||||
@@ -110,6 +122,7 @@ function Store(namespace, db) {
|
||||
this.namespace = namespace;
|
||||
this._db = db;
|
||||
}
|
||||
module.exports.Store = Store;
|
||||
|
||||
function collection(store, fn) {
|
||||
var db = store._db
|
||||
@@ -121,7 +134,7 @@ function collection(store, fn) {
|
||||
mdb.collection(store.namespace, function (err, collection) {
|
||||
if(err || !collection) return fn(err || Error('collection was undefined or an error occured'));
|
||||
|
||||
fn(null, collection)
|
||||
fn(null, collection);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -132,6 +145,49 @@ function collection(store, fn) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Change public IDs to private IDs.
|
||||
*
|
||||
* IDs are generated with a psuedo random number generator.
|
||||
* 24 hexidecimal chars, ~2 trillion combinations.
|
||||
*
|
||||
* @param {Object} object
|
||||
* @return {Object}
|
||||
*/
|
||||
|
||||
Store.prototype.identify = function (object) {
|
||||
if(!object) return;
|
||||
if(typeof object != 'object') throw new Error('identify requires an object');
|
||||
var store = this;
|
||||
function set(object) {
|
||||
if(object._id) {
|
||||
object.id = object._id;
|
||||
delete object._id;
|
||||
} else {
|
||||
var u = object.id || store.createUniqueIdentifier();
|
||||
object._id = u;
|
||||
delete object.id;
|
||||
}
|
||||
}
|
||||
if(Array.isArray(object)) {
|
||||
object.forEach(set);
|
||||
} else {
|
||||
set(object);
|
||||
}
|
||||
return object;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a unique identifier. Override this is derrived stores
|
||||
* to change the way IDs are generated.
|
||||
*
|
||||
* @return {String}
|
||||
*/
|
||||
|
||||
Store.prototype.createUniqueIdentifier = function() {
|
||||
return uuid.create();
|
||||
};
|
||||
|
||||
/**
|
||||
* Insert an object into the store.
|
||||
*
|
||||
@@ -147,10 +203,14 @@ function collection(store, fn) {
|
||||
*/
|
||||
|
||||
Store.prototype.insert = function (object, fn) {
|
||||
var store = this;
|
||||
this.identify(object);
|
||||
collection(this, function (err, col) {
|
||||
col.insert(object, function (err, result) {
|
||||
if(Array.isArray(result) && !Array.isArray(object)) result = result[0];
|
||||
fn(err, result);
|
||||
if(Array.isArray(result) && !Array.isArray(object)) {
|
||||
result = result[0];
|
||||
}
|
||||
fn(err, store.identify(result));
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -170,6 +230,7 @@ Store.prototype.insert = function (object, fn) {
|
||||
*/
|
||||
|
||||
Store.prototype.find = function (query, fn) {
|
||||
var store = this;
|
||||
if(typeof query == 'function') {
|
||||
fn = query;
|
||||
query = {};
|
||||
@@ -177,7 +238,7 @@ Store.prototype.find = function (query, fn) {
|
||||
collection(this, function (err, col) {
|
||||
col.find(query).toArray(function (err, arr) {
|
||||
if(arr.length === 0) arr = undefined;
|
||||
fn(err, arr);
|
||||
fn(err, store.identify(arr));
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -197,8 +258,12 @@ Store.prototype.find = function (query, fn) {
|
||||
*/
|
||||
|
||||
Store.prototype.first = function (query, fn) {
|
||||
this.identify(query);
|
||||
var store = this;
|
||||
collection(this, function (err, col) {
|
||||
col.findOne(query, fn);
|
||||
col.findOne(query, function (err, result) {
|
||||
fn(err, store.identify(result));
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@@ -210,7 +275,7 @@ Store.prototype.first = function (query, fn) {
|
||||
* db
|
||||
* .connect({host: 'localhost', port: 27015, name: 'test'})
|
||||
* .createStore('testing-store')
|
||||
* .update({_id: '<an object id>'}, fn)
|
||||
* .update({id: '<an object id>'}, fn)
|
||||
*
|
||||
* @param {Object} query
|
||||
* @param {Object} object
|
||||
@@ -218,8 +283,16 @@ Store.prototype.first = function (query, fn) {
|
||||
*/
|
||||
|
||||
Store.prototype.update = function (query, object, fn) {
|
||||
var store = this;
|
||||
if(typeof query == 'string') query = {id: query};
|
||||
if(typeof query != 'object') throw new Error('update requires a query object or string id');
|
||||
if(query.id) store.identify(query);
|
||||
|
||||
collection(this, function (err, col) {
|
||||
col.update(query, object, fn);
|
||||
col.update(query, object, function(err) {
|
||||
store.identify(query);
|
||||
fn(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@@ -231,13 +304,22 @@ Store.prototype.update = function (query, object, fn) {
|
||||
* db
|
||||
* .connect({host: 'localhost', port: 27015, name: 'test'})
|
||||
* .createStore('testing-store')
|
||||
* .remove({_id: '<an object id>'}, fn)
|
||||
* .remove({id: '<an object id>'}, fn)
|
||||
*
|
||||
* @param {Object} query
|
||||
* @param {Function} callback(err, obj)
|
||||
*/
|
||||
|
||||
Store.prototype.remove = function (query, fn) {
|
||||
var store = this;
|
||||
if(typeof query === 'string') query = {id: query};
|
||||
if(typeof query == 'function') {
|
||||
fn = query;
|
||||
query = {};
|
||||
}
|
||||
if(query.id) {
|
||||
store.identify(query);
|
||||
}
|
||||
collection(this, function (err, col) {
|
||||
col.remove(query, fn);
|
||||
});
|
||||
@@ -258,7 +340,9 @@ Store.prototype.remove = function (query, fn) {
|
||||
*/
|
||||
|
||||
Store.prototype.rename = function (namespace, fn) {
|
||||
var store = this;
|
||||
collection(this, function (err, col) {
|
||||
store.namespace = namespace;
|
||||
col.rename(namespace, fn);
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user