Skip to content

Commit 4914c6b

Browse files
committed
v0.1.0
0 parents  commit 4914c6b

File tree

9 files changed

+244
-0
lines changed

9 files changed

+244
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- 0.6

LICENCE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2011 Raynos.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
REPORTER = spec
2+
3+
test:
4+
@NODE_ENV=test ./node_modules/.bin/mocha \
5+
--ui bdd \
6+
--reporter $(REPORTER)
7+
8+
.PHONY: test

README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# mongo-col [![Build Status][1]][2]
2+
3+
mongoDB collection wrapper
4+
5+
## Status: production ready
6+
7+
## Example
8+
9+
var collection = require("mongo-col"),
10+
Users = collection("Users")
11+
12+
Users.insert({
13+
name: "foo",
14+
password: "bar"
15+
})
16+
17+
## Motivation
18+
19+
Setting up a mongodb database connection requires too much callback soup, remove it.
20+
21+
## Documentation
22+
23+
### <a name="colleciton" href="#collection">collection(collectionName)</a>
24+
25+
`collection` takes a collection name and returns a collection object. This collection object has all the mongodb collection methods and sets up a database connection internally
26+
27+
See the [MongoDB collection API][3]
28+
29+
var collection = require("mongo-col"),
30+
Users = collection("Users")
31+
32+
Users.insert({
33+
name: "foo",
34+
password: "bar"
35+
})
36+
37+
## Installation
38+
39+
`npm install mongo-col`
40+
41+
## Tests
42+
43+
`make test`
44+
45+
## Contributors
46+
47+
- Raynos
48+
49+
## MIT Licenced
50+
51+
[1]: https://secure.travis-ci.org/Raynos/mongo-col.png
52+
[2]: http://travis-ci.org/Raynos/mongo-col
53+
[3]: http://christkv.github.com/node-mongodb-native/api-generated/collection.html

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./lib/mongo")

lib/mongo.js

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
var mongodb = require("mongodb"),
2+
// Grab the database HOST, PORT, USER and PASSWORD from the environment
3+
HOST = process.env["MONGODB_HOST"] || "localhost",
4+
PORT = +process.env["MONGODB_PORT"] || 27017,
5+
USER = process.env["MONGODB_USER"],
6+
PASSWORD = process.env["MONGODB_PASSWORD"],
7+
Db = mongodb.Db,
8+
Server = mongodb.Server,
9+
cachedResults,
10+
callbackQueue
11+
12+
var Collection = {
13+
constructor: function (collection) {
14+
this.collection = collection
15+
return this
16+
}
17+
}
18+
19+
Object.keys(mongodb.Collection.prototype).forEach(addToCollection)
20+
21+
module.exports = collection
22+
23+
function mongo(collectionName) {
24+
return getCollection
25+
26+
function getCollection(callback) {
27+
openDatabase(openCollection)
28+
29+
function openCollection(err, db) {
30+
if (err) {
31+
return callback(err)
32+
}
33+
db.collection(collectionName, callCallback)
34+
}
35+
36+
function callCallback(err, collection) {
37+
callback.call(collection, err, collection)
38+
}
39+
}
40+
}
41+
42+
function openDatabase(callback) {
43+
if (cachedResults) {
44+
return callback.apply(cachedResults[1], memo)
45+
} else if (callbackQueue) {
46+
return callbackQueue.push(callback)
47+
}
48+
49+
callbackQueue = [callback]
50+
51+
var db = new Db('DATABASE', new Server(HOST, PORT, {
52+
auto_reconnect: true,
53+
poolSize: 4
54+
}), {})
55+
db.open(authenticateDatabase)
56+
57+
function authenticateDatabase(err, db) {
58+
if (err) {
59+
return invokeCallbacks(err)
60+
}
61+
62+
if (USER || PASSWORD) {
63+
db.authenticate(USER, PASSWORD, saveDatabase)
64+
} else {
65+
invokeCallbacks(err, db)
66+
}
67+
68+
function saveDatabase(err, success) {
69+
if (success) {
70+
return invokeCallbacks(err, db)
71+
}
72+
return invokeCallbacks(new Error("auth failed"))
73+
}
74+
}
75+
76+
function invokeCallbacks(err, data) {
77+
var callbackList = callbackQueue
78+
cachedResults = arguments
79+
callbackQueue = null
80+
for (var i = 0, len = callbackList.length; i < len; i++) {
81+
callbackList[i].apply(arguments[1], arguments)
82+
}
83+
}
84+
}
85+
86+
function collection (collectionName) {
87+
return Object.create(Collection).constructor(mongo(collectionName))
88+
}
89+
90+
function addToCollection(methodName) {
91+
if (typeof mongodb.Collection.prototype[methodName] === "function") {
92+
Collection[methodName] = tunnel(methodName)
93+
}
94+
}
95+
96+
function tunnel (methodName) {
97+
return proxy
98+
99+
function proxy() {
100+
var args = arguments,
101+
callback = args[args.length - 1]
102+
103+
this.collection(invokeMethod)
104+
105+
function invokeMethod(err, collection) {
106+
if (err) {
107+
if (callback.call) {
108+
callback(err)
109+
}
110+
return
111+
}
112+
collection[methodName].apply(collection, args)
113+
}
114+
}
115+
}

package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "mongo-col",
3+
"description": "mongoDB collection wrapper",
4+
"version": "0.1.0",
5+
"author": "Raynos <[email protected]>",
6+
"contributors": [
7+
{
8+
"name": "Raynos",
9+
"email": "[email protected]",
10+
"url": "http://raynos.org"
11+
}
12+
],
13+
"dependencies": {
14+
"mongodb": "0.9.9-7"
15+
},
16+
"scripts": {
17+
"test": "make test"
18+
},
19+
"devDependencies": {
20+
"mocha": "0.14.1"
21+
},
22+
"keywords": ["mongo", "db", "database", "collection", "orm"],
23+
"repository": "git://github.com/Raynos/mongo-col.git",
24+
"main": "index.js",
25+
"engines": {
26+
"node": "0.6"
27+
}
28+
}

test/mongo-test.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var collection = require("../"),
2+
assert = require("assert"),
3+
Users = collection("Users")
4+
5+
Users.drop()
6+
7+
describe("mongo-collection", function () {
8+
it("should allow inserting", function (done) {
9+
Users.insert({ name: "foo" })
10+
Users.findOne({ name: "foo" }, function (err, data) {
11+
assert(data.name === "foo",
12+
"data name incorrect")
13+
done()
14+
})
15+
})
16+
})

0 commit comments

Comments
 (0)