Skip to content

Commit

Permalink
Setting tingo options by global tingoDbOption node variable
Browse files Browse the repository at this point in the history
  • Loading branch information
fedescarpa committed Nov 12, 2014
1 parent c240f8e commit 83662db
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 124 deletions.
18 changes: 18 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"node" : true,
"camelcase" : true,
"eqeqeq" : true,
"bitwise" : true,
"curly" : true,
"freeze" : true,
"immed" : true,
"latedef" : true,
"newcap" : true,
"noarg" : true,
"noempty" : true,
"nonew" : true,
"undef" : true,
"strict" : true,
"sub": true,
"quotmark" : "single"
}
5 changes: 1 addition & 4 deletions driver/binary.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

/*!
* Module dependencies.
*/
'use strict';

var Binary = require('./tingodb').Binary;

Expand Down
110 changes: 50 additions & 60 deletions driver/collection.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
'use strict';

var _ = require('lodash');
var Collection = require('./tingodb').Collection;

function TingoCollection (name, conn, opts) {
if (undefined === opts) opts = {};
if (undefined === opts.capped) opts.capped = {};
if (undefined === opts) {
opts = {};
}
if (undefined === opts.capped) {
opts.capped = {};
}

opts.bufferCommands = undefined === opts.bufferCommands
? true
: opts.bufferCommands;
opts.bufferCommands = undefined === opts.bufferCommands ? true : opts.bufferCommands;

if ('number' == typeof opts.capped) {
if ('number' === typeof opts.capped) {
opts.capped = { size: opts.capped };
}

this.opts = opts;
this.name = name;
this.conn = conn;

this.queue = [];
this.buffer = this.opts.bufferCommands;

if (conn.db)
this.onOpen();
};
this.buffer = this.opts.bufferCommands;

if (conn.db){
this.onOpen();
}
}

TingoCollection.prototype.addQueue = function (name, args) {
this.queue.push([name, args]);
Expand All @@ -37,90 +43,74 @@ TingoCollection.prototype.doQueue = function () {
};

TingoCollection.prototype.onOpen = function () {
var self = this;

// always get a new collection in case the user changed host:port
// of parent db instance when re-opening the connection.
function callback (err, collection) {
if (err) {
self.conn.emit('error', err);
} else {
self.collection = collection;
self.buffer = false;
self.doQueue();
}
}

var self = this;

if (!self.opts.capped.size) {
// non-capped
return self.conn.db.collection(self.name, callback);
}

// capped
return self.conn.db.collection(self.name, function (err, c) {
if (err) return callback(err);
if (err) {
return callback(err);
}

// discover if this collection exists and if it is capped
c.options(function (err, exists) {
if (err) return callback(err);
if (err) {
return callback(err);
}

if (exists) {
if (exists.capped) {
callback(null, c);
} else {
var msg = 'A non-capped collection exists with the name: '+ self.name +'\n\n'
+ ' To use this collection as a capped collection, please '
+ 'first convert it.\n'
+ ' http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-Convertingacollectiontocapped'
var msg = 'A non-capped collection exists with the name: ' + self.name +'\n\n' +
' To use this collection as a capped collection, please ' +
'first convert it.\n' +
' http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-Convertingacollectiontocapped';
err = new Error(msg);
callback(err);
}
} else {
// create
var opts = utils.clone(self.opts.capped);
var opts = _.clone(self.opts.capped);
opts.capped = true;
self.conn.db.createCollection(self.name, opts, callback);
}
});
});

function callback (err, collection) {
if (err) {
// likely a strict mode error
self.conn.emit('error', err);
} else {
self.collection = collection;
self.buffer = false;
self.doQueue();
}
};
};

/*!
* Copy the collection methods and make them subject to queues
*/
_.forOwn(Collection.prototype, function (value, key) {

for (var i in Collection.prototype) {
// skip private methods
if (i.indexOf("_")==0) continue;
(function(i){
TingoCollection.prototype[i] = function () {
if (key.indexOf('_') !== 0) {

TingoCollection.prototype[key] = function () {
if (this.buffer) {
this.addQueue(i, arguments);
this.addQueue(key, arguments);
return;
}

var collection = this.collection
, args = arguments;
var collection = this.collection;
var args = arguments;

collection[i].apply(collection, args);
collection[key].apply(collection, args);
};
})(i);
}

/**
* Retreives information about this collections indexes.
*
* @param {Function} callback
* @method getIndexes
* @api public
*/
}
});

TingoCollection.prototype.getIndexes = Collection.prototype.indexInformation;

/*!
* Module exports.
*/

module.exports = TingoCollection;
103 changes: 55 additions & 48 deletions driver/connection.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,82 @@
var EventEmitter = require('events').EventEmitter
, _ = require('lodash')
, mkdirp = require('mkdirp')
, safe = require('safe')
, Collection = require('./collection.js')
, tingo = require('./tingodb')
, Db = tingo.Db;

'use strict';

var EventEmitter = require('events').EventEmitter;
var _ = require('lodash');
var safe = require('safe');
var Collection = require('./collection.js');
var tingo = require('./tingodb');
var Db = tingo.Db;

function TingoConnection(base) {
this.base = base;
this.collections = {};
this.models = {};
}

TingoConnection.prototype.db;
TingoConnection.prototype.db = {};

TingoConnection.prototype.__proto__ = EventEmitter.prototype;
TingoConnection.prototype = Object.create(EventEmitter.prototype);

TingoConnection.prototype.collection = function (name, options) {
if (!(name in this.collections))
TingoConnection.prototype.collection = function(name, options) {
if (!(name in this.collections)) {
this.collections[name] = new Collection(name, this, options);
}
return this.collections[name];
};

var dbs = {};

TingoConnection.prototype.open = function (uri,cb) {
TingoConnection.prototype.open = function(uri, cb) {
var self = this;
cb = _.isFunction(cb)?cb:function() {};

var path = uri.match("tingodb://(.*)");
if (!path)
throw new Error("Tungus support connection string format 'tingodb://{/path/to/valid/local/folder'");
cb = _.isFunction(cb) ? cb : function() {};

var path = uri.match('(mongodb|tingodb)://(.*)');

if (!path) {
throw new Error('Tungus support connection string format \'mongodb://{/path/to/valid/local/folder}\'');
}
path = path[1];
this.emit("connecting")

this.emit('connecting');

if (!dbs[path]) {
mkdirp.sync(path)
dbs[path] = new Db(path,{});
dbs[path] = new Db(path, {});
}
this.emit("connected")

this.emit('connected');

this.db = dbs[path];

for (var i in this.collections)
this.collections[i].onOpen();


for (var i in this.collections) {
this.collections[i].onOpen();
}

safe.yield(function() {
cb();
self.emit('open');
})
cb();
self.emit('open');
});

return this;
};

TingoConnection.prototype.close = function () {
TingoConnection.prototype.close = function() {
var self = this;
var cb = arguments[arguments.length-1];
cb = _.isFunction(cb)?cb:function() {};
var cb = arguments[arguments.length - 1];
cb = _.isFunction(cb) ? cb : function() {};

safe.yield(function() {
cb();
self.emit('close');
})
cb();
self.emit('close');
});
return this;
};

TingoConnection.prototype.model = function (name, schema, collection) {
TingoConnection.prototype.model = function(name, schema, collection) {
var Schema = this.base.Schema;
var MongooseError = this.base.Error;

// collection name discovery
if ('string' == typeof schema) {
if ('string' === typeof schema) {
collection = schema;
schema = false;
}
Expand All @@ -83,18 +87,21 @@ TingoConnection.prototype.model = function (name, schema, collection) {

if (this.models[name] && !collection) {
// model exists but we are not subclassing with custom collection
if (schema instanceof Schema && schema != this.models[name].schema) {
if (schema instanceof Schema && schema !== this.models[name].schema) {
throw new MongooseError.OverwriteModelError(name);
}
return this.models[name];
}

var opts = { cache: false, connection: this }
var opts = {
cache: false,
connection: this
};
var model;

if (schema instanceof Schema) {
// compile a model
model = this.base.model(name, schema, collection, opts)
model = this.base.model(name, schema, collection, opts);

// only the first model with this name is cached to allow
// for one-offs with custom collection names etc.
Expand Down Expand Up @@ -122,8 +129,7 @@ TingoConnection.prototype.model = function (name, schema, collection) {
throw new MongooseError.MissingSchemaError(name);
}

if (this == model.prototype.db
&& (!collection || collection == model.collection.name)) {
if (this === model.prototype.db && (!collection || collection === model.collection.name)) {
// model already uses this connection.

// only the first model with this name is cached to allow
Expand All @@ -135,7 +141,8 @@ TingoConnection.prototype.model = function (name, schema, collection) {
return model;
}

return this.models[name] = model.__subclass(this, schema, collection);
}
this.models[name] = model.__subclass(this, schema, collection);
return this.models[name];
};

module.exports = TingoConnection;
8 changes: 6 additions & 2 deletions driver/objectid.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
'use strict';

var ObjectId = require('./tingodb').ObjectID;

var ObjectIdToString = ObjectId.toString.bind(ObjectId);
var objectIdToString = ObjectId.toString.bind(ObjectId);
module.exports = exports = ObjectId;

ObjectId.fromString = function(str){
return new ObjectId(str);
};

ObjectId.toString = function(oid){
if (!arguments.length) return ObjectIdToString();
if (!arguments.length) {
return objectIdToString();
}
return oid.toJSON();
};

Expand Down
7 changes: 6 additions & 1 deletion driver/tingodb.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
module.exports = require('tingodb')({nativeObjectID:false,searchInArray:true})
'use strict';

module.exports = require('tingodb')(global.tingoDbOptions || {
nativeObjectID: false,
searchInArray: true
});
Loading

0 comments on commit 83662db

Please sign in to comment.