Skip to content
This repository was archived by the owner on Jul 15, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/node_modules/
node_modules/
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,42 @@ Mongo Sync is a synchronous MongoDB driver for use with [Common Node](http://ole

It is a thin wrapper around the official MongoDB driver for Node. Here is a quick usage example that you can use with Common Node:

```javascript
var Server = require("mongo-sync").Server;
var Client = require("mongo-sync").MongoClient;
var server = new Server('127.0.0.1');
var result = server.db("test").getCollection("posts").find().toArray();
var client = new Client(server)
var result = client.db("test").getCollection("posts").find().toArray();
console.log(result);
server.close();

```

For a complete application using this driver, check out [Notes](https://github.com/olegp/notes/).

If you want to use Mongo Sync directly with [node-fibers](https://github.com/laverdet/node-fibers) (i.e. without Common Node), make sure all database requests occur within a fiber, e.g.:

```javascript
var Fiber = require('fibers');
Fiber(function() {
// set up goes here ...
var result = server.db("LashDB").getCollection("users").find().toArray();
}).run();
```

### Tests

[![Build Status](https://travis-ci.org/olegp/mongo-sync.svg?branch=master)](https://travis-ci.org/olegp/mongo-sync)

Run the unit tests with the following command:

```javascript
common-node test/test.js
```

or using Fibers
```javascript
node test/test-fiber.js
```

### Community

Expand Down
222 changes: 155 additions & 67 deletions lib/mongo-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,63 @@ function sync(obj, fn) {

function Server(server, options) {
server = (server || '127.0.0.1').split(':');
var host = server[0], port = server[1] || 27017;
this._server = new mongodb.Server(host, port, objects.merge(options, {
this._host = server[0]
this._port = server[1] || 27017;
this._server = new mongodb.Server(this._host, this._port, objects.merge(options, {
poolSize:2,
socketOptions:{
noDelay:true
},
auto_reconnect:true,
disableDriverBSONSizeCheck:true
}));

}

exports.Server = Server;

Server.prototype.db = function(name) {
var result = new DB(this, new mongodb.Db(name, this._server, {w:1}));
result.open();
return result;
};

Server.prototype.connect = function(uri) {
return new DB(this, sync('MongoClient', 'connect').call(mongodb, uri));
};
function Client(server, options) {
this._server = server
this._client = new mongodb.MongoClient(server, options)
}

Server.prototype.close = function() {
this._server.close();
};
function DB(client, name) {
this._server = client._server;
this._client = client;
this._db = client.db(name);
}

function DB(server, db) {
this._server = server;
function Collection(db, name) {
if (arguments.length < 2) {
throw new Error('too few arguments');
}
this._db = db;
this._collection = db._db.collection(name);
}

function Cursor(cursor) {
this._cursor = cursor;
this._mapper = function(value) {
return value;
};
}

exports.Server = Server;
exports.Client = Client;
exports.DB = DB;
exports.Collection = Collection;

Server.prototype.close = function() {
this._client.close();
};

Client.prototype.connect = function(uri) {
var client = sync('_client', 'connect').call(this, uri);
var dbname = uri.split('/').slice(-1)[0]
if (dbname === undefined) {
throw new Error('Database name is missing')
}
this._db = new DB(client, dbname);
return this._db;
};

DB.prototype.getMongo = function() {
return this._server;
Expand All @@ -78,6 +102,22 @@ DB.prototype.getCollection = function(name) {
return new Collection(this, name);
};

DB.prototype.runCommand = sync('_db', 'command');
// DB.prototype.auth = sync('_db', 'authenticate');

// Just to be backward compatible
DB.prototype.getCollection = function() {
return this.listCollections();
}

DB.prototype.listCollections = function() {
this._res = this._db.listCollections();
cols = sync('_res', 'toArray').call(this);
return cols.map(function(entry) {
return entry.name;
});
};

[
'addUser',
'dropDatabase',
Expand All @@ -88,56 +128,80 @@ DB.prototype.getCollection = function(name) {
DB.prototype[name] = sync('_db', name);
});

DB.prototype.runCommand = sync('_db', 'command');
DB.prototype.auth = sync('_db', 'authenticate');
Collection.prototype.getDB = function() {
return this._db;
};

function Collection(db, name) {
if (arguments.length < 2) {
throw new Error('too few arguments');
// Keep backward compatibility
Collection.prototype.insert = function(doc) {
if (Array.isArray(doc)) {
return this.insertMany(doc);
} else {
return this.insertOne(doc);
}
this._db = db;
this._collection = db._db.collection(name);
}
};

exports.Collection = Collection;
Collection.prototype.insertMany = (function() {
var insert = sync('_collection', 'insertMany');
return function(query, docs) {
return insert.call(this, query, docs);
};
})();

Collection.prototype.getDB = function() {
return this._db;
Collection.prototype.insertOne = (function() {
var insert = sync('_collection', 'insertOne');
return function(query, single) {
return insert.call(this, query, {single:single});
};
})();

// Keep backward compatibility
Collection.prototype.remove = function(doc, flag) {
// if flag justOne is set
if (flag) {
return this.removeOne(doc);
}
// If nothing is set
else {
return this.removeMany(doc);
}
};

[
'aggregate',
'count',
'distinct',
'drop',
'dropIndex',
'ensureIndex',
'findOne',
'insert',
'mapReduce',
'save'
].forEach(function(name) {
Collection.prototype[name] = sync('_collection', name);
});
Collection.prototype.removeMany = (function() {
var remove = sync('_collection', 'removeMany');
return function(query, docs) {
return remove.call(this, query, docs);
};
})();

Collection.prototype.remove = (function() {
var remove = sync('_collection', 'remove');
Collection.prototype.removeOne = (function() {
var remove = sync('_collection', 'removeOne');
return function(query, single) {
return remove.call(this, query, {single:single});
};
})();

Collection.prototype.findAndModify = (function() {
var findAndModify = sync('_collection', 'findAndModify');
return function(document) {
return findAndModify.call(this, document.query, document.sort, document.update, document);
// Keep backward compatibility
Collection.prototype.update = function(query, doc) {
if (Array.isArray(doc)) {
return this.updateMany(query, doc);
} else {
return this.updateOne(query, doc);
}
};

Collection.prototype.updateOne = (function() {
var update = sync('_collection', 'updateOne');
return function() {
if (arguments.length < 2) {
throw new Error('too few arguments');
}
return update.apply(this, arguments);
};
})();

Collection.prototype.getIndexes = sync('_collection', 'indexes');

Collection.prototype.update = (function() {
var update = sync('_collection', 'update');
Collection.prototype.updateMany = (function() {
var update = sync('_collection', 'updateMany');
return function() {
if (arguments.length < 2) {
throw new Error('too few arguments');
Expand All @@ -146,26 +210,38 @@ Collection.prototype.update = (function() {
};
})();

Collection.prototype.findAndModify = (function() {
var findAndModify = sync('_collection', 'findAndModify');
return function(document) {
return findAndModify.call(this, document.query, document.sort, document.update, document);
};
})();

Collection.prototype.save = function() {
var save = sync('_collection', 'save');
return function(doc) {
console.log(doc);
return save.call(this, doc);
};
};

Collection.prototype.find = function() {
return new Cursor(this._collection.find.apply(this._collection, arguments));
};

function Cursor(cursor) {
this._cursor = cursor;
this._mapper = function(value) {
return value;
};
}
Collection.prototype.getIndexes = sync('_collection', 'indexes');

[
'limit',
'skip',
'sort'
'aggregate',
'count',
'distinct',
'drop',
'dropIndex',
'ensureIndex',
'findOne',
'mapReduce'
].forEach(function(name) {
Cursor.prototype[name] = function() {
this._cursor[name].apply(this._cursor, arguments);
return this;
};
Collection.prototype[name] = sync('_collection', name);
});

(function() {
Expand Down Expand Up @@ -204,4 +280,16 @@ Cursor.prototype.toArray = (function() {
};
})();

Cursor.prototype.close = sync('_cursor', 'close');
Cursor.prototype.close = sync('_cursor', 'close');

[
'limit',
'skip',
'sort'
].forEach(function(name) {
Cursor.prototype[name] = function() {
this._cursor[name].apply(this._cursor, arguments);
return this;
};
});

Loading