Comparing version 1.1.0 to 1.2.0
74
index.js
@@ -1,49 +0,49 @@ | ||
var thunky = require('thunky'); | ||
var toMongodbCore = require('to-mongodb-core'); | ||
var parse = require('parse-mongo-url'); | ||
var Database = require('./lib/database'); | ||
var getTopology = require('./lib/get-topology'); | ||
var bson = require('mongodb-core').BSON; | ||
var thunky = require('thunky') | ||
var toMongodbCore = require('to-mongodb-core') | ||
var parse = require('parse-mongo-url') | ||
var Database = require('./lib/database') | ||
var getTopology = require('./lib/get-topology') | ||
var bson = require('mongodb-core').BSON | ||
var getDbName = function(connString) { | ||
if (typeof connString !== 'string') return null; | ||
var config = parse(connString); | ||
return config.dbName; | ||
}; | ||
var getDbName = function (connString) { | ||
if (typeof connString !== 'string') return null | ||
var config = parse(connString) | ||
return config.dbName | ||
} | ||
module.exports = function(connString, cols, options) { | ||
var dbname = getDbName(connString); | ||
if (!options) options = {}; | ||
var onserver = thunky(function(cb) { | ||
getTopology(connString, options, function(err, topology) { | ||
if (err) return cb(err); | ||
cb(null, topology); | ||
}); | ||
}); | ||
module.exports = function (connString, cols, options) { | ||
var dbname = getDbName(connString) | ||
if (!options) options = {} | ||
var onserver = thunky(function (cb) { | ||
getTopology(connString, options, function (err, topology) { | ||
if (err) return cb(err) | ||
cb(null, topology) | ||
}) | ||
}) | ||
if (!dbname) { | ||
dbname = connString._dbname; | ||
onserver = thunky(function(cb) { | ||
toMongodbCore(connString, function(err, server) { | ||
if (err) cb(new Error('You must pass a connection string or a mongojs instance.')); | ||
cb(null, server); | ||
}); | ||
}); | ||
dbname = connString._dbname | ||
onserver = thunky(function (cb) { | ||
toMongodbCore(connString, function (err, server) { | ||
if (err) cb(new Error('You must pass a connection string or a mongojs instance.')) | ||
cb(null, server) | ||
}) | ||
}) | ||
} | ||
var that = new Database({name: dbname, cols: cols}, onserver); | ||
var that = new Database({name: dbname, cols: cols}, onserver) | ||
if (typeof Proxy !== 'undefined') { | ||
var p = Proxy.create({ | ||
get: function(obj, prop) { | ||
if (that[prop]) return that[prop]; | ||
that[prop] = that.collection(prop); | ||
return that[prop]; | ||
get: function (obj, prop) { | ||
if (that[prop]) return that[prop] | ||
that[prop] = that.collection(prop) | ||
return that[prop] | ||
} | ||
}); | ||
}) | ||
return p; | ||
return p | ||
} | ||
return that; | ||
}; | ||
return that | ||
} | ||
module.exports.ObjectId = bson.ObjectId; | ||
module.exports.ObjectId = bson.ObjectId |
@@ -1,13 +0,13 @@ | ||
var util = require('util'); | ||
var thunky = require('thunky'); | ||
var Cursor = require('./cursor'); | ||
var util = require('util') | ||
var thunky = require('thunky') | ||
var Cursor = require('./cursor') | ||
var AggregationCursor = function(opts) { | ||
Cursor.call(this, opts); | ||
var onserver = this._opts.onserver; | ||
var AggregationCursor = function (opts) { | ||
Cursor.call(this, opts) | ||
var onserver = this._opts.onserver | ||
var self = this; | ||
this._get = thunky(function(cb) { | ||
onserver(function(err, server) { | ||
if (err) return cb(err); | ||
var self = this | ||
this._get = thunky(function (cb) { | ||
onserver(function (err, server) { | ||
if (err) return cb(err) | ||
cb(null, server.cursor(self._opts.fullCollectionName, { | ||
@@ -17,9 +17,9 @@ aggregate: self._opts.colName, | ||
cursor: {batchSize: 1000} | ||
}, {cursor: {batchSize: 1000}})); | ||
}); | ||
}); | ||
}; | ||
}, {cursor: {batchSize: 1000}})) | ||
}) | ||
}) | ||
} | ||
util.inherits(AggregationCursor, Cursor); | ||
util.inherits(AggregationCursor, Cursor) | ||
module.exports = AggregationCursor; | ||
module.exports = AggregationCursor |
182
lib/bulk.js
@@ -1,19 +0,19 @@ | ||
var mongodb = require('mongodb-core'); | ||
var each = require('each-series'); | ||
var mongodb = require('mongodb-core') | ||
var each = require('each-series') | ||
var oid = mongodb.BSON.ObjectID.createPk; | ||
var oid = mongodb.BSON.ObjectID.createPk | ||
var Bulk = function(colName, ordered, onserver, dbname) { | ||
this._colname = colName; | ||
this._cmds = []; | ||
this._currCmd = null; | ||
this._ordered = ordered; | ||
this._onserver = onserver; | ||
this._dbname = dbname; | ||
var Bulk = function (colName, ordered, onserver, dbname) { | ||
this._colname = colName | ||
this._cmds = [] | ||
this._currCmd = null | ||
this._ordered = ordered | ||
this._onserver = onserver | ||
this._dbname = dbname | ||
var self = this; | ||
this.find = function(query) { | ||
var upsert = false; | ||
var findobj = {}; | ||
var remove = function(lim) { | ||
var self = this | ||
this.find = function (query) { | ||
var upsert = false | ||
var findobj = {} | ||
var remove = function (lim) { | ||
if (!self._currCmd) { | ||
@@ -25,6 +25,6 @@ self._currCmd = { | ||
writeConcern: {w: 1} | ||
}; | ||
} | ||
} | ||
if (!self._currCmd.delete) { | ||
self._cmds.push(self._currCmd); | ||
self._cmds.push(self._currCmd) | ||
self._currCmd = { | ||
@@ -35,8 +35,8 @@ delete: self._colname, | ||
writeConcern: {w: 1} | ||
}; | ||
} | ||
} | ||
self._currCmd.deletes.push({q: query, limit: lim}); | ||
}; | ||
self._currCmd.deletes.push({q: query, limit: lim}) | ||
} | ||
var update = function(updObj, multi) { | ||
var update = function (updObj, multi) { | ||
if (!self._currCmd) { | ||
@@ -48,6 +48,6 @@ self._currCmd = { | ||
writeConcern: {w: 1} | ||
}; | ||
} | ||
} | ||
if (!self._currCmd.update) { | ||
self._cmds.push(self._currCmd); | ||
self._cmds.push(self._currCmd) | ||
self._currCmd = { | ||
@@ -58,33 +58,33 @@ update: self._colname, | ||
writeConcern: {w: 1} | ||
}; | ||
} | ||
} | ||
self._currCmd.updates.push({q: query, u: updObj, multi: multi, upsert: upsert}); | ||
}; | ||
self._currCmd.updates.push({q: query, u: updObj, multi: multi, upsert: upsert}) | ||
} | ||
findobj.upsert = function(){ | ||
upsert = true; | ||
return findobj; | ||
}; | ||
findobj.upsert = function () { | ||
upsert = true | ||
return findobj | ||
} | ||
findobj.remove = function() { | ||
remove(0); | ||
}; | ||
findobj.remove = function () { | ||
remove(0) | ||
} | ||
findobj.removeOne = function() { | ||
remove(1); | ||
}; | ||
findobj.removeOne = function () { | ||
remove(1) | ||
} | ||
findobj.update = function(updObj) { | ||
update(updObj, true); | ||
}; | ||
findobj.update = function (updObj) { | ||
update(updObj, true) | ||
} | ||
findobj.updateOne = function(updObj) { | ||
update(updObj, false); | ||
}; | ||
findobj.updateOne = function (updObj) { | ||
update(updObj, false) | ||
} | ||
return findobj; | ||
}; | ||
}; | ||
return findobj | ||
} | ||
} | ||
Bulk.prototype.insert = function(doc) { | ||
Bulk.prototype.insert = function (doc) { | ||
if (!this._currCmd) { | ||
@@ -96,6 +96,6 @@ this._currCmd = { | ||
writeConcern: {w: 1} | ||
}; | ||
} | ||
} | ||
if (!this._currCmd.insert) { | ||
this._cmds.push(this._currCmd); | ||
this._cmds.push(this._currCmd) | ||
this._currCmd = { | ||
@@ -106,7 +106,7 @@ insert: this._colname, | ||
writeConcern: {w: 1} | ||
}; | ||
} | ||
} | ||
if (!doc._id) doc._id = oid(); | ||
this._currCmd.documents.push(doc); | ||
}; | ||
if (!doc._id) doc._id = oid() | ||
this._currCmd.documents.push(doc) | ||
} | ||
@@ -117,6 +117,6 @@ var cmdkeys = { | ||
update: 'nUpserted' | ||
}; | ||
} | ||
Bulk.prototype.tojson = function() { | ||
var obj = { | ||
Bulk.prototype.tojson = function () { | ||
var obj = { | ||
nInsertOps: 0, | ||
@@ -126,47 +126,47 @@ nUpdateOps: 0, | ||
nBatches: this._cmds.length | ||
}; | ||
} | ||
this._cmds.forEach(function(cmd) { | ||
this._cmds.forEach(function (cmd) { | ||
if (cmd.update) { | ||
obj.nUpdateOps += cmd.updates.length; | ||
obj.nUpdateOps += cmd.updates.length | ||
} else if (cmd.insert) { | ||
obj.nInsertOps += cmd.documents.length; | ||
obj.nInsertOps += cmd.documents.length | ||
} else if (cmd.delete) { | ||
obj.nRemoveOps += cmd.deletes.length; | ||
obj.nRemoveOps += cmd.deletes.length | ||
} | ||
}); | ||
}) | ||
return obj; | ||
}; | ||
return obj | ||
} | ||
Bulk.prototype.execute = function(cb) { | ||
var self = this; | ||
Bulk.prototype.execute = function (cb) { | ||
var self = this | ||
var result = { | ||
writeErrors : [ ], | ||
writeConcernErrors : [ ], | ||
nInserted : 0, | ||
nUpserted : 0, | ||
nMatched : 0, | ||
nModified : 0, | ||
nRemoved : 0, | ||
upserted : [ ] | ||
}; | ||
writeErrors: [ ], | ||
writeConcernErrors: [ ], | ||
nInserted: 0, | ||
nUpserted: 0, | ||
nMatched: 0, | ||
nModified: 0, | ||
nRemoved: 0, | ||
upserted: [ ] | ||
} | ||
this._cmds.push(this._currCmd); | ||
this._onserver(function(err, server) { | ||
if (err) return cb(err); | ||
each(self._cmds, function(cmd, i, done) { | ||
server.command(self._dbname + '.$cmd', cmd, function(err, res) { | ||
if (err) return done(err); | ||
result[cmdkeys[Object.keys(cmd)[0]]] += res.result.n; | ||
done(); | ||
}); | ||
}, function(err) { | ||
if (err) return cb(err); | ||
result.ok = 1; | ||
cb(null, result); | ||
}); | ||
}); | ||
}; | ||
this._cmds.push(this._currCmd) | ||
this._onserver(function (err, server) { | ||
if (err) return cb(err) | ||
each(self._cmds, function (cmd, i, done) { | ||
server.command(self._dbname + '.$cmd', cmd, function (err, res) { | ||
if (err) return done(err) | ||
result[cmdkeys[Object.keys(cmd)[0]]] += res.result.n | ||
done() | ||
}) | ||
}, function (err) { | ||
if (err) return cb(err) | ||
result.ok = 1 | ||
cb(null, result) | ||
}) | ||
}) | ||
} | ||
module.exports = Bulk; | ||
module.exports = Bulk |
@@ -1,32 +0,32 @@ | ||
var mongodb = require('mongodb-core'); | ||
var once = require('once'); | ||
var Cursor = require('./cursor'); | ||
var AggregationCursor = require('./aggregation-cursor'); | ||
var Bulk = require('./bulk'); | ||
var mongodb = require('mongodb-core') | ||
var once = require('once') | ||
var Cursor = require('./cursor') | ||
var AggregationCursor = require('./aggregation-cursor') | ||
var Bulk = require('./bulk') | ||
var writeOpts = {writeConcern: {w:1}, ordered:true}; | ||
var noop = function() {}; | ||
var oid = mongodb.BSON.ObjectID.createPk; | ||
var Code = mongodb.BSON.Code; | ||
var writeOpts = {writeConcern: {w: 1}, ordered: true} | ||
var noop = function () {} | ||
var oid = mongodb.BSON.ObjectID.createPk | ||
var Code = mongodb.BSON.Code | ||
var indexName = function(index) { | ||
return Object.keys(index).map(function(key) { | ||
return key + '_' + index[key]; | ||
}).join('_'); | ||
}; | ||
var indexName = function (index) { | ||
return Object.keys(index).map(function (key) { | ||
return key + '_' + index[key] | ||
}).join('_') | ||
} | ||
var Collection = function(opts, getServer) { | ||
this._name = opts.name; | ||
this._dbname = opts.dbname; | ||
this._getServer = getServer; | ||
}; | ||
var Collection = function (opts, getServer) { | ||
this._name = opts.name | ||
this._dbname = opts.dbname | ||
this._getServer = getServer | ||
} | ||
Collection.prototype._fullColName = function() { | ||
return this._dbname + '.' + this._name; | ||
}; | ||
Collection.prototype._fullColName = function () { | ||
return this._dbname + '.' + this._name | ||
} | ||
Collection.prototype.find = function(query, projection, opts, cb) { | ||
if (typeof query === 'function') return this.find({}, null, null, query); | ||
if (typeof projection === 'function') return this.find(query, null, null, projection); | ||
if (typeof opts === 'function') return this.find(query, projection, null, opts); | ||
Collection.prototype.find = function (query, projection, opts, cb) { | ||
if (typeof query === 'function') return this.find({}, null, null, query) | ||
if (typeof projection === 'function') return this.find(query, null, null, projection) | ||
if (typeof opts === 'function') return this.find(query, projection, null, opts) | ||
@@ -40,106 +40,106 @@ opts = opts || {} | ||
var cursor = new Cursor(opts); | ||
var cursor = new Cursor(opts) | ||
if (cb) return cursor.toArray(cb); | ||
return cursor; | ||
}; | ||
if (cb) return cursor.toArray(cb) | ||
return cursor | ||
} | ||
Collection.prototype.findOne = function(query, projection, cb) { | ||
if (typeof query === 'function') return this.findOne({}, null, query); | ||
if (typeof projection === 'function') return this.findOne(query, null, projection); | ||
this.find(query, projection).next(function(err, doc) { | ||
if (err) return cb(err); | ||
cb(null, doc); | ||
}); | ||
}; | ||
Collection.prototype.findOne = function (query, projection, cb) { | ||
if (typeof query === 'function') return this.findOne({}, null, query) | ||
if (typeof projection === 'function') return this.findOne(query, null, projection) | ||
this.find(query, projection).next(function (err, doc) { | ||
if (err) return cb(err) | ||
cb(null, doc) | ||
}) | ||
} | ||
Collection.prototype.findAndModify = function(opts, cb) { | ||
this.runCommand('findAndModify', opts, function(err, result) { | ||
if (err) return cb(err); | ||
cb(null, result.value, result.lastErrorObject || {n: 0}); | ||
}); | ||
}; | ||
Collection.prototype.findAndModify = function (opts, cb) { | ||
this.runCommand('findAndModify', opts, function (err, result) { | ||
if (err) return cb(err) | ||
cb(null, result.value, result.lastErrorObject || {n: 0}) | ||
}) | ||
} | ||
Collection.prototype.count = function(query, cb) { | ||
if (typeof query === 'function') return this.count({}, query); | ||
this.find(query).count(cb); | ||
}; | ||
Collection.prototype.count = function (query, cb) { | ||
if (typeof query === 'function') return this.count({}, query) | ||
this.find(query).count(cb) | ||
} | ||
Collection.prototype.distinct = function(field, query, cb) { | ||
this.runCommand('distinct', {key: field, query: query}, function(err, result) { | ||
if (err) return cb(err); | ||
cb(null, result.values); | ||
}); | ||
}; | ||
Collection.prototype.distinct = function (field, query, cb) { | ||
this.runCommand('distinct', {key: field, query: query}, function (err, result) { | ||
if (err) return cb(err) | ||
cb(null, result.values) | ||
}) | ||
} | ||
Collection.prototype.insert = function(docOrDocs, cb) { | ||
cb = cb || noop; | ||
var self = this; | ||
this._getServer(function(err, server) { | ||
if (err) return cb(err); | ||
Collection.prototype.insert = function (docOrDocs, cb) { | ||
cb = cb || noop | ||
var self = this | ||
this._getServer(function (err, server) { | ||
if (err) return cb(err) | ||
var docs = Array.isArray(docOrDocs) ? docOrDocs: [docOrDocs]; | ||
var docs = Array.isArray(docOrDocs) ? docOrDocs : [docOrDocs] | ||
for (var i = 0; i < docs.length; i++) { | ||
if (!docs[i]._id) docs[i]._id = oid(); | ||
if (!docs[i]._id) docs[i]._id = oid() | ||
} | ||
server.insert(self._fullColName(), docs, writeOpts, function(err) { | ||
if (err) return cb(err); | ||
cb(null, docOrDocs); | ||
}); | ||
}); | ||
}; | ||
server.insert(self._fullColName(), docs, writeOpts, function (err) { | ||
if (err) return cb(err) | ||
cb(null, docOrDocs) | ||
}) | ||
}) | ||
} | ||
Collection.prototype.update = function(query, update, opts, cb) { | ||
if (!opts && !cb) return this.update(query, update, {}, noop); | ||
if (typeof opts === 'function') return this.update(query, update, {}, opts); | ||
Collection.prototype.update = function (query, update, opts, cb) { | ||
if (!opts && !cb) return this.update(query, update, {}, noop) | ||
if (typeof opts === 'function') return this.update(query, update, {}, opts) | ||
cb = cb || noop; | ||
var self = this; | ||
this._getServer(function(err, server) { | ||
if (err) return cb(err); | ||
cb = cb || noop | ||
var self = this | ||
this._getServer(function (err, server) { | ||
if (err) return cb(err) | ||
opts.q = query; | ||
opts.u = update; | ||
server.update(self._fullColName(), [opts], writeOpts, function(err, res) { | ||
if (err) return cb(err); | ||
cb(null, res.result); | ||
}); | ||
}); | ||
}; | ||
opts.q = query | ||
opts.u = update | ||
server.update(self._fullColName(), [opts], writeOpts, function (err, res) { | ||
if (err) return cb(err) | ||
cb(null, res.result) | ||
}) | ||
}) | ||
} | ||
Collection.prototype.save = function(doc, cb) { | ||
cb = cb || noop; | ||
Collection.prototype.save = function (doc, cb) { | ||
cb = cb || noop | ||
if (doc._id) { | ||
this.update({_id: doc._id}, doc, {upsert: true}, function(err) { | ||
if (err) return cb(err); | ||
cb(null, doc); | ||
}); | ||
this.update({_id: doc._id}, doc, {upsert: true}, function (err) { | ||
if (err) return cb(err) | ||
cb(null, doc) | ||
}) | ||
} else { | ||
this.insert(doc, cb); | ||
this.insert(doc, cb) | ||
} | ||
}; | ||
} | ||
Collection.prototype.remove = function(query, justOne, cb) { | ||
if (typeof query === 'function') return this.remove({}, false, query); | ||
if (typeof justOne === 'function') return this.remove(query, false, justOne); | ||
Collection.prototype.remove = function (query, justOne, cb) { | ||
if (typeof query === 'function') return this.remove({}, false, query) | ||
if (typeof justOne === 'function') return this.remove(query, false, justOne) | ||
var self = this; | ||
this._getServer(function(err, server) { | ||
if (err) return cb(err); | ||
server.remove(self._fullColName(), [{q: query, limit: justOne ? 1 : 0}], writeOpts, function(err, res) { | ||
if (err) return cb(err); | ||
cb(null, res.result); | ||
}); | ||
}); | ||
}; | ||
var self = this | ||
this._getServer(function (err, server) { | ||
if (err) return cb(err) | ||
server.remove(self._fullColName(), [{q: query, limit: justOne ? 1 : 0}], writeOpts, function (err, res) { | ||
if (err) return cb(err) | ||
cb(null, res.result) | ||
}) | ||
}) | ||
} | ||
Collection.prototype.drop = function(cb) { | ||
this.runCommand('drop', cb); | ||
}; | ||
Collection.prototype.drop = function (cb) { | ||
this.runCommand('drop', cb) | ||
} | ||
Collection.prototype.stats = function(cb) { | ||
this.runCommand('collStats', cb); | ||
}; | ||
Collection.prototype.stats = function (cb) { | ||
this.runCommand('collStats', cb) | ||
} | ||
Collection.prototype.mapReduce = function(map, reduce, opts, cb) { | ||
Collection.prototype.mapReduce = function (map, reduce, opts, cb) { | ||
this.runCommand('mapReduce', { | ||
@@ -150,51 +150,51 @@ map: map.toString(), | ||
out: opts.out | ||
}, cb); | ||
}; | ||
}, cb) | ||
} | ||
Collection.prototype.runCommand = function(cmd, opts, cb) { | ||
if (typeof opts === 'function') return this.runCommand(cmd, null, opts); | ||
var self = this; | ||
opts = opts || {}; | ||
Collection.prototype.runCommand = function (cmd, opts, cb) { | ||
if (typeof opts === 'function') return this.runCommand(cmd, null, opts) | ||
var self = this | ||
opts = opts || {} | ||
var cmdObject = {}; | ||
cmdObject[cmd] = this._name; | ||
Object.keys(opts).forEach(function(key) { | ||
cmdObject[key] = opts[key]; | ||
}); | ||
this._getServer(function(err, server) { | ||
if (err) return cb(err); | ||
server.command(self._dbname + '.$cmd', cmdObject, function(err, result) { | ||
if (err) return cb(err); | ||
cb(null, result.result); | ||
}); | ||
}); | ||
}; | ||
var cmdObject = {} | ||
cmdObject[cmd] = this._name | ||
Object.keys(opts).forEach(function (key) { | ||
cmdObject[key] = opts[key] | ||
}) | ||
this._getServer(function (err, server) { | ||
if (err) return cb(err) | ||
server.command(self._dbname + '.$cmd', cmdObject, function (err, result) { | ||
if (err) return cb(err) | ||
cb(null, result.result) | ||
}) | ||
}) | ||
} | ||
Collection.prototype.toString = function() { | ||
return this._name; | ||
}; | ||
Collection.prototype.toString = function () { | ||
return this._name | ||
} | ||
Collection.prototype.dropIndexes = function(cb) { | ||
this.runCommand('dropIndexes', {index: '*'}, cb); | ||
}; | ||
Collection.prototype.dropIndexes = function (cb) { | ||
this.runCommand('dropIndexes', {index: '*'}, cb) | ||
} | ||
Collection.prototype.dropIndex = function(index, cb) { | ||
this.runCommand('dropIndexes', {index: index}, cb); | ||
}; | ||
Collection.prototype.dropIndex = function (index, cb) { | ||
this.runCommand('dropIndexes', {index: index}, cb) | ||
} | ||
Collection.prototype.createIndex = function(index, opts, cb) { | ||
if (typeof opts === 'function') return this.createIndex(index, {}, opts); | ||
if (typeof opts === 'undefined') return this.createIndex(index, {}, noop); | ||
if (typeof cb === 'undefined') return this.createIndex(index, opts, noop); | ||
Collection.prototype.createIndex = function (index, opts, cb) { | ||
if (typeof opts === 'function') return this.createIndex(index, {}, opts) | ||
if (typeof opts === 'undefined') return this.createIndex(index, {}, noop) | ||
if (typeof cb === 'undefined') return this.createIndex(index, opts, noop) | ||
opts.name = indexName(index); | ||
opts.key = index; | ||
this.runCommand('createIndexes', {indexes: [opts]}, cb); | ||
}; | ||
opts.name = indexName(index) | ||
opts.key = index | ||
this.runCommand('createIndexes', {indexes: [opts]}, cb) | ||
} | ||
Collection.prototype.ensureIndex = function(index, opts, cb) { | ||
this.createIndex(index, opts, cb); | ||
}; | ||
Collection.prototype.ensureIndex = function (index, opts, cb) { | ||
this.createIndex(index, opts, cb) | ||
} | ||
Collection.prototype.getIndexes = function(cb) { | ||
Collection.prototype.getIndexes = function (cb) { | ||
var cursor = new Cursor({ | ||
@@ -205,12 +205,12 @@ query: {ns: this._fullColName()}, | ||
fullCollectionName: this._dbname + '.system.indexes' | ||
}); | ||
}) | ||
cursor.toArray(cb); | ||
}; | ||
cursor.toArray(cb) | ||
} | ||
Collection.prototype.reIndex = function(cb) { | ||
this.runCommand('reIndex', cb); | ||
}; | ||
Collection.prototype.reIndex = function (cb) { | ||
this.runCommand('reIndex', cb) | ||
} | ||
Collection.prototype.isCapped = function(cb) { | ||
Collection.prototype.isCapped = function (cb) { | ||
var cursor = new Cursor({ | ||
@@ -221,11 +221,11 @@ query: {name: this._fullColName()}, | ||
fullCollectionName: this._dbname + '.system.namespaces' | ||
}); | ||
}) | ||
cursor.toArray(function(err, cols) { | ||
if (err) return cb(err); | ||
cb(null, (cols[0].options && cols[0].options.capped) || false); | ||
}); | ||
}; | ||
cursor.toArray(function (err, cols) { | ||
if (err) return cb(err) | ||
cb(null, (cols[0].options && cols[0].options.capped) || false) | ||
}) | ||
} | ||
Collection.prototype.group = function(doc, cb) { | ||
Collection.prototype.group = function (doc, cb) { | ||
var cmd = { | ||
@@ -240,34 +240,34 @@ group: { | ||
} | ||
}; | ||
} | ||
if (doc.finalize) cmd.group.finalize = new Code(doc.finalize.toString()); | ||
if (doc.keys) cmd.group.$keyf = new Code(doc.keys.toString()); | ||
if (doc.finalize) cmd.group.finalize = new Code(doc.finalize.toString()) | ||
if (doc.keys) cmd.group.$keyf = new Code(doc.keys.toString()) | ||
var self = this; | ||
this._getServer(function(err, server) { | ||
if (err) return cb(err); | ||
server.command(self._dbname + '.$cmd', cmd, function(err, result) { | ||
if (err) return cb(err); | ||
cb(null, result.result.retval); | ||
}); | ||
}); | ||
}; | ||
var self = this | ||
this._getServer(function (err, server) { | ||
if (err) return cb(err) | ||
server.command(self._dbname + '.$cmd', cmd, function (err, result) { | ||
if (err) return cb(err) | ||
cb(null, result.result.retval) | ||
}) | ||
}) | ||
} | ||
Collection.prototype.aggregate = function() { | ||
var cb; | ||
var pipeline = Array.prototype.slice.call(arguments); | ||
Collection.prototype.aggregate = function () { | ||
var cb | ||
var pipeline = Array.prototype.slice.call(arguments) | ||
if (typeof pipeline[pipeline.length - 1] === 'function') { | ||
cb = once(pipeline.pop()); | ||
cb = once(pipeline.pop()) | ||
} | ||
if (pipeline.length === 1 && Array.isArray(pipeline[0])) { | ||
pipeline = pipeline[0]; | ||
pipeline = pipeline[0] | ||
} | ||
if (cb) { | ||
this.runCommand('aggregate', {pipeline: pipeline}, function(err, res) { | ||
if (err) return cb(err); | ||
cb(null, res.result); | ||
}); | ||
return; | ||
this.runCommand('aggregate', {pipeline: pipeline}, function (err, res) { | ||
if (err) return cb(err) | ||
cb(null, res.result) | ||
}) | ||
return | ||
} | ||
@@ -279,15 +279,15 @@ var strm = new AggregationCursor({ | ||
pipeline: pipeline | ||
}); | ||
}) | ||
return strm; | ||
}; | ||
return strm | ||
} | ||
Collection.prototype.initializeOrderedBulkOp = function() { | ||
return new Bulk(this._name, true, this._getServer, this._dbname); | ||
}; | ||
Collection.prototype.initializeOrderedBulkOp = function () { | ||
return new Bulk(this._name, true, this._getServer, this._dbname) | ||
} | ||
Collection.prototype.initializeUnorderedBulkOp = function() { | ||
return new Bulk(this._name, false, this._getServer, this._dbname); | ||
}; | ||
Collection.prototype.initializeUnorderedBulkOp = function () { | ||
return new Bulk(this._name, false, this._getServer, this._dbname) | ||
} | ||
module.exports = Collection; | ||
module.exports = Collection |
@@ -1,14 +0,14 @@ | ||
var util = require('util'); | ||
var thunky = require('thunky'); | ||
var Readable = require('readable-stream').Readable; | ||
var util = require('util') | ||
var thunky = require('thunky') | ||
var Readable = require('readable-stream').Readable | ||
var Cursor = function(opts) { | ||
Readable.call(this, {objectMode:true, highWaterMark:0}); | ||
this._opts = opts; | ||
var onserver = this._opts.onserver; | ||
var Cursor = function (opts) { | ||
Readable.call(this, {objectMode: true, highWaterMark: 0}) | ||
this._opts = opts | ||
var onserver = this._opts.onserver | ||
var self = this; | ||
this._get = thunky(function(cb) { | ||
onserver(function(err, server) { | ||
if (err) return cb(err); | ||
var self = this | ||
this._get = thunky(function (cb) { | ||
onserver(function (err, server) { | ||
if (err) return cb(err) | ||
cb(null, server.cursor(self._opts.fullCollectionName, { | ||
@@ -27,156 +27,155 @@ find: self._opts.fullCollectionName, | ||
numberOfRetries: self._opts.numberOfRetries | ||
})); | ||
}); | ||
}); | ||
}; | ||
})) | ||
}) | ||
}) | ||
} | ||
util.inherits(Cursor, Readable); | ||
util.inherits(Cursor, Readable) | ||
Cursor.prototype.next = function(cb) { | ||
this._get(function(err, cursor) { | ||
if (err) return cb(err); | ||
cursor.next(cb); | ||
}); | ||
Cursor.prototype.next = function (cb) { | ||
this._get(function (err, cursor) { | ||
if (err) return cb(err) | ||
cursor.next(cb) | ||
}) | ||
return this; | ||
}; | ||
return this | ||
} | ||
Cursor.prototype.rewind = function(cb) { | ||
this._get(function(err, cursor) { | ||
if (err) return cb(err); | ||
cursor.rewind(cb); | ||
}); | ||
Cursor.prototype.rewind = function (cb) { | ||
this._get(function (err, cursor) { | ||
if (err) return cb(err) | ||
cursor.rewind(cb) | ||
}) | ||
return this; | ||
}; | ||
return this | ||
} | ||
Cursor.prototype.toArray = function(cb) { | ||
var array = []; | ||
var self = this; | ||
Cursor.prototype.toArray = function (cb) { | ||
var array = [] | ||
var self = this | ||
var loop = function() { | ||
self.next(function(err, obj) { | ||
if (err) return cb(err); | ||
if (!obj) return cb(null, array); | ||
array.push(obj); | ||
loop(); | ||
}); | ||
}; | ||
var loop = function () { | ||
self.next(function (err, obj) { | ||
if (err) return cb(err) | ||
if (!obj) return cb(null, array) | ||
array.push(obj) | ||
loop() | ||
}) | ||
} | ||
loop(); | ||
}; | ||
loop() | ||
} | ||
Cursor.prototype.map = function(mapfn, cb) { | ||
var array = []; | ||
var self = this; | ||
Cursor.prototype.map = function (mapfn, cb) { | ||
var array = [] | ||
var self = this | ||
var loop = function() { | ||
self.next(function(err, obj) { | ||
if (err) return cb(err); | ||
if (!obj) return cb(null, array); | ||
array.push(mapfn(obj)); | ||
loop(); | ||
}); | ||
}; | ||
var loop = function () { | ||
self.next(function (err, obj) { | ||
if (err) return cb(err) | ||
if (!obj) return cb(null, array) | ||
array.push(mapfn(obj)) | ||
loop() | ||
}) | ||
} | ||
loop(); | ||
}; | ||
loop() | ||
} | ||
Cursor.prototype.forEach = function(fn) { | ||
var array = []; | ||
var self = this; | ||
Cursor.prototype.forEach = function (fn) { | ||
var self = this | ||
var loop = function() { | ||
self.next(function(err, obj) { | ||
if (err) return fn(err); | ||
fn(err, obj); | ||
var loop = function () { | ||
self.next(function (err, obj) { | ||
if (err) return fn(err) | ||
fn(err, obj) | ||
if (!obj) return; | ||
loop(); | ||
}); | ||
}; | ||
if (!obj) return | ||
loop() | ||
}) | ||
} | ||
loop(); | ||
}; | ||
loop() | ||
} | ||
Cursor.prototype.limit = function(n, cb) { | ||
this._opts.limit = n; | ||
if (cb) return this.toArray(cb); | ||
return this; | ||
}; | ||
Cursor.prototype.limit = function (n, cb) { | ||
this._opts.limit = n | ||
if (cb) return this.toArray(cb) | ||
return this | ||
} | ||
Cursor.prototype.skip = function(n, cb) { | ||
this._opts.skip = n; | ||
if (cb) return this.toArray(cb); | ||
return this; | ||
}; | ||
Cursor.prototype.skip = function (n, cb) { | ||
this._opts.skip = n | ||
if (cb) return this.toArray(cb) | ||
return this | ||
} | ||
Cursor.prototype.batchSize = function(n, cb) { | ||
this._opts.batchSize = n; | ||
if (cb) return this.toArray(cb); | ||
return this; | ||
}; | ||
Cursor.prototype.batchSize = function (n, cb) { | ||
this._opts.batchSize = n | ||
if (cb) return this.toArray(cb) | ||
return this | ||
} | ||
Cursor.prototype.sort = function(sortObj, cb) { | ||
this._opts.sort = sortObj; | ||
if (cb) return this.toArray(cb); | ||
return this; | ||
}; | ||
Cursor.prototype.sort = function (sortObj, cb) { | ||
this._opts.sort = sortObj | ||
if (cb) return this.toArray(cb) | ||
return this | ||
} | ||
Cursor.prototype.count = function(cb) { | ||
var self = this; | ||
var onserver = this._opts.onserver; | ||
var dbname = this._opts.fullCollectionName.split('.')[0]; | ||
var colname = this._opts.fullCollectionName.split('.')[1]; | ||
onserver(function(err, server) { | ||
if (err) return cb(err); | ||
server.command(dbname + '.$cmd', {count: colname, query: self._opts.query}, function(err, result) { | ||
if (err) return cb(err); | ||
cb(null, result.result.n); | ||
}); | ||
}); | ||
}; | ||
Cursor.prototype.count = function (cb) { | ||
var self = this | ||
var onserver = this._opts.onserver | ||
var dbname = this._opts.fullCollectionName.split('.')[0] | ||
var colname = this._opts.fullCollectionName.split('.')[1] | ||
onserver(function (err, server) { | ||
if (err) return cb(err) | ||
server.command(dbname + '.$cmd', {count: colname, query: self._opts.query}, function (err, result) { | ||
if (err) return cb(err) | ||
cb(null, result.result.n) | ||
}) | ||
}) | ||
} | ||
Cursor.prototype.size = function(cb) { | ||
var self = this; | ||
Cursor.prototype.size = function (cb) { | ||
var self = this | ||
var onserver = this._opts.onserver; | ||
var dbname = this._opts.fullCollectionName.split('.')[0]; | ||
var colname = this._opts.fullCollectionName.split('.')[1]; | ||
onserver(function(err, server) { | ||
if (err) return cb(err); | ||
var onserver = this._opts.onserver | ||
var dbname = this._opts.fullCollectionName.split('.')[0] | ||
var colname = this._opts.fullCollectionName.split('.')[1] | ||
onserver(function (err, server) { | ||
if (err) return cb(err) | ||
var cmd = {count: colname}; | ||
if (self._opts.query) cmd.query = self._opts.query; | ||
if (self._opts.limit) cmd.limit = self._opts.limit; | ||
if (self._opts.skip) cmd.skip = self._opts.skip; | ||
server.command(dbname + '.$cmd', cmd, function(err, result) { | ||
if (err) return cb(err); | ||
cb(null, result.result.n); | ||
}); | ||
}); | ||
}; | ||
var cmd = {count: colname} | ||
if (self._opts.query) cmd.query = self._opts.query | ||
if (self._opts.limit) cmd.limit = self._opts.limit | ||
if (self._opts.skip) cmd.skip = self._opts.skip | ||
server.command(dbname + '.$cmd', cmd, function (err, result) { | ||
if (err) return cb(err) | ||
cb(null, result.result.n) | ||
}) | ||
}) | ||
} | ||
Cursor.prototype.explain = function(cb) { | ||
var q = this._opts.query || {}; | ||
this._opts.query = {$query: q, $explain: 1}; | ||
this.next(cb); | ||
}; | ||
Cursor.prototype.explain = function (cb) { | ||
var q = this._opts.query || {} | ||
this._opts.query = {$query: q, $explain: 1} | ||
this.next(cb) | ||
} | ||
Cursor.prototype.destroy = function() { | ||
var self = this; | ||
this._get(function(err, cursor) { | ||
if (err) return self.emit('error', err); | ||
if (cursor.kill) cursor.kill(); | ||
}); | ||
}; | ||
Cursor.prototype.destroy = function () { | ||
var self = this | ||
this._get(function (err, cursor) { | ||
if (err) return self.emit('error', err) | ||
if (cursor.kill) cursor.kill() | ||
}) | ||
} | ||
Cursor.prototype._read = function() { | ||
var self = this; | ||
this.next(function(err, data) { | ||
if (err) return self.emit('error', err); | ||
self.push(data); | ||
}); | ||
}; | ||
Cursor.prototype._read = function () { | ||
var self = this | ||
this.next(function (err, data) { | ||
if (err) return self.emit('error', err) | ||
self.push(data) | ||
}) | ||
} | ||
module.exports = Cursor; | ||
module.exports = Cursor |
@@ -1,125 +0,126 @@ | ||
var Collection = require('./collection'); | ||
var bson = require('mongodb-core').BSON; | ||
var xtend = require('xtend'); | ||
var Collection = require('./collection') | ||
var bson = require('mongodb-core').BSON | ||
var xtend = require('xtend') | ||
var noop = function() {}; | ||
var noop = function () {} | ||
var Database = function(opts, onserver) { | ||
this._getServer = onserver; | ||
this._dbname = opts.name; | ||
var self = this; | ||
this.ObjectId = bson.ObjectId; | ||
opts.cols = opts.cols || []; | ||
opts.cols.forEach(function(colName) { | ||
self[colName] = self.collection(colName); | ||
var Database = function (opts, onserver) { | ||
this._getServer = onserver | ||
this._dbname = opts.name | ||
var parts = colName.split('.'); | ||
var self = this | ||
this.ObjectId = bson.ObjectId | ||
opts.cols = opts.cols || [] | ||
opts.cols.forEach(function (colName) { | ||
self[colName] = self.collection(colName) | ||
var last = parts.pop(); | ||
var parent = parts.reduce(function(parent, prefix) { | ||
return parent[prefix] = parent[prefix] || {}; | ||
}, self); | ||
var parts = colName.split('.') | ||
parent[last] = self.collection(colName); | ||
}); | ||
}; | ||
var last = parts.pop() | ||
var parent = parts.reduce(function (parent, prefix) { | ||
parent[prefix] = parent[prefix] || {} | ||
return parent[prefix] | ||
}, self) | ||
Database.prototype.collection = function(colName) { | ||
return new Collection({name: colName, dbname: this._dbname}, this._getServer); | ||
}; | ||
parent[last] = self.collection(colName) | ||
}) | ||
} | ||
Database.prototype.close = function(cb) { | ||
cb = cb || noop; | ||
this._getServer(function(err, server) { | ||
if (err) return cb(err); | ||
server.destroy(); | ||
cb(); | ||
}); | ||
}; | ||
Database.prototype.collection = function (colName) { | ||
return new Collection({name: colName, dbname: this._dbname}, this._getServer) | ||
} | ||
Database.prototype.runCommand = function(opts, cb) { | ||
cb = cb || noop; | ||
Database.prototype.close = function (cb) { | ||
cb = cb || noop | ||
this._getServer(function (err, server) { | ||
if (err) return cb(err) | ||
server.destroy() | ||
cb() | ||
}) | ||
} | ||
Database.prototype.runCommand = function (opts, cb) { | ||
cb = cb || noop | ||
if (typeof opts === 'string') { | ||
var tmp = opts; | ||
opts = {}; | ||
opts[tmp] = 1; | ||
var tmp = opts | ||
opts = {} | ||
opts[tmp] = 1 | ||
} | ||
var self = this; | ||
this._getServer(function(err, server) { | ||
if (err) return cb(err); | ||
server.command(self._dbname + '.$cmd', opts, function(err, result) { | ||
if (err) return cb(err); | ||
cb(null, result.result); | ||
}); | ||
}); | ||
}; | ||
var self = this | ||
this._getServer(function (err, server) { | ||
if (err) return cb(err) | ||
server.command(self._dbname + '.$cmd', opts, function (err, result) { | ||
if (err) return cb(err) | ||
cb(null, result.result) | ||
}) | ||
}) | ||
} | ||
Database.prototype.getCollectionNames = function(cb) { | ||
this.collection('system.namespaces').find({name:/^((?!\$).)*$/}, function(err, cols) { | ||
if (err) return cb(err); | ||
cb(null, cols.map(function(col) { | ||
return col.name.split('.').splice(1).join('.'); | ||
})); | ||
}); | ||
}; | ||
Database.prototype.getCollectionNames = function (cb) { | ||
this.collection('system.namespaces').find({name: /^((?!\$).)*$/}, function (err, cols) { | ||
if (err) return cb(err) | ||
cb(null, cols.map(function (col) { | ||
return col.name.split('.').splice(1).join('.') | ||
})) | ||
}) | ||
} | ||
Database.prototype.createCollection = function(name, opts, cb) { | ||
if (typeof opts === 'function') return this.createCollection(name, {}, opts); | ||
Database.prototype.createCollection = function (name, opts, cb) { | ||
if (typeof opts === 'function') return this.createCollection(name, {}, opts) | ||
var cmd = {create: name}; | ||
Object.keys(opts).forEach(function(opt) { | ||
cmd[opt] = opts[opt]; | ||
}); | ||
this.runCommand(cmd, cb); | ||
}; | ||
var cmd = {create: name} | ||
Object.keys(opts).forEach(function (opt) { | ||
cmd[opt] = opts[opt] | ||
}) | ||
this.runCommand(cmd, cb) | ||
} | ||
Database.prototype.stats = function(scale, cb) { | ||
if (typeof scale === 'function') return this.stats(1, scale); | ||
this.runCommand({dbStats:1, scale: scale}, cb); | ||
}; | ||
Database.prototype.stats = function (scale, cb) { | ||
if (typeof scale === 'function') return this.stats(1, scale) | ||
this.runCommand({dbStats: 1, scale: scale}, cb) | ||
} | ||
Database.prototype.dropDatabase = function(cb) { | ||
this.runCommand('dropDatabase', cb); | ||
}; | ||
Database.prototype.dropDatabase = function (cb) { | ||
this.runCommand('dropDatabase', cb) | ||
} | ||
Database.prototype.createUser = function(usr, cb) { | ||
var cmd = xtend({createUser: usr.user}, usr); | ||
delete cmd.user; | ||
this.runCommand(cmd, cb); | ||
}; | ||
Database.prototype.addUser = Database.prototype.createUser; | ||
Database.prototype.createUser = function (usr, cb) { | ||
var cmd = xtend({createUser: usr.user}, usr) | ||
delete cmd.user | ||
this.runCommand(cmd, cb) | ||
} | ||
Database.prototype.addUser = Database.prototype.createUser | ||
Database.prototype.dropUser = function(username, cb) { | ||
this.runCommand({dropUser: username}, cb); | ||
}; | ||
Database.prototype.removeUser = Database.prototype.dropUser; | ||
Database.prototype.dropUser = function (username, cb) { | ||
this.runCommand({dropUser: username}, cb) | ||
} | ||
Database.prototype.removeUser = Database.prototype.dropUser | ||
Database.prototype.eval = function(fn) { | ||
var cb = arguments[arguments.length - 1]; | ||
Database.prototype.eval = function (fn) { | ||
var cb = arguments[arguments.length - 1] | ||
this.runCommand({ | ||
eval: fn.toString(), | ||
args: Array.prototype.slice.call(arguments, 1, arguments.length - 1) | ||
}, function(err, res) { | ||
if (err) return cb(err); | ||
cb(null, res.retval); | ||
}); | ||
}; | ||
}, function (err, res) { | ||
if (err) return cb(err) | ||
cb(null, res.retval) | ||
}) | ||
} | ||
Database.prototype.getLastErrorObj = function(cb) { | ||
this.runCommand('getLastError', cb); | ||
}; | ||
Database.prototype.getLastErrorObj = function (cb) { | ||
this.runCommand('getLastError', cb) | ||
} | ||
Database.prototype.getLastError = function(cb) { | ||
this.runCommand('getLastError', function(err, res) { | ||
if (err) return cb(err); | ||
cb(null, res.err); | ||
}); | ||
}; | ||
Database.prototype.getLastError = function (cb) { | ||
this.runCommand('getLastError', function (err, res) { | ||
if (err) return cb(err) | ||
cb(null, res.err) | ||
}) | ||
} | ||
Database.prototype.toString = function() { | ||
return this._dbname; | ||
}; | ||
Database.prototype.toString = function () { | ||
return this._dbname | ||
} | ||
module.exports = Database; | ||
module.exports = Database |
@@ -1,7 +0,7 @@ | ||
var once = require('once'); | ||
var parse = require('parse-mongo-url'); | ||
var mongodb = require('mongodb-core'); | ||
var once = require('once') | ||
var parse = require('parse-mongo-url') | ||
var mongodb = require('mongodb-core') | ||
var Server = mongodb.Server; | ||
var ReplSet = mongodb.ReplSet; | ||
var Server = mongodb.Server | ||
var ReplSet = mongodb.ReplSet | ||
@@ -11,52 +11,52 @@ var authMechanisms = { | ||
ScramSHA1: mongodb.ScramSHA1 | ||
}; | ||
} | ||
module.exports = function(connString, options, cb) { | ||
cb = once(cb); | ||
var config = parse(connString); | ||
var srv; | ||
module.exports = function (connString, options, cb) { | ||
cb = once(cb) | ||
var config = parse(connString) | ||
var srv | ||
var authMechanism = 'MongoCR'; | ||
var authMechanism = 'MongoCR' | ||
if (options && options.authMechanism) { | ||
if (!authMechanisms[options.authMechanism]) { | ||
return cb(new Error(options.authMechanism + ' is not a supported authentication mechanism')); | ||
return cb(new Error(options.authMechanism + ' is not a supported authentication mechanism')) | ||
} | ||
authMechanism = options.authMechanism; | ||
authMechanism = options.authMechanism | ||
} | ||
if (config.servers.length === 1) { | ||
var opts = config.server_options; | ||
opts.host = config.servers[0].host || 'localhost'; | ||
opts.port = config.servers[0].port || 27017; | ||
opts.reconnect = true; | ||
opts.reconnectInterval = 50; | ||
srv = new Server(opts); | ||
var opts = config.server_options | ||
opts.host = config.servers[0].host || 'localhost' | ||
opts.port = config.servers[0].port || 27017 | ||
opts.reconnect = true | ||
opts.reconnectInterval = 50 | ||
srv = new Server(opts) | ||
} else { | ||
var rsopts = config.rs_options; | ||
rsopts.setName = rsopts.rs_name; | ||
rsopts.reconnect = true; | ||
rsopts.reconnectInterval = 50; | ||
srv = new ReplSet(config.servers, rsopts); | ||
var rsopts = config.rs_options | ||
rsopts.setName = rsopts.rs_name | ||
rsopts.reconnect = true | ||
rsopts.reconnectInterval = 50 | ||
srv = new ReplSet(config.servers, rsopts) | ||
} | ||
if (config.auth) { | ||
srv.addAuthProvider(authMechanism, new authMechanisms[authMechanism]()); | ||
srv.on('connect', function(server) { | ||
server.auth(authMechanism, config.dbName, config.auth.user, config.auth.password, function(err, r) { | ||
if (err) return cb(err); | ||
cb(null, r); | ||
}); | ||
}); | ||
srv.addAuthProvider(authMechanism, new authMechanisms[authMechanism]()) | ||
srv.on('connect', function (server) { | ||
server.auth(authMechanism, config.dbName, config.auth.user, config.auth.password, function (err, r) { | ||
if (err) return cb(err) | ||
cb(null, r) | ||
}) | ||
}) | ||
} else { | ||
srv.on('connect', function(server) { | ||
cb(null, server); | ||
}); | ||
srv.on('connect', function (server) { | ||
cb(null, server) | ||
}) | ||
} | ||
srv.on('error', function(err) { | ||
cb(err); | ||
}); | ||
srv.on('error', function (err) { | ||
cb(err) | ||
}) | ||
srv.connect(); | ||
}; | ||
srv.connect() | ||
} |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"repository": "git://github.com/mafintosh/mongojs.git", | ||
@@ -27,3 +27,4 @@ "author": "Mathias Buus Madsen <mathiasbuus@gmail.com>", | ||
"Benedikt Arnold", | ||
"Kevin McTigue" | ||
"Kevin McTigue", | ||
"Thomas Watson Steen <w@tson.dk> https://twitter.com/wa7son" | ||
], | ||
@@ -36,3 +37,3 @@ "dependencies": { | ||
"pump": "^1.0.0", | ||
"readable-stream": "^1.0.33", | ||
"readable-stream": "^2.0.2", | ||
"thunky": "^0.1.0", | ||
@@ -43,3 +44,3 @@ "to-mongodb-core": "^2.0.0", | ||
"scripts": { | ||
"test": "tape test/test-*.js; echo \"Harmony tests\"; node --harmony --harmony-proxies node_modules/tape/bin/tape test/test-*.js", | ||
"test": "standard && tape test/test-*.js && node --harmony --harmony-proxies node_modules/tape/bin/tape test/test-*.js", | ||
"cover": "node --harmony --harmony-proxies node_modules/istanbul/lib/cli.js cover node_modules/tape/bin/tape test/test-*.js --report html", | ||
@@ -52,8 +53,9 @@ "geotag": "geopkg" | ||
"istanbul": "^0.3.17", | ||
"standard": "^4.5.4", | ||
"tape": "^3.4.0" | ||
}, | ||
"coordinates": [ | ||
48.22872539999999, | ||
16.3954159 | ||
55.64657330000001, | ||
12.5492344 | ||
] | ||
} |
@@ -9,3 +9,3 @@ # mongojs | ||
[![Build Status](https://travis-ci.org/mafintosh/mongojs.svg?branch=master)](https://travis-ci.org/mafintosh/mongojs) | ||
[![Tips](https://img.shields.io/gratipay/mafintosh.svg)](https://gratipay.com/mafintosh) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) | ||
@@ -12,0 +12,0 @@ ## Usage |
@@ -1,10 +0,10 @@ | ||
var mongojs = require('../index'); | ||
var db = mongojs('localhost', ['test']); | ||
var mongojs = require('../index') | ||
var db = mongojs('localhost', ['test']) | ||
db.test.findOne(function() { | ||
throw new Error('I should crash the program'); | ||
}); | ||
db.test.findOne(function () { | ||
throw new Error('I should crash the program') | ||
}) | ||
setTimeout(function() { | ||
throw new Error('timeout'); | ||
}, 5000); | ||
setTimeout(function () { | ||
throw new Error('timeout') | ||
}, 5000) |
@@ -1,28 +0,28 @@ | ||
var test = require('./tape'); | ||
var mongojs = require('../index'); | ||
var db = mongojs('test', ['a']); | ||
var test = require('./tape') | ||
var mongojs = require('../index') | ||
var db = mongojs('test', ['a']) | ||
module.exports = function(testName, docs, testFn) { | ||
test(testName, function(t) { | ||
db.a.remove(function(err) { | ||
t.ok(!err); | ||
module.exports = function (testName, docs, testFn) { | ||
test(testName, function (t) { | ||
db.a.remove(function (err) { | ||
t.error(err) | ||
db.a.insert(docs, function(err) { | ||
t.ok(!err); | ||
testFn(db, t, function() { | ||
db.a.remove(function(err) { | ||
t.ok(!err); | ||
t.end(); | ||
}); | ||
}); | ||
}); | ||
db.a.insert(docs, function (err) { | ||
t.error(err) | ||
testFn(db, t, function () { | ||
db.a.remove(function (err) { | ||
t.error(err) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}); | ||
}); | ||
}; | ||
}) | ||
}) | ||
} | ||
module.exports.skip = function(testName) { | ||
test.skip(testName, function(t) { | ||
t.end(); | ||
}); | ||
}; | ||
module.exports.skip = function (testName) { | ||
test.skip(testName, function (t) { | ||
t.end() | ||
}) | ||
} |
@@ -1,11 +0,12 @@ | ||
var test = require('tape'); | ||
var test = require('tape') | ||
if (typeof setImmediate === 'undefined') setImmediate = process.nextTick; | ||
setImmediate(function() { | ||
test('end', function(t) { | ||
t.end(); | ||
process.exit(); | ||
}); | ||
}); | ||
var wait = global.setImmediate || process.nextTick | ||
module.exports = test; | ||
wait(function () { | ||
test('end', function (t) { | ||
t.end() | ||
process.exit() | ||
}) | ||
}) | ||
module.exports = test |
@@ -1,41 +0,41 @@ | ||
var insert = require('./insert'); | ||
var concat = require('concat-stream'); | ||
var insert = require('./insert') | ||
var concat = require('concat-stream') | ||
insert('aggregate', [{ | ||
name:'Squirtle', type:'water' | ||
name: 'Squirtle', type: 'water' | ||
}, { | ||
name:'Starmie' , type:'water' | ||
name: 'Starmie', type: 'water' | ||
}, { | ||
name:'Charmander' , type:'fire' | ||
name: 'Charmander', type: 'fire' | ||
}, { | ||
name:'Lapras' , type:'water' | ||
}], function(db, t, done) { | ||
db.a.aggregate([{$group: {_id: '$type'}}, {$project: { _id: 0, foo: "$_id" }}], function(err, types) { | ||
console.log(err, types); | ||
var arr = types.map(function(x) {return x.foo}); | ||
console.log('arr', arr); | ||
t.equal(types.length, 2); | ||
console.log('here'); | ||
t.notEqual(arr.indexOf('fire'), -1); | ||
console.log('there'); | ||
t.notEqual(arr.indexOf('water'), -1); | ||
console.log('where'); | ||
name: 'Lapras', type: 'water' | ||
}], function (db, t, done) { | ||
db.a.aggregate([{$group: {_id: '$type'}}, {$project: { _id: 0, foo: '$_id' }}], function (err, types) { | ||
console.log(err, types) | ||
var arr = types.map(function (x) {return x.foo}) | ||
console.log('arr', arr) | ||
t.equal(types.length, 2) | ||
console.log('here') | ||
t.notEqual(arr.indexOf('fire'), -1) | ||
console.log('there') | ||
t.notEqual(arr.indexOf('water'), -1) | ||
console.log('where') | ||
// test as a stream | ||
var strm = db.a.aggregate([{$group: {_id: '$type'}}, {$project: {_id: 0, foo: "$_id"}}]) | ||
strm.pipe(concat(function(types) { | ||
var arr = types.map(function(x) {return x.foo}); | ||
t.equal(types.length, 2); | ||
t.notEqual(arr.indexOf('fire'), -1); | ||
t.notEqual(arr.indexOf('water'), -1); | ||
t.end(); | ||
})); | ||
strm.on('error', function(err) { | ||
var strm = db.a.aggregate([{$group: {_id: '$type'}}, {$project: {_id: 0, foo: '$_id'}}]) | ||
strm.pipe(concat(function (types) { | ||
var arr = types.map(function (x) {return x.foo}) | ||
t.equal(types.length, 2) | ||
t.notEqual(arr.indexOf('fire'), -1) | ||
t.notEqual(arr.indexOf('water'), -1) | ||
t.end() | ||
})) | ||
strm.on('error', function (err) { | ||
// Aggregation cursors are only supported on mongodb 2.6+ | ||
// this shouldn't fail the tests for other versions of mongodb | ||
if (err.message === 'unrecognized field "cursor') t.ok(1); | ||
else t.fail(err); | ||
t.end(); | ||
}); | ||
}); | ||
}); | ||
if (err.message === 'unrecognized field "cursor') t.ok(1) | ||
else t.fail(err) | ||
t.end() | ||
}) | ||
}) | ||
}) |
@@ -1,37 +0,37 @@ | ||
var insert = require('./insert'); | ||
var concat = require('concat-stream'); | ||
var insert = require('./insert') | ||
var concat = require('concat-stream') | ||
insert('aggregate', [{ | ||
name:'Squirtle', type:'water' | ||
name: 'Squirtle', type: 'water' | ||
}, { | ||
name:'Starmie' , type:'water' | ||
name: 'Starmie', type: 'water' | ||
}, { | ||
name:'Charmander' , type:'fire' | ||
name: 'Charmander', type: 'fire' | ||
}, { | ||
name:'Lapras' , type:'water' | ||
}], function(db, t, done) { | ||
db.a.aggregate({$group: {_id: '$type'}}, function(err, types) { | ||
console.log(err, types); | ||
var arr = types.map(function(x) {return x._id}); | ||
t.equal(types.length, 2); | ||
t.notEqual(arr.indexOf('fire'), -1); | ||
t.notEqual(arr.indexOf('water'), -1); | ||
name: 'Lapras', type: 'water' | ||
}], function (db, t, done) { | ||
db.a.aggregate({$group: {_id: '$type'}}, function (err, types) { | ||
console.log(err, types) | ||
var arr = types.map(function (x) {return x._id}) | ||
t.equal(types.length, 2) | ||
t.notEqual(arr.indexOf('fire'), -1) | ||
t.notEqual(arr.indexOf('water'), -1) | ||
// test as a stream | ||
var strm = db.a.aggregate({$group: {_id: '$type'}}) | ||
strm.pipe(concat(function(types) { | ||
var arr = types.map(function(x) {return x._id}); | ||
t.equal(types.length, 2); | ||
t.notEqual(arr.indexOf('fire'), -1); | ||
t.notEqual(arr.indexOf('water'), -1); | ||
t.end(); | ||
})); | ||
strm.on('error', function(err) { | ||
strm.pipe(concat(function (types) { | ||
var arr = types.map(function (x) {return x._id}) | ||
t.equal(types.length, 2) | ||
t.notEqual(arr.indexOf('fire'), -1) | ||
t.notEqual(arr.indexOf('water'), -1) | ||
t.end() | ||
})) | ||
strm.on('error', function (err) { | ||
// Aggregation cursors are only supported on mongodb 2.6+ | ||
// this shouldn't fail the tests for other versions of mongodb | ||
if (err.message === 'unrecognized field "cursor') t.ok(1); | ||
else t.fail(err); | ||
t.end(); | ||
}); | ||
}); | ||
}); | ||
if (err.message === 'unrecognized field "cursor') t.ok(1) | ||
else t.fail(err) | ||
t.end() | ||
}) | ||
}) | ||
}) |
@@ -1,49 +0,51 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('bulk', [{ | ||
name:'Squirtle', type:'water' | ||
name: 'Squirtle', type: 'water' | ||
}, { | ||
name:'Starmie' , type:'water' | ||
name: 'Starmie', type: 'water' | ||
}, { | ||
name:'Lapras' , type:'water' | ||
name: 'Lapras', type: 'water' | ||
}, { | ||
name: 'Charmander', type: 'fire' | ||
}], function(db, t, done) { | ||
db.runCommand('serverStatus', function(err, resp) { | ||
if (err) return t.fail(); | ||
if (parseFloat(resp.version) < 2.6) return t.end(); | ||
}], function (db, t, done) { | ||
db.runCommand('serverStatus', function (err, resp) { | ||
t.error(err) | ||
if (parseFloat(resp.version) < 2.6) return t.end() | ||
var bulk = db.a.initializeOrderedBulkOp(); | ||
bulk.find({type: 'water'}).update({$set: {level: 1}}); | ||
bulk.find({type: 'water'}).update({$inc: {level: 2}}); | ||
bulk.insert({name: 'Spearow', type: 'flying'}); | ||
bulk.insert({name: 'Pidgeotto', type: 'flying'}); | ||
bulk.insert({name: 'Charmeleon', type: 'fire'}); | ||
bulk.find({type: 'flying'}).removeOne(); | ||
bulk.find({type: 'fire'}).remove(); | ||
bulk.find({type: 'water'}).updateOne({$set: {hp: 100}}); | ||
var bulk = db.a.initializeOrderedBulkOp() | ||
bulk.find({type: 'water'}).update({$set: {level: 1}}) | ||
bulk.find({type: 'water'}).update({$inc: {level: 2}}) | ||
bulk.insert({name: 'Spearow', type: 'flying'}) | ||
bulk.insert({name: 'Pidgeotto', type: 'flying'}) | ||
bulk.insert({name: 'Charmeleon', type: 'fire'}) | ||
bulk.find({type: 'flying'}).removeOne() | ||
bulk.find({type: 'fire'}).remove() | ||
bulk.find({type: 'water'}).updateOne({$set: {hp: 100}}) | ||
bulk.find({name: 'Squirtle'}).upsert().updateOne({$set : {name: 'Wartortle', type: 'water'}}); | ||
bulk.find({name: 'Bulbasaur'}).upsert().updateOne({$setOnInsert: {name: "Bulbasaur"}, $set: {type: 'grass', level: 1}}); | ||
bulk.find({name: 'Squirtle'}).upsert().updateOne({$set: {name: 'Wartortle', type: 'water'}}) | ||
bulk.find({name: 'Bulbasaur'}).upsert().updateOne({$setOnInsert: {name: 'Bulbasaur'}, $set: {type: 'grass', level: 1}}) | ||
bulk.execute(function(err, res) { | ||
t.ok(res.ok); | ||
db.a.find(function(err, res) { | ||
t.equal(res[0].name, 'Wartortle'); | ||
t.equal(res[1].name, 'Starmie'); | ||
t.equal(res[2].name, 'Lapras'); | ||
t.equal(res[3].name, 'Pidgeotto'); | ||
t.equal(res[4].name, 'Bulbasaur'); | ||
t.equal(res[4].type, 'grass'); | ||
bulk.execute(function (err, res) { | ||
t.error(err) | ||
t.ok(res.ok) | ||
db.a.find(function (err, res) { | ||
t.error(err) | ||
t.equal(res[0].name, 'Wartortle') | ||
t.equal(res[1].name, 'Starmie') | ||
t.equal(res[2].name, 'Lapras') | ||
t.equal(res[3].name, 'Pidgeotto') | ||
t.equal(res[4].name, 'Bulbasaur') | ||
t.equal(res[4].type, 'grass') | ||
t.equal(res[0].level, 3); | ||
t.equal(res[1].level, 3); | ||
t.equal(res[2].level, 3); | ||
t.equal(res[4].level, 1); | ||
t.equal(res[0].level, 3) | ||
t.equal(res[1].level, 3) | ||
t.equal(res[2].level, 3) | ||
t.equal(res[4].level, 1) | ||
t.equal(res[0].hp, 100); | ||
t.end(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
t.equal(res[0].hp, 100) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,16 +0,18 @@ | ||
var test = require('./tape'); | ||
var mongojs = require('../index'); | ||
var db = mongojs('test', ['b.c']); | ||
var test = require('./tape') | ||
var mongojs = require('../index') | ||
var db = mongojs('test', ['b.c']) | ||
test('chained-collection-names', function(t) { | ||
db.b.c.remove(function() { | ||
db.b.c.save({hello: "world"}, function(err, rs) { | ||
db.b.c.find(function(err, docs) { | ||
t.equal(docs[0].hello, "world"); | ||
db.b.c.remove(function() { | ||
t.end(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
test('chained-collection-names', function (t) { | ||
db.b.c.remove(function () { | ||
db.b.c.save({hello: 'world'}, function (err, rs) { | ||
t.error(err) | ||
db.b.c.find(function (err, docs) { | ||
t.error(err) | ||
t.equal(docs[0].hello, 'world') | ||
db.b.c.remove(function () { | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,10 +0,10 @@ | ||
var test = require('./tape'); | ||
var cp = require('child_process'); | ||
var test = require('./tape') | ||
var cp = require('child_process') | ||
test('crash', function(t) { | ||
var proc = cp.spawn('node', ['./crash.js']); | ||
proc.on('exit', function(code) { | ||
t.notEqual(code, 0); | ||
t.end(); | ||
}); | ||
}); | ||
test('crash', function (t) { | ||
var proc = cp.spawn('node', ['./crash.js']) | ||
proc.on('exit', function (code) { | ||
t.notEqual(code, 0) | ||
t.end() | ||
}) | ||
}) |
@@ -1,15 +0,15 @@ | ||
var test = require('./tape'); | ||
var mongojs = require('../index'); | ||
var db = mongojs('test', ['test123']); | ||
var test = require('./tape') | ||
var mongojs = require('../index') | ||
var db = mongojs('test', ['test123']) | ||
test('createCollection', function(t) { | ||
db.test123.drop(function() { | ||
db.createCollection('test123', function(err) { | ||
t.ok(!err); | ||
db.createCollection('test123', function(err) { | ||
t.ok(err); | ||
t.end(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
test('createCollection', function (t) { | ||
db.test123.drop(function () { | ||
db.createCollection('test123', function (err) { | ||
t.error(err) | ||
db.createCollection('test123', function (err) { | ||
t.ok(err) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,19 +0,21 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('remove', [{ | ||
name:'Squirtle', type:'water' | ||
name: 'Squirtle', type: 'water' | ||
}, { | ||
name:'Charmander' , type:'fire' | ||
name: 'Charmander', type: 'fire' | ||
}, { | ||
name:'Starmie' , type:'water' | ||
name: 'Starmie', type: 'water' | ||
}, { | ||
name:'Lapras' , type:'water' | ||
}], function(db, t, done) { | ||
db.a.find().count(function(err, cnt) { | ||
t.equal(cnt, 4); | ||
db.a.find({type: 'water'}).count(function(err, cnt2) { | ||
t.equal(cnt2, 3); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
name: 'Lapras', type: 'water' | ||
}], function (db, t, done) { | ||
db.a.find().count(function (err, cnt) { | ||
t.error(err) | ||
t.equal(cnt, 4) | ||
db.a.find({type: 'water'}).count(function (err, cnt2) { | ||
t.error(err) | ||
t.equal(cnt2, 3) | ||
done() | ||
}) | ||
}) | ||
}) |
@@ -1,18 +0,18 @@ | ||
var test = require('./tape'); | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('cursor.explain', [{ | ||
hello:'world1' | ||
},{ | ||
hello:'world2' | ||
}], function(db, t, done) { | ||
var cursor = db.a.find(); | ||
cursor.explain(function(err, result) { | ||
hello: 'world1' | ||
}, { | ||
hello: 'world2' | ||
}], function (db, t, done) { | ||
var cursor = db.a.find() | ||
cursor.explain(function (err, result) { | ||
t.error(err) | ||
if (result.executionStats) { | ||
t.equal(result.executionStats.totalDocsExamined, 2); | ||
t.equal(result.executionStats.totalDocsExamined, 2) | ||
} else { | ||
t.equal(result.nscannedObjects, 2); | ||
t.equal(result.nscannedObjects, 2) | ||
} | ||
done(); | ||
}); | ||
}); | ||
done() | ||
}) | ||
}) |
@@ -1,17 +0,18 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
var pokemons = [{ | ||
name:'Squirtle', type:'water' | ||
name: 'Squirtle', type: 'water' | ||
}, { | ||
name:'Starmie' , type:'water' | ||
name: 'Starmie', type: 'water' | ||
}, { | ||
name:'Lapras' , type:'water' | ||
}]; | ||
name: 'Lapras', type: 'water' | ||
}] | ||
insert('cursor foreach', pokemons, function(db, t, done) { | ||
var i = 0; | ||
db.a.find().forEach(function(err, pkm) { | ||
if (++i === 4) return t.end(); | ||
t.equal(pkm.name, pokemons[i - 1].name); | ||
}); | ||
}); | ||
insert('cursor foreach', pokemons, function (db, t, done) { | ||
var i = 0 | ||
db.a.find().forEach(function (err, pkm) { | ||
t.error(err) | ||
if (++i === 4) return t.end() | ||
t.equal(pkm.name, pokemons[i - 1].name) | ||
}) | ||
}) |
@@ -1,17 +0,18 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('cursor.map', [{ | ||
hello:'world1' | ||
},{ | ||
hello:'world2' | ||
}], function(db, t, done) { | ||
var cursor = db.a.find(); | ||
cursor.map(function(x) { | ||
return x.hello | ||
}, function(err, res) { | ||
t.equal(res[0], 'world1'); | ||
t.equal(res[1], 'world2'); | ||
done(); | ||
}); | ||
}); | ||
hello: 'world1' | ||
}, { | ||
hello: 'world2' | ||
}], function (db, t, done) { | ||
var cursor = db.a.find() | ||
cursor.map(function (x) { | ||
return x.hello | ||
}, function (err, res) { | ||
t.error(err) | ||
t.equal(res[0], 'world1') | ||
t.equal(res[1], 'world2') | ||
done() | ||
}) | ||
}) | ||
@@ -1,22 +0,25 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('remove', [{ | ||
name:'Squirtle', type:'water' | ||
name: 'Squirtle', type: 'water' | ||
}, { | ||
name:'Starmie' , type:'water' | ||
name: 'Starmie', type: 'water' | ||
}, { | ||
name:'Lapras' , type:'water' | ||
}], function(db, t, done) { | ||
var cursor = db.a.find().sort({name: 1}); | ||
cursor.next(function(err, obj1) { | ||
name: 'Lapras', type: 'water' | ||
}], function (db, t, done) { | ||
var cursor = db.a.find().sort({name: 1}) | ||
cursor.next(function (err, obj1) { | ||
t.error(err) | ||
t.equal(obj1.name, 'Lapras') | ||
cursor.next(function(err, obj2) { | ||
cursor.next(function (err, obj2) { | ||
t.error(err) | ||
t.equal(obj2.name, 'Squirtle') | ||
cursor.rewind(); | ||
cursor.next(function(err) { | ||
cursor.rewind() | ||
cursor.next(function (err) { | ||
t.error(err) | ||
t.equal(obj1.name, 'Lapras') | ||
t.end(); | ||
}); | ||
t.end() | ||
}) | ||
}) | ||
}); | ||
}); | ||
}) | ||
}) |
@@ -1,20 +0,22 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('cursor.size', [{ | ||
hello:'world1' | ||
},{ | ||
hello:'world2' | ||
},{ | ||
hello:'world3' | ||
},{ | ||
hello:'world4' | ||
}], function(db, t, done) { | ||
db.a.find().skip(1).size(function(err, thesize) { | ||
t.equal(thesize, 3); | ||
db.a.find().limit(2).size(function(err, theothersize) { | ||
t.equal(theothersize, 2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
hello: 'world1' | ||
}, { | ||
hello: 'world2' | ||
}, { | ||
hello: 'world3' | ||
}, { | ||
hello: 'world4' | ||
}], function (db, t, done) { | ||
db.a.find().skip(1).size(function (err, thesize) { | ||
t.error(err) | ||
t.equal(thesize, 3) | ||
db.a.find().limit(2).size(function (err, theothersize) { | ||
t.error(err) | ||
t.equal(theothersize, 2) | ||
done() | ||
}) | ||
}) | ||
}) | ||
@@ -1,23 +0,22 @@ | ||
var test = require('./tape'); | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('distinct', [{ | ||
goodbye:'world', | ||
hello:'space' | ||
goodbye: 'world', | ||
hello: 'space' | ||
}, { | ||
goodbye:'world', | ||
hello:'space' | ||
goodbye: 'world', | ||
hello: 'space' | ||
}, { | ||
goodbye:'earth', | ||
hello:'space' | ||
goodbye: 'earth', | ||
hello: 'space' | ||
}, { | ||
goodbye:'world', | ||
hello:'space' | ||
}], function(db, t, done) { | ||
db.a.distinct('goodbye',{hello:'space'},function(err, docs) { | ||
t.ok(!err); | ||
t.equal(docs.length, 2); | ||
t.equal(docs[0], 'world'); | ||
done(); | ||
}); | ||
}); | ||
goodbye: 'world', | ||
hello: 'space' | ||
}], function (db, t, done) { | ||
db.a.distinct('goodbye', {hello: 'space'}, function (err, docs) { | ||
t.error(err) | ||
t.equal(docs.length, 2) | ||
t.equal(docs[0], 'world') | ||
done() | ||
}) | ||
}) |
@@ -1,34 +0,34 @@ | ||
var insert = require('./insert'); | ||
var concat = require('concat-stream'); | ||
var insert = require('./insert') | ||
insert('drop indexes', [{ | ||
name:'Squirtle', type:'water' | ||
name: 'Squirtle', type: 'water' | ||
}, { | ||
name:'Starmie' , type:'water' | ||
name: 'Starmie', type: 'water' | ||
}, { | ||
name:'Charmander' , type:'fire' | ||
name: 'Charmander', type: 'fire' | ||
}, { | ||
name:'Lapras' , type:'water' | ||
}], function(db, t, done) { | ||
db.a.ensureIndex({type: 1}, function(err) { | ||
name: 'Lapras', type: 'water' | ||
}], function (db, t, done) { | ||
db.a.ensureIndex({type: 1}, function (err) { | ||
if (err && err.message === 'no such cmd: createIndexes') { | ||
// Index creation and deletion not supported for mongodb 2.4 and lower. | ||
t.ok(true); | ||
t.end(); | ||
return; | ||
t.ok(true) | ||
t.end() | ||
return | ||
} | ||
t.ok(!err); | ||
db.a.getIndexes(function(err, indexes) { | ||
t.ok(!err); | ||
t.equal(indexes.length, 2); | ||
db.a.dropIndexes(function(err) { | ||
t.ok(!err); | ||
t.error(err) | ||
db.a.getIndexes(function (err, indexes) { | ||
t.error(err) | ||
t.equal(indexes.length, 2) | ||
db.a.dropIndexes(function (err) { | ||
t.error(err) | ||
db.a.getIndexes(function(err, indexes) { | ||
t.equal(indexes.length, 1); | ||
t.end(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
db.a.getIndexes(function (err, indexes) { | ||
t.error(err) | ||
t.equal(indexes.length, 1) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
@@ -9,14 +9,14 @@ insert('findAndModify', [{ | ||
hello: 'other' | ||
}], function(db, t, done) { | ||
}], function (db, t, done) { | ||
// Update and find the old document | ||
db.a.findAndModify({ | ||
query: { id: 1 }, | ||
update: { $set: { hello: 'world' } }, | ||
update: { $set: { hello: 'world' } } | ||
}, | ||
function(err, doc, lastErrorObject) { | ||
t.ok(!err); | ||
t.equal(doc.id, 1); | ||
t.equal(doc.hello, 'you'); | ||
t.equal(lastErrorObject.updatedExisting, true); | ||
t.equal(lastErrorObject.n, 1); | ||
function (err, doc, lastErrorObject) { | ||
t.error(err) | ||
t.equal(doc.id, 1) | ||
t.equal(doc.hello, 'you') | ||
t.equal(lastErrorObject.updatedExisting, true) | ||
t.equal(lastErrorObject.n, 1) | ||
@@ -28,8 +28,8 @@ // Update and find the new document | ||
update: { $set: { hello: 'me' } } | ||
}, function(err, doc, lastErrorObject) { | ||
t.ok(!err); | ||
t.equal(doc.id, 2); | ||
t.equal(doc.hello, 'me'); | ||
t.equal(lastErrorObject.updatedExisting, true); | ||
t.equal(lastErrorObject.n, 1); | ||
}, function (err, doc, lastErrorObject) { | ||
t.error(err) | ||
t.equal(doc.id, 2) | ||
t.equal(doc.hello, 'me') | ||
t.equal(lastErrorObject.updatedExisting, true) | ||
t.equal(lastErrorObject.n, 1) | ||
@@ -40,6 +40,6 @@ // Remove and find document | ||
remove: true | ||
}, function(err, doc, lastErrorObject) { | ||
t.ok(!err); | ||
t.equal(doc.id, 1); | ||
t.equal(lastErrorObject.n, 1); | ||
}, function (err, doc, lastErrorObject) { | ||
t.error(err) | ||
t.equal(doc.id, 1) | ||
t.equal(lastErrorObject.n, 1) | ||
@@ -52,9 +52,9 @@ // Insert document using upsert | ||
upsert: true | ||
}, function(err, doc, lastErrorObject) { | ||
t.ok(!err); | ||
t.equal(doc.id, 3); | ||
t.equal(doc.hello, 'girl'); | ||
t.equal(lastErrorObject.updatedExisting, false); | ||
t.equal(lastErrorObject.n, 1); | ||
t.equal(String(lastErrorObject.upserted), String(doc._id)); | ||
}, function (err, doc, lastErrorObject) { | ||
t.error(err) | ||
t.equal(doc.id, 3) | ||
t.equal(doc.hello, 'girl') | ||
t.equal(lastErrorObject.updatedExisting, false) | ||
t.equal(lastErrorObject.n, 1) | ||
t.equal(String(lastErrorObject.upserted), String(doc._id)) | ||
@@ -65,12 +65,12 @@ // Find non existing document | ||
update: { $set: { hello: 'boy' } } | ||
}, function(err, doc, lastErrorObject) { | ||
t.ok(!err); | ||
t.equal(lastErrorObject.n, 0); | ||
}, function (err, doc, lastErrorObject) { | ||
t.error(err) | ||
t.equal(lastErrorObject.n, 0) | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,14 +0,14 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('find and select', [{ | ||
hello:'world', | ||
another:'value' | ||
}], function(db, t, done) { | ||
db.a.find({}, {another:1}, function(err, docs) { | ||
t.ok(!err); | ||
t.equal(docs.length, 1); | ||
t.equal(docs[0].hello, undefined); | ||
t.equal(docs[0].another, 'value'); | ||
done(); | ||
}); | ||
}); | ||
hello: 'world', | ||
another: 'value' | ||
}], function (db, t, done) { | ||
db.a.find({}, {another: 1}, function (err, docs) { | ||
t.error(err) | ||
t.equal(docs.length, 1) | ||
t.equal(docs[0].hello, undefined) | ||
t.equal(docs[0].another, 'value') | ||
done() | ||
}) | ||
}) |
@@ -1,19 +0,19 @@ | ||
var insert = require('./insert'); | ||
var mongojs = require('../'); | ||
var insert = require('./insert') | ||
var mongojs = require('../') | ||
insert('find by ObjectId', [{ | ||
hello:'world' | ||
}], function(db, t, done) { | ||
db.a.find({_id:db.ObjectId('abeabeabeabeabeabeabeabe')}, {hello:1}, function(err, docs) { | ||
t.ok(!err); | ||
t.equal(docs.length, 0); | ||
hello: 'world' | ||
}], function (db, t, done) { | ||
db.a.find({_id: db.ObjectId('abeabeabeabeabeabeabeabe')}, {hello: 1}, function (err, docs) { | ||
t.error(err) | ||
t.equal(docs.length, 0) | ||
db.a.save({_id:mongojs.ObjectId('abeabeabeabeabeabeabeabe')}, function() { | ||
db.a.find({_id:db.ObjectId('abeabeabeabeabeabeabeabe')}, {hello:1}, function(err, docs) { | ||
t.ok(!err); | ||
t.equal(docs.length, 1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
db.a.save({_id: mongojs.ObjectId('abeabeabeabeabeabeabeabe')}, function () { | ||
db.a.find({_id: db.ObjectId('abeabeabeabeabeabeabeabe')}, {hello: 1}, function (err, docs) { | ||
t.error(err) | ||
t.equal(docs.length, 1) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,23 +0,23 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('find cursor options', [{ | ||
hello:'world1' | ||
},{ | ||
hello:'world2' | ||
}], function(db, t, done) { | ||
var cursor = db.a.find().limit(1).skip(1); | ||
var runs = 0; | ||
hello: 'world1' | ||
}, { | ||
hello: 'world2' | ||
}], function (db, t, done) { | ||
var cursor = db.a.find().limit(1).skip(1) | ||
var runs = 0 | ||
cursor.next(function loop(err, doc) { | ||
cursor.next(function loop (err, doc) { | ||
if (!doc) { | ||
t.equal(runs, 1); | ||
done(); | ||
return; | ||
t.equal(runs, 1) | ||
done() | ||
return | ||
} | ||
t.ok(!err); | ||
t.equal(doc.hello, 'world2'); | ||
t.equal(typeof doc, 'object'); | ||
runs++; | ||
cursor.next(loop); | ||
}); | ||
}); | ||
t.error(err) | ||
t.equal(doc.hello, 'world2') | ||
t.equal(typeof doc, 'object') | ||
runs++ | ||
cursor.next(loop) | ||
}) | ||
}) |
@@ -1,23 +0,23 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('find cursor', [{ | ||
hello:'world1' | ||
},{ | ||
hello:'world2' | ||
}], function(db, t, done) { | ||
var cursor = db.a.find(); | ||
var runs = 0; | ||
hello: 'world1' | ||
}, { | ||
hello: 'world2' | ||
}], function (db, t, done) { | ||
var cursor = db.a.find() | ||
var runs = 0 | ||
cursor.next(function loop(err, doc) { | ||
cursor.next(function loop (err, doc) { | ||
if (!doc) { | ||
t.equal(runs, 2); | ||
done(); | ||
return; | ||
t.equal(runs, 2) | ||
done() | ||
return | ||
} | ||
t.ok(!err); | ||
t.ok(doc.hello === 'world1' || doc.hello === 'world2'); | ||
t.equal(typeof doc, 'object'); | ||
runs++; | ||
cursor.next(loop); | ||
}); | ||
}); | ||
t.error(err) | ||
t.ok(doc.hello === 'world1' || doc.hello === 'world2') | ||
t.equal(typeof doc, 'object') | ||
runs++ | ||
cursor.next(loop) | ||
}) | ||
}) |
@@ -1,12 +0,12 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('find().limit', [{ | ||
hello:'world' | ||
}], function(db, t, done) { | ||
db.a.find().limit(1, function(err, docs) { | ||
t.ok(!err); | ||
t.equal(docs.length, 1); | ||
t.equal(docs[0].hello, 'world'); | ||
done(); | ||
}); | ||
}); | ||
hello: 'world' | ||
}], function (db, t, done) { | ||
db.a.find().limit(1, function (err, docs) { | ||
t.error(err) | ||
t.equal(docs.length, 1) | ||
t.equal(docs[0].hello, 'world') | ||
done() | ||
}) | ||
}) |
@@ -1,14 +0,14 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('findOne', [{ | ||
hello:'world1' | ||
},{ | ||
hello:'world2' | ||
}], function(db, t, done) { | ||
db.a.findOne(function(err, doc) { | ||
t.ok(!err); | ||
t.equal(typeof doc, 'object'); | ||
t.ok(doc.hello === 'world1' || doc.hello === 'world2'); | ||
done(); | ||
}); | ||
}); | ||
hello: 'world1' | ||
}, { | ||
hello: 'world2' | ||
}], function (db, t, done) { | ||
db.a.findOne(function (err, doc) { | ||
t.error(err) | ||
t.equal(typeof doc, 'object') | ||
t.ok(doc.hello === 'world1' || doc.hello === 'world2') | ||
done() | ||
}) | ||
}) |
@@ -1,14 +0,14 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('find query', [{ | ||
hello:'world1' | ||
hello: 'world1' | ||
}, { | ||
hello:'world2' | ||
}], function(db, t, done) { | ||
db.a.find({hello:'world2'}, function(err, docs) { | ||
t.ok(!err); | ||
t.equal(docs.length, 1); | ||
t.equal(docs[0].hello, 'world2'); | ||
done(); | ||
}); | ||
}); | ||
hello: 'world2' | ||
}], function (db, t, done) { | ||
db.a.find({hello: 'world2'}, function (err, docs) { | ||
t.error(err) | ||
t.equal(docs.length, 1) | ||
t.equal(docs[0].hello, 'world2') | ||
done() | ||
}) | ||
}) |
@@ -1,21 +0,20 @@ | ||
var insert = require('./insert'); | ||
var concat = require('concat-stream'); | ||
var insert = require('./insert') | ||
insert('sort', [{ | ||
name:'Squirtle', type:'water' | ||
name: 'Squirtle', type: 'water' | ||
}, { | ||
name:'Starmie' , type:'water' | ||
name: 'Starmie', type: 'water' | ||
}, { | ||
name:'Charmander' , type:'fire' | ||
name: 'Charmander', type: 'fire' | ||
}, { | ||
name:'Lapras' , type:'water' | ||
}], function(db, t, done) { | ||
db.a.find().sort({name: 1}, function(err, docs) { | ||
t.ok(!err); | ||
t.equal(docs[0].name, 'Charmander'); | ||
t.equal(docs[1].name, 'Lapras'); | ||
t.equal(docs[2].name, 'Squirtle'); | ||
t.equal(docs[3].name, 'Starmie'); | ||
t.end(); | ||
}); | ||
}); | ||
name: 'Lapras', type: 'water' | ||
}], function (db, t, done) { | ||
db.a.find().sort({name: 1}, function (err, docs) { | ||
t.error(err) | ||
t.equal(docs[0].name, 'Charmander') | ||
t.equal(docs[1].name, 'Lapras') | ||
t.equal(docs[2].name, 'Squirtle') | ||
t.equal(docs[3].name, 'Starmie') | ||
t.end() | ||
}) | ||
}) |
@@ -1,12 +0,12 @@ | ||
var test = require('./tape'); | ||
var mongojs = require('../index'); | ||
var db = mongojs('test', ['tailable']); | ||
var test = require('./tape') | ||
var mongojs = require('../index') | ||
var db = mongojs('test', ['tailable']) | ||
test('tailable find', function(t) { | ||
db.tailable.drop(function(err) { | ||
db.createCollection('tailable', {capped: true, size: 1024}, function(err) { | ||
t.notOk(err, 'no error in creating the collection'); | ||
test('tailable find', function (t) { | ||
db.tailable.drop(function () { | ||
db.createCollection('tailable', {capped: true, size: 1024}, function (err) { | ||
t.error(err, 'no error in creating the collection') | ||
var expected1 = { hello: 'world' }; | ||
var expected2 = { hello: 'matteo' }; | ||
var expected1 = { hello: 'world' } | ||
var expected2 = { hello: 'matteo' } | ||
@@ -18,21 +18,21 @@ var stream = db.tailable.find({}, {}, { | ||
numberOfRetries: Number.MAX_VALUE | ||
}); | ||
}) | ||
db.tailable.insert(expected1, function(err) { | ||
t.notOk(err, 'no error in insert'); | ||
stream.once('data', function(obj) { | ||
t.deepEqual(obj, expected1, 'fetched object match'); | ||
stream.once('data', function(obj) { | ||
t.deepEqual(obj, expected2, 'fetched object match'); | ||
stream.destroy(); | ||
db.tailable.drop(t.end.bind(t)); | ||
}); | ||
db.tailable.insert(expected1, function (err) { | ||
t.error(err, 'no error in insert') | ||
stream.once('data', function (obj) { | ||
t.deepEqual(obj, expected1, 'fetched object match') | ||
stream.once('data', function (obj) { | ||
t.deepEqual(obj, expected2, 'fetched object match') | ||
stream.destroy() | ||
db.tailable.drop(t.end.bind(t)) | ||
}) | ||
db.tailable.insert(expected2, function(err) { | ||
t.notOk(err, 'no error in insert'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
db.tailable.insert(expected2, function (err) { | ||
t.error(err, 'no error in insert') | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,13 +0,12 @@ | ||
var test = require('./tape'); | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('find', [{ | ||
hello:'world' | ||
}], function(db, t, done) { | ||
db.a.find(function(err, docs) { | ||
t.ok(!err); | ||
t.equal(docs.length, 1); | ||
t.equal(docs[0].hello, 'world'); | ||
done(); | ||
}); | ||
}); | ||
hello: 'world' | ||
}], function (db, t, done) { | ||
db.a.find(function (err, docs) { | ||
t.error(err) | ||
t.equal(docs.length, 1) | ||
t.equal(docs[0].hello, 'world') | ||
done() | ||
}) | ||
}) |
@@ -1,13 +0,15 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('getCollectionNames', [{ | ||
hello:'world' | ||
}], function(db, t, done) { | ||
db.collection('b').save({hello: "world"}, function(err, b) { | ||
db.getCollectionNames(function(err, colNames) { | ||
t.notEqual(colNames.indexOf('a'), -1); | ||
t.notEqual(colNames.indexOf('b'), -1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
hello: 'world' | ||
}], function (db, t, done) { | ||
db.collection('b').save({hello: 'world'}, function (err, b) { | ||
t.error(err) | ||
db.getCollectionNames(function (err, colNames) { | ||
t.error(err) | ||
t.notEqual(colNames.indexOf('a'), -1) | ||
t.notEqual(colNames.indexOf('b'), -1) | ||
done() | ||
}) | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
@@ -9,3 +9,3 @@ insert('group', [{ | ||
online: 0 | ||
}], function(db, t, done) { | ||
}], function (db, t, done) { | ||
db.a.group({ | ||
@@ -15,16 +15,16 @@ key: {}, | ||
initial: {count: 0, online: 0}, | ||
reduce: function(doc, out) { | ||
out.count++; | ||
out.online += doc.online; | ||
reduce: function (doc, out) { | ||
out.count++ | ||
out.online += doc.online | ||
}, | ||
finalize: function(out) { | ||
out.avgOnline = out.online / out.count; | ||
finalize: function (out) { | ||
out.avgOnline = out.online / out.count | ||
} | ||
}, function(err, curOnline) { | ||
t.ok(!err); | ||
t.equal(curOnline[0].count, 1); | ||
t.equal(curOnline[0].online, 1); | ||
done(); | ||
}); | ||
}, function (err, curOnline) { | ||
t.error(err) | ||
t.equal(curOnline[0].count, 1) | ||
t.equal(curOnline[0].online, 1) | ||
done() | ||
}) | ||
}) | ||
@@ -1,27 +0,30 @@ | ||
var test = require('./tape'); | ||
var mongojs = require('../index'); | ||
var db = mongojs('test', ['a','b']); | ||
var test = require('./tape') | ||
var mongojs = require('../index') | ||
var db = mongojs('test', ['a', 'b']) | ||
test('insert', function(t) { | ||
db.a.insert([{name: "Squirtle"}, {name: "Charmander"}, {name: "Bulbasaur"}], function(err, docs) { | ||
t.ok(docs[0]._id); | ||
t.ok(docs[1]._id); | ||
t.ok(docs[2]._id); | ||
test('insert', function (t) { | ||
db.a.insert([{name: 'Squirtle'}, {name: 'Charmander'}, {name: 'Bulbasaur'}], function (err, docs) { | ||
t.error(err) | ||
t.ok(docs[0]._id) | ||
t.ok(docs[1]._id) | ||
t.ok(docs[2]._id) | ||
// It should only return one document in the | ||
// It should only return one document in the | ||
// callback when one document is passed instead of an array | ||
db.a.insert({name: "Lapras"}, function(err, doc) { | ||
t.equal(doc.name, "Lapras"); | ||
db.a.insert({name: 'Lapras'}, function (err, doc) { | ||
t.error(err) | ||
t.equal(doc.name, 'Lapras') | ||
// If you pass a one element array the callback should | ||
// have a one element array | ||
db.a.insert([{name: "Pidgeotto"}], function (err, docs) { | ||
t.equal(docs[0].name, "Pidgeotto"); | ||
t.equal(docs.length, 1); | ||
db.a.remove(function() { | ||
db.close(t.end.bind(t)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
db.a.insert([{name: 'Pidgeotto'}], function (err, docs) { | ||
t.error(err) | ||
t.equal(docs[0].name, 'Pidgeotto') | ||
t.equal(docs.length, 1) | ||
db.a.remove(function () { | ||
db.close(t.end.bind(t)) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,22 +0,23 @@ | ||
var test = require('./tape'); | ||
var mongojs = require('../index'); | ||
var db = mongojs('test', ['a', 'mycappedcol']); | ||
var test = require('./tape') | ||
var mongojs = require('../index') | ||
var db = mongojs('test', ['a', 'mycappedcol']) | ||
test('isCapped', function(t) { | ||
db.mycappedcol.drop(function(err) { | ||
db.createCollection('mycappedcol', {capped: true, size: 1024}, function(err) { | ||
t.notOk(err); | ||
db.mycappedcol.isCapped(function(err, ic) { | ||
t.notOk(err); | ||
t.ok(ic); | ||
db.a.insert({}, function(err) { | ||
t.notOk(err); | ||
db.a.isCapped(function(err, ic2) { | ||
t.notOk(ic2); | ||
db.mycappedcol.drop(t.end.bind(t)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
test('isCapped', function (t) { | ||
db.mycappedcol.drop(function () { | ||
db.createCollection('mycappedcol', {capped: true, size: 1024}, function (err) { | ||
t.error(err) | ||
db.mycappedcol.isCapped(function (err, ic) { | ||
t.error(err) | ||
t.ok(ic) | ||
db.a.insert({}, function (err) { | ||
t.error(err) | ||
db.a.isCapped(function (err, ic2) { | ||
t.error(err) | ||
t.notOk(ic2) | ||
db.mycappedcol.drop(t.end.bind(t)) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,28 +0,30 @@ | ||
var test = require('./tape'); | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('remove', [{ | ||
name:'Squirtle', type:'water', level: 10 | ||
name: 'Squirtle', type: 'water', level: 10 | ||
}, { | ||
name:'Starmie' , type:'water', level: 8 | ||
name: 'Starmie', type: 'water', level: 8 | ||
}, { | ||
name:'Charmander' , type:'fire', level: 8 | ||
name: 'Charmander', type: 'fire', level: 8 | ||
}, { | ||
name:'Lapras' , type:'water', level: 12 | ||
}], function(db, t, done) { | ||
db.a.mapReduce(function() { | ||
emit(this.type, this.level); | ||
}, function(key, values) { | ||
return Array.sum(values); | ||
name: 'Lapras', type: 'water', level: 12 | ||
}], function (db, t, done) { | ||
db.a.mapReduce(function () { | ||
/* eslint-disable no-undef */ | ||
emit(this.type, this.level) | ||
/* eslint-enable no-undef */ | ||
}, function (key, values) { | ||
return Array.sum(values) | ||
}, { | ||
query: {type: 'water'}, | ||
out: 'levelSum' | ||
}, function(err) { | ||
t.notOk(err); | ||
db.collection('levelSum').findOne(function(err, res) { | ||
t.equal(res._id, 'water'); | ||
t.equal(res.value, 30); | ||
db.collection('levelSum').drop(done); | ||
}); | ||
}); | ||
}); | ||
}, function (err) { | ||
t.error(err) | ||
db.collection('levelSum').findOne(function (err, res) { | ||
t.error(err) | ||
t.equal(res._id, 'water') | ||
t.equal(res.value, 30) | ||
db.collection('levelSum').drop(done) | ||
}) | ||
}) | ||
}) |
@@ -1,12 +0,12 @@ | ||
var test = require('./tape'); | ||
var mongojs = require('../index'); | ||
var db = mongojs('test', ['a','b']); | ||
var test = require('./tape') | ||
var mongojs = require('../index') | ||
var db = mongojs('test', ['a', 'b']) | ||
test('optional callback', function(t) { | ||
db.a.ensureIndex({hello:'world'}) | ||
setTimeout(function() { | ||
db.a.count(function() { | ||
db.close(t.end.bind(t)); | ||
test('optional callback', function (t) { | ||
db.a.ensureIndex({hello: 'world'}) | ||
setTimeout(function () { | ||
db.a.count(function () { | ||
db.close(t.end.bind(t)) | ||
}) | ||
}, 100); | ||
}); | ||
}, 100) | ||
}) |
@@ -1,34 +0,35 @@ | ||
var test = require('./tape'); | ||
var mongojs = require('../'); | ||
var test = require('./tape') | ||
var mongojs = require('../') | ||
test('receive a driver db or mongojs instance', function(t) { | ||
test('receive a driver db or mongojs instance', function (t) { | ||
var db = mongojs(mongojs('test', []), ['a']); | ||
var afterFind = function() { | ||
db.a.remove(function(err) { | ||
t.ok(!err); | ||
t.equal(db.toString(), 'test'); | ||
var db = mongojs(mongojs('test', []), ['a']) | ||
var afterFind = function () { | ||
db.a.remove(function (err) { | ||
t.error(err) | ||
t.equal(db.toString(), 'test') | ||
db.close(function(err) { | ||
t.ok(!err); | ||
t.end(); | ||
}); | ||
}); | ||
}; | ||
db.close(function (err) { | ||
t.error(err) | ||
t.end() | ||
}) | ||
}) | ||
} | ||
var afterInsert = function(err) { | ||
t.ok(!err); | ||
var afterInsert = function (err) { | ||
t.error(err) | ||
db.a.findOne(function(err, data) { | ||
t.equal(data.name, 'Pidgey'); | ||
afterFind(); | ||
}); | ||
}; | ||
db.a.findOne(function (err, data) { | ||
t.error(err) | ||
t.equal(data.name, 'Pidgey') | ||
afterFind() | ||
}) | ||
} | ||
var afterRemove = function(err) { | ||
t.ok(!err); | ||
db.a.insert({name: 'Pidgey'}, afterInsert); | ||
}; | ||
db.a.remove(afterRemove); | ||
var afterRemove = function (err) { | ||
t.error(err) | ||
db.a.insert({name: 'Pidgey'}, afterInsert) | ||
} | ||
db.a.remove(afterRemove) | ||
}); | ||
}) |
@@ -1,16 +0,17 @@ | ||
var test = require('./tape'); | ||
var mongojs = require('../index'); | ||
var db = mongojs('test'); | ||
var test = require('./tape') | ||
var mongojs = require('../index') | ||
var db = mongojs('test') | ||
test('proxy', function(t) { | ||
if (typeof Proxy === 'undefined') return t.end(); | ||
test('proxy', function (t) { | ||
if (typeof Proxy === 'undefined') return t.end() | ||
db.a.remove(function() { | ||
db.a.insert({hello: 'world'}, function() { | ||
db.a.findOne(function(err, doc) { | ||
t.equal(doc.hello, 'world'); | ||
t.end(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
db.a.remove(function () { | ||
db.a.insert({hello: 'world'}, function () { | ||
db.a.findOne(function (err, doc) { | ||
t.error(err) | ||
t.equal(doc.hello, 'world') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,30 +0,34 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
// Delete just one | ||
insert('remove', [{ | ||
name:'Squirtle', type:'water' | ||
name: 'Squirtle', type: 'water' | ||
}, { | ||
name:'Starmie' , type:'water' | ||
name: 'Starmie', type: 'water' | ||
}, { | ||
name:'Lapras' , type:'water' | ||
}], function(db, t, done) { | ||
name: 'Lapras', type: 'water' | ||
}], function (db, t, done) { | ||
// Remove just one | ||
db.a.remove({type:'water'}, true, function(err, lastErrorObject) { | ||
t.equal(lastErrorObject.n, 1); | ||
db.a.remove({type: 'water'}, true, function (err, lastErrorObject) { | ||
t.error(err) | ||
t.equal(lastErrorObject.n, 1) | ||
db.a.find({type:'water'}, function(err, docs) { | ||
t.equal(docs.length, 2); | ||
db.a.find({type: 'water'}, function (err, docs) { | ||
t.error(err) | ||
t.equal(docs.length, 2) | ||
t.equal(docs[0].name, 'Starmie') | ||
// Normal remove | ||
db.a.remove({type:'water'}, function(err, lastErrorObject) { | ||
t.equal(lastErrorObject.n, 2); | ||
db.a.remove({type: 'water'}, function (err, lastErrorObject) { | ||
t.error(err) | ||
t.equal(lastErrorObject.n, 2) | ||
db.a.find({type:'water'}, function(err, docs) { | ||
t.equal(docs.length, 0); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
db.a.find({type: 'water'}, function (err, docs) { | ||
t.error(err) | ||
t.equal(docs.length, 0) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,31 +0,37 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('runCommand', [{ | ||
hello: "world" | ||
},{ | ||
hello: "world2" | ||
},{ | ||
hello: "world3" | ||
},{ | ||
hello: "world" | ||
}], function(db, t, done) { | ||
db.runCommand({count: "a", query:{}}, function(err, res) { | ||
t.equal(res.n, 4); | ||
db.a.runCommand('count', {query: {hello: "world"}}, function(err, res) { | ||
t.equal(res.n, 2); | ||
db.a.runCommand('distinct', {key: "hello", query:{}}, function(err, docs) { | ||
t.equal(docs.values.length, 3); | ||
db.runCommand({distinct:'a', key:"hello", query:{hello:"world"}}, function(err, docs) { | ||
t.equal(docs.values.length, 1); | ||
db.runCommand("ping", function(err, res) { | ||
t.equal(res.ok,1); | ||
db.a.runCommand("count", function(err, res) { | ||
t.equal(res.n, 4); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
hello: 'world' | ||
}, { | ||
hello: 'world2' | ||
}, { | ||
hello: 'world3' | ||
}, { | ||
hello: 'world' | ||
}], function (db, t, done) { | ||
db.runCommand({count: 'a', query: {}}, function (err, res) { | ||
t.error(err) | ||
t.equal(res.n, 4) | ||
db.a.runCommand('count', {query: {hello: 'world'}}, function (err, res) { | ||
t.error(err) | ||
t.equal(res.n, 2) | ||
db.a.runCommand('distinct', {key: 'hello', query: {}}, function (err, docs) { | ||
t.error(err) | ||
t.equal(docs.values.length, 3) | ||
db.runCommand({distinct: 'a', key: 'hello', query: {hello: 'world'}}, function (err, docs) { | ||
t.error(err) | ||
t.equal(docs.values.length, 1) | ||
db.runCommand('ping', function (err, res) { | ||
t.error(err) | ||
t.equal(res.ok, 1) | ||
db.a.runCommand('count', function (err, res) { | ||
t.error(err) | ||
t.equal(res.n, 4) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,19 +0,21 @@ | ||
var test = require('./tape'); | ||
var mongojs = require('../index'); | ||
var db = mongojs('test', ['a','b']); | ||
var test = require('./tape') | ||
var mongojs = require('../index') | ||
var db = mongojs('test', ['a', 'b']) | ||
test('save', function(t) { | ||
db.a.save({hello: "world"}, function(err, doc) { | ||
t.equal(doc.hello, "world"); | ||
t.ok(doc._id); | ||
test('save', function (t) { | ||
db.a.save({hello: 'world'}, function (err, doc) { | ||
t.error(err) | ||
t.equal(doc.hello, 'world') | ||
t.ok(doc._id) | ||
doc.hello = "verden"; | ||
db.a.save(doc, function(err, doc) { | ||
t.ok(doc._id); | ||
t.equal(doc.hello, "verden") | ||
db.a.remove(function() { | ||
db.close(t.end.bind(t)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
doc.hello = 'verden' | ||
db.a.save(doc, function (err, doc) { | ||
t.error(err) | ||
t.ok(doc._id) | ||
t.equal(doc.hello, 'verden') | ||
db.a.remove(function () { | ||
db.close(t.end.bind(t)) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,11 +0,11 @@ | ||
var test = require('./tape'); | ||
var mongojs = require('../index'); | ||
var db = mongojs('test', ['a','b']); | ||
var test = require('./tape') | ||
var mongojs = require('../index') | ||
var db = mongojs('test', ['a', 'b']) | ||
test('simple', function(t) { | ||
db.a.find(function(err, docs) { | ||
t.ok(!err); | ||
t.equal(docs.length, 0); | ||
db.close(t.end.bind(t)); | ||
}); | ||
}); | ||
test('simple', function (t) { | ||
db.a.find(function (err, docs) { | ||
t.error(err) | ||
t.equal(docs.length, 0) | ||
db.close(t.end.bind(t)) | ||
}) | ||
}) |
@@ -1,29 +0,29 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('streaming cursor', [{ | ||
hello:'world1' | ||
},{ | ||
hello:'world2' | ||
}], function(db, t, done) { | ||
var cursor = db.a.find(); | ||
var runs = 0; | ||
hello: 'world1' | ||
}, { | ||
hello: 'world2' | ||
}], function (db, t, done) { | ||
var cursor = db.a.find() | ||
var runs = 0 | ||
var loop = function() { | ||
var doc; | ||
var loop = function () { | ||
var doc | ||
while (doc = cursor.read()) { | ||
t.ok(doc.hello === 'world1' || doc.hello === 'world2'); | ||
t.equal(typeof doc, 'object'); | ||
runs++; | ||
while ((doc = cursor.read()) !== null) { | ||
t.ok(doc.hello === 'world1' || doc.hello === 'world2') | ||
t.equal(typeof doc, 'object') | ||
runs++ | ||
} | ||
cursor.once('readable', loop); | ||
}; | ||
cursor.once('readable', loop) | ||
} | ||
cursor.on('end', function() { | ||
t.equal(runs, 2); | ||
done(); | ||
}); | ||
cursor.on('end', function () { | ||
t.equal(runs, 2) | ||
done() | ||
}) | ||
loop(); | ||
}); | ||
loop() | ||
}) |
@@ -1,15 +0,15 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('update and callback', [{ | ||
hello:'world' | ||
}], function(db, t, done) { | ||
var sync = true; | ||
db.a.update({hello:'world'}, {$set:{hello:'verden'}}, function(err, lastErrorObject) { | ||
t.ok(!sync); | ||
t.ok(!err); | ||
t.equal(lastErrorObject.n, 1); | ||
hello: 'world' | ||
}], function (db, t, done) { | ||
var sync = true | ||
db.a.update({hello: 'world'}, {$set: {hello: 'verden'}}, function (err, lastErrorObject) { | ||
t.ok(!sync) | ||
t.error(err) | ||
t.equal(lastErrorObject.n, 1) | ||
done(); | ||
}); | ||
sync = false; | ||
}); | ||
done() | ||
}) | ||
sync = false | ||
}) |
@@ -1,22 +0,22 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('update multi', [{ | ||
hello:'world1' | ||
},{ | ||
hello:'world2' | ||
}], function(db, t, done) { | ||
db.a.update({}, {$set:{updated:true}}, {multi:true}, function(err, lastErrorObject) { | ||
t.ok(!err); | ||
t.equal(lastErrorObject.n, 2); | ||
hello: 'world1' | ||
}, { | ||
hello: 'world2' | ||
}], function (db, t, done) { | ||
db.a.update({}, {$set: {updated: true}}, {multi: true}, function (err, lastErrorObject) { | ||
t.error(err) | ||
t.equal(lastErrorObject.n, 2) | ||
db.a.find(function(err, docs) { | ||
t.ok(!err); | ||
t.equal(docs.length, 2); | ||
t.ok(docs[0].updated); | ||
t.equal(docs[0].hello, 'world1'); | ||
t.ok(docs[1].updated); | ||
t.equal(docs[1].hello, 'world2'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
db.a.find(function (err, docs) { | ||
t.error(err) | ||
t.equal(docs.length, 2) | ||
t.ok(docs[0].updated) | ||
t.equal(docs[0].hello, 'world1') | ||
t.ok(docs[1].updated) | ||
t.equal(docs[1].hello, 'world2') | ||
done() | ||
}) | ||
}) | ||
}) |
@@ -1,16 +0,16 @@ | ||
var insert = require('./insert'); | ||
var insert = require('./insert') | ||
insert('update', [{ | ||
hello:'world' | ||
}], function(db, t, done) { | ||
db.a.update({hello:'world'}, {$set:{hello:'verden'}}, function(err, lastErrorObject) { | ||
t.ok(!err); | ||
t.equal(lastErrorObject.n, 1); | ||
hello: 'world' | ||
}], function (db, t, done) { | ||
db.a.update({hello: 'world'}, {$set: {hello: 'verden'}}, function (err, lastErrorObject) { | ||
t.error(err) | ||
t.equal(lastErrorObject.n, 1) | ||
db.a.findOne(function(err, doc) { | ||
t.ok(!err); | ||
t.equal(doc.hello, 'verden'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
db.a.findOne(function (err, doc) { | ||
t.error(err) | ||
t.equal(doc.hello, 'verden') | ||
done() | ||
}) | ||
}) | ||
}) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
62261
1695
5
+ Addedisarray@1.0.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removedisarray@0.0.1(transitive)
- Removedreadable-stream@1.1.14(transitive)
- Removedstring_decoder@0.10.31(transitive)
Updatedreadable-stream@^2.0.2