Already up-to-date.
| Line | Hits | Source |
|---|---|---|
| 1 | /*! | |
| 2 | * mongoskin - index.js | |
| 3 | * | |
| 4 | * Copyright(c) 2011 - 2012 kissjs.org | |
| 5 | * MIT Licensed | |
| 6 | */ | |
| 7 | ||
| 8 | 1 | "use strict"; |
| 9 | ||
| 10 | /** | |
| 11 | * Module dependencies. | |
| 12 | */ | |
| 13 | ||
| 14 | 1 | var url = require('url'); |
| 15 | 1 | var Router = require('./router').Router; |
| 16 | 1 | var mongo = require('mongodb'); |
| 17 | 1 | var SkinServer = require('./server').SkinServer; |
| 18 | 1 | var SkinDb =require('./db').SkinDb; |
| 19 | 1 | var Db = mongo.Db; |
| 20 | 1 | var Server = mongo.Server; |
| 21 | 1 | var ReplSetServers = mongo.ReplSetServers; |
| 22 | 1 | var BSONNative = mongo.BSONNative; |
| 23 | 1 | var constant = require('./constant'); |
| 24 | 1 | var DEFAULT_PORT = constant.DEFAULT_PORT; |
| 25 | ||
| 26 | 1 | function toBool(value) { |
| 27 | 11 | 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 | */ | |
| 47 | 1 | var parseUrl = function (serverUrl) { |
| 48 | 11 | serverUrl = /\w+:\/\//.test(serverUrl) ? serverUrl : 'db://' + serverUrl; |
| 49 | 11 | var uri = url.parse(serverUrl, true); |
| 50 | 11 | var config = {}; |
| 51 | 11 | var serverOptions = uri.query; |
| 52 | ||
| 53 | 11 | config.host = uri.hostname; |
| 54 | 11 | config.port = parseInt(uri.port, 10) || DEFAULT_PORT; |
| 55 | 11 | if (uri.pathname) { |
| 56 | 0 | config.database = uri.pathname.replace(/\//g, ''); |
| 57 | } | |
| 58 | 11 | config.options = {}; |
| 59 | 11 | config.options.auto_reconnect = toBool(serverOptions.auto_reconnect); |
| 60 | 11 | config.options.poolSize = parseInt(serverOptions.poolSize || 1, 10); |
| 61 | 11 | if (uri && uri.auth) { |
| 62 | 2 | var auth = uri.auth; |
| 63 | 2 | var separator = auth.indexOf(':'); |
| 64 | 2 | config.username = auth.substr(0, separator); |
| 65 | 2 | config.password = auth.substr(separator + 1); |
| 66 | } | |
| 67 | 11 | return config; |
| 68 | }; | |
| 69 | ||
| 70 | /** | |
| 71 | * constructor Server from url | |
| 72 | * | |
| 73 | * @param {String} serverUrl | |
| 74 | * @return {Server} | |
| 75 | * @api private | |
| 76 | */ | |
| 77 | 1 | var parseServer = function (serverUrl) { |
| 78 | 0 | var config = parseUrl(serverUrl); |
| 79 | 0 | return new Server(config.host, config.port, config.options); |
| 80 | }; | |
| 81 | ||
| 82 | /* | |
| 83 | * exports mongo classes ObjectID Long Code DbRef ... to mongoskin | |
| 84 | */ | |
| 85 | 1 | for (var key in mongo) { |
| 86 | 38 | 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 | */ | |
| 122 | 1 | exports.db = function (serverUrl, dbOptions, replicasetOptions) { |
| 123 | 11 | dbOptions = dbOptions || {}; |
| 124 | ||
| 125 | 11 | var server, database, config; |
| 126 | ||
| 127 | 11 | if (Array.isArray(serverUrl)) { |
| 128 | 0 | if (!dbOptions.database) { |
| 129 | 0 | throw new Error('Please provide a database in `dbOptions` to connect.'); |
| 130 | } | |
| 131 | 0 | database = dbOptions.database; |
| 132 | ||
| 133 | 0 | var len = serverUrl.length; |
| 134 | 0 | var servers = []; |
| 135 | 0 | for (var i = 0; i < len; i++) { |
| 136 | 0 | config = parseUrl(serverUrl[i]); |
| 137 | 0 | if (config.database || config.username) { |
| 138 | 0 | console.log('MONGOSKIN:WARN: database or username found in RepliSet server URL, ' + serverUrl[i]); |
| 139 | } | |
| 140 | 0 | servers.push(new Server(config.host, config.port, config.options)); |
| 141 | } | |
| 142 | 0 | server = new ReplSetServers(servers, replicasetOptions); |
| 143 | } else { | |
| 144 | 11 | config = parseUrl(serverUrl); |
| 145 | 11 | database = dbOptions.database || config.database; |
| 146 | 11 | if (!database) { |
| 147 | 0 | throw new Error('Please provide a database to connect to.'); |
| 148 | } | |
| 149 | 11 | var socketOptions = dbOptions.socketOptions; |
| 150 | 11 | if (socketOptions) { |
| 151 | 1 | delete dbOptions.socketOptions; |
| 152 | 1 | config.options.socketOptions = socketOptions; |
| 153 | } | |
| 154 | 11 | server = new Server(config.host, config.port, config.options); |
| 155 | ||
| 156 | 11 | if (dbOptions.username === undefined) { |
| 157 | 10 | dbOptions.username = config.username; |
| 158 | 10 | dbOptions.password = config.password; |
| 159 | } | |
| 160 | } | |
| 161 | ||
| 162 | 11 | var skinServer = new SkinServer(server); |
| 163 | 11 | 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 | */ | |
| 188 | 1 | exports.router = function (select) { |
| 189 | 0 | return new Router(select); |
| 190 | }; | |
| 191 | ||
| 192 | /* | |
| 193 | * export Skin classes from ./db ./collection ./cursor ./admin | |
| 194 | */ | |
| 195 | 1 | ['server', 'db', 'collection', 'cursor', 'admin'].forEach(function (path) { |
| 196 | 5 | var module = require('./' + path); |
| 197 | 5 | for (var name in module) { |
| 198 | 5 | exports[name] = module[name]; |
| 199 | } | |
| 200 | }); |
| Line | Hits | Source |
|---|---|---|
| 1 | /*! | |
| 2 | * mongoskin - router.js | |
| 3 | * | |
| 4 | * Copyright(c) 2011 - 2012 kissjs.org | |
| 5 | * MIT Licensed | |
| 6 | */ | |
| 7 | ||
| 8 | 1 | "use strict"; |
| 9 | ||
| 10 | /** | |
| 11 | * Module dependencies. | |
| 12 | */ | |
| 13 | ||
| 14 | /** | |
| 15 | * Router | |
| 16 | * | |
| 17 | * @param {Function(name)} select | |
| 18 | * @constructor | |
| 19 | * @api public | |
| 20 | */ | |
| 21 | 1 | var Router = exports.Router = function (select) { |
| 22 | 0 | this._select = select; |
| 23 | 0 | 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 | */ | |
| 34 | 1 | Router.prototype.bind = function (name, options) { |
| 35 | 0 | var args = Array.prototype.slice.call(arguments); |
| 36 | 0 | var database = this._select(name); |
| 37 | 0 | var collection = database.bind.apply(database, args); |
| 38 | 0 | this._collections[name] = collection; |
| 39 | 0 | Object.defineProperty(this, name, { |
| 40 | value: collection, | |
| 41 | writable: false, | |
| 42 | enumerable: true | |
| 43 | }); | |
| 44 | 0 | return this; |
| 45 | }; | |
| 46 | ||
| 47 | 1 | Router.prototype.collection = function (name) { |
| 48 | 0 | return this._collections[name] || (this._collections[name] = this._select(name).collection(name)); |
| 49 | }; |
| Line | Hits | Source |
|---|---|---|
| 1 | /*! | |
| 2 | * mongoskin - server.js | |
| 3 | * | |
| 4 | * Copyright(c) 2011 - 2012 kissjs.org | |
| 5 | * MIT Licensed | |
| 6 | */ | |
| 7 | ||
| 8 | 1 | "use strict"; |
| 9 | ||
| 10 | /** | |
| 11 | * Module dependencies. | |
| 12 | */ | |
| 13 | ||
| 14 | 1 | var mongodb = require('mongodb'); |
| 15 | 1 | var Db = mongodb.Db; |
| 16 | 1 | var Server = mongodb.Server; |
| 17 | 1 | var SkinDb = require('./db').SkinDb; |
| 18 | ||
| 19 | /** | |
| 20 | * Construct SkinServer with native Server | |
| 21 | * | |
| 22 | * @param {Server} server | |
| 23 | * @constructor | |
| 24 | * @api public | |
| 25 | */ | |
| 26 | 1 | var SkinServer = exports.SkinServer = function (server) { |
| 27 | 11 | this.server = server; |
| 28 | 11 | 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 | */ | |
| 39 | 1 | SkinServer.prototype.db = function (name, options) { |
| 40 | 11 | options = options || {}; |
| 41 | 11 | var username = options.username || ''; |
| 42 | 11 | var key = username + '@' + name; |
| 43 | 11 | var skinDb = this._cache_[key]; |
| 44 | 11 | if (!skinDb || skinDb.fail) { |
| 45 | 11 | var password = options.password; |
| 46 | 11 | if (!options.native_parser) { |
| 47 | 11 | options.native_parser = !! mongodb.BSONNative; |
| 48 | } | |
| 49 | 11 | var db = new Db(name, this.server, options); |
| 50 | 11 | skinDb = new SkinDb(db, username, password); |
| 51 | 11 | this._cache_[key] = skinDb; |
| 52 | } | |
| 53 | 11 | return skinDb; |
| 54 | }; |
| Line | Hits | Source |
|---|---|---|
| 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 | ||
| 9 | 1 | "use strict"; |
| 10 | ||
| 11 | /** | |
| 12 | * Module dependencies. | |
| 13 | */ | |
| 14 | ||
| 15 | 1 | var __slice = Array.prototype.slice; |
| 16 | 1 | var mongodb = require('mongodb'); |
| 17 | 1 | var EventEmitter = require('events').EventEmitter; |
| 18 | 1 | var utils = require('./utils'); |
| 19 | 1 | var SkinAdmin = require('./admin').SkinAdmin; |
| 20 | 1 | var SkinCollection = require('./collection').SkinCollection; |
| 21 | 1 | var SkinGridStore = require('./gridfs').SkinGridStore; |
| 22 | 1 | var Db = mongodb.Db; |
| 23 | 1 | var constant = require('./constant'); |
| 24 | 1 | var STATE_CLOSE = constant.STATE_CLOSE; |
| 25 | 1 | var STATE_OPENNING = constant.STATE_OPENNING; |
| 26 | 1 | var 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 | */ | |
| 37 | 1 | var SkinDb = exports.SkinDb = function (dbconn, username, password) { |
| 38 | 11 | utils.SkinObject.call(this); |
| 39 | 11 | this.emitter.setMaxListeners(100); |
| 40 | ||
| 41 | 11 | this._dbconn = dbconn; |
| 42 | 11 | this.db = null; |
| 43 | 11 | this.username = username; |
| 44 | 11 | this.password = password; |
| 45 | 11 | this.admin = new SkinAdmin(this); |
| 46 | 11 | this._collections = {}; |
| 47 | 11 | this.bson_serializer = dbconn.bson_serializer; |
| 48 | 11 | this.ObjectID = mongodb.ObjectID /* 0.9.7-3-2 */ || dbconn.bson_serializer.ObjectID /* <= 0.9.7 */; |
| 49 | }; | |
| 50 | ||
| 51 | 1 | utils.inherits(SkinDb, utils.SkinObject); |
| 52 | ||
| 53 | /** | |
| 54 | * Convert to ObjectID. | |
| 55 | * | |
| 56 | * @param {String} hex | |
| 57 | * @return {ObjectID} | |
| 58 | */ | |
| 59 | 1 | SkinDb.prototype.toObjectID = SkinDb.prototype.toId = function (hex) { |
| 60 | 15 | if (hex instanceof this.ObjectID) { |
| 61 | 3 | return hex; |
| 62 | } | |
| 63 | 12 | if (!hex || hex.length !== 24) { |
| 64 | 5 | return hex; |
| 65 | } | |
| 66 | 7 | 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 | */ | |
| 77 | 1 | SkinDb.prototype.open = function (callback) { |
| 78 | 117 | switch (this.state) { |
| 79 | case STATE_OPEN: | |
| 80 | 4 | callback && callback(null, this.db); |
| 81 | 4 | break; |
| 82 | case STATE_OPENNING: | |
| 83 | // if call 'open' method multi times before opened | |
| 84 | 101 | callback && this.emitter.once('open', callback); |
| 85 | 101 | break; |
| 86 | // case STATE_CLOSE: | |
| 87 | default: | |
| 88 | 12 | var onDbOpen = function (err, db) { |
| 89 | 12 | if (!err && db) { |
| 90 | 7 | this.db = db; |
| 91 | 7 | this.state = STATE_OPEN; |
| 92 | } else { | |
| 93 | 5 | db && db.close(); |
| 94 | // close the openning connection. | |
| 95 | 5 | this._dbconn.close(); |
| 96 | 5 | this.db = null; |
| 97 | 5 | this.state = STATE_CLOSE; |
| 98 | } | |
| 99 | 12 | this.emitter.emit('open', err, this.db); |
| 100 | }.bind(this); | |
| 101 | 12 | callback && this.emitter.once('open', callback); |
| 102 | 12 | this.state = STATE_OPENNING; |
| 103 | 12 | this._dbconn.open(function (err, db) { |
| 104 | 12 | if (db && this.username) { |
| 105 | // do authenticate | |
| 106 | 4 | db.authenticate(this.username, this.password, function (err) { |
| 107 | 4 | onDbOpen(err, db); |
| 108 | }); | |
| 109 | } else { | |
| 110 | 8 | onDbOpen(err, db); |
| 111 | } | |
| 112 | }.bind(this)); | |
| 113 | 12 | break; |
| 114 | } | |
| 115 | 117 | return this; |
| 116 | }; | |
| 117 | ||
| 118 | /** | |
| 119 | * Close the database connection. | |
| 120 | * | |
| 121 | * @param {Function(err)} [callback] | |
| 122 | * @return {SkinDb} this | |
| 123 | * @api public | |
| 124 | */ | |
| 125 | 1 | SkinDb.prototype.close = function (callback) { |
| 126 | 105 | if (this.state === STATE_CLOSE) { |
| 127 | 101 | callback && callback(); |
| 128 | 4 | } else if (this.state === STATE_OPEN) { |
| 129 | 3 | this.state = STATE_CLOSE; |
| 130 | 3 | this.db.close(callback); |
| 131 | 1 | } else if (this.state === STATE_OPENNING) { |
| 132 | 1 | var that = this; |
| 133 | 1 | this.emitter.once('open', function (err, db) { |
| 134 | 1 | that.state = STATE_CLOSE; |
| 135 | 1 | db ? db.close(callback) : callback && callback(err); |
| 136 | }); | |
| 137 | } | |
| 138 | 105 | 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 | */ | |
| 149 | 1 | SkinDb.prototype.collection = function (name, options) { |
| 150 | 7 | var collection = this._collections[name]; |
| 151 | 7 | if (!collection) { |
| 152 | 7 | this._collections[name] = collection = new SkinCollection(this, name, options); |
| 153 | } | |
| 154 | 7 | return collection; |
| 155 | }; | |
| 156 | ||
| 157 | /** | |
| 158 | * gridfs | |
| 159 | * | |
| 160 | * @return {SkinGridStore} | |
| 161 | * @api public | |
| 162 | */ | |
| 163 | 1 | SkinDb.prototype.gridfs = function () { |
| 164 | 1 | 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 | */ | |
| 179 | 1 | SkinDb.prototype.bind = function (collectionName, options) { |
| 180 | 24 | var args = __slice.call(arguments); |
| 181 | 24 | var name = args[0]; |
| 182 | ||
| 183 | 24 | if (typeof name !== 'string' || !name.trim()) { |
| 184 | 11 | throw new Error('Must provide collection name to bind.'); |
| 185 | } | |
| 186 | 13 | if (args.length === 1) { |
| 187 | 4 | return this.bind(name, this.collection(name)); |
| 188 | } | |
| 189 | 9 | if (args.length === 2 && args[1].constructor === SkinCollection) { |
| 190 | 6 | this._collections[name] = args[1]; |
| 191 | 6 | Object.defineProperty(this, name, { |
| 192 | value: args[1], | |
| 193 | writable: false, | |
| 194 | enumerable: true | |
| 195 | }); | |
| 196 | // support bind for system.js | |
| 197 | 6 | var names = name.split('.'); |
| 198 | 6 | if (names.length > 1){ |
| 199 | 0 | var prev = this, next; |
| 200 | 0 | for (var i = 0; i < names.length - 1; i++) { |
| 201 | 0 | next = prev[names[i]]; |
| 202 | 0 | if (!next) { |
| 203 | 0 | next = {}; |
| 204 | 0 | Object.defineProperty(prev, names[i], { |
| 205 | value: next, | |
| 206 | writable: false, | |
| 207 | enumerable : true | |
| 208 | }); | |
| 209 | } | |
| 210 | 0 | prev = next; |
| 211 | } | |
| 212 | 0 | Object.defineProperty(prev, names[names.length - 1], { |
| 213 | value: args[1], | |
| 214 | writable: false, | |
| 215 | enumerable : true | |
| 216 | }); | |
| 217 | } | |
| 218 | 6 | return args[1]; |
| 219 | } | |
| 220 | ||
| 221 | 3 | var collection = this.collection(name, options); |
| 222 | 3 | for (var index = 1, len = args.length; index < len; index++) { |
| 223 | 3 | var extend = args[index]; |
| 224 | 3 | if (typeof extend !== 'object') { |
| 225 | 1 | throw new Error('the args[' + index + '] should be object, but is `' + extend + '`'); |
| 226 | } | |
| 227 | 2 | utils.extend(collection, extend); |
| 228 | } | |
| 229 | 2 | return this.bind(name, collection); |
| 230 | }; | |
| 231 | ||
| 232 | 1 | var IGNORE_NAMES = [ |
| 233 | 'bind', 'open', 'close', 'collection', 'admin', 'state' | |
| 234 | ]; | |
| 235 | // bind method of mongodb.Db to SkinDb | |
| 236 | 1 | for (var key in Db.prototype) { |
| 237 | 53 | if (!key || key[0] === '_' || IGNORE_NAMES.indexOf(key) >= 0) { |
| 238 | 14 | continue; |
| 239 | } | |
| 240 | 39 | var method = Db.prototype[key]; |
| 241 | 39 | utils.bindSkin('SkinDb', SkinDb, 'db', key, method); |
| 242 | } |
| Line | Hits | Source |
|---|---|---|
| 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 | ||
| 9 | 1 | "use strict"; |
| 10 | ||
| 11 | /** | |
| 12 | * Module dependencies. | |
| 13 | */ | |
| 14 | ||
| 15 | 1 | var __slice = Array.prototype.slice; |
| 16 | 1 | var EventEmitter = require('events').EventEmitter; |
| 17 | 1 | var constant = require('./constant'); |
| 18 | 1 | var STATE_OPEN = constant.STATE_OPEN; |
| 19 | 1 | var STATE_OPENNING = constant.STATE_OPENNING; |
| 20 | 1 | var STATE_CLOSE = constant.STATE_CLOSE; |
| 21 | ||
| 22 | 1 | exports.inherits = require('util').inherits; |
| 23 | ||
| 24 | 1 | exports.error = function (err, args, name) { |
| 25 | 2 | var cb = args.pop(); |
| 26 | 2 | if (cb && typeof cb === 'function') { |
| 27 | 2 | cb(err); |
| 28 | } else { | |
| 29 | 0 | 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 | */ | |
| 39 | 1 | exports.SkinObject = function () { |
| 40 | 46 | this.emitter = new EventEmitter(); |
| 41 | 46 | 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 | */ | |
| 54 | 1 | exports.bindSkin = function (objName, obj, nativeObjName, methodName, method) { |
| 55 | 94 | if (typeof method !== 'function') { |
| 56 | 2 | return; |
| 57 | } | |
| 58 | 92 | return obj.prototype[methodName] = function () { |
| 59 | 41 | var args = __slice.call(arguments); |
| 60 | 41 | if (this.state === STATE_OPEN) { |
| 61 | 27 | method.apply(this[nativeObjName], args); |
| 62 | 27 | return this; |
| 63 | } | |
| 64 | 14 | this.open(function (err, nativeObj) { |
| 65 | 14 | if (err) { |
| 66 | 2 | exports.error(err, args, objName + '.' + methodName); |
| 67 | } else { | |
| 68 | 12 | return method.apply(nativeObj, args); |
| 69 | } | |
| 70 | }); | |
| 71 | 14 | return this; |
| 72 | }; | |
| 73 | }; | |
| 74 | ||
| 75 | 1 | exports.extend = function (destination, source) { |
| 76 | 2 | for (var property in source) { |
| 77 | 2 | destination[property] = source[property]; |
| 78 | } | |
| 79 | 2 | return destination; |
| 80 | }; | |
| 81 | ||
| 82 | 1 | exports.noop = function () {}; |
| Line | Hits | Source |
|---|---|---|
| 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 | ||
| 9 | 1 | "use strict"; |
| 10 | ||
| 11 | 1 | exports.DEFAULT_PORT = 27017; |
| 12 | ||
| 13 | 1 | exports.STATE_CLOSE = 0; |
| 14 | 1 | exports.STATE_OPENNING = 1; |
| 15 | 1 | exports.STATE_OPEN = 2; |
| Line | Hits | Source |
|---|---|---|
| 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 | ||
| 9 | 1 | "use strict"; |
| 10 | ||
| 11 | /** | |
| 12 | * Module dependencies. | |
| 13 | */ | |
| 14 | ||
| 15 | 1 | var Admin = require('mongodb').Admin; |
| 16 | 1 | var utils = require('./utils'); |
| 17 | 1 | var constant = require('./constant'); |
| 18 | ||
| 19 | /** | |
| 20 | * SkinAdmin | |
| 21 | * | |
| 22 | * @param {SkinDb} skinDb | |
| 23 | * @constructor | |
| 24 | * @api public | |
| 25 | */ | |
| 26 | 1 | var SkinAdmin = exports.SkinAdmin = function (skinDb) { |
| 27 | 13 | utils.SkinObject.call(this); |
| 28 | 13 | this.skinDb = skinDb; |
| 29 | 13 | this.admin = null; |
| 30 | }; | |
| 31 | ||
| 32 | 1 | utils.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 | */ | |
| 41 | 1 | SkinAdmin.prototype.open = function (callback) { |
| 42 | 4 | if (this.state === constant.STATE_OPEN) { |
| 43 | 1 | callback(null, this.admin); |
| 44 | 1 | return this; |
| 45 | } | |
| 46 | 3 | this.emitter.once('open', callback); |
| 47 | 3 | if (this.state === constant.STATE_OPENNING) { |
| 48 | 1 | return this; |
| 49 | } | |
| 50 | 2 | this.state = constant.STATE_OPENNING; |
| 51 | 2 | this.skinDb.open(function (err, db) { |
| 52 | 2 | if (err) { |
| 53 | 1 | this.admin = null; |
| 54 | 1 | this.state = constant.STATE_CLOSE; |
| 55 | } else { | |
| 56 | 1 | this.admin = new Admin(db); |
| 57 | 1 | this.state = constant.STATE_OPEN; |
| 58 | } | |
| 59 | 2 | this.emitter.emit('open', err, this.admin); |
| 60 | }.bind(this)); | |
| 61 | 2 | return this; |
| 62 | }; | |
| 63 | ||
| 64 | 1 | for (var key in Admin.prototype) { |
| 65 | 15 | var method = Admin.prototype[key]; |
| 66 | 15 | utils.bindSkin('SkinAdmin', SkinAdmin, 'admin', key, method); |
| 67 | } |
| Line | Hits | Source |
|---|---|---|
| 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 | ||
| 9 | 1 | "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 | */ | |
| 41 | 1 | var __slice = Array.prototype.slice; |
| 42 | 1 | var events = require('events'); |
| 43 | 1 | var Collection = require('mongodb').Collection; |
| 44 | 1 | var SkinCursor = require('./cursor').SkinCursor; |
| 45 | 1 | var utils = require('./utils'); |
| 46 | 1 | var constant = require('./constant'); |
| 47 | 1 | var STATE_CLOSE = constant.STATE_CLOSE; |
| 48 | 1 | var STATE_OPENNING = constant.STATE_OPENNING; |
| 49 | 1 | var 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 | */ | |
| 61 | 1 | var SkinCollection = exports.SkinCollection = function (skinDb, collectionName, options) { |
| 62 | 10 | utils.SkinObject.call(this); |
| 63 | ||
| 64 | 10 | this.options = options; |
| 65 | 10 | this.skinDb = skinDb; |
| 66 | 10 | this.ObjectID = this.skinDb.ObjectID; |
| 67 | 10 | this.collectionName = collectionName; |
| 68 | 10 | this.collection = null; |
| 69 | 10 | this.internalHint = null; |
| 70 | 10 | this.__defineGetter__('hint', function () { |
| 71 | 6 | return this.internalHint; |
| 72 | }); | |
| 73 | 10 | this.__defineSetter__('hint', function (value) { |
| 74 | 1 | this.internalHint = value; |
| 75 | 1 | this.open(function (err, collection) { |
| 76 | 1 | collection.hint = value; |
| 77 | 1 | this.internalHint = collection.hint; |
| 78 | }.bind(this)); | |
| 79 | }); | |
| 80 | }; | |
| 81 | ||
| 82 | 1 | utils.inherits(SkinCollection, utils.SkinObject); |
| 83 | ||
| 84 | 1 | for (var _name in Collection.prototype) { |
| 85 | 30 | var method = Collection.prototype[_name]; |
| 86 | 30 | utils.bindSkin('SkinCollection', SkinCollection, 'collection', _name, method); |
| 87 | } | |
| 88 | ||
| 89 | /* | |
| 90 | * find is a special method, because it could return a SkinCursor instance | |
| 91 | */ | |
| 92 | 1 | SkinCollection.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 | */ | |
| 101 | 1 | SkinCollection.prototype.open = function (callback) { |
| 102 | 16 | switch (this.state) { |
| 103 | case STATE_OPEN: | |
| 104 | 5 | callback(null, this.collection); |
| 105 | 5 | break; |
| 106 | case STATE_OPENNING: | |
| 107 | 2 | this.emitter.once('open', callback); |
| 108 | 2 | break; |
| 109 | // case STATE_CLOSE: | |
| 110 | default: | |
| 111 | 9 | this.emitter.once('open', callback); |
| 112 | 9 | this.state = STATE_OPENNING; |
| 113 | 9 | this.skinDb.open(function (err, db) { |
| 114 | 9 | if (err) { |
| 115 | 1 | this.state = STATE_CLOSE; |
| 116 | 1 | return this.emitter.emit('open', err, null); |
| 117 | } | |
| 118 | 8 | db.collection(this.collectionName, this.options, function (err, collection) { |
| 119 | 8 | if (err) { |
| 120 | 2 | this.state = STATE_CLOSE; |
| 121 | } else { | |
| 122 | 6 | this.state = STATE_OPEN; |
| 123 | 6 | this.collection = collection; |
| 124 | 6 | if (this.hint) { |
| 125 | 1 | this.collection.hint = this.hit; |
| 126 | } | |
| 127 | } | |
| 128 | 8 | this.emitter.emit('open', err, collection); |
| 129 | }.bind(this)); | |
| 130 | }.bind(this)); | |
| 131 | 9 | break; |
| 132 | } | |
| 133 | 16 | return this; |
| 134 | }; | |
| 135 | ||
| 136 | /** | |
| 137 | * Close current collection. | |
| 138 | * | |
| 139 | * @param {Function(err)} callback | |
| 140 | * @return {SkinCollection} this | |
| 141 | * @api public | |
| 142 | */ | |
| 143 | 1 | SkinCollection.prototype.close = function (callback) { |
| 144 | 5 | this.collection = null; |
| 145 | 5 | this.state = STATE_CLOSE; |
| 146 | 5 | return this; |
| 147 | }; | |
| 148 | ||
| 149 | /** | |
| 150 | * Drop current collection. | |
| 151 | * | |
| 152 | * @param {Function(err)} callback | |
| 153 | * @return {SkinCollection} this | |
| 154 | * @api public | |
| 155 | */ | |
| 156 | 1 | SkinCollection.prototype.drop = function (callback) { |
| 157 | 5 | this.skinDb.dropCollection(this.collectionName, callback); |
| 158 | 5 | this.close(); |
| 159 | 5 | 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 | */ | |
| 184 | 1 | SkinCollection.prototype.findItems = function (query, options, callback) { |
| 185 | 6 | var args = __slice.call(arguments); |
| 186 | 6 | var fn = args[args.length - 1]; |
| 187 | 6 | args[args.length - 1] = function (err, cursor) { |
| 188 | 6 | if (err) { |
| 189 | 1 | return fn(err); |
| 190 | } | |
| 191 | 5 | cursor.toArray(fn); |
| 192 | }; | |
| 193 | 6 | this.find.apply(this, args); |
| 194 | 6 | 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 | */ | |
| 206 | 1 | SkinCollection.prototype.findEach = function (query, options, eachCallback) { |
| 207 | 3 | var args = __slice.call(arguments); |
| 208 | 3 | var fn = args[args.length - 1]; |
| 209 | 3 | args[args.length - 1] = function (err, cursor) { |
| 210 | 3 | if (err) { |
| 211 | 1 | return fn(err); |
| 212 | } | |
| 213 | 2 | cursor.each(fn); |
| 214 | }; | |
| 215 | 3 | this.find.apply(this, args); |
| 216 | 3 | return this; |
| 217 | }; | |
| 218 | ||
| 219 | /** | |
| 220 | * @deprecated use `SkinDb.toId` instead | |
| 221 | * | |
| 222 | * @param {String} hex | |
| 223 | * @return {ObjectID|String} | |
| 224 | * @api public | |
| 225 | */ | |
| 226 | 1 | SkinCollection.prototype.id = function (hex) { |
| 227 | 7 | 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 | */ | |
| 239 | 1 | SkinCollection.prototype._operateById = function (methodName, id, args) { |
| 240 | 8 | args = __slice.call(args); |
| 241 | 8 | args[0] = {_id: this.skinDb.toId(id)}; |
| 242 | 8 | this[methodName].apply(this, args); |
| 243 | 8 | 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 | */ | |
| 254 | 1 | SkinCollection.prototype.findById = function (id, callback) { |
| 255 | 6 | 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 | */ | |
| 266 | 1 | SkinCollection.prototype.updateById = function (id, doc, callback) { |
| 267 | 1 | 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 | */ | |
| 277 | 1 | SkinCollection.prototype.removeById = function (id, callback) { |
| 278 | 1 | 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 | */ | |
| 291 | 1 | SkinCollection.prototype.find = function (query, options, callback) { |
| 292 | 12 | var args = __slice.call(arguments); |
| 293 | 12 | if (args.length > 0 && typeof args[args.length - 1] === 'function') { |
| 294 | 7 | this._find.apply(this, args); |
| 295 | 7 | return this; |
| 296 | } else { | |
| 297 | 5 | return new SkinCursor(null, this, args); |
| 298 | } | |
| 299 | }; |
| Line | Hits | Source |
|---|---|---|
| 1 | /*! | |
| 2 | * mongoskin - cursor.js | |
| 3 | * | |
| 4 | * Copyright(c) 2011 - 2012 kissjs.org | |
| 5 | * MIT Licensed | |
| 6 | */ | |
| 7 | ||
| 8 | 1 | "use strict"; |
| 9 | ||
| 10 | /** | |
| 11 | * Module dependencies. | |
| 12 | */ | |
| 13 | ||
| 14 | 1 | var EventEmitter = require('events').EventEmitter; |
| 15 | 1 | var Cursor = require('mongodb').Cursor; |
| 16 | 1 | var utils = require('./utils'); |
| 17 | 1 | var constant = require('./constant'); |
| 18 | 1 | var STATE_CLOSE = constant.STATE_CLOSE; |
| 19 | 1 | var STATE_OPENNING = constant.STATE_OPENNING; |
| 20 | 1 | var STATE_OPEN = constant.STATE_OPEN; |
| 21 | ||
| 22 | 1 | var SkinCursor = exports.SkinCursor = function (cursor, skinCollection, args) { |
| 23 | 11 | utils.SkinObject.call(this); |
| 24 | ||
| 25 | 11 | this.cursor = cursor; |
| 26 | 11 | this.skinCollection = skinCollection; |
| 27 | 11 | this.args = args || []; |
| 28 | 11 | this.emitter = new EventEmitter(); |
| 29 | 11 | if (cursor) { |
| 30 | 2 | this.state = STATE_OPEN; |
| 31 | } | |
| 32 | }; | |
| 33 | ||
| 34 | 1 | utils.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 | */ | |
| 43 | 1 | SkinCursor.prototype.open = function (callback) { |
| 44 | 10 | switch (this.state) { |
| 45 | case STATE_OPEN: | |
| 46 | 1 | callback(null, this.cursor); |
| 47 | 1 | break; |
| 48 | case STATE_OPENNING: | |
| 49 | 1 | this.emitter.once('open', callback); |
| 50 | 1 | break; |
| 51 | // case STATE_CLOSE: | |
| 52 | default: | |
| 53 | 8 | this.emitter.once('open', callback); |
| 54 | 8 | this.state = STATE_OPENNING; |
| 55 | 8 | this.skinCollection.open(function (err, collection) { |
| 56 | 8 | if (err) { |
| 57 | 1 | this.state = STATE_CLOSE; |
| 58 | 1 | this.emitter.emit('open', err); |
| 59 | 1 | return; |
| 60 | } | |
| 61 | // copy args | |
| 62 | 7 | var args = this.args.slice(); |
| 63 | 7 | args.push(function (err, cursor) { |
| 64 | 7 | if (cursor) { |
| 65 | 7 | this.state = STATE_OPEN; |
| 66 | 7 | this.cursor = cursor; |
| 67 | } | |
| 68 | 7 | this.emitter.emit('open', err, cursor); |
| 69 | }.bind(this)); | |
| 70 | 7 | collection.find.apply(collection, args); |
| 71 | }.bind(this)); | |
| 72 | 8 | break; |
| 73 | } | |
| 74 | 10 | return this; |
| 75 | }; | |
| 76 | ||
| 77 | 1 | [ |
| 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) { | |
| 85 | 10 | var method = Cursor.prototype[name]; |
| 86 | 10 | utils.bindSkin('SkinCursor', SkinCursor, 'cursor', name, method); |
| 87 | }); |
| Line | Hits | Source |
|---|---|---|
| 1 | /*! | |
| 2 | * mongoskin - gridfs.js | |
| 3 | * | |
| 4 | * Copyright(c) 2011 - 2012 kissjs.org | |
| 5 | * MIT Licensed | |
| 6 | */ | |
| 7 | ||
| 8 | 1 | "use strict"; |
| 9 | ||
| 10 | /** | |
| 11 | * Module dependencies. | |
| 12 | */ | |
| 13 | ||
| 14 | 1 | var GridStore = require('mongodb').GridStore; |
| 15 | 1 | var utils = require('./utils'); |
| 16 | ||
| 17 | /** | |
| 18 | * @param filename: filename or ObjectId | |
| 19 | */ | |
| 20 | 1 | var SkinGridStore = exports.SkinGridStore = function (skinDb) { |
| 21 | 1 | utils.SkinObject.call(this); |
| 22 | 1 | this.skinDb = skinDb; |
| 23 | }; | |
| 24 | ||
| 25 | 1 | utils.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 | */ | |
| 35 | 1 | SkinGridStore.prototype.open = function (id, filename, mode, options, callback) { |
| 36 | 0 | var args = Array.prototype.slice.call(arguments); |
| 37 | 0 | callback = args.pop(); |
| 38 | 0 | this.skinDb.open(function (err, db) { |
| 39 | 0 | var gs = new GridStore(db, args[0], args[1], args[2], args[3]); |
| 40 | 0 | var props = { |
| 41 | length: gs.length, | |
| 42 | contentType: gs.contentType, | |
| 43 | uploadDate: gs.uploadDate, | |
| 44 | metadata: gs.metadata, | |
| 45 | chunkSize: gs.chunkSize | |
| 46 | }; | |
| 47 | ||
| 48 | 0 | gs.open(function (error, reply) { |
| 49 | 0 | callback(error, reply, props); |
| 50 | }); | |
| 51 | }); | |
| 52 | }; | |
| 53 | ||
| 54 | /** | |
| 55 | * @param filename: filename or ObjectId | |
| 56 | */ | |
| 57 | 1 | SkinGridStore.prototype.unlink = SkinGridStore.prototype.remove = function (filename, callback) { |
| 58 | 0 | this.skinDb.open(function (err, db) { |
| 59 | 0 | GridStore.unlink(db, filename, callback); |
| 60 | }); | |
| 61 | }; | |
| 62 | ||
| 63 | 1 | SkinGridStore.prototype.exist = function (filename, rootCollection, callback) { |
| 64 | 0 | this.skinDb.open(function (err, db) { |
| 65 | 0 | GridStore.exist(db, filename, rootCollection, callback); |
| 66 | }); | |
| 67 | }; |