Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit 4c2b6e5e7836eb4d9513ce6f3e4291da35e9c468 @meritt committed Feb 5, 2012
Showing with 189 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +12 −0 Cakefile
  3. +86 −0 lib/index.js
  4. +20 −0 package.json
  5. +17 −0 readme.md
  6. +52 −0 src/index.coffee
2 .gitignore
@@ -0,0 +1,2 @@
+.DS_Store
+node_modules/*
12 Cakefile
@@ -0,0 +1,12 @@
+{print} = require 'util'
+{spawn} = require 'child_process'
+
+build = (callback) ->
+ coffee = spawn 'coffee', ['-c', '-o', 'lib', 'src']
+ coffee.stderr.on 'data', (data) ->
+ process.stderr.write data.toString()
+ coffee.stdout.on 'data', (data) ->
+ print data.toString()
+
+task 'build', 'Build lib/ from src/', ->
+ build()
86 lib/index.js
@@ -0,0 +1,86 @@
+(function() {
+ var Db, EasyMongo, ObjectID, Server, ensureObjectId, _ref;
+
+ _ref = require('mongodb'), Db = _ref.Db, Server = _ref.Server, ObjectID = _ref.ObjectID;
+
+ ensureObjectId = function(id) {
+ if (typeof id === 'string') {
+ return new ObjectID(id);
+ } else {
+ return id;
+ }
+ };
+
+ EasyMongo = (function() {
+
+ function EasyMongo() {}
+
+ EasyMongo.prototype.configure = function(options) {
+ this.options = options;
+ if (this.options.host == null) this.options.host = '127.0.0.1';
+ if (this.options.port == null) return this.options.port = 27017;
+ };
+
+ EasyMongo.prototype.getInstance = function(table, after) {
+ var db;
+ if (this.options.db == null) {
+ throw new Error('The database name must be configured (options.db)');
+ }
+ db = new Db(this.options.db, new Server(this.options.host, this.options.port, {}));
+ return db.open(function(error, db) {
+ if (error) {
+ console.log('Error with connection to MongoDB server: ' + error);
+ }
+ return db.collection(table, function(error, collection) {
+ if (error) console.log('Error with fetching collection: ' + error);
+ return after(db, collection);
+ });
+ });
+ };
+
+ EasyMongo.prototype.findById = function(table, id, after) {
+ if (after == null) after = function() {};
+ return this.getInstance(table, function(db, collection) {
+ return collection.find({
+ _id: ensureObjectId(id)
+ }).toArray(function(error, results) {
+ if (error) console.log('Error with fetching document by id: ' + error);
+ db.close();
+ return after(results.length === 1 ? results[0] : false);
+ });
+ });
+ };
+
+ EasyMongo.prototype.find = function(table, params, after) {
+ if (after == null) after = function() {};
+ return this.getInstance(table, function(db, collection) {
+ if (params._id != null) params._id = ensureObjectId(params._id);
+ return collection.find(params).toArray(function(error, results) {
+ if (error) console.log('Error with fetching documents: ' + error);
+ db.close();
+ return after(results);
+ });
+ });
+ };
+
+ EasyMongo.prototype.save = function(table, params, after) {
+ if (after == null) after = function() {};
+ return this.getInstance(table, function(db, collection) {
+ if (params._id != null) params._id = ensureObjectId(params._id);
+ return collection.save(params, {
+ safe: true
+ }, function(error, results) {
+ if (error) console.log('Error with saving data: ' + error);
+ db.close();
+ return after(params._id != null ? params : results[0]);
+ });
+ });
+ };
+
+ return EasyMongo;
+
+ })();
+
+ module.exports = EasyMongo;
+
+}).call(this);
20 package.json
@@ -0,0 +1,20 @@
+{
+ "name": "easymongo"
+ , "version": "0.0.1"
+ , "keywords": ["mongo", "mongodb"]
+ , "author": "Alexey Simonenko <alexey@simonenko.su>"
+ , "main": "./lib/easymongo"
+ , "repository": {
+ "type": "git"
+ , "url": "https://github.com/meritt/easymongo.ru"
+ }
+ , "engines": {
+ "node": ">=0.6.0 <0.7.0"
+ }
+ , "dependencies": {
+ "mongodb": ">=0.9.8 <0.9.9"
+ }
+ , "devDependencies": {
+ "coffee-script": ">= 1.2.0"
+ }
+}
17 readme.md
@@ -0,0 +1,17 @@
+# Easy Mongo
+
+It's a small exstension native mongodb package.
+
+```javascript
+var easymongo = require('easymongo');
+
+var mongo = new easymongo({db: 'test'});
+
+mongo.findById('users', 'id', function(results) {
+ console.log(results); // false if not found
+});
+
+mongo.save('users', {name: 'Alexey', surname: 'Simonenko'}, function(results) {
+ console.log(results); // new mongo document
+});
+```
52 src/index.coffee
@@ -0,0 +1,52 @@
+{Db, Server, ObjectID} = require 'mongodb'
+
+ensureObjectId = (id) ->
+ if typeof id is 'string' then new ObjectID id else id
+
+class EasyMongo
+ configure: (@options) ->
+ @options.host = '127.0.0.1' unless @options.host?
+ @options.port = 27017 unless @options.port?
+
+ getInstance: (table, after) ->
+ throw new Error 'The database name must be configured (options.db)' unless @options.db?
+
+ db = new Db @options.db, new Server @options.host, @options.port, {}
+
+ db.open (error, db) ->
+ console.log 'Error with connection to MongoDB server: ' + error if error
+
+ db.collection table, (error, collection) ->
+ console.log 'Error with fetching collection: ' + error if error
+
+ after db, collection
+
+ findById: (table, id, after = ->) ->
+ @getInstance table, (db, collection) ->
+ collection.find(_id: ensureObjectId id).toArray (error, results) ->
+ console.log 'Error with fetching document by id: ' + error if error
+
+ db.close()
+ after if results.length is 1 then results[0] else false
+
+ find: (table, params, after = ->) ->
+ @getInstance table, (db, collection) ->
+ params._id = ensureObjectId params._id if params._id?
+
+ collection.find(params).toArray (error, results) ->
+ console.log 'Error with fetching documents: ' + error if error
+
+ db.close()
+ after results
+
+ save: (table, params, after = ->) ->
+ @getInstance table, (db, collection) ->
+ params._id = ensureObjectId params._id if params._id?
+
+ collection.save params, safe: true, (error, results) ->
+ console.log 'Error with saving data: ' + error if error
+
+ db.close()
+ after if params._id? then params else results[0]
+
+module.exports = EasyMongo

0 comments on commit 4c2b6e5

Please sign in to comment.
Something went wrong with that request. Please try again.