Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial commit

  • Loading branch information...
commit 4c2b6e5e7836eb4d9513ce6f3e4291da35e9c468 0 parents
Alexey Simonenko authored
2  .gitignore
... ... @@ -0,0 +1,2 @@
  1 +.DS_Store
  2 +node_modules/*
12 Cakefile
... ... @@ -0,0 +1,12 @@
  1 +{print} = require 'util'
  2 +{spawn} = require 'child_process'
  3 +
  4 +build = (callback) ->
  5 + coffee = spawn 'coffee', ['-c', '-o', 'lib', 'src']
  6 + coffee.stderr.on 'data', (data) ->
  7 + process.stderr.write data.toString()
  8 + coffee.stdout.on 'data', (data) ->
  9 + print data.toString()
  10 +
  11 +task 'build', 'Build lib/ from src/', ->
  12 + build()
86 lib/index.js
... ... @@ -0,0 +1,86 @@
  1 +(function() {
  2 + var Db, EasyMongo, ObjectID, Server, ensureObjectId, _ref;
  3 +
  4 + _ref = require('mongodb'), Db = _ref.Db, Server = _ref.Server, ObjectID = _ref.ObjectID;
  5 +
  6 + ensureObjectId = function(id) {
  7 + if (typeof id === 'string') {
  8 + return new ObjectID(id);
  9 + } else {
  10 + return id;
  11 + }
  12 + };
  13 +
  14 + EasyMongo = (function() {
  15 +
  16 + function EasyMongo() {}
  17 +
  18 + EasyMongo.prototype.configure = function(options) {
  19 + this.options = options;
  20 + if (this.options.host == null) this.options.host = '127.0.0.1';
  21 + if (this.options.port == null) return this.options.port = 27017;
  22 + };
  23 +
  24 + EasyMongo.prototype.getInstance = function(table, after) {
  25 + var db;
  26 + if (this.options.db == null) {
  27 + throw new Error('The database name must be configured (options.db)');
  28 + }
  29 + db = new Db(this.options.db, new Server(this.options.host, this.options.port, {}));
  30 + return db.open(function(error, db) {
  31 + if (error) {
  32 + console.log('Error with connection to MongoDB server: ' + error);
  33 + }
  34 + return db.collection(table, function(error, collection) {
  35 + if (error) console.log('Error with fetching collection: ' + error);
  36 + return after(db, collection);
  37 + });
  38 + });
  39 + };
  40 +
  41 + EasyMongo.prototype.findById = function(table, id, after) {
  42 + if (after == null) after = function() {};
  43 + return this.getInstance(table, function(db, collection) {
  44 + return collection.find({
  45 + _id: ensureObjectId(id)
  46 + }).toArray(function(error, results) {
  47 + if (error) console.log('Error with fetching document by id: ' + error);
  48 + db.close();
  49 + return after(results.length === 1 ? results[0] : false);
  50 + });
  51 + });
  52 + };
  53 +
  54 + EasyMongo.prototype.find = function(table, params, after) {
  55 + if (after == null) after = function() {};
  56 + return this.getInstance(table, function(db, collection) {
  57 + if (params._id != null) params._id = ensureObjectId(params._id);
  58 + return collection.find(params).toArray(function(error, results) {
  59 + if (error) console.log('Error with fetching documents: ' + error);
  60 + db.close();
  61 + return after(results);
  62 + });
  63 + });
  64 + };
  65 +
  66 + EasyMongo.prototype.save = function(table, params, after) {
  67 + if (after == null) after = function() {};
  68 + return this.getInstance(table, function(db, collection) {
  69 + if (params._id != null) params._id = ensureObjectId(params._id);
  70 + return collection.save(params, {
  71 + safe: true
  72 + }, function(error, results) {
  73 + if (error) console.log('Error with saving data: ' + error);
  74 + db.close();
  75 + return after(params._id != null ? params : results[0]);
  76 + });
  77 + });
  78 + };
  79 +
  80 + return EasyMongo;
  81 +
  82 + })();
  83 +
  84 + module.exports = EasyMongo;
  85 +
  86 +}).call(this);
20 package.json
... ... @@ -0,0 +1,20 @@
  1 +{
  2 + "name": "easymongo"
  3 + , "version": "0.0.1"
  4 + , "keywords": ["mongo", "mongodb"]
  5 + , "author": "Alexey Simonenko <alexey@simonenko.su>"
  6 + , "main": "./lib/easymongo"
  7 + , "repository": {
  8 + "type": "git"
  9 + , "url": "https://github.com/meritt/easymongo.ru"
  10 + }
  11 + , "engines": {
  12 + "node": ">=0.6.0 <0.7.0"
  13 + }
  14 + , "dependencies": {
  15 + "mongodb": ">=0.9.8 <0.9.9"
  16 + }
  17 + , "devDependencies": {
  18 + "coffee-script": ">= 1.2.0"
  19 + }
  20 +}
17 readme.md
Source Rendered
... ... @@ -0,0 +1,17 @@
  1 +# Easy Mongo
  2 +
  3 +It's a small exstension native mongodb package.
  4 +
  5 +```javascript
  6 +var easymongo = require('easymongo');
  7 +
  8 +var mongo = new easymongo({db: 'test'});
  9 +
  10 +mongo.findById('users', 'id', function(results) {
  11 + console.log(results); // false if not found
  12 +});
  13 +
  14 +mongo.save('users', {name: 'Alexey', surname: 'Simonenko'}, function(results) {
  15 + console.log(results); // new mongo document
  16 +});
  17 +```
52 src/index.coffee
... ... @@ -0,0 +1,52 @@
  1 +{Db, Server, ObjectID} = require 'mongodb'
  2 +
  3 +ensureObjectId = (id) ->
  4 + if typeof id is 'string' then new ObjectID id else id
  5 +
  6 +class EasyMongo
  7 + configure: (@options) ->
  8 + @options.host = '127.0.0.1' unless @options.host?
  9 + @options.port = 27017 unless @options.port?
  10 +
  11 + getInstance: (table, after) ->
  12 + throw new Error 'The database name must be configured (options.db)' unless @options.db?
  13 +
  14 + db = new Db @options.db, new Server @options.host, @options.port, {}
  15 +
  16 + db.open (error, db) ->
  17 + console.log 'Error with connection to MongoDB server: ' + error if error
  18 +
  19 + db.collection table, (error, collection) ->
  20 + console.log 'Error with fetching collection: ' + error if error
  21 +
  22 + after db, collection
  23 +
  24 + findById: (table, id, after = ->) ->
  25 + @getInstance table, (db, collection) ->
  26 + collection.find(_id: ensureObjectId id).toArray (error, results) ->
  27 + console.log 'Error with fetching document by id: ' + error if error
  28 +
  29 + db.close()
  30 + after if results.length is 1 then results[0] else false
  31 +
  32 + find: (table, params, after = ->) ->
  33 + @getInstance table, (db, collection) ->
  34 + params._id = ensureObjectId params._id if params._id?
  35 +
  36 + collection.find(params).toArray (error, results) ->
  37 + console.log 'Error with fetching documents: ' + error if error
  38 +
  39 + db.close()
  40 + after results
  41 +
  42 + save: (table, params, after = ->) ->
  43 + @getInstance table, (db, collection) ->
  44 + params._id = ensureObjectId params._id if params._id?
  45 +
  46 + collection.save params, safe: true, (error, results) ->
  47 + console.log 'Error with saving data: ' + error if error
  48 +
  49 + db.close()
  50 + after if params._id? then params else results[0]
  51 +
  52 +module.exports = EasyMongo

0 comments on commit 4c2b6e5

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