Already up-to-date. Coverage

Coverage

89%
441
396
45

mongoskin/index.js

77%
72
56
16
LineHitsSource
1/*!
2 * mongoskin - index.js
3 *
4 * Copyright(c) 2011 - 2012 kissjs.org
5 * MIT Licensed
6 */
7
81"use strict";
9
10/**
11 * Module dependencies.
12 */
13
141var url = require('url');
151var Router = require('./router').Router;
161var mongo = require('mongodb');
171var SkinServer = require('./server').SkinServer;
181var SkinDb =require('./db').SkinDb;
191var Db = mongo.Db;
201var Server = mongo.Server;
211var ReplSetServers = mongo.ReplSetServers;
221var BSONNative = mongo.BSONNative;
231var constant = require('./constant');
241var DEFAULT_PORT = constant.DEFAULT_PORT;
25
261function toBool(value) {
2711 return value !== undefined && value !== 'false' && value !== 'no' && value !== 'off';
28}
29
30/**
31 * parse the database url to config
32 *
33 * [*://]username:password@host[:port]/database?options
34 *
35 * @param {String} serverUrl
36 * @return {Object} config
37 * - {String} host
38 * - {Number} port, default is `DEFAULT_PORT`.
39 * - {String} [database], no database by default.
40 * - {Object} options
41 * - {Bool} auto_reconnect, default is `false`.
42 * - {Number} poolSize, default is `1`.
43 * - {String} [username], no username by default.
44 * - {String} [password], no password by default.
45 * @api private
46 */
471var parseUrl = function (serverUrl) {
4811 serverUrl = /\w+:\/\//.test(serverUrl) ? serverUrl : 'db://' + serverUrl;
4911 var uri = url.parse(serverUrl, true);
5011 var config = {};
5111 var serverOptions = uri.query;
52
5311 config.host = uri.hostname;
5411 config.port = parseInt(uri.port, 10) || DEFAULT_PORT;
5511 if (uri.pathname) {
560 config.database = uri.pathname.replace(/\//g, '');
57 }
5811 config.options = {};
5911 config.options.auto_reconnect = toBool(serverOptions.auto_reconnect);
6011 config.options.poolSize = parseInt(serverOptions.poolSize || 1, 10);
6111 if (uri && uri.auth) {
622 var auth = uri.auth;
632 var separator = auth.indexOf(':');
642 config.username = auth.substr(0, separator);
652 config.password = auth.substr(separator + 1);
66 }
6711 return config;
68};
69
70/**
71 * constructor Server from url
72 *
73 * @param {String} serverUrl
74 * @return {Server}
75 * @api private
76 */
771var parseServer = function (serverUrl) {
780 var config = parseUrl(serverUrl);
790 return new Server(config.host, config.port, config.options);
80};
81
82/*
83 * exports mongo classes ObjectID Long Code DbRef ... to mongoskin
84 */
851for (var key in mongo) {
8638 exports[key] = mongo[key];
87}
88
89/**
90 * constructor SkinDb from serverURL[s]
91 *
92 * ReplicaSet: mongoskin.db(serverURLs, dbOptions, replicasetOptions)
93 *
94 * ```js
95 * mongoskin.db([
96 * '192.168.0.1:27017/',
97 * '192.168.0.2/?auto_reconnect',
98 * '192.168.0.3'
99 * ], {database: 'mydb'}, {connectArbiter: false, socketOptions: {timeout: 2000}});
100 * ```
101 *
102 * Single Server: mongoskin.db(dbURL, options)
103 *
104 * ```js
105 * mongoskin.db('192.168.0.1:27017/mydb');
106 * // or
107 * mongoskin.db('192.168.0.1:27017', {database: 'mydb'});
108 * // set the connection timeout to `2000ms`
109 * mongoskin.db('192.168.0.1:27017', {database: 'mydb', socketOptions: {timeout: 2000}});
110 * ```
111 *
112 * @param {String|Array} serverUrl or server urls.
113 * @param {Object} [dbOptions]
114 * - {Object} socketOptions: @see http://mongodb.github.com/node-mongodb-native/markdown-docs/database.html#socket-options
115 * - the other, @see http://mongodb.github.com/node-mongodb-native/markdown-docs/database.html#db-options
116 * @param {Object} [replicasetOptions], options for replicaset.
117 * The detail of this options, please
118 * @see https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/connection/repl_set.js#L27.
119 * @return {SkinDb}
120 * @api public
121 */
1221exports.db = function (serverUrl, dbOptions, replicasetOptions) {
12311 dbOptions = dbOptions || {};
124
12511 var server, database, config;
126
12711 if (Array.isArray(serverUrl)) {
1280 if (!dbOptions.database) {
1290 throw new Error('Please provide a database in `dbOptions` to connect.');
130 }
1310 database = dbOptions.database;
132
1330 var len = serverUrl.length;
1340 var servers = [];
1350 for (var i = 0; i < len; i++) {
1360 config = parseUrl(serverUrl[i]);
1370 if (config.database || config.username) {
1380 console.log('MONGOSKIN:WARN: database or username found in RepliSet server URL, ' + serverUrl[i]);
139 }
1400 servers.push(new Server(config.host, config.port, config.options));
141 }
1420 server = new ReplSetServers(servers, replicasetOptions);
143 } else {
14411 config = parseUrl(serverUrl);
14511 database = dbOptions.database || config.database;
14611 if (!database) {
1470 throw new Error('Please provide a database to connect to.');
148 }
14911 var socketOptions = dbOptions.socketOptions;
15011 if (socketOptions) {
1511 delete dbOptions.socketOptions;
1521 config.options.socketOptions = socketOptions;
153 }
15411 server = new Server(config.host, config.port, config.options);
155
15611 if (dbOptions.username === undefined) {
15710 dbOptions.username = config.username;
15810 dbOptions.password = config.password;
159 }
160 }
161
16211 var skinServer = new SkinServer(server);
16311 return skinServer.db(database, dbOptions);
164};
165
166/**
167 * select different db by collection name
168 *
169 * @param select `function(name)` returns SkinDb
170 *
171 * ```js
172 * var router = mongoskin.router(function (name) {
173 * swhich (name) {
174 * case 'user':
175 * case 'group':
176 * return authDb;
177 * default:
178 * return appDb;
179 * }
180 * });
181 * router.collection('user')
182 * ```
183 *
184 * @param {Function(name)} select
185 * @return {Router}
186 * @api public
187 */
1881exports.router = function (select) {
1890 return new Router(select);
190};
191
192/*
193 * export Skin classes from ./db ./collection ./cursor ./admin
194 */
1951['server', 'db', 'collection', 'cursor', 'admin'].forEach(function (path) {
1965 var module = require('./' + path);
1975 for (var name in module) {
1985 exports[name] = module[name];
199 }
200});

mongoskin/router.js

30%
13
4
9
LineHitsSource
1/*!
2 * mongoskin - router.js
3 *
4 * Copyright(c) 2011 - 2012 kissjs.org
5 * MIT Licensed
6 */
7
81"use strict";
9
10/**
11 * Module dependencies.
12 */
13
14/**
15 * Router
16 *
17 * @param {Function(name)} select
18 * @constructor
19 * @api public
20 */
211var Router = exports.Router = function (select) {
220 this._select = select;
230 this._collections = {};
24};
25
26/**
27 * Bind custom methods
28 *
29 * @param {String} name, collection name.
30 * @param {Object} [options]
31 * @return {Router} this
32 * @api public
33 */
341Router.prototype.bind = function (name, options) {
350 var args = Array.prototype.slice.call(arguments);
360 var database = this._select(name);
370 var collection = database.bind.apply(database, args);
380 this._collections[name] = collection;
390 Object.defineProperty(this, name, {
40 value: collection,
41 writable: false,
42 enumerable: true
43 });
440 return this;
45};
46
471Router.prototype.collection = function (name) {
480 return this._collections[name] || (this._collections[name] = this._select(name).collection(name));
49};

mongoskin/server.js

100%
21
21
0
LineHitsSource
1/*!
2 * mongoskin - server.js
3 *
4 * Copyright(c) 2011 - 2012 kissjs.org
5 * MIT Licensed
6 */
7
81"use strict";
9
10/**
11 * Module dependencies.
12 */
13
141var mongodb = require('mongodb');
151var Db = mongodb.Db;
161var Server = mongodb.Server;
171var SkinDb = require('./db').SkinDb;
18
19/**
20 * Construct SkinServer with native Server
21 *
22 * @param {Server} server
23 * @constructor
24 * @api public
25 */
261var SkinServer = exports.SkinServer = function (server) {
2711 this.server = server;
2811 this._cache_ = {};
29};
30
31/**
32 * Create SkinDb from a SkinServer
33 *
34 * @param {String} name database name
35 * @param {Object} [options]
36 * @return {SkinDb}
37 * @api public
38 */
391SkinServer.prototype.db = function (name, options) {
4011 options = options || {};
4111 var username = options.username || '';
4211 var key = username + '@' + name;
4311 var skinDb = this._cache_[key];
4411 if (!skinDb || skinDb.fail) {
4511 var password = options.password;
4611 if (!options.native_parser) {
4711 options.native_parser = !! mongodb.BSONNative;
48 }
4911 var db = new Db(name, this.server, options);
5011 skinDb = new SkinDb(db, username, password);
5111 this._cache_[key] = skinDb;
52 }
5311 return skinDb;
54};

mongoskin/db.js

92%
108
100
8
LineHitsSource
1/*!
2 * mongoskin - db.js
3 *
4 * Copyright(c) 2011 - 2012 kissjs.org
5 * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
6 * MIT Licensed
7 */
8
91"use strict";
10
11/**
12 * Module dependencies.
13 */
14
151var __slice = Array.prototype.slice;
161var mongodb = require('mongodb');
171var EventEmitter = require('events').EventEmitter;
181var utils = require('./utils');
191var SkinAdmin = require('./admin').SkinAdmin;
201var SkinCollection = require('./collection').SkinCollection;
211var SkinGridStore = require('./gridfs').SkinGridStore;
221var Db = mongodb.Db;
231var constant = require('./constant');
241var STATE_CLOSE = constant.STATE_CLOSE;
251var STATE_OPENNING = constant.STATE_OPENNING;
261var STATE_OPEN = constant.STATE_OPEN;
27
28/**
29 * SkinDb
30 *
31 * @param {Db} dbconn, mongodb.Db instance
32 * @param {String} [username]
33 * @param {String} [password]
34 * @constructor
35 * @api public
36 */
371var SkinDb = exports.SkinDb = function (dbconn, username, password) {
3811 utils.SkinObject.call(this);
3911 this.emitter.setMaxListeners(100);
40
4111 this._dbconn = dbconn;
4211 this.db = null;
4311 this.username = username;
4411 this.password = password;
4511 this.admin = new SkinAdmin(this);
4611 this._collections = {};
4711 this.bson_serializer = dbconn.bson_serializer;
4811 this.ObjectID = mongodb.ObjectID /* 0.9.7-3-2 */ || dbconn.bson_serializer.ObjectID /* <= 0.9.7 */;
49};
50
511utils.inherits(SkinDb, utils.SkinObject);
52
53/**
54 * Convert to ObjectID.
55 *
56 * @param {String} hex
57 * @return {ObjectID}
58 */
591SkinDb.prototype.toObjectID = SkinDb.prototype.toId = function (hex) {
6015 if (hex instanceof this.ObjectID) {
613 return hex;
62 }
6312 if (!hex || hex.length !== 24) {
645 return hex;
65 }
667 return this.ObjectID.createFromHexString(hex);
67};
68
69
70/**
71 * Open the database connection.
72 *
73 * @param {Function(err, nativeDb)} [callback]
74 * @return {SkinDb} this
75 * @api public
76 */
771SkinDb.prototype.open = function (callback) {
78117 switch (this.state) {
79 case STATE_OPEN:
804 callback && callback(null, this.db);
814 break;
82 case STATE_OPENNING:
83 // if call 'open' method multi times before opened
84101 callback && this.emitter.once('open', callback);
85101 break;
86 // case STATE_CLOSE:
87 default:
8812 var onDbOpen = function (err, db) {
8912 if (!err && db) {
907 this.db = db;
917 this.state = STATE_OPEN;
92 } else {
935 db && db.close();
94 // close the openning connection.
955 this._dbconn.close();
965 this.db = null;
975 this.state = STATE_CLOSE;
98 }
9912 this.emitter.emit('open', err, this.db);
100 }.bind(this);
10112 callback && this.emitter.once('open', callback);
10212 this.state = STATE_OPENNING;
10312 this._dbconn.open(function (err, db) {
10412 if (db && this.username) {
105 // do authenticate
1064 db.authenticate(this.username, this.password, function (err) {
1074 onDbOpen(err, db);
108 });
109 } else {
1108 onDbOpen(err, db);
111 }
112 }.bind(this));
11312 break;
114 }
115117 return this;
116};
117
118/**
119 * Close the database connection.
120 *
121 * @param {Function(err)} [callback]
122 * @return {SkinDb} this
123 * @api public
124 */
1251SkinDb.prototype.close = function (callback) {
126105 if (this.state === STATE_CLOSE) {
127101 callback && callback();
1284 } else if (this.state === STATE_OPEN) {
1293 this.state = STATE_CLOSE;
1303 this.db.close(callback);
1311 } else if (this.state === STATE_OPENNING) {
1321 var that = this;
1331 this.emitter.once('open', function (err, db) {
1341 that.state = STATE_CLOSE;
1351 db ? db.close(callback) : callback && callback(err);
136 });
137 }
138105 return this;
139};
140
141/**
142 * Create or retrieval skin collection
143 *
144 * @param {String} name, the collection name.
145 * @param {Object} [options] collection options.
146 * @return {SkinCollection}
147 * @api public
148 */
1491SkinDb.prototype.collection = function (name, options) {
1507 var collection = this._collections[name];
1517 if (!collection) {
1527 this._collections[name] = collection = new SkinCollection(this, name, options);
153 }
1547 return collection;
155};
156
157/**
158 * gridfs
159 *
160 * @return {SkinGridStore}
161 * @api public
162 */
1631SkinDb.prototype.gridfs = function () {
1641 return this.skinGridStore || (this.skinGridStore = new SkinGridStore(this));
165};
166
167/**
168 * bind additional method to SkinCollection
169 *
170 * 1. collectionName
171 * 2. collectionName, extends1, extends2,... extendsn
172 * 3. collectionName, SkinCollection
173 *
174 * @param {String} collectionName
175 * @param {Object|SkinCollection} [options]
176 * @return {SkinCollection}
177 * @api public
178 */
1791SkinDb.prototype.bind = function (collectionName, options) {
18024 var args = __slice.call(arguments);
18124 var name = args[0];
182
18324 if (typeof name !== 'string' || !name.trim()) {
18411 throw new Error('Must provide collection name to bind.');
185 }
18613 if (args.length === 1) {
1874 return this.bind(name, this.collection(name));
188 }
1899 if (args.length === 2 && args[1].constructor === SkinCollection) {
1906 this._collections[name] = args[1];
1916 Object.defineProperty(this, name, {
192 value: args[1],
193 writable: false,
194 enumerable: true
195 });
196 // support bind for system.js
1976 var names = name.split('.');
1986 if (names.length > 1){
1990 var prev = this, next;
2000 for (var i = 0; i < names.length - 1; i++) {
2010 next = prev[names[i]];
2020 if (!next) {
2030 next = {};
2040 Object.defineProperty(prev, names[i], {
205 value: next,
206 writable: false,
207 enumerable : true
208 });
209 }
2100 prev = next;
211 }
2120 Object.defineProperty(prev, names[names.length - 1], {
213 value: args[1],
214 writable: false,
215 enumerable : true
216 });
217 }
2186 return args[1];
219 }
220
2213 var collection = this.collection(name, options);
2223 for (var index = 1, len = args.length; index < len; index++) {
2233 var extend = args[index];
2243 if (typeof extend !== 'object') {
2251 throw new Error('the args[' + index + '] should be object, but is `' + extend + '`');
226 }
2272 utils.extend(collection, extend);
228 }
2292 return this.bind(name, collection);
230};
231
2321var IGNORE_NAMES = [
233 'bind', 'open', 'close', 'collection', 'admin', 'state'
234];
235// bind method of mongodb.Db to SkinDb
2361for (var key in Db.prototype) {
23753 if (!key || key[0] === '_' || IGNORE_NAMES.indexOf(key) >= 0) {
23814 continue;
239 }
24039 var method = Db.prototype[key];
24139 utils.bindSkin('SkinDb', SkinDb, 'db', key, method);
242}

mongoskin/utils.js

97%
34
33
1
LineHitsSource
1/*!
2 * mongoskin - utils.js
3 *
4 * Copyright(c) 2011 - 2012 kissjs.org
5 * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
6 * MIT Licensed
7 */
8
91"use strict";
10
11/**
12 * Module dependencies.
13 */
14
151var __slice = Array.prototype.slice;
161var EventEmitter = require('events').EventEmitter;
171var constant = require('./constant');
181var STATE_OPEN = constant.STATE_OPEN;
191var STATE_OPENNING = constant.STATE_OPENNING;
201var STATE_CLOSE = constant.STATE_CLOSE;
21
221exports.inherits = require('util').inherits;
23
241exports.error = function (err, args, name) {
252 var cb = args.pop();
262 if (cb && typeof cb === 'function') {
272 cb(err);
28 } else {
290 console.error("Error occured with no callback to handle it while calling " + name, err);
30 }
31};
32
33/**
34 * SkinObject
35 *
36 * @constructor
37 * @api public
38 */
391exports.SkinObject = function () {
4046 this.emitter = new EventEmitter();
4146 this.state = STATE_CLOSE;
42};
43
44/**
45 * Skin method binding.
46 *
47 * @param {String} objName
48 * @param {Function} obj
49 * @param {String} nativeObjName
50 * @param {String} methodName
51 * @param {Function} method
52 * @return {Function}
53 */
541exports.bindSkin = function (objName, obj, nativeObjName, methodName, method) {
5594 if (typeof method !== 'function') {
562 return;
57 }
5892 return obj.prototype[methodName] = function () {
5941 var args = __slice.call(arguments);
6041 if (this.state === STATE_OPEN) {
6127 method.apply(this[nativeObjName], args);
6227 return this;
63 }
6414 this.open(function (err, nativeObj) {
6514 if (err) {
662 exports.error(err, args, objName + '.' + methodName);
67 } else {
6812 return method.apply(nativeObj, args);
69 }
70 });
7114 return this;
72 };
73};
74
751exports.extend = function (destination, source) {
762 for (var property in source) {
772 destination[property] = source[property];
78 }
792 return destination;
80};
81
821exports.noop = function () {};

mongoskin/constant.js

100%
5
5
0
LineHitsSource
1/*!
2 * mongoskin - constant.js
3 *
4 * Copyright(c) 2011 - 2012 kissjs.org
5 * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
6 * MIT Licensed
7 */
8
91"use strict";
10
111exports.DEFAULT_PORT = 27017;
12
131exports.STATE_CLOSE = 0;
141exports.STATE_OPENNING = 1;
151exports.STATE_OPEN = 2;

mongoskin/admin.js

100%
28
28
0
LineHitsSource
1/*!
2 * mongoskin - admin.js
3 *
4 * Copyright(c) 2011 - 2012 kissjs.org
5 * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
6 * MIT Licensed
7 */
8
91"use strict";
10
11/**
12 * Module dependencies.
13 */
14
151var Admin = require('mongodb').Admin;
161var utils = require('./utils');
171var constant = require('./constant');
18
19/**
20 * SkinAdmin
21 *
22 * @param {SkinDb} skinDb
23 * @constructor
24 * @api public
25 */
261var SkinAdmin = exports.SkinAdmin = function (skinDb) {
2713 utils.SkinObject.call(this);
2813 this.skinDb = skinDb;
2913 this.admin = null;
30};
31
321utils.inherits(SkinAdmin, utils.SkinObject);
33
34/**
35 * Retrieve mongodb.Admin instance.
36 *
37 * @param {Function(err, admin)} callback
38 * @return {SkinAdmin} this
39 * @api public
40 */
411SkinAdmin.prototype.open = function (callback) {
424 if (this.state === constant.STATE_OPEN) {
431 callback(null, this.admin);
441 return this;
45 }
463 this.emitter.once('open', callback);
473 if (this.state === constant.STATE_OPENNING) {
481 return this;
49 }
502 this.state = constant.STATE_OPENNING;
512 this.skinDb.open(function (err, db) {
522 if (err) {
531 this.admin = null;
541 this.state = constant.STATE_CLOSE;
55 } else {
561 this.admin = new Admin(db);
571 this.state = constant.STATE_OPEN;
58 }
592 this.emitter.emit('open', err, this.admin);
60 }.bind(this));
612 return this;
62};
63
641for (var key in Admin.prototype) {
6515 var method = Admin.prototype[key];
6615 utils.bindSkin('SkinAdmin', SkinAdmin, 'admin', key, method);
67}

mongoskin/collection.js

100%
97
97
0
LineHitsSource
1/*!
2 * mongoskin - collection.js
3 *
4 * Copyright(c) 2011 - 2012 kissjs.org
5 * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
6 * MIT Licensed
7 */
8
91"use strict";
10
11/**
12 * Module dependencies.
13 */
14
15/**
16 bind these methods from Collection.prototype to Provider
17
18 methods:
19 insert
20 checkCollectionName
21 remove
22 rename
23 save
24 update
25 distinct
26 count
27 drop
28 findAndModify
29 find
30 normalizeHintField
31 findOne
32 createIndex
33 ensureIndex
34 indexInformation
35 dropIndex
36 dropIndexes
37 mapReduce
38 group
39 options
40*/
411var __slice = Array.prototype.slice;
421var events = require('events');
431var Collection = require('mongodb').Collection;
441var SkinCursor = require('./cursor').SkinCursor;
451var utils = require('./utils');
461var constant = require('./constant');
471var STATE_CLOSE = constant.STATE_CLOSE;
481var STATE_OPENNING = constant.STATE_OPENNING;
491var STATE_OPEN = constant.STATE_OPEN;
50
51/**
52 * Construct SkinCollection from SkinDb and collectionName
53 * use skinDb.collection('name') usually
54 *
55 * @param {SkinDb} skinDb
56 * @param {String} collectionName
57 * @param {Object} [options] collection options
58 * @constructor
59 * @api public
60 */
611var SkinCollection = exports.SkinCollection = function (skinDb, collectionName, options) {
6210 utils.SkinObject.call(this);
63
6410 this.options = options;
6510 this.skinDb = skinDb;
6610 this.ObjectID = this.skinDb.ObjectID;
6710 this.collectionName = collectionName;
6810 this.collection = null;
6910 this.internalHint = null;
7010 this.__defineGetter__('hint', function () {
716 return this.internalHint;
72 });
7310 this.__defineSetter__('hint', function (value) {
741 this.internalHint = value;
751 this.open(function (err, collection) {
761 collection.hint = value;
771 this.internalHint = collection.hint;
78 }.bind(this));
79 });
80};
81
821utils.inherits(SkinCollection, utils.SkinObject);
83
841for (var _name in Collection.prototype) {
8530 var method = Collection.prototype[_name];
8630 utils.bindSkin('SkinCollection', SkinCollection, 'collection', _name, method);
87}
88
89/*
90 * find is a special method, because it could return a SkinCursor instance
91 */
921SkinCollection.prototype._find = SkinCollection.prototype.find;
93
94/**
95 * Retrieve mongodb.Collection
96 *
97 * @param {Function(err, collection)} callback
98 * @return {SkinCollection} this
99 * @api public
100 */
1011SkinCollection.prototype.open = function (callback) {
10216 switch (this.state) {
103 case STATE_OPEN:
1045 callback(null, this.collection);
1055 break;
106 case STATE_OPENNING:
1072 this.emitter.once('open', callback);
1082 break;
109 // case STATE_CLOSE:
110 default:
1119 this.emitter.once('open', callback);
1129 this.state = STATE_OPENNING;
1139 this.skinDb.open(function (err, db) {
1149 if (err) {
1151 this.state = STATE_CLOSE;
1161 return this.emitter.emit('open', err, null);
117 }
1188 db.collection(this.collectionName, this.options, function (err, collection) {
1198 if (err) {
1202 this.state = STATE_CLOSE;
121 } else {
1226 this.state = STATE_OPEN;
1236 this.collection = collection;
1246 if (this.hint) {
1251 this.collection.hint = this.hit;
126 }
127 }
1288 this.emitter.emit('open', err, collection);
129 }.bind(this));
130 }.bind(this));
1319 break;
132 }
13316 return this;
134};
135
136/**
137 * Close current collection.
138 *
139 * @param {Function(err)} callback
140 * @return {SkinCollection} this
141 * @api public
142 */
1431SkinCollection.prototype.close = function (callback) {
1445 this.collection = null;
1455 this.state = STATE_CLOSE;
1465 return this;
147};
148
149/**
150 * Drop current collection.
151 *
152 * @param {Function(err)} callback
153 * @return {SkinCollection} this
154 * @api public
155 */
1561SkinCollection.prototype.drop = function (callback) {
1575 this.skinDb.dropCollection(this.collectionName, callback);
1585 this.close();
1595 return this;
160};
161
162/**
163 * same args as find, but use Array as callback result but not use Cursor
164 *
165 * findItems(args, function (err, items) {});
166 *
167 * same as
168 *
169 * find(args).toArray(function (err, items) {});
170 *
171 * or using `mongodb.collection.find()`
172 *
173 * find(args, function (err, cursor) {
174 * cursor.toArray(function (err, items) {
175 * });
176 * });
177 *
178 * @param {Object} [query]
179 * @param {Object} [options]
180 * @param {Function(err, docs)} callback
181 * @return {SkinCollection} this
182 * @api public
183 */
1841SkinCollection.prototype.findItems = function (query, options, callback) {
1856 var args = __slice.call(arguments);
1866 var fn = args[args.length - 1];
1876 args[args.length - 1] = function (err, cursor) {
1886 if (err) {
1891 return fn(err);
190 }
1915 cursor.toArray(fn);
192 };
1936 this.find.apply(this, args);
1946 return this;
195};
196
197/**
198 * find and cursor.each(fn).
199 *
200 * @param {Object} [query]
201 * @param {Object} [options]
202 * @param {Function(err, item)} eachCallback
203 * @return {SkinCollection} this
204 * @api public
205 */
2061SkinCollection.prototype.findEach = function (query, options, eachCallback) {
2073 var args = __slice.call(arguments);
2083 var fn = args[args.length - 1];
2093 args[args.length - 1] = function (err, cursor) {
2103 if (err) {
2111 return fn(err);
212 }
2132 cursor.each(fn);
214 };
2153 this.find.apply(this, args);
2163 return this;
217};
218
219/**
220 * @deprecated use `SkinDb.toId` instead
221 *
222 * @param {String} hex
223 * @return {ObjectID|String}
224 * @api public
225 */
2261SkinCollection.prototype.id = function (hex) {
2277 return this.skinDb.toId(hex);
228};
229
230/**
231 * Operate by object.`_id`
232 *
233 * @param {String} methodName
234 * @param {String|ObjectID|Number} id
235 * @param {Arguments|Array} args
236 * @return {SkinCollection} this
237 * @api private
238 */
2391SkinCollection.prototype._operateById = function (methodName, id, args) {
2408 args = __slice.call(args);
2418 args[0] = {_id: this.skinDb.toId(id)};
2428 this[methodName].apply(this, args);
2438 return this;
244};
245
246/**
247 * Find one object by _id.
248 *
249 * @param {String|ObjectID|Number} id, doc primary key `_id`
250 * @param {Function(err, doc)} callback
251 * @return {SkinCollection} this
252 * @api public
253 */
2541SkinCollection.prototype.findById = function (id, callback) {
2556 return this._operateById('findOne', id, arguments);
256};
257
258/**
259 * Update doc by _id.
260 * @param {String|ObjectID|Number} id, doc primary key `_id`
261 * @param {Object} doc
262 * @param {Function(err)} callback
263 * @return {SkinCollection} this
264 * @api public
265 */
2661SkinCollection.prototype.updateById = function (id, doc, callback) {
2671 return this._operateById('update', id, arguments);
268};
269
270/**
271 * Remove doc by _id.
272 * @param {String|ObjectID|Number} id, doc primary key `_id`
273 * @param {Function(err)} callback
274 * @return {SkinCollection} this
275 * @api public
276 */
2771SkinCollection.prototype.removeById = function (id, callback) {
2781 return this._operateById('remove', id, arguments);
279};
280
281/**
282 * Creates a cursor for a query that can be used to iterate over results from MongoDB.
283 *
284 * @param {Object} query
285 * @param {Object} options
286 * @param {Function(err, docs)} callback
287 * @return {SkinCursor|SkinCollection} if last argument is not a function, then returns a SkinCursor,
288 * otherise return this
289 * @api public
290 */
2911SkinCollection.prototype.find = function (query, options, callback) {
29212 var args = __slice.call(arguments);
29312 if (args.length > 0 && typeof args[args.length - 1] === 'function') {
2947 this._find.apply(this, args);
2957 return this;
296 } else {
2975 return new SkinCursor(null, this, args);
298 }
299};

mongoskin/cursor.js

100%
42
42
0
LineHitsSource
1/*!
2 * mongoskin - cursor.js
3 *
4 * Copyright(c) 2011 - 2012 kissjs.org
5 * MIT Licensed
6 */
7
81"use strict";
9
10/**
11 * Module dependencies.
12 */
13
141var EventEmitter = require('events').EventEmitter;
151var Cursor = require('mongodb').Cursor;
161var utils = require('./utils');
171var constant = require('./constant');
181var STATE_CLOSE = constant.STATE_CLOSE;
191var STATE_OPENNING = constant.STATE_OPENNING;
201var STATE_OPEN = constant.STATE_OPEN;
21
221var SkinCursor = exports.SkinCursor = function (cursor, skinCollection, args) {
2311 utils.SkinObject.call(this);
24
2511 this.cursor = cursor;
2611 this.skinCollection = skinCollection;
2711 this.args = args || [];
2811 this.emitter = new EventEmitter();
2911 if (cursor) {
302 this.state = STATE_OPEN;
31 }
32};
33
341utils.inherits(SkinCursor, utils.SkinObject);
35
36/**
37 * Retrieve mongodb.Cursor instance.
38 *
39 * @param {Function(err, cursor)} callback
40 * @return {SkinCursor} this
41 * @api public
42 */
431SkinCursor.prototype.open = function (callback) {
4410 switch (this.state) {
45 case STATE_OPEN:
461 callback(null, this.cursor);
471 break;
48 case STATE_OPENNING:
491 this.emitter.once('open', callback);
501 break;
51 // case STATE_CLOSE:
52 default:
538 this.emitter.once('open', callback);
548 this.state = STATE_OPENNING;
558 this.skinCollection.open(function (err, collection) {
568 if (err) {
571 this.state = STATE_CLOSE;
581 this.emitter.emit('open', err);
591 return;
60 }
61 // copy args
627 var args = this.args.slice();
637 args.push(function (err, cursor) {
647 if (cursor) {
657 this.state = STATE_OPEN;
667 this.cursor = cursor;
67 }
687 this.emitter.emit('open', err, cursor);
69 }.bind(this));
707 collection.find.apply(collection, args);
71 }.bind(this));
728 break;
73 }
7410 return this;
75};
76
771[
78 // callbacks
79 'toArray', 'each', 'count', 'nextObject', 'getMore', 'explain',
80 // self return
81 'sort', 'limit', 'skip', 'batchSize',
82 // unsupported
83 //'rewind', 'close' ,...
84].forEach(function (name) {
8510 var method = Cursor.prototype[name];
8610 utils.bindSkin('SkinCursor', SkinCursor, 'cursor', name, method);
87});

mongoskin/gridfs.js

47%
21
10
11
LineHitsSource
1/*!
2 * mongoskin - gridfs.js
3 *
4 * Copyright(c) 2011 - 2012 kissjs.org
5 * MIT Licensed
6 */
7
81"use strict";
9
10/**
11 * Module dependencies.
12 */
13
141var GridStore = require('mongodb').GridStore;
151var utils = require('./utils');
16
17/**
18 * @param filename: filename or ObjectId
19 */
201var SkinGridStore = exports.SkinGridStore = function (skinDb) {
211 utils.SkinObject.call(this);
221 this.skinDb = skinDb;
23};
24
251utils.inherits(SkinGridStore, utils.SkinObject);
26
27/**
28 * @param id
29 * @param filename
30 * @param mode
31 * @param options
32 * @param callback
33 * callback(err, gridStoreObject)
34 */
351SkinGridStore.prototype.open = function (id, filename, mode, options, callback) {
360 var args = Array.prototype.slice.call(arguments);
370 callback = args.pop();
380 this.skinDb.open(function (err, db) {
390 var gs = new GridStore(db, args[0], args[1], args[2], args[3]);
400 var props = {
41 length: gs.length,
42 contentType: gs.contentType,
43 uploadDate: gs.uploadDate,
44 metadata: gs.metadata,
45 chunkSize: gs.chunkSize
46 };
47
480 gs.open(function (error, reply) {
490 callback(error, reply, props);
50 });
51 });
52};
53
54/**
55 * @param filename: filename or ObjectId
56 */
571SkinGridStore.prototype.unlink = SkinGridStore.prototype.remove = function (filename, callback) {
580 this.skinDb.open(function (err, db) {
590 GridStore.unlink(db, filename, callback);
60 });
61};
62
631SkinGridStore.prototype.exist = function (filename, rootCollection, callback) {
640 this.skinDb.open(function (err, db) {
650 GridStore.exist(db, filename, rootCollection, callback);
66 });
67};
process exit, stop mongod...