Skip to content

Commit

Permalink
Merge pull request #19 from paulbdavis/update-mongoskin
Browse files Browse the repository at this point in the history
Update mongoskin
  • Loading branch information
rschmukler committed Oct 8, 2014
2 parents bfd1b77 + 95877dc commit 70f7d20
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
20 changes: 15 additions & 5 deletions lib/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

var debug = require('debug')('modella:mongo'),
mongoskin = require('mongoskin'),
mquery = require('./mquery'),
maggregate = require('./maggregate'),
sync = {};
Expand All @@ -13,21 +14,27 @@ var debug = require('debug')('modella:mongo'),
var FLOAT_REGEXP = /[0-9]*\.[0-9]*/;
var SCIENTIFIC_REGEXP = /[0-9.]+e[0-9]+/;

var URL_REGEXP = /^mongodb:\/\//;
/**
* Export `Mongo`
*/

module.exports = function(url) {
var mongo = require('mongoskin').db(url, {w: 1}, function() { });
if (!URL_REGEXP.test(url)) {
url = 'mongodb://' + url;
}
var mongo = mongoskin.db(url, {w: 1}, function() { });

function plugin(collection, Model) {
var db = mongo.collection(collection);
Model.db = db;

db.open(function(err) {
if (err) throw err;
mquery(Model, db.collection);
maggregate(Model, db.collection);
mquery(Model, db._native);
maggregate(Model, db._native);
Model.db = db;
db.collection = db._native;
db.id = mongoskin.ObjectID;
});

Model.index = db.ensureIndex.bind(db);
Expand All @@ -42,10 +49,13 @@ module.exports = function(url) {
});

Model.once('initialize', function() {
var indexCb = function(err) {
if (err) throw err;
};
for(var attr in Model.attrs) {
if (Model.attrs.hasOwnProperty(attr)) {
var options = Model.attrs[attr];
if (options.unique) Model.index(attr, { w: 0, unique: true, sparse: true });
if (options.unique) Model.index(attr, { w: 0, unique: true, sparse: true }, indexCb);
}
}
});
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"dependencies": {
"batch": "~0.2.1",
"debug": "~0.7.0",
"mquery": "0.3.3",
"maggregate": "~0.1.0",
"mongoskin": "0.6.1"
"maggregate": "~0.2.1",
"mongoskin": "~1.4.4",
"mquery": "~1.2.1"
},
"devDependencies": {
"async": "~0.9.0",
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var modella = require('modella'),
mongo = require('../')('localhost:27017/modella-mongo'),
mongoskin = require('mongoskin'),
db = require('mongoskin').db('localhost:27017/modella-mongo', {w: 1}),
db = require('mongoskin').db('mongodb://localhost:27017/modella-mongo', {w: 1}),
mquery = require('mquery'),
maggregate = require('maggregate'),
Batch = require('batch'),
Expand Down

0 comments on commit 70f7d20

Please sign in to comment.