mirror of
https://github.com/sstent/node.git
synced 2026-01-29 00:22:23 +00:00
big update - restarted app including jquery
This commit is contained in:
35
node_modules/mongoskin/lib/mongoskin/admin.js
generated
vendored
Normal file
35
node_modules/mongoskin/lib/mongoskin/admin.js
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
var Admin = require('mongodb').Admin
|
||||
, utils = require('./utils');
|
||||
|
||||
var SkinAdmin = exports.SkinAdmin = function(skinDb) {
|
||||
this.skinDb = skinDb;
|
||||
}
|
||||
|
||||
SkinAdmin.prototype.open = function(callback) {
|
||||
if(this.admin) return callback(null, this.admin);
|
||||
this.skinDb.open(function(err, db){
|
||||
if(err) return callback(err);
|
||||
this.admin = new Admin(db);
|
||||
callback(null, this.admin);
|
||||
})
|
||||
}
|
||||
|
||||
var bindSkin = function(name, method) {
|
||||
SkinAdmin.prototype[name] = function() {
|
||||
var args = arguments.length > 0 ? Array.prototype.slice.call(arguments, 0) : [];
|
||||
return this.open(function(err, admin) {
|
||||
if (err) {
|
||||
utils.error(err, args, 'SkinAdmin.' + name);
|
||||
} else {
|
||||
method.apply(admin, args);
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
for (var name in Admin.prototype) {
|
||||
var method = Admin.prototype[name];
|
||||
bindSkin(name, method);
|
||||
}
|
||||
|
||||
exports.SkinAdmin = SkinAdmin;
|
||||
227
node_modules/mongoskin/lib/mongoskin/collection.js
generated
vendored
Normal file
227
node_modules/mongoskin/lib/mongoskin/collection.js
generated
vendored
Normal file
@@ -0,0 +1,227 @@
|
||||
/**
|
||||
bind these methods from Collection.prototype to Provider
|
||||
|
||||
methods:
|
||||
insert
|
||||
checkCollectionName
|
||||
remove
|
||||
rename
|
||||
save
|
||||
update
|
||||
distinct
|
||||
count
|
||||
drop
|
||||
findAndModify
|
||||
find
|
||||
normalizeHintField
|
||||
findOne
|
||||
createIndex
|
||||
ensureIndex
|
||||
indexInformation
|
||||
dropIndex
|
||||
dropIndexes
|
||||
mapReduce
|
||||
group
|
||||
options
|
||||
*/
|
||||
var __slice = Array.prototype.slice,
|
||||
events = require('events'),
|
||||
Collection = require('mongodb').Collection,
|
||||
SkinCursor = require('./cursor').SkinCursor,
|
||||
utils = require('./utils'),
|
||||
STATE_CLOSE = 0,
|
||||
STATE_OPENNING = 1,
|
||||
STATE_OPEN = 2;
|
||||
|
||||
/**
|
||||
* Construct SkinCollection from SkinDb and collectionName
|
||||
* use skinDb.collection('name') usually
|
||||
*
|
||||
* @param skinDb
|
||||
* @param collectionName
|
||||
*
|
||||
*/
|
||||
var SkinCollection = exports.SkinCollection = function(skinDb, collectionName) {
|
||||
this.skinDb = skinDb;
|
||||
this.ObjectID = this.skinDb.ObjectID;
|
||||
this.collectionName = collectionName;
|
||||
this.collection;
|
||||
this.state = STATE_CLOSE;
|
||||
this.internalHint;
|
||||
var that = this;
|
||||
this.__defineGetter__('hint', function() { return this.internalHint; });
|
||||
this.__defineSetter__('hint', function(value) {
|
||||
this.internalHint = value;
|
||||
this.open(function(err, collection) {
|
||||
collection.hint = value;
|
||||
that.internalHint = collection.hint;
|
||||
});
|
||||
});
|
||||
|
||||
this.emitter = new events.EventEmitter();
|
||||
};
|
||||
|
||||
/**
|
||||
* bind method of mongodb.Collection to mongoskin.SkinCollection
|
||||
*/
|
||||
var bindSkin = function(name, method) {
|
||||
SkinCollection.prototype[name] = function() {
|
||||
var args = arguments.length > 0 ? __slice.call(arguments, 0) : [];
|
||||
this.open(function(err, collection) {
|
||||
if (err) {
|
||||
utils.error(err, args, 'SkinCollection.' + name);
|
||||
} else {
|
||||
method.apply(collection, args);
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
for (var name in Collection.prototype) {
|
||||
var method = Collection.prototype[name];
|
||||
bindSkin(name, method);
|
||||
}
|
||||
|
||||
/*
|
||||
* find is a special method, because it could return a SkinCursor instance
|
||||
*/
|
||||
SkinCollection.prototype._find = SkinCollection.prototype.find;
|
||||
|
||||
/**
|
||||
* retrieve mongodb.Collection
|
||||
*/
|
||||
SkinCollection.prototype.open = function(fn) {
|
||||
switch (this.state) {
|
||||
case STATE_OPEN:
|
||||
return fn(null, this.collection);
|
||||
case STATE_OPENNING:
|
||||
return this.emitter.once('open', fn);
|
||||
case STATE_CLOSE:
|
||||
default:
|
||||
var that = this;
|
||||
this.emitter.once('open', fn);
|
||||
this.state = STATE_OPENNING;
|
||||
this.skinDb.open(function(err, db) {
|
||||
if (err) {
|
||||
that.state = STATE_CLOSE;
|
||||
return that.emitter.emit('open', err, null);
|
||||
}
|
||||
that.skinDb.db.collection(that.collectionName, function(err, collection) {
|
||||
if (collection) {
|
||||
that.state = STATE_OPEN;
|
||||
that.collection = collection;
|
||||
if (that.hint) {
|
||||
that.collection.hint = that.hit;
|
||||
}
|
||||
}else {
|
||||
that.state = STATE_CLOSE;
|
||||
}
|
||||
that.emitter.emit('open', err, collection);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
SkinCollection.prototype.close = function(){
|
||||
this.state = STATE_CLOSE;
|
||||
};
|
||||
|
||||
SkinCollection.prototype.drop = function(fn) {
|
||||
this.skinDb.dropCollection(this.collectionName, fn);
|
||||
this.close();
|
||||
};
|
||||
|
||||
/**
|
||||
* same args as find, but use Array as callback result but not use Cursor
|
||||
*
|
||||
* findItems(args, function(err, items){});
|
||||
*
|
||||
* same as
|
||||
*
|
||||
* find(args, function(err, cursor){cursor.toArray(err, items){}});
|
||||
*
|
||||
*/
|
||||
SkinCollection.prototype.findItems = function() {
|
||||
var args = __slice.call(arguments),
|
||||
fn = args[args.length - 1];
|
||||
|
||||
args[args.length - 1] = function(err, cursor) {
|
||||
if (err) {
|
||||
fn(err);
|
||||
} else {
|
||||
cursor.toArray(fn);
|
||||
}
|
||||
}
|
||||
|
||||
this._find.apply(this, args);
|
||||
};
|
||||
|
||||
/**
|
||||
* find and cursor.each
|
||||
*/
|
||||
SkinCollection.prototype.findEach = function() {
|
||||
var args = __slice.call(arguments),
|
||||
fn = args[args.length - 1];
|
||||
|
||||
args[args.length - 1] = function(err, cursor) {
|
||||
if (err) {
|
||||
fn(err);
|
||||
} else {
|
||||
cursor.each(fn);
|
||||
}
|
||||
}
|
||||
|
||||
this._find.apply(this, args);
|
||||
};
|
||||
|
||||
/**
|
||||
* @deprecated use SkinDb.id instead
|
||||
*/
|
||||
SkinCollection.prototype.id = function(hex) {
|
||||
return this.skinDb.toId(hex);
|
||||
};
|
||||
|
||||
/**
|
||||
* use hex id as first argument, support ObjectID and String id
|
||||
*
|
||||
* @param {String/ObjectID} id
|
||||
* @param {Function} callback
|
||||
* @return {Object} cursor
|
||||
* @api public
|
||||
*/
|
||||
SkinCollection.prototype.findById = function() {
|
||||
var args = __slice.call(arguments);
|
||||
args[0] = {_id: this.skinDb.toId(args[0])};
|
||||
this.findOne.apply(this, args);
|
||||
};
|
||||
|
||||
/**
|
||||
* use hex id as first argument
|
||||
*/
|
||||
SkinCollection.prototype.updateById = function() {
|
||||
var args = __slice.call(arguments);
|
||||
args[0] = {_id: this.skinDb.toId(args[0])};
|
||||
this.update.apply(this, args);
|
||||
};
|
||||
|
||||
/**
|
||||
* use hex id as first argument
|
||||
*/
|
||||
SkinCollection.prototype.removeById = function() {
|
||||
var args = __slice.call(arguments);
|
||||
args[0] = {_id: this.skinDb.toId(args[0])};
|
||||
this.remove.apply(this, args);
|
||||
};
|
||||
|
||||
/**
|
||||
* if last argument is not a function, then returns a SkinCursor
|
||||
*/
|
||||
SkinCollection.prototype.find = function() {
|
||||
var args = arguments.length > 0 ? __slice.call(arguments, 0) : [];
|
||||
if (args.length > 0 && typeof(args[args.length - 1]) === 'function') {
|
||||
this._find.apply(this, args);
|
||||
}else {
|
||||
return new SkinCursor(null, this, args);
|
||||
}
|
||||
};
|
||||
|
||||
77
node_modules/mongoskin/lib/mongoskin/cursor.js
generated
vendored
Normal file
77
node_modules/mongoskin/lib/mongoskin/cursor.js
generated
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
var __slice = Array.prototype.slice,
|
||||
events = require('events'),
|
||||
Cursor = require('mongodb').Cursor,
|
||||
utils = require('./utils'),
|
||||
STATE_CLOSE = 0,
|
||||
STATE_OPENNING = 1,
|
||||
STATE_OPEN = 2;
|
||||
|
||||
var SkinCursor = exports.SkinCursor = function(cursor, skinCollection, args ) {
|
||||
this.cursor = cursor;
|
||||
this.skinCollection = skinCollection;
|
||||
this.args = args;
|
||||
this.emitter = new events.EventEmitter();
|
||||
if (!cursor) {
|
||||
this.state = STATE_CLOSE;
|
||||
}else {
|
||||
this.state = STATE_OPEN;
|
||||
}
|
||||
}
|
||||
|
||||
SkinCursor.prototype.open = function(fn) {
|
||||
switch (this.state) {
|
||||
case STATE_OPEN:
|
||||
return fn(null, this.cursor);
|
||||
case STATE_OPENNING:
|
||||
return this.emitter.once('open', fn);
|
||||
case STATE_CLOSE:
|
||||
default:
|
||||
var that = this;
|
||||
this.emitter.once('open', fn);
|
||||
this.state = STATE_OPENNING;
|
||||
this.skinCollection.open(function(err, collection) {
|
||||
if (err) {
|
||||
that.state = STATE_CLOSE;
|
||||
that.emitter.emit('open', err);
|
||||
return
|
||||
}
|
||||
// copy args
|
||||
var args = that.args.slice();
|
||||
args.push(function(err, cursor) {
|
||||
if (cursor) {
|
||||
that.state = STATE_OPEN;
|
||||
that.cursor = cursor;
|
||||
}
|
||||
that.emitter.emit('open', err, cursor);
|
||||
});
|
||||
|
||||
collection.find.apply(collection, args);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
var bindSkin = function(name, method) {
|
||||
SkinCursor.prototype[name] = function() {
|
||||
var args = arguments.length > 0 ? __slice.call(arguments, 0) : [];
|
||||
this.open(function(err, cursor) {
|
||||
if (err) {
|
||||
utils.error(err, args, 'SkinCursor.' + name);
|
||||
} else {
|
||||
method.apply(cursor, args);
|
||||
}
|
||||
});
|
||||
return this;
|
||||
};
|
||||
};
|
||||
|
||||
[
|
||||
// callbacks
|
||||
'toArray','each','count','nextObject','getMore', 'explain',
|
||||
// self return
|
||||
'sort','limit','skip','batchSize',
|
||||
// unsupported
|
||||
//'rewind', 'close' ,...
|
||||
].forEach(function(name) {
|
||||
var method = Cursor.prototype[name];
|
||||
bindSkin(name, method);
|
||||
});
|
||||
205
node_modules/mongoskin/lib/mongoskin/db.js
generated
vendored
Normal file
205
node_modules/mongoskin/lib/mongoskin/db.js
generated
vendored
Normal file
@@ -0,0 +1,205 @@
|
||||
var __slice = Array.prototype.slice,
|
||||
mongodb = require('mongodb'),
|
||||
events = require('events'),
|
||||
utils = require('./utils'),
|
||||
SkinAdmin = require('./admin').SkinAdmin,
|
||||
SkinCollection = require('./collection').SkinCollection,
|
||||
SkinGridStore = require('./gridfs').SkinGridStore,
|
||||
Db = mongodb.Db,
|
||||
|
||||
STATE_CLOSE = 0,
|
||||
STATE_OPENNING = 1,
|
||||
STATE_OPEN = 2;
|
||||
|
||||
var _extend = function(destination, source) {
|
||||
for (var property in source) {
|
||||
destination[property] = source[property];
|
||||
}
|
||||
return destination;
|
||||
};
|
||||
|
||||
var SkinDb = exports.SkinDb = function(db, username, password) {
|
||||
this.db = db;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.state = STATE_CLOSE;
|
||||
this.emitter = new events.EventEmitter();
|
||||
this.admin = new SkinAdmin(this);
|
||||
this._collections = {};
|
||||
this.bson_serializer = db.bson_serializer;
|
||||
this.ObjectID = mongodb.ObjectID /* 0.9.7-3-2 */ || db.bson_serializer.ObjectID /* <= 0.9.7 */;
|
||||
};
|
||||
|
||||
SkinDb.prototype.toObjectID = SkinDb.prototype.toId = function(hex) {
|
||||
if(hex instanceof this.ObjectID) {
|
||||
return hex;
|
||||
}
|
||||
return this.ObjectID.createFromHexString(hex);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* retrieve native_db
|
||||
*
|
||||
* @param fn function(err, native_db)
|
||||
*
|
||||
*/
|
||||
SkinDb.prototype.open = function(fn) {
|
||||
switch (this.state) {
|
||||
|
||||
case STATE_OPEN:
|
||||
return fn(null, this.db);
|
||||
|
||||
case STATE_OPENNING:
|
||||
// if call 'open' method multi times before opened
|
||||
return this.emitter.once('open', fn);
|
||||
|
||||
case STATE_CLOSE:
|
||||
default:
|
||||
var that = this;
|
||||
var onDbOpen = function(err, db) {
|
||||
if (!err && db) {
|
||||
that.state = STATE_OPEN;
|
||||
that.db = db;
|
||||
}else {
|
||||
db = null;
|
||||
that.state = STATE_CLOSE;
|
||||
}
|
||||
that.emitter.emit('open', err, db);
|
||||
};
|
||||
|
||||
this.emitter.once('open', fn);
|
||||
this.state = STATE_OPENNING;
|
||||
|
||||
this.db.open(function(err, db) {
|
||||
if (db && that.username) {
|
||||
//do authenticate
|
||||
db.authenticate(that.username, that.password, function(err) {
|
||||
onDbOpen(err, db);
|
||||
});
|
||||
} else {
|
||||
onDbOpen(err, db);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Close skinDb
|
||||
*/
|
||||
SkinDb.prototype.close = function(callback) {
|
||||
if (this.state === STATE_CLOSE) {
|
||||
return callback && callback();
|
||||
}else if (this.state === STATE_OPEN) {
|
||||
this.state = STATE_CLOSE;
|
||||
this.db.close(callback);
|
||||
}else if (this.state === STATE_OPENNING) {
|
||||
var that = this;
|
||||
this.emitter.once('open', function(err, db) {
|
||||
that.state = STATE_CLOSE;
|
||||
db.close(callback);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* create or retrieval skin collection
|
||||
*/
|
||||
SkinDb.prototype.collection = function(name) {
|
||||
var collection = this._collections[name];
|
||||
if (!collection) {
|
||||
this._collections[name] = collection = new SkinCollection(this, name);
|
||||
}
|
||||
return collection;
|
||||
};
|
||||
|
||||
/**
|
||||
* gridfs
|
||||
*/
|
||||
SkinDb.prototype.gridfs = function() {
|
||||
return this.skinGridStore || (this.skinGridStore = new SkinGridStore(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* bind additional method to SkinCollection
|
||||
*
|
||||
* 1. collectionName
|
||||
* 2. collectionName, extends1, extends2,... extendsn
|
||||
* 3. collectionName, SkinCollection
|
||||
*/
|
||||
SkinDb.prototype.bind = function() {
|
||||
var args = __slice.call(arguments),
|
||||
name = args[0];
|
||||
|
||||
if (typeof name !== 'string' || name.length === 0) {
|
||||
throw new Error('Must provide name parameter for bind.');
|
||||
}
|
||||
if (args.length === 1) {
|
||||
return this.bind(name, this.collection(name));
|
||||
}
|
||||
if (args.length === 2 && args[1].constructor === SkinCollection) {
|
||||
this._collections[name] = args[1];
|
||||
Object.defineProperty(this, name, {
|
||||
value: args[1],
|
||||
writable: false,
|
||||
enumerable: true
|
||||
});
|
||||
// support bind for system.js
|
||||
var names = name.split('.');
|
||||
if(names.length > 1){
|
||||
var prev = this, next;
|
||||
for(var i =0; i<names.length - 1; i++){
|
||||
next = prev[names[i]];
|
||||
if(!next){
|
||||
next = {};
|
||||
Object.defineProperty(prev, names[i], {
|
||||
value : next
|
||||
, writable : false
|
||||
, enumerable : true
|
||||
});
|
||||
}
|
||||
prev = next;
|
||||
}
|
||||
Object.defineProperty(prev, names[names.length - 1], {
|
||||
value : args[1]
|
||||
, writable : false
|
||||
, enumerable : true
|
||||
});
|
||||
}
|
||||
return args[1];
|
||||
}
|
||||
|
||||
for (var i = 1, len = args.length; i < len; i++) {
|
||||
if (typeof args[i] != 'object')
|
||||
throw new Error('the arg' + i + ' should be object, but is ' + args[i]);
|
||||
}
|
||||
|
||||
var coll = this.collection(name);
|
||||
for (var i = 0, len = args.length; i < len; i++) {
|
||||
_extend(coll, args[i]);
|
||||
}
|
||||
return this.bind(name, coll);
|
||||
};
|
||||
|
||||
var bindSkin = function(name, method) {
|
||||
return SkinDb.prototype[name] = function() {
|
||||
var args = arguments.length > 0 ? __slice.call(arguments, 0) : [];
|
||||
return this.open(function(err, db) {
|
||||
if (err) {
|
||||
utils.error(err, args, 'SkinDb.' + name);
|
||||
} else {
|
||||
return method.apply(db, args);
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
//bind method of mongodb.Db to SkinDb
|
||||
for (var name in Db.prototype) {
|
||||
if(!name || name[0] == '_' || name == 'state') continue;
|
||||
var method = Db.prototype[name];
|
||||
if (name !== 'bind' && name !== 'open' && name !== 'collection' && name !== 'admin') {
|
||||
bindSkin(name, method);
|
||||
}
|
||||
}
|
||||
|
||||
41
node_modules/mongoskin/lib/mongoskin/gridfs.js
generated
vendored
Normal file
41
node_modules/mongoskin/lib/mongoskin/gridfs.js
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
var GridStore = require('mongodb').GridStore;
|
||||
|
||||
/**
|
||||
* @param filename: filename or ObjectId
|
||||
*/
|
||||
var SkinGridStore = exports.SkinGridStore = function(skinDb) {
|
||||
this.skinDb = skinDb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @param filename
|
||||
* @param mode
|
||||
* @param options
|
||||
* @param callback
|
||||
* callback(err, gridStoreObject)
|
||||
*/
|
||||
SkinGridStore.prototype.open = function(id, filename, mode, options, callback){
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var callback = args.pop();
|
||||
this.skinDb.open(function(err, db) {
|
||||
new GridStore(db, args[0], args[1], args[2], args[3]).open(callback);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param filename: filename or ObjectId
|
||||
*/
|
||||
SkinGridStore.prototype.unlink = SkinGridStore.prototype.remove = function(filename, callback){
|
||||
this.skinDb.open(function(err, db) {
|
||||
GridStore.unlink(db, filename, callback);
|
||||
});
|
||||
}
|
||||
|
||||
SkinGridStore.prototype.exist = function(filename, rootCollection, callback){
|
||||
this.skinDb.open(function(err, db) {
|
||||
GridStore.exist(db, filename, rootCollection, callback);
|
||||
});
|
||||
}
|
||||
|
||||
exports.SkinGridStore = SkinGridStore;
|
||||
150
node_modules/mongoskin/lib/mongoskin/index.js
generated
vendored
Normal file
150
node_modules/mongoskin/lib/mongoskin/index.js
generated
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
var url = require('url'),
|
||||
Router = require('./router').Router,
|
||||
mongo = require('mongodb'),
|
||||
SkinServer = require('./server').SkinServer,
|
||||
SkinDb =require('./db').SkinDb,
|
||||
Db = mongo.Db,
|
||||
Server = mongo.Server,
|
||||
ReplSetServers = mongo.ReplSetServers,
|
||||
BSONNative = mongo.BSONNative,
|
||||
DEFAULT_PORT = 27017;
|
||||
|
||||
function toBool(value) {
|
||||
return value !== undefined && value != 'false' && value != 'no' && value != 'off';
|
||||
}
|
||||
|
||||
/**
|
||||
* parse the database url to config
|
||||
*
|
||||
* [*://]username:password@host[:port]/database?options
|
||||
*
|
||||
*/
|
||||
var parseUrl = function(serverUrl) {
|
||||
var serverUrl = /\w+:\/\//.test(serverUrl) ? serverUrl : 'db://' + serverUrl,
|
||||
uri = url.parse(serverUrl, true),
|
||||
config = {},
|
||||
serverOptions = uri.query,
|
||||
reconnect = serverOptions['auto_reconnect'];
|
||||
|
||||
config.host = uri.hostname;
|
||||
config.port = Number(uri.port) || DEFAULT_PORT;
|
||||
if(uri.pathname) {
|
||||
config.database = uri.pathname.replace(/\//g, '');
|
||||
}
|
||||
config.options = {};
|
||||
config.options['auto_reconnect'] = toBool(serverOptions['auto_reconnect']);
|
||||
config.options['poolSize'] = parseInt(serverOptions['poolSize'] || 1);
|
||||
if (uri && uri.auth) {
|
||||
var auth = uri.auth,
|
||||
separator = auth.indexOf(':');
|
||||
config.username = auth.substr(0, separator);
|
||||
config.password = auth.substr(separator + 1);
|
||||
}
|
||||
return config;
|
||||
};
|
||||
|
||||
/**
|
||||
* constructor Server from url
|
||||
*
|
||||
*/
|
||||
var parseServer = function(serverUrl) {
|
||||
var config = parseUrl(serverUrl);
|
||||
return new Server(config.host, config.port, config.options);
|
||||
};
|
||||
|
||||
/*
|
||||
* exports mongo classes ObjectID Long Code DbRef ... to mongoskin
|
||||
*/
|
||||
for(var key in mongo) {
|
||||
exports[key] = mongo[key];
|
||||
}
|
||||
|
||||
/**
|
||||
* constructor SkinDb from serverUrls
|
||||
*
|
||||
* repliSet: mongoskin.db(serverUrls, rs_options, db_options)
|
||||
*
|
||||
* mongoskin.db(['192.168.0.1:27017/', '192.168.0.2/?auto_reconnect', '192.168.0.3/?auto_reconnect'], {
|
||||
* database: 'mydb'
|
||||
* })
|
||||
*
|
||||
* single Server: mongoskin.db(dbUrl, db_options)
|
||||
*
|
||||
* mongoskin.db('192.168.0.1:27017/mydb')
|
||||
*
|
||||
*/
|
||||
exports.db = function(serverUrl, options) {
|
||||
if(!options) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
var server, database;
|
||||
|
||||
if(Array.isArray(serverUrl)) {
|
||||
if(!options.database) {
|
||||
throw new Error('Please provide a database in options to connect.');
|
||||
}
|
||||
database = options.database;
|
||||
|
||||
var len = serverUrl.length;
|
||||
var servers = [];
|
||||
for(var i = 0; i < len; i++) {
|
||||
var config = parseUrl(serverUrl[i]);
|
||||
if(config.database || config.username) {
|
||||
console.log('MONGOSKIN:WARN: database or username found in RepliSet server URL, ' + serverUrl[i]);
|
||||
}
|
||||
servers.push( new Server(config.host, config.port, config.options) );
|
||||
}
|
||||
server = new ReplSetServers(servers);
|
||||
} else {
|
||||
var config = parseUrl(serverUrl);
|
||||
if (!config.database) {
|
||||
throw new Error('Please provide a database to connect to.');
|
||||
}
|
||||
database = config.database;
|
||||
|
||||
server = new Server(config.host, config.port, config.options);
|
||||
|
||||
if(options.username === undefined) {
|
||||
options.username = config.username;
|
||||
options.password = config.password;
|
||||
}
|
||||
}
|
||||
|
||||
var skinServer = new SkinServer(server);
|
||||
return skinServer.db(database, options);
|
||||
};
|
||||
|
||||
/**
|
||||
* select different db by collection name
|
||||
*
|
||||
* @param select function(name) returns SkinDb
|
||||
*
|
||||
* var router = mongoskin.router(function(name){
|
||||
* select(name){
|
||||
* case 'user':
|
||||
* case 'group':
|
||||
* return authDb;
|
||||
* default:
|
||||
* return appDb;
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* router.collection('user')
|
||||
*
|
||||
*/
|
||||
exports.router = function(select) {
|
||||
return new Router(select);
|
||||
};
|
||||
|
||||
/*
|
||||
* export Skin classes from ./db ./collection ./cursor ./admin
|
||||
*/
|
||||
['server', 'db', 'collection', 'cursor', 'admin'].forEach(function(path) {
|
||||
var foo, module, name;
|
||||
module = require('./' + path);
|
||||
for (name in module) {
|
||||
foo = module[name];
|
||||
exports[name] = foo;
|
||||
}
|
||||
});
|
||||
24
node_modules/mongoskin/lib/mongoskin/router.js
generated
vendored
Normal file
24
node_modules/mongoskin/lib/mongoskin/router.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
var Router = exports.Router = function(select) {
|
||||
this._select = select;
|
||||
this._collections = {};
|
||||
}
|
||||
|
||||
Router.prototype.bind = function() {
|
||||
var args = Array.prototype.slice.call(arguments),
|
||||
name = args[0];
|
||||
|
||||
var database = this._select(name);
|
||||
var coll = database.bind.apply(database, args);
|
||||
|
||||
this._collections[name] = coll;
|
||||
Object.defineProperty(this, name, {
|
||||
value: coll,
|
||||
writable: false,
|
||||
enumerable: true
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
Router.prototype.collection = function(name) {
|
||||
return this._collections[name] || (this._collections[name] = this._select(name).collection(name));
|
||||
};
|
||||
42
node_modules/mongoskin/lib/mongoskin/server.js
generated
vendored
Normal file
42
node_modules/mongoskin/lib/mongoskin/server.js
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
var __slice = Array.prototype.slice,
|
||||
mongodb = require('mongodb'),
|
||||
Db = mongodb.Db,
|
||||
Server = mongodb.Server,
|
||||
SkinDb = require('./db').SkinDb;
|
||||
|
||||
/**
|
||||
* Construct SkinServer with native Server
|
||||
*
|
||||
* @param server
|
||||
*/
|
||||
var SkinServer = exports.SkinServer = function(server) {
|
||||
this.server = server;
|
||||
this._cache_ = [];
|
||||
};
|
||||
|
||||
/**
|
||||
* Create SkinDb from a SkinServer
|
||||
*
|
||||
* @param name database name
|
||||
*
|
||||
* @return SkinDb
|
||||
*
|
||||
* TODO add options
|
||||
*/
|
||||
SkinServer.prototype.db = function(name, options) {
|
||||
var key = (username || '') + '@' + name;
|
||||
var skinDb = this._cache_[key];
|
||||
if (!skinDb || skinDb.fail) {
|
||||
var username = options.username,
|
||||
password = options.password;
|
||||
delete options.username;
|
||||
delete options.password;
|
||||
if(options.native_parser === undefined) {
|
||||
options.native_parser = !! mongodb.BSONNative;
|
||||
}
|
||||
var db = new Db(name, this.server, options);
|
||||
skinDb = new SkinDb(db, username, password);
|
||||
this._cache_[key] = skinDb;
|
||||
}
|
||||
return skinDb;
|
||||
};
|
||||
8
node_modules/mongoskin/lib/mongoskin/utils.js
generated
vendored
Normal file
8
node_modules/mongoskin/lib/mongoskin/utils.js
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
exports.error = function(err, args, name) {
|
||||
var cb = args.pop();
|
||||
if(cb && typeof cb === 'function') {
|
||||
cb(err)
|
||||
} else {
|
||||
console.error("Error occured with no callback to handle it while calling " + name, err);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user