mirror of
https://github.com/sstent/alex_app1.git
synced 2026-03-17 18:35:30 +00:00
lots of work so far-- mostly tidying
This commit is contained in:
67
node_modules/mongoskin/lib/mongoskin/admin.js
generated
vendored
Normal file
67
node_modules/mongoskin/lib/mongoskin/admin.js
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
/*!
|
||||
* mongoskin - admin.js
|
||||
*
|
||||
* Copyright(c) 2011 - 2012 kissjs.org
|
||||
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var Admin = require('mongodb').Admin;
|
||||
var utils = require('./utils');
|
||||
var constant = require('./constant');
|
||||
|
||||
/**
|
||||
* SkinAdmin
|
||||
*
|
||||
* @param {SkinDb} skinDb
|
||||
* @constructor
|
||||
* @api public
|
||||
*/
|
||||
var SkinAdmin = exports.SkinAdmin = function (skinDb) {
|
||||
utils.SkinObject.call(this);
|
||||
this.skinDb = skinDb;
|
||||
this.admin = null;
|
||||
};
|
||||
|
||||
utils.inherits(SkinAdmin, utils.SkinObject);
|
||||
|
||||
/**
|
||||
* Retrieve mongodb.Admin instance.
|
||||
*
|
||||
* @param {Function(err, admin)} callback
|
||||
* @return {SkinAdmin} this
|
||||
* @api public
|
||||
*/
|
||||
SkinAdmin.prototype.open = function (callback) {
|
||||
if (this.state === constant.STATE_OPEN) {
|
||||
callback(null, this.admin);
|
||||
return this;
|
||||
}
|
||||
this.emitter.once('open', callback);
|
||||
if (this.state === constant.STATE_OPENNING) {
|
||||
return this;
|
||||
}
|
||||
this.state = constant.STATE_OPENNING;
|
||||
this.skinDb.open(function (err, db) {
|
||||
if (err) {
|
||||
this.admin = null;
|
||||
this.state = constant.STATE_CLOSE;
|
||||
} else {
|
||||
this.admin = new Admin(db);
|
||||
this.state = constant.STATE_OPEN;
|
||||
}
|
||||
this.emitter.emit('open', err, this.admin);
|
||||
}.bind(this));
|
||||
return this;
|
||||
};
|
||||
|
||||
for (var key in Admin.prototype) {
|
||||
var method = Admin.prototype[key];
|
||||
utils.bindSkin('SkinAdmin', SkinAdmin, 'admin', key, method);
|
||||
}
|
||||
300
node_modules/mongoskin/lib/mongoskin/collection.js
generated
vendored
Normal file
300
node_modules/mongoskin/lib/mongoskin/collection.js
generated
vendored
Normal file
@@ -0,0 +1,300 @@
|
||||
/*!
|
||||
* mongoskin - collection.js
|
||||
*
|
||||
* Copyright(c) 2011 - 2012 kissjs.org
|
||||
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
/**
|
||||
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;
|
||||
var events = require('events');
|
||||
var Collection = require('mongodb').Collection;
|
||||
var SkinCursor = require('./cursor').SkinCursor;
|
||||
var utils = require('./utils');
|
||||
var constant = require('./constant');
|
||||
var STATE_CLOSE = constant.STATE_CLOSE;
|
||||
var STATE_OPENNING = constant.STATE_OPENNING;
|
||||
var STATE_OPEN = constant.STATE_OPEN;
|
||||
|
||||
/**
|
||||
* Construct SkinCollection from SkinDb and collectionName
|
||||
* use skinDb.collection('name') usually
|
||||
*
|
||||
* @param {SkinDb} skinDb
|
||||
* @param {String} collectionName
|
||||
* @param {Object} [options] collection options
|
||||
* @constructor
|
||||
* @api public
|
||||
*/
|
||||
var SkinCollection = exports.SkinCollection = function (skinDb, collectionName, options) {
|
||||
utils.SkinObject.call(this);
|
||||
this.emitter.setMaxListeners(50);
|
||||
|
||||
this.options = options;
|
||||
this.skinDb = skinDb;
|
||||
this.ObjectID = this.skinDb.ObjectID;
|
||||
this.collectionName = collectionName;
|
||||
this.collection = null;
|
||||
this.internalHint = null;
|
||||
this.__defineGetter__('hint', function () {
|
||||
return this.internalHint;
|
||||
});
|
||||
this.__defineSetter__('hint', function (value) {
|
||||
this.internalHint = value;
|
||||
this.open(function (err, collection) {
|
||||
collection.hint = value;
|
||||
this.internalHint = collection.hint;
|
||||
}.bind(this));
|
||||
});
|
||||
};
|
||||
|
||||
utils.inherits(SkinCollection, utils.SkinObject);
|
||||
|
||||
for (var _name in Collection.prototype) {
|
||||
var method = Collection.prototype[_name];
|
||||
utils.bindSkin('SkinCollection', SkinCollection, 'collection', _name, method);
|
||||
}
|
||||
|
||||
/*
|
||||
* find is a special method, because it could return a SkinCursor instance
|
||||
*/
|
||||
SkinCollection.prototype._find = SkinCollection.prototype.find;
|
||||
|
||||
/**
|
||||
* Retrieve mongodb.Collection
|
||||
*
|
||||
* @param {Function(err, collection)} callback
|
||||
* @return {SkinCollection} this
|
||||
* @api public
|
||||
*/
|
||||
SkinCollection.prototype.open = function (callback) {
|
||||
switch (this.state) {
|
||||
case STATE_OPEN:
|
||||
callback(null, this.collection);
|
||||
break;
|
||||
case STATE_OPENNING:
|
||||
this.emitter.once('open', callback);
|
||||
break;
|
||||
// case STATE_CLOSE:
|
||||
default:
|
||||
this.emitter.once('open', callback);
|
||||
this.state = STATE_OPENNING;
|
||||
this.skinDb.open(function (err, db) {
|
||||
if (err) {
|
||||
this.state = STATE_CLOSE;
|
||||
return this.emitter.emit('open', err, null);
|
||||
}
|
||||
db.collection(this.collectionName, this.options, function (err, collection) {
|
||||
if (err) {
|
||||
this.state = STATE_CLOSE;
|
||||
} else {
|
||||
this.state = STATE_OPEN;
|
||||
this.collection = collection;
|
||||
if (this.hint) {
|
||||
this.collection.hint = this.hit;
|
||||
}
|
||||
}
|
||||
this.emitter.emit('open', err, collection);
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
break;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Close current collection.
|
||||
*
|
||||
* @param {Function(err)} callback
|
||||
* @return {SkinCollection} this
|
||||
* @api public
|
||||
*/
|
||||
SkinCollection.prototype.close = function (callback) {
|
||||
this.collection = null;
|
||||
this.state = STATE_CLOSE;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Drop current collection.
|
||||
*
|
||||
* @param {Function(err)} callback
|
||||
* @return {SkinCollection} this
|
||||
* @api public
|
||||
*/
|
||||
SkinCollection.prototype.drop = function (callback) {
|
||||
this.skinDb.dropCollection(this.collectionName, callback);
|
||||
this.close();
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* same args as find, but use Array as callback result but not use Cursor
|
||||
*
|
||||
* findItems(args, function (err, items) {});
|
||||
*
|
||||
* same as
|
||||
*
|
||||
* find(args).toArray(function (err, items) {});
|
||||
*
|
||||
* or using `mongodb.collection.find()`
|
||||
*
|
||||
* find(args, function (err, cursor) {
|
||||
* cursor.toArray(function (err, items) {
|
||||
* });
|
||||
* });
|
||||
*
|
||||
* @param {Object} [query]
|
||||
* @param {Object} [options]
|
||||
* @param {Function(err, docs)} callback
|
||||
* @return {SkinCollection} this
|
||||
* @api public
|
||||
*/
|
||||
SkinCollection.prototype.findItems = function (query, options, callback) {
|
||||
var args = __slice.call(arguments);
|
||||
var fn = args[args.length - 1];
|
||||
args[args.length - 1] = function (err, cursor) {
|
||||
if (err) {
|
||||
return fn(err);
|
||||
}
|
||||
cursor.toArray(fn);
|
||||
};
|
||||
this.find.apply(this, args);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* find and cursor.each(fn).
|
||||
*
|
||||
* @param {Object} [query]
|
||||
* @param {Object} [options]
|
||||
* @param {Function(err, item)} eachCallback
|
||||
* @return {SkinCollection} this
|
||||
* @api public
|
||||
*/
|
||||
SkinCollection.prototype.findEach = function (query, options, eachCallback) {
|
||||
var args = __slice.call(arguments);
|
||||
var fn = args[args.length - 1];
|
||||
args[args.length - 1] = function (err, cursor) {
|
||||
if (err) {
|
||||
return fn(err);
|
||||
}
|
||||
cursor.each(fn);
|
||||
};
|
||||
this.find.apply(this, args);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* @deprecated use `SkinDb.toId` instead
|
||||
*
|
||||
* @param {String} hex
|
||||
* @return {ObjectID|String}
|
||||
* @api public
|
||||
*/
|
||||
SkinCollection.prototype.id = function (hex) {
|
||||
return this.skinDb.toId(hex);
|
||||
};
|
||||
|
||||
/**
|
||||
* Operate by object.`_id`
|
||||
*
|
||||
* @param {String} methodName
|
||||
* @param {String|ObjectID|Number} id
|
||||
* @param {Arguments|Array} args
|
||||
* @return {SkinCollection} this
|
||||
* @api private
|
||||
*/
|
||||
SkinCollection.prototype._operateById = function (methodName, id, args) {
|
||||
args = __slice.call(args);
|
||||
args[0] = {_id: this.skinDb.toId(id)};
|
||||
this[methodName].apply(this, args);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Find one object by _id.
|
||||
*
|
||||
* @param {String|ObjectID|Number} id, doc primary key `_id`
|
||||
* @param {Function(err, doc)} callback
|
||||
* @return {SkinCollection} this
|
||||
* @api public
|
||||
*/
|
||||
SkinCollection.prototype.findById = function (id, callback) {
|
||||
return this._operateById('findOne', id, arguments);
|
||||
};
|
||||
|
||||
/**
|
||||
* Update doc by _id.
|
||||
* @param {String|ObjectID|Number} id, doc primary key `_id`
|
||||
* @param {Object} doc
|
||||
* @param {Function(err)} callback
|
||||
* @return {SkinCollection} this
|
||||
* @api public
|
||||
*/
|
||||
SkinCollection.prototype.updateById = function (id, doc, callback) {
|
||||
return this._operateById('update', id, arguments);
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove doc by _id.
|
||||
* @param {String|ObjectID|Number} id, doc primary key `_id`
|
||||
* @param {Function(err)} callback
|
||||
* @return {SkinCollection} this
|
||||
* @api public
|
||||
*/
|
||||
SkinCollection.prototype.removeById = function (id, callback) {
|
||||
return this._operateById('remove', id, arguments);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a cursor for a query that can be used to iterate over results from MongoDB.
|
||||
*
|
||||
* @param {Object} query
|
||||
* @param {Object} options
|
||||
* @param {Function(err, docs)} callback
|
||||
* @return {SkinCursor|SkinCollection} if last argument is not a function, then returns a SkinCursor,
|
||||
* otherise return this
|
||||
* @api public
|
||||
*/
|
||||
SkinCollection.prototype.find = function (query, options, callback) {
|
||||
var args = __slice.call(arguments);
|
||||
if (args.length > 0 && typeof args[args.length - 1] === 'function') {
|
||||
this._find.apply(this, args);
|
||||
return this;
|
||||
} else {
|
||||
return new SkinCursor(null, this, args);
|
||||
}
|
||||
};
|
||||
15
node_modules/mongoskin/lib/mongoskin/constant.js
generated
vendored
Normal file
15
node_modules/mongoskin/lib/mongoskin/constant.js
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
/*!
|
||||
* mongoskin - constant.js
|
||||
*
|
||||
* Copyright(c) 2011 - 2012 kissjs.org
|
||||
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
exports.DEFAULT_PORT = 27017;
|
||||
|
||||
exports.STATE_CLOSE = 0;
|
||||
exports.STATE_OPENNING = 1;
|
||||
exports.STATE_OPEN = 2;
|
||||
87
node_modules/mongoskin/lib/mongoskin/cursor.js
generated
vendored
Normal file
87
node_modules/mongoskin/lib/mongoskin/cursor.js
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
/*!
|
||||
* mongoskin - cursor.js
|
||||
*
|
||||
* Copyright(c) 2011 - 2012 kissjs.org
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var Cursor = require('mongodb').Cursor;
|
||||
var utils = require('./utils');
|
||||
var constant = require('./constant');
|
||||
var STATE_CLOSE = constant.STATE_CLOSE;
|
||||
var STATE_OPENNING = constant.STATE_OPENNING;
|
||||
var STATE_OPEN = constant.STATE_OPEN;
|
||||
|
||||
var SkinCursor = exports.SkinCursor = function (cursor, skinCollection, args) {
|
||||
utils.SkinObject.call(this);
|
||||
|
||||
this.cursor = cursor;
|
||||
this.skinCollection = skinCollection;
|
||||
this.args = args || [];
|
||||
this.emitter = new EventEmitter();
|
||||
if (cursor) {
|
||||
this.state = STATE_OPEN;
|
||||
}
|
||||
};
|
||||
|
||||
utils.inherits(SkinCursor, utils.SkinObject);
|
||||
|
||||
/**
|
||||
* Retrieve mongodb.Cursor instance.
|
||||
*
|
||||
* @param {Function(err, cursor)} callback
|
||||
* @return {SkinCursor} this
|
||||
* @api public
|
||||
*/
|
||||
SkinCursor.prototype.open = function (callback) {
|
||||
switch (this.state) {
|
||||
case STATE_OPEN:
|
||||
callback(null, this.cursor);
|
||||
break;
|
||||
case STATE_OPENNING:
|
||||
this.emitter.once('open', callback);
|
||||
break;
|
||||
// case STATE_CLOSE:
|
||||
default:
|
||||
this.emitter.once('open', callback);
|
||||
this.state = STATE_OPENNING;
|
||||
this.skinCollection.open(function (err, collection) {
|
||||
if (err) {
|
||||
this.state = STATE_CLOSE;
|
||||
this.emitter.emit('open', err);
|
||||
return;
|
||||
}
|
||||
// copy args
|
||||
var args = this.args.slice();
|
||||
args.push(function (err, cursor) {
|
||||
if (cursor) {
|
||||
this.state = STATE_OPEN;
|
||||
this.cursor = cursor;
|
||||
}
|
||||
this.emitter.emit('open', err, cursor);
|
||||
}.bind(this));
|
||||
collection.find.apply(collection, args);
|
||||
}.bind(this));
|
||||
break;
|
||||
}
|
||||
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];
|
||||
utils.bindSkin('SkinCursor', SkinCursor, 'cursor', name, method);
|
||||
});
|
||||
254
node_modules/mongoskin/lib/mongoskin/db.js
generated
vendored
Normal file
254
node_modules/mongoskin/lib/mongoskin/db.js
generated
vendored
Normal file
@@ -0,0 +1,254 @@
|
||||
/*!
|
||||
* mongoskin - db.js
|
||||
*
|
||||
* Copyright(c) 2011 - 2012 kissjs.org
|
||||
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var __slice = Array.prototype.slice;
|
||||
var mongodb = require('mongodb');
|
||||
var utils = require('./utils');
|
||||
var SkinAdmin = require('./admin').SkinAdmin;
|
||||
var SkinCollection = require('./collection').SkinCollection;
|
||||
var SkinGridStore = require('./gridfs').SkinGridStore;
|
||||
var Db = mongodb.Db;
|
||||
var constant = require('./constant');
|
||||
var STATE_CLOSE = constant.STATE_CLOSE;
|
||||
var STATE_OPENNING = constant.STATE_OPENNING;
|
||||
var STATE_OPEN = constant.STATE_OPEN;
|
||||
|
||||
/**
|
||||
* SkinDb
|
||||
*
|
||||
* @param {Db} dbconn, mongodb.Db instance
|
||||
* @param {String} [username]
|
||||
* @param {String} [password]
|
||||
* @constructor
|
||||
* @api public
|
||||
*/
|
||||
var SkinDb = exports.SkinDb = function (dbconn, username, password) {
|
||||
utils.SkinObject.call(this);
|
||||
this.emitter.setMaxListeners(100);
|
||||
|
||||
this._dbconn = dbconn;
|
||||
this.db = null;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.admin = new SkinAdmin(this);
|
||||
this._collections = {};
|
||||
this.bson_serializer = dbconn.bson_serializer;
|
||||
this.ObjectID = mongodb.ObjectID /* 0.9.7-3-2 */ || dbconn.bson_serializer.ObjectID /* <= 0.9.7 */;
|
||||
};
|
||||
|
||||
utils.inherits(SkinDb, utils.SkinObject);
|
||||
|
||||
/**
|
||||
* Convert to ObjectID.
|
||||
*
|
||||
* @param {String} hex
|
||||
* @return {ObjectID}
|
||||
*/
|
||||
SkinDb.prototype.toObjectID = SkinDb.prototype.toId = function (hex) {
|
||||
if (hex instanceof this.ObjectID) {
|
||||
return hex;
|
||||
}
|
||||
if (!hex || hex.length !== 24) {
|
||||
return hex;
|
||||
}
|
||||
return this.ObjectID.createFromHexString(hex);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Open the database connection.
|
||||
*
|
||||
* @param {Function(err, nativeDb)} [callback]
|
||||
* @return {SkinDb} this
|
||||
* @api public
|
||||
*/
|
||||
SkinDb.prototype.open = function (callback) {
|
||||
switch (this.state) {
|
||||
case STATE_OPEN:
|
||||
callback && callback(null, this.db);
|
||||
break;
|
||||
case STATE_OPENNING:
|
||||
// if call 'open' method multi times before opened
|
||||
callback && this.emitter.once('open', callback);
|
||||
break;
|
||||
// case STATE_CLOSE:
|
||||
default:
|
||||
var onDbOpen = function (err, db) {
|
||||
if (!err && db) {
|
||||
this.db = db;
|
||||
this.state = STATE_OPEN;
|
||||
} else {
|
||||
db && db.close();
|
||||
// close the openning connection.
|
||||
this._dbconn.close();
|
||||
this.db = null;
|
||||
this.state = STATE_CLOSE;
|
||||
}
|
||||
this.emitter.emit('open', err, this.db);
|
||||
}.bind(this);
|
||||
callback && this.emitter.once('open', callback);
|
||||
this.state = STATE_OPENNING;
|
||||
this._dbconn.open(function (err, db) {
|
||||
if (db && this.username) {
|
||||
// do authenticate
|
||||
db.authenticate(this.username, this.password, function (err) {
|
||||
onDbOpen(err, db);
|
||||
});
|
||||
} else {
|
||||
onDbOpen(err, db);
|
||||
}
|
||||
}.bind(this));
|
||||
break;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Close the database connection.
|
||||
*
|
||||
* @param {Function(err)} [callback]
|
||||
* @return {SkinDb} this
|
||||
* @api public
|
||||
*/
|
||||
SkinDb.prototype.close = function (callback) {
|
||||
if (this.state === STATE_CLOSE) {
|
||||
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 ? db.close(callback) : callback && callback(err);
|
||||
});
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create or retrieval skin collection
|
||||
*
|
||||
* @param {String} name, the collection name.
|
||||
* @param {Object} [options] collection options.
|
||||
* @return {SkinCollection}
|
||||
* @api public
|
||||
*/
|
||||
SkinDb.prototype.collection = function (name, options) {
|
||||
var collection = this._collections[name];
|
||||
if (!collection) {
|
||||
this._collections[name] = collection = new SkinCollection(this, name, options);
|
||||
}
|
||||
return collection;
|
||||
};
|
||||
|
||||
/**
|
||||
* gridfs
|
||||
*
|
||||
* @return {SkinGridStore}
|
||||
* @api public
|
||||
*/
|
||||
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
|
||||
*
|
||||
* @param {String} collectionName
|
||||
* @param {Object|SkinCollection} [options]
|
||||
* @return {SkinCollection}
|
||||
* @api public
|
||||
*/
|
||||
SkinDb.prototype.bind = function (collectionName, options) {
|
||||
var args = __slice.call(arguments);
|
||||
var name = args[0];
|
||||
|
||||
if (typeof name !== 'string' || !name.trim()) {
|
||||
throw new Error('Must provide collection name to 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];
|
||||
}
|
||||
|
||||
var isOptions = false;
|
||||
var argsIndex = 1;
|
||||
if (options && typeof options === 'object') {
|
||||
isOptions = true;
|
||||
argsIndex = 2;
|
||||
for (var k in options) {
|
||||
if (typeof options[k] === 'function') {
|
||||
isOptions = false;
|
||||
argsIndex = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
var collection = this.collection(name, isOptions ? options : null);
|
||||
for (var len = args.length; argsIndex < len; argsIndex++) {
|
||||
var extend = args[argsIndex];
|
||||
if (typeof extend !== 'object') {
|
||||
throw new Error('the args[' + argsIndex + '] should be object, but is `' + extend + '`');
|
||||
}
|
||||
utils.extend(collection, extend);
|
||||
}
|
||||
return this.bind(name, collection);
|
||||
};
|
||||
|
||||
var IGNORE_NAMES = [
|
||||
'bind', 'open', 'close', 'collection', 'admin', 'state'
|
||||
];
|
||||
// bind method of mongodb.Db to SkinDb
|
||||
for (var key in Db.prototype) {
|
||||
if (!key || key[0] === '_' || IGNORE_NAMES.indexOf(key) >= 0) {
|
||||
continue;
|
||||
}
|
||||
var method = Db.prototype[key];
|
||||
utils.bindSkin('SkinDb', SkinDb, 'db', key, method);
|
||||
}
|
||||
67
node_modules/mongoskin/lib/mongoskin/gridfs.js
generated
vendored
Normal file
67
node_modules/mongoskin/lib/mongoskin/gridfs.js
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
/*!
|
||||
* mongoskin - gridfs.js
|
||||
*
|
||||
* Copyright(c) 2011 - 2012 kissjs.org
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var GridStore = require('mongodb').GridStore;
|
||||
var utils = require('./utils');
|
||||
|
||||
/**
|
||||
* @param filename: filename or ObjectId
|
||||
*/
|
||||
var SkinGridStore = exports.SkinGridStore = function (skinDb) {
|
||||
utils.SkinObject.call(this);
|
||||
this.skinDb = skinDb;
|
||||
};
|
||||
|
||||
utils.inherits(SkinGridStore, utils.SkinObject);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
callback = args.pop();
|
||||
this.skinDb.open(function (err, db) {
|
||||
var gs = new GridStore(db, args[0], args[1], args[2], args[3]);
|
||||
var props = {
|
||||
length: gs.length,
|
||||
contentType: gs.contentType,
|
||||
uploadDate: gs.uploadDate,
|
||||
metadata: gs.metadata,
|
||||
chunkSize: gs.chunkSize
|
||||
};
|
||||
|
||||
gs.open(function (error, reply) {
|
||||
callback(error, reply, props);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @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);
|
||||
});
|
||||
};
|
||||
200
node_modules/mongoskin/lib/mongoskin/index.js
generated
vendored
Normal file
200
node_modules/mongoskin/lib/mongoskin/index.js
generated
vendored
Normal file
@@ -0,0 +1,200 @@
|
||||
/*!
|
||||
* mongoskin - index.js
|
||||
*
|
||||
* Copyright(c) 2011 - 2012 kissjs.org
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var url = require('url');
|
||||
var Router = require('./router').Router;
|
||||
var mongo = require('mongodb');
|
||||
var SkinServer = require('./server').SkinServer;
|
||||
var SkinDb =require('./db').SkinDb;
|
||||
var Db = mongo.Db;
|
||||
var Server = mongo.Server;
|
||||
var ReplSetServers = mongo.ReplSetServers;
|
||||
var BSONNative = mongo.BSONNative;
|
||||
var constant = require('./constant');
|
||||
var DEFAULT_PORT = constant.DEFAULT_PORT;
|
||||
|
||||
function toBool(value) {
|
||||
return value !== undefined && value !== 'false' && value !== 'no' && value !== 'off';
|
||||
}
|
||||
|
||||
/**
|
||||
* parse the database url to config
|
||||
*
|
||||
* [*://]username:password@host[:port]/database?options
|
||||
*
|
||||
* @param {String} serverUrl
|
||||
* @return {Object} config
|
||||
* - {String} host
|
||||
* - {Number} port, default is `DEFAULT_PORT`.
|
||||
* - {String} [database], no database by default.
|
||||
* - {Object} options
|
||||
* - {Bool} auto_reconnect, default is `false`.
|
||||
* - {Number} poolSize, default is `1`.
|
||||
* - {String} [username], no username by default.
|
||||
* - {String} [password], no password by default.
|
||||
* @api private
|
||||
*/
|
||||
var parseUrl = function (serverUrl) {
|
||||
serverUrl = /\w+:\/\//.test(serverUrl) ? serverUrl : 'db://' + serverUrl;
|
||||
var uri = url.parse(serverUrl, true);
|
||||
var config = {};
|
||||
var serverOptions = uri.query;
|
||||
|
||||
config.host = uri.hostname;
|
||||
config.port = parseInt(uri.port, 10) || 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, 10);
|
||||
if (uri && uri.auth) {
|
||||
var auth = uri.auth;
|
||||
var separator = auth.indexOf(':');
|
||||
config.username = auth.substr(0, separator);
|
||||
config.password = auth.substr(separator + 1);
|
||||
}
|
||||
return config;
|
||||
};
|
||||
|
||||
/**
|
||||
* constructor Server from url
|
||||
*
|
||||
* @param {String} serverUrl
|
||||
* @return {Server}
|
||||
* @api private
|
||||
*/
|
||||
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 serverURL[s]
|
||||
*
|
||||
* ReplicaSet: mongoskin.db(serverURLs, dbOptions, replicasetOptions)
|
||||
*
|
||||
* ```js
|
||||
* mongoskin.db([
|
||||
* '192.168.0.1:27017/',
|
||||
* '192.168.0.2/?auto_reconnect',
|
||||
* '192.168.0.3'
|
||||
* ], {database: 'mydb'}, {connectArbiter: false, socketOptions: {timeout: 2000}});
|
||||
* ```
|
||||
*
|
||||
* Single Server: mongoskin.db(dbURL, options)
|
||||
*
|
||||
* ```js
|
||||
* mongoskin.db('192.168.0.1:27017/mydb');
|
||||
* // or
|
||||
* mongoskin.db('192.168.0.1:27017', {database: 'mydb'});
|
||||
* // set the connection timeout to `2000ms`
|
||||
* mongoskin.db('192.168.0.1:27017', {database: 'mydb', socketOptions: {timeout: 2000}});
|
||||
* ```
|
||||
*
|
||||
* @param {String|Array} serverUrl or server urls.
|
||||
* @param {Object} [dbOptions]
|
||||
* - {Object} socketOptions: @see http://mongodb.github.com/node-mongodb-native/markdown-docs/database.html#socket-options
|
||||
* - the other, @see http://mongodb.github.com/node-mongodb-native/markdown-docs/database.html#db-options
|
||||
* @param {Object} [replicasetOptions], options for replicaset.
|
||||
* The detail of this options, please
|
||||
* @see https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/connection/repl_set.js#L27.
|
||||
* @return {SkinDb}
|
||||
* @api public
|
||||
*/
|
||||
exports.db = function (serverUrl, dbOptions, replicasetOptions) {
|
||||
dbOptions = dbOptions || {};
|
||||
|
||||
var server, database, config;
|
||||
|
||||
if (Array.isArray(serverUrl)) {
|
||||
if (!dbOptions.database) {
|
||||
throw new Error('Please provide a database in `dbOptions` to connect.');
|
||||
}
|
||||
database = dbOptions.database;
|
||||
|
||||
var len = serverUrl.length;
|
||||
var servers = [];
|
||||
for (var i = 0; i < len; i++) {
|
||||
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, replicasetOptions);
|
||||
} else {
|
||||
config = parseUrl(serverUrl);
|
||||
database = dbOptions.database || config.database;
|
||||
if (!database) {
|
||||
throw new Error('Please provide a database to connect to.');
|
||||
}
|
||||
var socketOptions = dbOptions.socketOptions;
|
||||
if (socketOptions) {
|
||||
delete dbOptions.socketOptions;
|
||||
config.options.socketOptions = socketOptions;
|
||||
}
|
||||
server = new Server(config.host, config.port, config.options);
|
||||
|
||||
if (dbOptions.username === undefined) {
|
||||
dbOptions.username = config.username;
|
||||
dbOptions.password = config.password;
|
||||
}
|
||||
}
|
||||
|
||||
var skinServer = new SkinServer(server);
|
||||
return skinServer.db(database, dbOptions);
|
||||
};
|
||||
|
||||
/**
|
||||
* select different db by collection name
|
||||
*
|
||||
* @param select `function(name)` returns SkinDb
|
||||
*
|
||||
* ```js
|
||||
* var router = mongoskin.router(function (name) {
|
||||
* swhich (name) {
|
||||
* case 'user':
|
||||
* case 'group':
|
||||
* return authDb;
|
||||
* default:
|
||||
* return appDb;
|
||||
* }
|
||||
* });
|
||||
* router.collection('user')
|
||||
* ```
|
||||
*
|
||||
* @param {Function(name)} select
|
||||
* @return {Router}
|
||||
* @api public
|
||||
*/
|
||||
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 module = require('./' + path);
|
||||
for (var name in module) {
|
||||
exports[name] = module[name];
|
||||
}
|
||||
});
|
||||
49
node_modules/mongoskin/lib/mongoskin/router.js
generated
vendored
Normal file
49
node_modules/mongoskin/lib/mongoskin/router.js
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
/*!
|
||||
* mongoskin - router.js
|
||||
*
|
||||
* Copyright(c) 2011 - 2012 kissjs.org
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Router
|
||||
*
|
||||
* @param {Function(name)} select
|
||||
* @constructor
|
||||
* @api public
|
||||
*/
|
||||
var Router = exports.Router = function (select) {
|
||||
this._select = select;
|
||||
this._collections = {};
|
||||
};
|
||||
|
||||
/**
|
||||
* Bind custom methods
|
||||
*
|
||||
* @param {String} name, collection name.
|
||||
* @param {Object} [options]
|
||||
* @return {Router} this
|
||||
* @api public
|
||||
*/
|
||||
Router.prototype.bind = function (name, options) {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var database = this._select(name);
|
||||
var collection = database.bind.apply(database, args);
|
||||
this._collections[name] = collection;
|
||||
Object.defineProperty(this, name, {
|
||||
value: collection,
|
||||
writable: false,
|
||||
enumerable: true
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
Router.prototype.collection = function (name) {
|
||||
return this._collections[name] || (this._collections[name] = this._select(name).collection(name));
|
||||
};
|
||||
54
node_modules/mongoskin/lib/mongoskin/server.js
generated
vendored
Normal file
54
node_modules/mongoskin/lib/mongoskin/server.js
generated
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
/*!
|
||||
* mongoskin - server.js
|
||||
*
|
||||
* Copyright(c) 2011 - 2012 kissjs.org
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var mongodb = require('mongodb');
|
||||
var Db = mongodb.Db;
|
||||
var Server = mongodb.Server;
|
||||
var SkinDb = require('./db').SkinDb;
|
||||
|
||||
/**
|
||||
* Construct SkinServer with native Server
|
||||
*
|
||||
* @param {Server} server
|
||||
* @constructor
|
||||
* @api public
|
||||
*/
|
||||
var SkinServer = exports.SkinServer = function (server) {
|
||||
this.server = server;
|
||||
this._cache_ = {};
|
||||
};
|
||||
|
||||
/**
|
||||
* Create SkinDb from a SkinServer
|
||||
*
|
||||
* @param {String} name database name
|
||||
* @param {Object} [options]
|
||||
* @return {SkinDb}
|
||||
* @api public
|
||||
*/
|
||||
SkinServer.prototype.db = function (name, options) {
|
||||
options = options || {};
|
||||
var username = options.username || '';
|
||||
var key = username + '@' + name;
|
||||
var skinDb = this._cache_[key];
|
||||
if (!skinDb || skinDb.fail) {
|
||||
var password = options.password;
|
||||
if (!options.native_parser) {
|
||||
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;
|
||||
};
|
||||
82
node_modules/mongoskin/lib/mongoskin/utils.js
generated
vendored
Normal file
82
node_modules/mongoskin/lib/mongoskin/utils.js
generated
vendored
Normal file
@@ -0,0 +1,82 @@
|
||||
/*!
|
||||
* mongoskin - utils.js
|
||||
*
|
||||
* Copyright(c) 2011 - 2012 kissjs.org
|
||||
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var __slice = Array.prototype.slice;
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var constant = require('./constant');
|
||||
var STATE_OPEN = constant.STATE_OPEN;
|
||||
var STATE_OPENNING = constant.STATE_OPENNING;
|
||||
var STATE_CLOSE = constant.STATE_CLOSE;
|
||||
|
||||
exports.inherits = require('util').inherits;
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* SkinObject
|
||||
*
|
||||
* @constructor
|
||||
* @api public
|
||||
*/
|
||||
exports.SkinObject = function () {
|
||||
this.emitter = new EventEmitter();
|
||||
this.state = STATE_CLOSE;
|
||||
};
|
||||
|
||||
/**
|
||||
* Skin method binding.
|
||||
*
|
||||
* @param {String} objName
|
||||
* @param {Function} obj
|
||||
* @param {String} nativeObjName
|
||||
* @param {String} methodName
|
||||
* @param {Function} method
|
||||
* @return {Function}
|
||||
*/
|
||||
exports.bindSkin = function (objName, obj, nativeObjName, methodName, method) {
|
||||
if (typeof method !== 'function') {
|
||||
return;
|
||||
}
|
||||
return obj.prototype[methodName] = function () {
|
||||
var args = __slice.call(arguments);
|
||||
if (this.state === STATE_OPEN) {
|
||||
method.apply(this[nativeObjName], args);
|
||||
return this;
|
||||
}
|
||||
this.open(function (err, nativeObj) {
|
||||
if (err) {
|
||||
exports.error(err, args, objName + '.' + methodName);
|
||||
} else {
|
||||
return method.apply(nativeObj, args);
|
||||
}
|
||||
});
|
||||
return this;
|
||||
};
|
||||
};
|
||||
|
||||
exports.extend = function (destination, source) {
|
||||
for (var property in source) {
|
||||
destination[property] = source[property];
|
||||
}
|
||||
return destination;
|
||||
};
|
||||
|
||||
exports.noop = function () {};
|
||||
Reference in New Issue
Block a user