Skip to content
This repository has been archived by the owner on Dec 23, 2018. It is now read-only.

Commit

Permalink
added observable cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Condon committed Apr 13, 2012
1 parent f278085 commit 82b2ad9
Show file tree
Hide file tree
Showing 39 changed files with 327 additions and 375 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ Inspired by [meteor](http://meteor.com)
- I wanted a decoupled library with the same level of functionality provided in [meteor](http://meteor.com)
- Cached on the client-side, and server-side. Less hits to mongodb itself.
- Allows for users to be sandboxed in their own collection (authentication).
- Ability
17 changes: 17 additions & 0 deletions docs/pseudo-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var Profile = new Schema({
comments: { ref: 'Comment' },
email: String,
password: String
});


var Comment = new Schema({
profile: { ref: 'Profile' },
title: String,
message: String
});





45 changes: 45 additions & 0 deletions examples/memory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var mdblite = require("../lib"),
db = mdblite.db(new mdblite.drivers.Memory());

var helloCollection = db.collection("hello");


helloCollection.insert([
{
name: "Craig",
age: 22
},
{
name: "Tim",
age:21
},
{
name: "John",
age:22
}
]);

setTimeout(function() {
helloCollection.insert([
{
name: "Sarah",
age: 22
}
]);
}, 1000);

var cursor = helloCollection.find({age:{$gt:21}});

cursor.on("insert", function(item) {
console.log("new item added!");
console.log(item)
});




cursor.each(function(err, item) {
if(item) console.log(item);
})


6 changes: 6 additions & 0 deletions lib/abstract/abstract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

module.exports = {
_abstract: function() {
throw new Error("Not implemented");
}
}
13 changes: 2 additions & 11 deletions src/common/abstract/collection.js → lib/abstract/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ EventEmitter = require('events').EventEmiter,
ObjectId = require('../types/objectid'),
_ = require('underscore');

var Collection = module.exports = structr(EventEmitter, {
var Collection = module.exports = structr(EventEmitter, require("./abstract"), {

/**
* constructor.
Expand All @@ -14,7 +14,7 @@ var Collection = module.exports = structr(EventEmitter, {
__construct: function(database, name) {
this.db = database;
this.name = name;
}
},

/**
* finds more than one item in a collection
Expand Down Expand Up @@ -88,14 +88,5 @@ var Collection = module.exports = structr(EventEmitter, {

reset: function(items) {
this._abstract();
},


/**
* throws an error
*/

__abstract: function() {
throw new Error("Not implemented");
}
});
4 changes: 2 additions & 2 deletions src/common/abstract/cursor.js → lib/abstract/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = structr({
this.selector = selector;
this.collection = collection;
this.options = options || {};
this._sift = sift(this.selector);
this.sift = sift(this.selector);
this._position = 0;
},

Expand Down Expand Up @@ -82,7 +82,7 @@ module.exports = structr({
if(!document) return fn(err, docs);
docs.push(document);
});
},
}

/**
*/
Expand Down
11 changes: 11 additions & 0 deletions lib/abstract/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var structr = require("structr");

module.exports = structr(require("./abstract"), {

/**
*/

connect: function() {
this._abstract();
}
});
20 changes: 16 additions & 4 deletions src/common/chainable.js → lib/chainable.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ module.exports = structr({
*/

"__construct": function() {
this._queue = tq.queue();
this._isReady = false;
this._em = new EventEmitter();
this._queue = tq.queue().start();
this._em = new EventEmitter();
},

/**
Expand All @@ -26,6 +26,13 @@ module.exports = structr({
this._em.on(type, callback);
},

/**
*/

"emit": function(type, value) {
this._em.emit(type, value);
},

/**
* pushes a callback to the queue
*/
Expand All @@ -39,12 +46,15 @@ module.exports = structr({
*/

"target": function(value) {

if(!arguments.length) return this._target;

if(!this._target) {
this._target = value;
this._em.emit("ready");
this._em.removeAllListeners("ready");
}

return value;
},

Expand All @@ -65,11 +75,13 @@ module.exports = structr({
*/

"_wrap": function() {

var args = arguments, self = this;

return function() {
var i = 0;
for(; n = args.length; i < n; i++) {
args[i].apply(self, arguments);
for(n = args.length; i < n; i++) {
(args[i] || function(){}).apply(self, arguments);
}
}
}
Expand Down
45 changes: 38 additions & 7 deletions src/common/collection.js → lib/collection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var Cursor = require('./cursor');
var Cursor = require('./cursor'),
_ = require("underscore"),
ObjectId = require("./types/objectid");

/**
* wrapper for the drivers
Expand All @@ -24,13 +26,22 @@ var Cursor = require('./cursor');
//ready? get the collection
this.next(function() {
var next = this;
db.target.collection(name, function(target) {
db.target().collection(name, function(err, target) {
self.target(target);
next();
})
});
});
},


/**
*/

"modelClass": function(value) {
if(!arguments.length) return this._modelClass;
return this._modelClass = value;
},

/**
*/

Expand Down Expand Up @@ -66,37 +77,57 @@ var Cursor = require('./cursor');


"insert": function(item, next) {

var items = item instanceof Array ? item : [item],
self = this;

this.next(function() {
self.collection.insert(items, self._wrap(next || function(){}, this));

_.each(items, function(item) {
if(!item._id) item._id = new ObjectId();
})

self.target().insert(items, self._wrap(next || function(){}, this, function(err, items) {
if(err) return;
self.emit("insert", items);
}));
});
},

/**
*/

"ensureIndex": function(key) {
this.next(function() {
self.target().ensureIndex(key);
})
},

/**
*/

"update": function(selector, toUpdate, options, next) {

var self = this;

if(typeof options == 'function') {
next = options;
next = options;
options = undefined;
}

this.next(function() {
self.collection.update(selector, toUpdate, options || {}, self._wrap(next || function(){}, this))
self.target().update(selector, toUpdate, options || {}, self._wrap(next || function(){}, this))
});
},

/**
*/

"remove": function(selector, next) {

var self = this;
this.next(function() {
self.colletion.remove(selector, self._wrap(next || function(){}, next));
self.target().remove(selector, self._wrap(next || function(){}, next));
});
}

Expand Down
27 changes: 22 additions & 5 deletions src/common/cursor.js → lib/cursor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var tq = require('tq'),
Chainable = require('./chainable');
Chainable = require('./chainable'),
Observer = require("./cursorObserver");

module.exports = require("./abstract/cursor").extend({

Expand All @@ -14,7 +15,7 @@ module.exports = require("./abstract/cursor").extend({

this._chain.next(function() {
collection.ready(this);
})
});
},

/**
Expand All @@ -29,15 +30,30 @@ module.exports = require("./abstract/cursor").extend({
*/

"target": function() {
return this._chain.apply(this._chain, arguments);
return this._chain.target.apply(this._chain, arguments);
},



/**
* listens for insert / update / remove
*/

on: function(type, callback) {
if(!this._em) {
this._observer = new Observer(this);
}
this._observer.on(type, callback);
},


/**
*/


"nextObject": function(fn) {
var self = this;

this._cursor(function(cursor) {
cursor.nextObject(function(err, item) {
if(!item) return fn(err);
Expand All @@ -56,8 +72,8 @@ module.exports = require("./abstract/cursor").extend({
}

this._waiting = true;

var self = this;

this._chain.next(function() {
var next = this;
self.collection.target().find(self.selector, self.options, function(err, cursor) {
Expand All @@ -67,4 +83,5 @@ module.exports = require("./abstract/cursor").extend({
});
});
}
})
});

Loading

0 comments on commit 82b2ad9

Please sign in to comment.