Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

general improvements

  • Loading branch information...
commit da900c7a1a45a43dbe6e1f3c49beaecdfcac3638 1 parent fb311d0
@vdemedes authored
Showing with 106 additions and 74 deletions.
  1. +0 −1  Readme.md
  2. +34 −26 lib/mongorito.coffee
  3. +71 −46 lib/mongorito.js
  4. +1 −1  package.json
View
1  Readme.md
@@ -9,7 +9,6 @@
- Based on Mongolian
- Super-fast, lightweight, all codebase is in one file
- Covered by tests
-- Used in production projects
- Active development
# Installation
View
60 lib/mongorito.coffee
@@ -124,6 +124,8 @@ class MongoritoModel
query = (id, done) ->
Client.collection(that.collection).find({ _id: new mongolian.ObjectId(id.toString()) }).toArray (err, items) ->
process.nextTick ->
+ for item in items
+ item._id = item._id.toString()
done err, items
if not Cache
@@ -137,12 +139,12 @@ class MongoritoModel
if not result
query id, (err, items) ->
Cache.set key, JSON.stringify(items), 2592000, [that.collection], ->
- models = that.bakeModelsFromItems items, that.model
- process.nextTick ->
- callback err, models
+ process.nextTick ->
+ models = that.bakeModelsFromItems items, that.model
+ callback err, models
else
- models = that.bakeModelsFromItems JSON.parse(result), that.model
process.nextTick ->
+ models = that.bakeModelsFromItems JSON.parse(result), that.model
callback err, models
@findWithOrderAndLimit: (criteria, order, limit, skip, callback) ->
@@ -166,12 +168,14 @@ class MongoritoModel
query = (criteria, order, limit, skip, done) ->
Client.collection(that.collection).find(criteria).sort(order).limit(limit).skip(skip).toArray (err, items) ->
+ for item in items
+ item._id = item._id.toString()
done err, items
if not Cache
return query criteria, order, limit, skip, (err, items) ->
- models = that.bakeModelsFromItems items, that.model
process.nextTick ->
+ models = that.bakeModelsFromItems items, that.model
callback err, models
key = "#{ @collection }-order-#{ order }-limit-#{ limit }-skip-#{ skip }-#{ JSON.stringify(criteria) }"
@@ -180,12 +184,12 @@ class MongoritoModel
if not result
query criteria, order, limit, skip, (err, items) ->
Cache.set key, JSON.stringify(items), 2592000, [that.collection], ->
- models = that.bakeModelsFromItems items, that.model
- process.nextTick ->
- callback err, models
+ process.nextTick ->
+ models = that.bakeModelsFromItems items, that.model
+ callback err, models
else
- models = that.bakeModelsFromItems JSON.parse(result), that.model
process.nextTick ->
+ models = that.bakeModelsFromItems JSON.parse(result), that.model
callback err, models
@findWithOrder: (criteria, order, callback) ->
@@ -199,12 +203,14 @@ class MongoritoModel
query = (criteria, order, done) ->
Client.collection(that.collection).find(criteria).sort(order).toArray (err, items) ->
+ for item in items
+ item._id = item._id.toString()
done err, items
if not Cache
return query criteria, order, (err, items) ->
- models = that.bakeModelsFromItems items, that.model
process.nextTick ->
+ models = that.bakeModelsFromItems items, that.model
callback err, models
key = "#{ @collection }-order-#{ order }-#{ JSON.stringify(criteria) }"
@@ -213,12 +219,12 @@ class MongoritoModel
if not result
query criteria, order, (err, items) ->
Cache.set key, JSON.stringify(items), 2592000, [that.collection], ->
- models = that.bakeModelsFromItems items, that.model
- process.nextTick ->
- callback err, models
+ process.nextTick ->
+ models = that.bakeModelsFromItems items, that.model
+ callback err, models
else
- models = that.bakeModelsFromItems JSON.parse(result), that.model
process.nextTick ->
+ models = that.bakeModelsFromItems JSON.parse(result), that.model
callback err, models
@findWithLimit: (criteria, limit, skip, callback) ->
@@ -244,13 +250,14 @@ class MongoritoModel
query = (criteria, limit, skip, done) ->
Client.collection(that.collection).find(criteria).limit(limit).skip(skip).toArray (err, items) ->
- process.nextTick ->
- done err, items
+ for item in items
+ item._id = item._id.toString()
+ done err, items
if not Cache
return query criteria, limit, skip, (err, items) ->
- models = that.bakeModelsFromItems items, that.model
process.nextTick ->
+ models = that.bakeModelsFromItems items, that.model
callback err, models
key = "#{ @collection }-limit-#{ limit }-skip-#{ skip }-#{ JSON.stringify(criteria) }"
@@ -259,12 +266,12 @@ class MongoritoModel
if not result
query criteria, limit, skip, (err, items) ->
Cache.set key, JSON.stringify(items), 2592000, [that.collection], ->
- models = that.bakeModelsFromItems items, that.model
- process.nextTick ->
- callback err, models
+ process.nextTick ->
+ models = that.bakeModelsFromItems items, that.model
+ callback err, models
else
- models = that.bakeModelsFromItems JSON.parse(result), that.model
process.nextTick ->
+ models = that.bakeModelsFromItems JSON.parse(result), that.model
callback err, models
@find: (criteria = {}, callback) ->
@@ -276,13 +283,14 @@ class MongoritoModel
query = (criteria, done) ->
Client.collection(that.collection).find(criteria).toArray (err, items) ->
- process.nextTick ->
- done err, items
+ for item in items
+ item._id = item._id.toString()
+ done err, items
if not Cache
return query criteria, (err, items) ->
- models = that.bakeModelsFromItems items, that.model
process.nextTick ->
+ models = that.bakeModelsFromItems items, that.model
callback err, models
key = "#{ @collection }-#{ JSON.stringify(criteria) }"
@@ -290,13 +298,13 @@ class MongoritoModel
Cache.get key, (err, result) ->
if not result
query criteria, (err, items) ->
- models = that.bakeModelsFromItems items, that.model
Cache.set key, JSON.stringify(items), 2592000, [that.collection], ->
process.nextTick ->
+ models = that.bakeModelsFromItems items, that.model
callback err, models
else
- models = that.bakeModelsFromItems JSON.parse(result), that.model
process.nextTick ->
+ models = that.bakeModelsFromItems JSON.parse(result), that.model
callback err, models
save: (callback) ->
View
117 lib/mongorito.js
@@ -165,6 +165,11 @@ MongoritoModel = (function() {
_id: new mongolian.ObjectId(id.toString())
}).toArray(function(err, items) {
return process.nextTick(function() {
+ var item, _i, _len;
+ for (_i = 0, _len = items.length; _i < _len; _i++) {
+ item = items[_i];
+ item._id = item._id.toString();
+ }
return done(err, items);
});
});
@@ -178,19 +183,20 @@ MongoritoModel = (function() {
}
key = "" + this.collection + "-" + (id.toString());
return Cache.get(key, function(err, result) {
- var models;
if (!result) {
return query(id, function(err, items) {
- var models;
- Cache.set(key, JSON.stringify(items), 2592000, [that.collection], function() {});
- models = that.bakeModelsFromItems(items, that.model);
- return process.nextTick(function() {
- return callback(err, models);
+ return Cache.set(key, JSON.stringify(items), 2592000, [that.collection], function() {
+ return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(items, that.model);
+ return callback(err, models);
+ });
});
});
} else {
- models = that.bakeModelsFromItems(JSON.parse(result), that.model);
return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(JSON.parse(result), that.model);
return callback(err, models);
});
}
@@ -222,33 +228,39 @@ MongoritoModel = (function() {
that = this;
query = function(criteria, order, limit, skip, done) {
return Client.collection(that.collection).find(criteria).sort(order).limit(limit).skip(skip).toArray(function(err, items) {
+ var item, _i, _len;
+ for (_i = 0, _len = items.length; _i < _len; _i++) {
+ item = items[_i];
+ item._id = item._id.toString();
+ }
return done(err, items);
});
};
if (!Cache) {
return query(criteria, order, limit, skip, function(err, items) {
- var models;
- models = that.bakeModelsFromItems(items, that.model);
return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(items, that.model);
return callback(err, models);
});
});
}
key = "" + this.collection + "-order-" + order + "-limit-" + limit + "-skip-" + skip + "-" + (JSON.stringify(criteria));
return Cache.get(key, function(err, result) {
- var models;
if (!result) {
return query(criteria, order, limit, skip, function(err, items) {
- var models;
- Cache.set(key, JSON.stringify(items), 2592000, [that.collection], function() {});
- models = that.bakeModelsFromItems(items, that.model);
- return process.nextTick(function() {
- return callback(err, models);
+ return Cache.set(key, JSON.stringify(items), 2592000, [that.collection], function() {
+ return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(items, that.model);
+ return callback(err, models);
+ });
});
});
} else {
- models = that.bakeModelsFromItems(JSON.parse(result), that.model);
return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(JSON.parse(result), that.model);
return callback(err, models);
});
}
@@ -268,33 +280,39 @@ MongoritoModel = (function() {
that = this;
query = function(criteria, order, done) {
return Client.collection(that.collection).find(criteria).sort(order).toArray(function(err, items) {
+ var item, _i, _len;
+ for (_i = 0, _len = items.length; _i < _len; _i++) {
+ item = items[_i];
+ item._id = item._id.toString();
+ }
return done(err, items);
});
};
if (!Cache) {
return query(criteria, order, function(err, items) {
- var models;
- models = that.bakeModelsFromItems(items, that.model);
return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(items, that.model);
return callback(err, models);
});
});
}
key = "" + this.collection + "-order-" + order + "-" + (JSON.stringify(criteria));
return Cache.get(key, function(err, result) {
- var models;
if (!result) {
return query(criteria, order, function(err, items) {
- var models;
- Cache.set(key, JSON.stringify(items), 2592000, [that.collection], function() {});
- models = that.bakeModelsFromItems(items, that.model);
- return process.nextTick(function() {
- return callback(err, models);
+ return Cache.set(key, JSON.stringify(items), 2592000, [that.collection], function() {
+ return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(items, that.model);
+ return callback(err, models);
+ });
});
});
} else {
- models = that.bakeModelsFromItems(JSON.parse(result), that.model);
return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(JSON.parse(result), that.model);
return callback(err, models);
});
}
@@ -329,35 +347,39 @@ MongoritoModel = (function() {
that = this;
query = function(criteria, limit, skip, done) {
return Client.collection(that.collection).find(criteria).limit(limit).skip(skip).toArray(function(err, items) {
- return process.nextTick(function() {
- return done(err, items);
- });
+ var item, _i, _len;
+ for (_i = 0, _len = items.length; _i < _len; _i++) {
+ item = items[_i];
+ item._id = item._id.toString();
+ }
+ return done(err, items);
});
};
if (!Cache) {
return query(criteria, limit, skip, function(err, items) {
- var models;
- models = that.bakeModelsFromItems(items, that.model);
return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(items, that.model);
return callback(err, models);
});
});
}
key = "" + this.collection + "-limit-" + limit + "-skip-" + skip + "-" + (JSON.stringify(criteria));
return Cache.get(key, function(err, result) {
- var models;
if (!result) {
return query(criteria, limit, skip, function(err, items) {
- var models;
- Cache.set(key, JSON.stringify(items), 2592000, [that.collection], function() {});
- models = that.bakeModelsFromItems(items, that.model);
- return process.nextTick(function() {
- return callback(err, models);
+ return Cache.set(key, JSON.stringify(items), 2592000, [that.collection], function() {
+ return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(items, that.model);
+ return callback(err, models);
+ });
});
});
} else {
- models = that.bakeModelsFromItems(JSON.parse(result), that.model);
return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(JSON.parse(result), that.model);
return callback(err, models);
});
}
@@ -374,36 +396,39 @@ MongoritoModel = (function() {
that = this;
query = function(criteria, done) {
return Client.collection(that.collection).find(criteria).toArray(function(err, items) {
- return process.nextTick(function() {
- return done(err, items);
- });
+ var item, _i, _len;
+ for (_i = 0, _len = items.length; _i < _len; _i++) {
+ item = items[_i];
+ item._id = item._id.toString();
+ }
+ return done(err, items);
});
};
if (!Cache) {
return query(criteria, function(err, items) {
- var models;
- models = that.bakeModelsFromItems(items, that.model);
return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(items, that.model);
return callback(err, models);
});
});
}
key = "" + this.collection + "-" + (JSON.stringify(criteria));
return Cache.get(key, function(err, result) {
- var models;
if (!result) {
return query(criteria, function(err, items) {
- var models;
- models = that.bakeModelsFromItems(items, that.model);
return Cache.set(key, JSON.stringify(items), 2592000, [that.collection], function() {
return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(items, that.model);
return callback(err, models);
});
});
});
} else {
- models = that.bakeModelsFromItems(JSON.parse(result), that.model);
return process.nextTick(function() {
+ var models;
+ models = that.bakeModelsFromItems(JSON.parse(result), that.model);
return callback(err, models);
});
}
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "mongorito",
- "version": "0.2.1",
+ "version": "0.2.2",
"description": "ODM for MongoDB.",
"keywords": ["mongo", "mongodb"],
"author": "Vadim Demedes <sbioko@gmail.com>",
Please sign in to comment.
Something went wrong with that request. Please try again.