Skip to content

Commit

Permalink
Merge pull request #256 from mafintosh/saintedlama/mongodb-native
Browse files Browse the repository at this point in the history
Use mongodb-native instead of mongodb-core driver
  • Loading branch information
saintedlama committed Dec 2, 2015
2 parents 5e04437 + 3c59a49 commit 2111916
Show file tree
Hide file tree
Showing 30 changed files with 482 additions and 331 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.DS_Store
coverage/
*.*~
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ var db = mongojs('example.com/mydb', ['mycollection'])
var db = mongojs('username:[email protected]/mydb', ['mycollection'])

// connect using SCRAM-SHA-1 mechanism
var db = mongojs('username:[email protected]/mydb', ['mycollection'], {authMechanism: 'ScramSHA1'})
var db = mongojs('username:[email protected]/mydb?authMechanism=SCRAM-SHA-1', ['mycollection'])

// connect using a different auth source
var db = mongojs('username:[email protected]/mydb?authSource=authdb', ['mycollection'])

// connect now, and worry about collections later
var db = mongojs('mydb')
var mycollection = db.collection('mycollection')
```

__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/)

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.
The format for callbacks is always `callback(error, value)` where error is null if no exception has occured. The update methods `save`, `remove`, `update` and `findAndModify` also pass the `lastErrorObject` as the last argument to the callback function.
Expand Down
28 changes: 19 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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]
db[prop] = db.collection(prop)
return db[prop]
Expand All @@ -20,9 +24,15 @@ module.exports = function (connString, cols, options) {
}

// 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
24 changes: 0 additions & 24 deletions lib/aggregation-cursor.js

This file was deleted.

15 changes: 7 additions & 8 deletions lib/bulk.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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
this._cmds = []
this._currCmd = null
this._ordered = ordered
this._onserver = onserver
this._dbname = dbname
this._getConnection = onserver

var self = this
this.find = function (query) {
Expand Down Expand Up @@ -145,12 +144,12 @@ Bulk.prototype.execute = function (cb) {
}

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()
})
}, function (err) {
Expand Down
Loading

0 comments on commit 2111916

Please sign in to comment.