Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bump version

  • Loading branch information...
commit a6eababcc8cc2985c93bdc2c350899337addc3f4 1 parent bc8299d
@meritt authored
Showing with 68 additions and 14 deletions.
  1. +67 −13 lib/index.js
  2. +1 −1  package.json
View
80 lib/index.js
@@ -1,3 +1,4 @@
+// Generated by CoffeeScript 1.3.1
(function() {
var Db, EasyMongo, Server, ensureObjectId, isFunction, isObject, mongodb;
@@ -23,14 +24,20 @@
EasyMongo = (function() {
+ EasyMongo.name = 'EasyMongo';
+
EasyMongo.prototype.db = null;
EasyMongo.prototype.collection = {};
function EasyMongo(options) {
this.options = options;
- if (this.options.host == null) this.options.host = '127.0.0.1';
- if (this.options.port == null) this.options.port = 27017;
+ if (this.options.host == null) {
+ this.options.host = '127.0.0.1';
+ }
+ if (this.options.port == null) {
+ this.options.port = 27017;
+ }
}
EasyMongo.prototype.getInstance = function(table, after) {
@@ -61,7 +68,9 @@
return after(this.collection[table]);
} else {
return this.db.collection(table, function(error, collection) {
- if (error) console.log('Error with fetching collection: ' + error);
+ if (error) {
+ console.log('Error with fetching collection: ' + error);
+ }
_this.collection[table] = collection;
return after(collection);
});
@@ -71,7 +80,9 @@
EasyMongo.prototype.findById = function(table, id, after) {
var params,
_this = this;
- if (after == null) after = function() {};
+ if (after == null) {
+ after = function() {};
+ }
try {
params = {
_id: ensureObjectId(id)
@@ -91,6 +102,31 @@
});
};
+ EasyMongo.prototype.removeById = function(table, id, after) {
+ var params,
+ _this = this;
+ if (after == null) {
+ after = function() {};
+ }
+ try {
+ params = {
+ _id: ensureObjectId(id)
+ };
+ } catch (exception) {
+ console.log('Error with preparing params for removeById: ' + exception);
+ return after(false);
+ }
+ return this.getInstance(table, function(collection) {
+ return collection.findAndRemove(params, function(error, results) {
+ if (error) {
+ console.log('Error with removing document by id: ' + error);
+ return after(false);
+ }
+ return after(results);
+ });
+ });
+ };
+
EasyMongo.prototype.find = function(table, params, options, after) {
var _ref,
_this = this;
@@ -112,9 +148,15 @@
return this.getInstance(table, function(collection) {
var cursor;
cursor = collection.find(params);
- if (options.sort) cursor.sort(options.sort);
- if (options.limit) cursor.limit(options.limit);
- if (options.skip) cursor.skip(options.skip);
+ if (options.sort) {
+ cursor.sort(options.sort);
+ }
+ if (options.limit) {
+ cursor.limit(options.limit);
+ }
+ if (options.skip) {
+ cursor.skip(options.skip);
+ }
return cursor.toArray(function(error, results) {
if (error) {
console.log('Error with fetching documents: ' + error);
@@ -131,7 +173,9 @@
after = params;
params = null;
}
- if (after === null) after = (function() {});
+ if (after === null) {
+ after = (function() {});
+ }
return this.getInstance(table, function(collection) {
return collection.count(params, function(error, results) {
if (error) {
@@ -145,9 +189,13 @@
EasyMongo.prototype.save = function(table, params, after) {
var _this = this;
- if (after == null) after = function() {};
+ if (after == null) {
+ after = function() {};
+ }
try {
- if (params._id != null) params._id = ensureObjectId(params._id);
+ if (params._id != null) {
+ params._id = ensureObjectId(params._id);
+ }
} catch (exception) {
console.log('Error with preparing params for save: ' + exception);
return after(false);
@@ -167,7 +215,9 @@
EasyMongo.prototype.close = function() {
if (this.db !== null) {
- if (this.collection !== {}) this.collection = {};
+ if (this.collection !== {}) {
+ this.collection = {};
+ }
this.db.close();
this.db = null;
}
@@ -212,8 +262,12 @@
after = options;
options = {};
}
- if (!after) after = (function() {});
- if (!options) options = {};
+ if (!after) {
+ after = (function() {});
+ }
+ if (!options) {
+ options = {};
+ }
return [params, options, after];
};
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "easymongo"
- , "version": "0.1.1"
+ , "version": "0.1.2"
, "description": "Easy MongoDB"
, "keywords": ["mongo", "mongodb", "easy"]
, "author": "Alexey Simonenko <alexey@simonenko.su>"
Please sign in to comment.
Something went wrong with that request. Please try again.