Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial working commit

  • Loading branch information...
commit e06b0250978b9a0d600a8fbcba5dc35a2ae6c5d5 1 parent de57a6d
@vpulim authored
View
2  index.js
@@ -1 +1 @@
-module.exports = require('./lib/database.js');
+module.exports = require('./lib/db.js');
View
64 lib/collection.js
@@ -0,0 +1,64 @@
+var Cursor = require('./cursor').Cursor;
+
+var Collection = function(name) {
+ this.name = name;
+ this._collection = null;
+ this._isConnected = false;
+ this._waiting = [];
+}
+
+Collection.prototype._connected = function(collection) {
+ var self = this;
+
+ self._collection = collection;
+ self._isConnected = true;
+ self._waiting.forEach(function(cursor) {
+ var fn = collection[cursor.command],
+ c = fn.apply(collection, cursor.args);
+ cursor._connected(c);
+ })
+ self._waiting = [];
+}
+
+Collection.functions = [
+ 'insert',
+ 'remove',
+ 'rename',
+ 'insertAll',
+ 'save',
+ 'update',
+ 'distinct',
+ 'count',
+ 'drop',
+ 'findAndModify',
+ 'find',
+ 'findOne',
+ 'createIndex',
+ 'ensureIndex',
+ 'indexInformation',
+ 'dropIndex',
+ 'dropIndexes',
+ 'mapReduce',
+ 'group',
+ 'options'
+];
+
+Collection.functions.forEach(function(name) {
+ Collection.prototype[name] = function() {
+ var self = this,
+ args = Array.prototype.slice.call(arguments),
+ cursor = new Cursor(name, args);
+
+ self._waiting.push(cursor);
+
+ if (self._isConnected) {
+ process.nextTick(function() {
+ self._connected(self._collection);
+ })
+ }
+
+ return cursor;
+ }
+})
+
+exports.Collection = Collection;
View
57 lib/cursor.js
@@ -0,0 +1,57 @@
+var Cursor = function(command, args) {
+ this.command = command;
+ this.args = args;
+ this._cursor = null;
+ this._waiting = [];
+ this._hasCallback = false;
+}
+
+Cursor.prototype._connected = function(cursor) {
+ var self = this;
+
+ self._cursor = cursor;
+ self._isConnected = true;
+
+ self._waiting.forEach(function(op) {
+ var fn = self._cursor[op.command];
+ fn.apply(self._cursor, op.args)
+ });
+ self._waiting = [];
+}
+
+Cursor.functions = [
+ 'rewind',
+ 'toArray',
+ 'each',
+ 'count',
+ 'sort',
+ 'limit',
+ 'skip',
+ 'batchSize',
+ 'nextObject',
+ 'getMore',
+ 'explain',
+ 'close'
+];
+
+Cursor.functions.forEach(function(name) {
+ Cursor.prototype[name] = function() {
+ var self = this,
+ args = Array.prototype.slice.call(arguments);
+
+ self._waiting.push({
+ command: name,
+ args: args
+ });
+
+ if (self._isConnected) {
+ process.nextTick(function() {
+ self._connected(self._cursor);
+ })
+ }
+
+ return self;
+ }
+});
+
+exports.Cursor = Cursor;
View
1  lib/database.js
@@ -1 +0,0 @@
-
View
83 lib/db.js
@@ -0,0 +1,83 @@
+var mongodb = require('mongodb'),
+ url = require('url'),
+ util = require('util'),
+ EventEmitter = require('events').EventEmitter,
+ Collection = require('./collection').Collection;
+
+var Database = function(name, serverURIs, options) {
+ EventEmitter.call(this);
+
+ var self = this,
+ servers = [],
+ serverConfig;
+
+ options = options || {};
+
+ self._waiting = [];
+ self._isConnected = false;
+
+ if (Array.isArray(serverURIs)) {
+ serverURIs.forEach(function(uri) {
+ servers.push(Database._serverFromUri(uri, options));
+ })
+ serverConfig = new mongodb.ReplSetServers(servers, options);
+ }
+ else {
+ serverConfig = Database._serverFromUri(serverURIs, options);
+ }
+
+ self._db = new mongodb.Db(name, serverConfig, { native_parser:true })
+
+ self._db.open(function(err) {
+ if (err) {
+ self.emit('error', err);
+ }
+ else {
+ self._connected();
+ }
+ })
+}
+util.inherits(Database, EventEmitter);
+
+Database.prototype.collection = function(name) {
+ var self = this,
+ collection = new Collection(name);
+
+ self._waiting.push(collection);
+
+ if (self._isConnected) {
+ process.nextTick(function() {
+ self._connected();
+ })
+ }
+
+ return collection;
+}
+
+Database.prototype._connected = function() {
+ var self = this,
+ db = self._db;
+
+ self._isConnected = true;
+
+ self._waiting.forEach(function(collection) {
+ db.collection(collection.name, function(err, c) {
+ if (err) {
+ self.emit('error', err);
+ }
+ else {
+ collection._connected(c);
+ }
+ });
+ });
+ self._waiting = [];
+}
+
+Database._serverFromUri = function(uri, options) {
+ uri = url.parse(uri);
+ if (uri.protocol !== 'mongodb:')
+ throw new Error('database URI must start with mongodb://')
+ return new mongodb.Server(uri.hostname, +(uri.port || 27017), options)
+}
+
+exports.Database = Database;
Please sign in to comment.
Something went wrong with that request. Please try again.