Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongojs

Package Overview
Dependencies
Maintainers
4
Versions
105
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongojs - npm Package Compare versions

Comparing version 1.4.1 to 2.0.0

.idea/.name

28

index.js
var Database = require('./lib/database')
var bson = require('mongodb-core').BSON
var xtend = require('xtend')
var mongodb = require('mongodb')
module.exports = function (connString, cols, options) {
var db = new Database(xtend({connString: connString, cols: cols}, options))
var db = new Database(connString, cols, options)
if (typeof Proxy !== 'undefined') {
var p = Proxy.create({
get: function (obj, prop) {
// Work around for event emitters to work together with harmony proxy
if (prop === 'on' || prop === 'emit') {
return db[prop].bind(db)
}
if (db[prop]) return db[prop]

@@ -23,7 +27,13 @@ db[prop] = db.collection(prop)

// expose bson stuff visible in the shell
module.exports.ObjectId = bson.ObjectId
module.exports.DBRef = bson.DBRef
module.exports.Timestamp = bson.Timestamp
module.exports.MinKey = bson.MinKey
module.exports.MaxKey = bson.MaxKey
module.exports.NumberLong = bson.Long
module.exports.Binary = mongodb.Binary
module.exports.Code = mongodb.Code
module.exports.DBRef = mongodb.DBRef
module.exports.Double = mongodb.Double
module.exports.Long = mongodb.Long
module.exports.NumberLong = mongodb.Long // Alias for shell compatibility
module.exports.MinKey = mongodb.MinKey
module.exports.MaxKey = mongodb.MaxKey
module.exports.ObjectID = mongodb.ObjectID
module.exports.ObjectId = mongodb.ObjectId
module.exports.Symbol = mongodb.Symbol
module.exports.Timestamp = mongodb.Timestamp

@@ -1,7 +0,7 @@

var mongodb = require('mongodb-core')
var mongodb = require('mongodb')
var each = require('each-series')
var oid = mongodb.BSON.ObjectID.createPk
var oid = mongodb.ObjectID.createPk
var Bulk = function (colName, ordered, onserver, dbname) {
var Bulk = function (colName, ordered, onserver) {
this._colname = colName

@@ -11,4 +11,3 @@ this._cmds = []

this._ordered = ordered
this._onserver = onserver
this._dbname = dbname
this._getConnection = onserver

@@ -149,8 +148,8 @@ var self = this

this._cmds.push(this._currCmd)
this._onserver(function (err, server) {
this._getConnection(function (err, connection) {
if (err) return cb(err)
each(self._cmds, function (cmd, i, done) {
server.command(self._dbname + '.$cmd', cmd, function (err, res) {
connection.command(cmd, function (err, res) {
if (err) return done(err)
result[cmdkeys[Object.keys(cmd)[0]]] += res.result.n
result[cmdkeys[Object.keys(cmd)[0]]] += res.n
done()

@@ -157,0 +156,0 @@ })

@@ -1,11 +0,10 @@

var mongodb = require('mongodb-core')
var mongodb = require('mongodb')
var once = require('once')
var xtend = require('xtend')
var Cursor = require('./cursor')
var AggregationCursor = require('./aggregation-cursor')
var Bulk = require('./bulk')
// TODO: Make this configurable by users
var writeOpts = {writeConcern: {w: 1}, ordered: true}
var noop = function () {}
var oid = mongodb.BSON.ObjectID.createPk
var Code = mongodb.BSON.Code
var oid = mongodb.ObjectID.createPk

@@ -18,10 +17,14 @@ var indexName = function (index) {

var Collection = function (opts, getServer) {
var Collection = function (opts, getConnection) {
this._name = opts.name
this._dbname = opts.dbname
this._getServer = getServer
}
this._getConnection = getConnection
this._getCollection = function (cb) {
var collectionName = this._name
Collection.prototype._fullColName = function () {
return this._dbname + '.' + this._name
this._getConnection(function (err, connection) {
if (err) { return cb(err) }
cb(null, connection.collection(collectionName))
})
}
}

@@ -34,10 +37,12 @@

opts = opts || {}
var self = this
function getCursor (cb) {
self._getCollection(function (err, collection) {
if (err) { return cb(err) }
opts.query = query
opts.projection = projection
opts.onserver = this._getServer
opts.fullCollectionName = this._fullColName()
cb(null, collection.find(query, projection, opts))
})
}
var cursor = new Cursor(opts)
var cursor = new Cursor(getCursor)

@@ -76,6 +81,8 @@ if (cb) return cursor.toArray(cb)

Collection.prototype.insert = function (docOrDocs, cb) {
cb = cb || noop
var self = this
this._getServer(function (err, server) {
Collection.prototype.insert = function (docOrDocs, opts, cb) {
if (!opts && !cb) return this.insert(docOrDocs, {}, noop)
if (typeof opts === 'function') return this.insert(docOrDocs, {}, opts)
if (opts && !cb) return this.insert(docOrDocs, opts, noop)
this._getCollection(function (err, collection) {
if (err) return cb(err)

@@ -87,5 +94,7 @@

}
server.insert(self._fullColName(), docs, writeOpts, function (err, res) {
collection.insert(docs, xtend(writeOpts, opts), function (err) {
if (err) return cb(err)
if (res && res.result && res.result.writeErrors && res.result.writeErrors.length > 0) return cb(res.result.writeErrors[0])
// TODO: Add a test for this - is this really not needed anymore?
// if (res && res.result && res.result.writeErrors && res.result.writeErrors.length > 0) return cb(res.result.writeErrors[0])
cb(null, docOrDocs)

@@ -101,11 +110,8 @@ })

cb = cb || noop
var self = this
this._getServer(function (err, server) {
this._getCollection(function (err, collection) {
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)
collection.update(query, update, opts, function (err, result) {
if (err) { return cb(err) }
cb(null, result.result)
})

@@ -127,12 +133,15 @@ })

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, options, cb) {
if (typeof query === 'function') return this.remove({}, {justOne: false}, query)
if (typeof options === 'function') return this.remove(query, {justOne: false}, options)
if (typeof options === 'boolean') return this.remove(query, {justOne: options}, cb)
var self = this
this._getServer(function (err, server) {
this._getCollection(function (err, collection) {
if (err) return cb(err)
server.remove(self._fullColName(), [{q: query, limit: justOne ? 1 : 0}], writeOpts, function (err, res) {
var deleteOperation = options.justOne ? 'deleteOne' : 'deleteMany'
collection[deleteOperation](query, xtend(writeOpts, options), function (err, result) {
if (err) return cb(err)
cb(null, res.result)
cb(null, result.result)
})

@@ -161,3 +170,2 @@ })

if (typeof opts === 'function') return this.runCommand(cmd, null, opts)
var self = this
opts = opts || {}

@@ -170,8 +178,5 @@

})
this._getServer(function (err, server) {
this._getConnection(function (err, connection) {
if (err) return cb(err)
server.command(self._dbname + '.$cmd', cmdObject, function (err, result) {
if (err) return cb(err)
cb(null, result.result)
})
connection.command(cmdObject, cb)
})

@@ -207,10 +212,7 @@ }

Collection.prototype.getIndexes = function (cb) {
var cursor = new Cursor({
query: {ns: this._fullColName()},
projection: {},
onserver: this._getServer,
fullCollectionName: this._dbname + '.system.indexes'
this._getCollection(function (err, collection) {
if (err) { return cb(err) }
collection.indexes(cb)
})
cursor.toArray(cb)
}

@@ -223,12 +225,6 @@

Collection.prototype.isCapped = function (cb) {
var cursor = new Cursor({
query: {name: this._fullColName()},
projection: {},
onserver: this._getServer,
fullCollectionName: this._dbname + '.system.namespaces'
})
this._getCollection(function (err, collection) {
if (err) { return cb(err) }
cursor.toArray(function (err, cols) {
if (err) return cb(err)
cb(null, (cols[0].options && cols[0].options.capped) || false)
collection.isCapped(cb)
})

@@ -238,23 +234,5 @@ }

Collection.prototype.group = function (doc, cb) {
var cmd = {
group: {
ns: this._name,
key: doc.key,
initial: doc.initial,
$reduce: new Code(doc.reduce.toString()),
out: 'inline',
cond: doc.cond
}
}
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) {
this._getCollection(function (err, collection) {
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.group(doc.key || doc.keyf, doc.cond, doc.initial, doc.reduce, doc.finalize, cb)
})

@@ -274,16 +252,16 @@ }

if (cb) {
this.runCommand('aggregate', {pipeline: pipeline}, function (err, res) {
var self = this
var strm = new Cursor(function (cb) {
self._getCollection(function (err, collection) {
if (err) return cb(err)
cb(null, res.result)
cb(null, collection.aggregate(pipeline))
})
return
}
var strm = new AggregationCursor({
onserver: this._getServer,
colName: this._name,
fullCollectionName: this._fullColName(),
pipeline: pipeline
})
if (cb) {
return strm.toArray(cb)
}
return strm

@@ -293,9 +271,9 @@ }

Collection.prototype.initializeOrderedBulkOp = function () {
return new Bulk(this._name, true, this._getServer, this._dbname)
return new Bulk(this._name, true, this._getConnection, this._dbname)
}
Collection.prototype.initializeUnorderedBulkOp = function () {
return new Bulk(this._name, false, this._getServer, this._dbname)
return new Bulk(this._name, false, this._getConnection, this._dbname)
}
module.exports = Collection

@@ -5,25 +5,20 @@ var util = require('util')

var Cursor = function (opts) {
var Cursor = function (getCursor) {
Readable.call(this, {objectMode: true, highWaterMark: 0})
this._opts = opts
var onserver = this._opts.onserver
this._opts = {}
var self = this
this._get = thunky(function (cb) {
onserver(function (err, server) {
if (err) return cb(err)
cb(null, server.cursor(self._opts.fullCollectionName, {
find: self._opts.fullCollectionName,
query: self._opts.query || {},
fields: self._opts.projection,
sort: self._opts.sort,
skip: self._opts.skip,
limit: self._opts.limit,
batchSize: self._opts.batchSize,
explain: self._opts.explain,
tailable: self._opts.tailable,
timeout: self._opts.timeout,
awaitData: self._opts.awaitData,
numberOfRetries: self._opts.numberOfRetries
}))
getCursor(function (err, cursor) {
if (err) { return cb(err) }
// Apply all opts
for (var key in self._opts) {
if (self._opts.hasOwnProperty(key)) {
cursor = cursor[key](self._opts[key])
}
}
cb(null, cursor)
})

@@ -38,3 +33,8 @@ })

if (err) return cb(err)
cursor.next(cb)
if (cursor.cursorState.dead || cursor.cursorState.killed) {
return cb(null, null)
} else {
cursor.next(cb)
}
})

@@ -102,37 +102,18 @@

Cursor.prototype.limit = function (n, cb) {
this._opts.limit = n
if (cb) return this.toArray(cb)
return this
}
var opts = ['limit', 'skip', 'batchSize', 'sort']
Cursor.prototype.skip = function (n, cb) {
this._opts.skip = n
if (cb) return this.toArray(cb)
return this
}
opts.forEach(function (opt) {
Cursor.prototype[opt] = function (obj, cb) {
this._opts[opt] = obj
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.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)
})
this._get(function (err, cursor) {
if (err) { return cb(err) }
cursor.count(false, self.opts, cb)
})

@@ -144,16 +125,5 @@ }

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)
})
this._get(function (err, cursor) {
if (err) { return cb(err) }
cursor.count(true, self.opts, cb)
})

@@ -163,5 +133,6 @@ }

Cursor.prototype.explain = function (cb) {
var q = this._opts.query || {}
this._opts.query = {$query: q, $explain: 1}
this.next(cb)
this._get(function (err, cursor) {
if (err) { return cb(err) }
cursor.explain(cb)
})
}

@@ -173,3 +144,7 @@

if (err) return self.emit('error', err)
if (cursor.kill) cursor.kill()
if (cursor.close) {
cursor.close(function (err) {
if (err) { self.emit('error', err) }
})
}
})

@@ -176,0 +151,0 @@ }

var Collection = require('./collection')
var bson = require('mongodb-core').BSON
var mongodb = require('mongodb')
var xtend = require('xtend')
var thunky = require('thunky')
var toMongodbCore = require('to-mongodb-core')
var parse = require('parse-mongo-url')
var getTopology = require('./get-topology')
var connect = require('./connect')
var util = require('util')
var EventEmitter = require('events').EventEmitter
var noop = function () {}
var Database = function (options) {
var Database = function (connString, cols, options) {
var self = this
if (typeof options.connString === 'string') {
var config = parse(options.connString)
EventEmitter.call(this)
this._dbname = config.dbName
this._server = getTopology(config, options)
if (typeof connString === 'string') {
this._dbname = parse(connString).dbName
this._getServer = thunky(function (cb) {
connect(self._server, config, options, cb)
})
} else {
this._dbname = options.connString._dbname
this._server = options.connString
// Fix short cut connection URLs consisting only of a db name or db + host
if (connString.indexOf('/') < 0) {
connString = 'localhost/' + connString
}
this._getServer = thunky(function (cb) {
toMongodbCore(options.connString, function (err, server) {
if (err) return cb(new Error('You must pass a connection string or a mongojs instance.'))
cb(null, server)
if (connString.indexOf('mongodb://') < 0) {
connString = 'mongodb://' + connString
}
this._getConnection = thunky(function (cb) {
mongodb.connect(connString, options, function (err, db) {
if (err) {
self.emit('error', err) // It's safer to emit an error instead of rely on the cb to handle the error
return cb(err)
}
self.emit('connect')
cb(null, db)
})
})
} else if (typeof connString._getConnection === 'function') { // mongojs
this._dbname = connString._dbname
this._getConnection = connString._getConnection
} else { // try mongodb-native
this._dbname = parse(connString.options.url).dbName
this._getConnection = thunky(function (cb) {
cb(null, connString)
})
}
this.ObjectId = bson.ObjectId
options.cols = options.cols || []
options.cols.forEach(function (colName) {
this.ObjectId = mongodb.ObjectId
cols = cols || []
cols.forEach(function (colName) {
self[colName] = self.collection(colName)

@@ -53,16 +68,20 @@

Database.prototype.on = function (event, handler) {
this._server.on(event, handler)
return this
}
util.inherits(Database, EventEmitter)
Database.prototype.collection = function (colName) {
return new Collection({name: colName, dbname: this._dbname}, this._getServer)
return new Collection({name: colName}, this._getConnection)
}
Database.prototype.close = function (cb) {
Database.prototype.close = function (force, cb) {
if (typeof force === 'function') { return this.close(false, force) }
var self = this
cb = cb || noop
this._getServer(function (err, server) {
this._getConnection(function (err, server) {
if (err) return cb(err)
server.destroy(true, true)
server.close(force)
self.emit('close')
cb()

@@ -80,8 +99,7 @@ })

var self = this
this._getServer(function (err, server) {
this._getConnection(function (err, connection) {
if (err) return cb(err)
server.command(self._dbname + '.$cmd', opts, function (err, result) {
connection.command(opts, function (err, result) {
if (err) return cb(err)
cb(null, result.result)
cb(null, result)
})

@@ -92,7 +110,12 @@ })

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('.')
}))
function mapCollectionNames (collection) {
return collection.name
}
this._getConnection(function (err, connection) {
if (err) { return cb(err) }
connection.listCollections().toArray(function (err, collections) {
if (err) { return cb(err) }
cb(null, collections.map(mapCollectionNames))
})
})

@@ -99,0 +122,0 @@ }

@@ -9,3 +9,3 @@ {

],
"version": "1.4.1",
"version": "2.0.0",
"repository": "git://github.com/mafintosh/mongojs.git",

@@ -32,3 +32,3 @@ "author": "Mathias Buus Madsen <mathiasbuus@gmail.com>",

"each-series": "^1.0.0",
"mongodb-core": "^1.2.8",
"mongodb": "^2.0.45",
"once": "^1.3.2",

@@ -45,9 +45,9 @@ "parse-mongo-url": "^1.1.0",

"cover": "node --harmony --harmony-proxies node_modules/istanbul/lib/cli.js cover node_modules/tape/bin/tape test/test-*.js --report html",
"geotag": "geopkg"
"geotag": "geopkg update"
},
"devDependencies": {
"concat-stream": "^1.5.0",
"geopkg": "^2.1.2",
"istanbul": "^0.3.17",
"standard": "^4.5.4",
"geopkg": "^4.0.3",
"istanbul": "^0.4.1",
"standard": "^5.4.1",
"tape": "^4.0.1"

@@ -54,0 +54,0 @@ },

@@ -34,4 +34,7 @@ # mongojs

// connect using SCRAM-SHA-1 mechanism
var db = mongojs('username:password@example.com/mydb', ['mycollection'], {authMechanism: 'ScramSHA1'})
var db = mongojs('username:password@example.com/mydb?authMechanism=SCRAM-SHA-1', ['mycollection'])
// connect using a different auth source
var db = mongojs('username:password@example.com/mydb?authSource=authdb', ['mycollection'])
// connect now, and worry about collections later

@@ -42,3 +45,3 @@ var db = mongojs('mydb')

__Attention MongoDB 3 users:__ In MongoDB 3 the default auth mechanism is ScramSHA1 not MongoCR (the default auth mechanism in mongojs). When connecting to an auth enabled MongoDB 3 instance providing the authMechanism option value 'ScramSHA1' is mandatory!
[More connection string examples](http://mongodb.github.io/node-mongodb-native/2.0/reference/connecting/authenticating/)

@@ -45,0 +48,0 @@ After we connected we can query or update the database just how we would using the mongo API with the exception that we use a callback.

@@ -19,3 +19,2 @@ var test = require('./tape')

})
})

@@ -22,0 +21,0 @@ })

@@ -15,3 +15,3 @@ var insert = require('./insert')

console.log(err, types)
var arr = types.map(function (x) {return x.foo})
var arr = types.map(function (x) { return x.foo })
console.log('arr', arr)

@@ -28,3 +28,3 @@ t.equal(types.length, 2)

strm.pipe(concat(function (types) {
var arr = types.map(function (x) {return x.foo})
var arr = types.map(function (x) { return x.foo })
t.equal(types.length, 2)

@@ -31,0 +31,0 @@ t.notEqual(arr.indexOf('fire'), -1)

@@ -15,3 +15,3 @@ var insert = require('./insert')

console.log(err, types)
var arr = types.map(function (x) {return x._id})
var arr = types.map(function (x) { return x._id })
t.equal(types.length, 2)

@@ -24,3 +24,3 @@ t.notEqual(arr.indexOf('fire'), -1)

strm.pipe(concat(function (types) {
var arr = types.map(function (x) {return x._id})
var arr = types.map(function (x) { return x._id })
t.equal(types.length, 2)

@@ -27,0 +27,0 @@ t.notEqual(arr.indexOf('fire'), -1)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc