├── .gitignore ├── ChangeLog ├── LICENSE ├── README.md ├── lib └── mongomery.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | npm-debug.log 2 | node_modules 3 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | 2015.11.24, v1.0.3 2 | 3 | feature: 4 | - (package) ruff v1.5.0 5 | 6 | 7 | 2015.04.03, v1.0.2 8 | 9 | fix: 10 | - (mongomery) mongo -> [mongo 11 | - (package) ruff v1.4.1 12 | 13 | feature: 14 | - (package) v1.0.1 15 | 16 | 17 | 2015.04.03, v1.0.1 18 | 19 | fix: 20 | - (package) ruff v1.4.1 21 | 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 coderaiser 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Mongomery 2 | ======= 3 | 4 | Generator based flow-control driver for [MongoDB](http://mongodb.org "MongoDB"). 5 | 6 | `Mongomery` has API like [mongodb-native](https://github.com/mongodb/node-mongodb-native "MongoDB Native") has, but based on `es6 generators`. 7 | 8 | ## Install 9 | 10 | `npm i mongomery --save` 11 | 12 | ## How come? 13 | 14 | With help of `ES6` generators flow-control of interaction with `MongoDB` could be much simpler. 15 | `Mongomery` will save your code from [callback hell](http://callbackhell.com/ "Callback Hell"). 16 | 17 | ## Hot to use? 18 | 19 | ```js 20 | var mongomery = require('mongomery'); 21 | 22 | mongomery(function*(mongo) { 23 | var url = 'mongodb://localhost:27017/myproject', 24 | db = yield mongo.connect(url), 25 | collection = db.collection('mongolog'), 26 | docs = yield collection.find({}).toArray(); 27 | 28 | docs.forEach(function(item) { 29 | console.log(item); 30 | }); 31 | 32 | db.close(); 33 | }).on('error', function(error) { 34 | console.log(error.message); 35 | }); 36 | ``` 37 | 38 | ## License 39 | 40 | MIT 41 | 42 | -------------------------------------------------------------------------------- /lib/mongomery.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | 'use strict'; 3 | 4 | var ruff = require('ruff'), 5 | mongodb = require('mongodb').MongoClient; 6 | 7 | module.exports = function(fn) { 8 | var mongo = new Mongo(); 9 | 10 | return ruff(fn, [mongo]); 11 | }; 12 | 13 | function Mongo() { 14 | } 15 | 16 | Mongo.prototype.connect = function(url) { 17 | var fn = function(callback) { 18 | mongodb.connect(url, function(error, db) { 19 | var _db = new DB(db); 20 | 21 | callback(error, _db); 22 | }); 23 | }; 24 | 25 | return fn; 26 | }; 27 | 28 | function DB(db) { 29 | this._db = db; 30 | } 31 | 32 | DB.prototype.collection = function(name) { 33 | var real = this._db.collection(name), 34 | collection = new Collection(real); 35 | 36 | return collection; 37 | }; 38 | 39 | DB.prototype.close = function() { 40 | this._db.close(); 41 | }; 42 | 43 | function Collection(collection) { 44 | this._collection = collection; 45 | } 46 | 47 | [ 48 | 'insert', 49 | 'update', 50 | 'remove', 51 | ].forEach(function(name) { 52 | Collection.prototype[name] = function(param) { 53 | var collection = this._collection, 54 | fnName = collection[name].bind(collection), 55 | fn = thunk(fnName, param); 56 | 57 | return fn; 58 | }; 59 | }); 60 | 61 | [ 62 | 'find', 63 | 'findOne', 64 | ].forEach(function(name) { 65 | Collection.prototype[name] = function(param) { 66 | var real = this._collection[name](param), 67 | cursor = new Cursor(real); 68 | 69 | return cursor; 70 | }; 71 | }); 72 | 73 | function Cursor(cursor) { 74 | this._cursor = cursor; 75 | } 76 | 77 | Cursor.prototype.toArray = function() { 78 | var cursor = this._cursor, 79 | fn = function(callback) { 80 | cursor.toArray(callback); 81 | }; 82 | 83 | return fn; 84 | }; 85 | 86 | function thunk(method, param) { 87 | var fn = function(callback) { 88 | method(param, callback); 89 | }; 90 | 91 | return fn; 92 | } 93 | 94 | })(); 95 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mongomery", 3 | "version": "1.0.3", 4 | "description": "Generator based flow-control driver for MongoDB", 5 | "main": "lib/mongomery.js", 6 | "dependencies": { 7 | "mongodb": "~2.0.25", 8 | "ruff": "~1.5.0" 9 | }, 10 | "devDependencies": {}, 11 | "scripts": { 12 | "test": "echo \"Error: no test specified\" && exit 1" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git://github.com/coderaiser/node-mongomery.git" 17 | }, 18 | "keywords": [ 19 | "mongodb", 20 | "mongo", 21 | "driver", 22 | "generators", 23 | "flow-control" 24 | ], 25 | "author": "coderaiser (http://coderaiser.github.io/)", 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/coderaiser/node-mongomery/issues" 29 | }, 30 | "homepage": "https://github.com/coderaiser/node-mongomery" 31 | } 32 | --------------------------------------------------------------------------------