Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

first version

  • Loading branch information...
commit 986d2b5c679b8b70879abcbcd6e9988ebffa9b52 0 parents
Raynos (Jake Verbaten) authored January 05, 2013
16  .gitignore
... ...
@@ -0,0 +1,16 @@
  1
+.DS_Store
  2
+.monitor
  3
+.*.swp
  4
+.nodemonignore
  5
+releases
  6
+*.log
  7
+*.err
  8
+fleet.json
  9
+public/browserify
  10
+bin/*.json
  11
+.bin
  12
+build
  13
+compile
  14
+.lock-wscript
  15
+**/examples*
  16
+node_modules
12  .testem.json
... ...
@@ -0,0 +1,12 @@
  1
+{
  2
+    "launchers": {
  3
+        "node": {
  4
+            "command": "npm test",
  5
+            "protocol": "tap"
  6
+        }
  7
+    },
  8
+    "src_files": [
  9
+        "./**/*.js"
  10
+    ],
  11
+    "launch_in_dev": ["node"]
  12
+}
4  .travis.yml
... ...
@@ -0,0 +1,4 @@
  1
+language: node_js
  2
+node_js:
  3
+  - 0.8
  4
+  - 0.9
19  LICENCE
... ...
@@ -0,0 +1,19 @@
  1
+Copyright (c) 2012 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.
66  README.md
Source Rendered
... ...
@@ -0,0 +1,66 @@
  1
+# mongo-client
  2
+
  3
+[![build status][1]][2]
  4
+
  5
+No bullshit mongo wrapper
  6
+
  7
+## Example
  8
+
  9
+```js
  10
+var uuid = require("node-uuid")
  11
+var assert = require("assert")
  12
+var fold = require("reducers/fold")
  13
+var expand = require("reducers/expand")
  14
+
  15
+var mongo = require("mongo-client")
  16
+var insert = require("mongo-client/insert")
  17
+var findOne = require("mongo-client/findOne")
  18
+var close = require("mongo-client/close")
  19
+
  20
+var client = mongo("mongodb://localhost:27017/mongo-client-example")
  21
+var collection = client(uuid())
  22
+
  23
+var insertResult = insert(collection, {
  24
+    hello: "world"
  25
+})
  26
+
  27
+var findResult = expand(insertResult, function (item) {
  28
+    assert.equal(item.hello, "world")
  29
+    return findOne(collection, {
  30
+        hello: "world"
  31
+    })
  32
+})
  33
+
  34
+fold(findResult, function (value) {
  35
+    assert.equal(value.hello, "world")
  36
+    console.log("value", value)
  37
+    close(collection)
  38
+})
  39
+```
  40
+
  41
+## Commands
  42
+
  43
+Supports
  44
+
  45
+ - update DONE
  46
+ - insert DONE
  47
+ - remove DONE
  48
+ - findAndModify
  49
+ - findAndRemove
  50
+ - findOne DONE
  51
+ - find DONE
  52
+ - mapReduce DONE
  53
+
  54
+## Installation
  55
+
  56
+`npm install mongo-client`
  57
+
  58
+## Contributors
  59
+
  60
+ - Raynos
  61
+
  62
+## MIT Licenced
  63
+
  64
+
  65
+  [1]: https://secure.travis-ci.org/Raynos/mongo-client.png
  66
+  [2]: http://travis-ci.org/Raynos/mongo-client
13  close.js
... ...
@@ -0,0 +1,13 @@
  1
+var fold = require("reducers/fold")
  2
+
  3
+module.exports = close
  4
+
  5
+function close(collection, callback) {
  6
+    return fold(collection, function (collection) {
  7
+        if (collection.close) {
  8
+            return collection.close(callback)
  9
+        }
  10
+
  11
+        collection.db.close(callback)
  12
+    })
  13
+}
32  cursor.js
... ...
@@ -0,0 +1,32 @@
  1
+var reduce = require("reducible/reduce")
  2
+var end = require("reducible/end")
  3
+var isReduced = require("reducible/is-reduced")
  4
+var Cursor = require("mongodb").Cursor
  5
+
  6
+reduce.define(Cursor, function (cursor, next, initial) {
  7
+    var result = initial
  8
+
  9
+    recurse()
  10
+
  11
+    function recurse() {
  12
+        cursor.nextObject(function (error, value) {
  13
+            if (error) {
  14
+                return next(error, result)
  15
+            }
  16
+
  17
+            if (value === null) {
  18
+                return next(end, result)
  19
+            }
  20
+
  21
+            result = next(value, result)
  22
+
  23
+            if (isReduced(result)) {
  24
+                cursor.close(function () {
  25
+                    next(end, result.value)
  26
+                })
  27
+            } else {
  28
+                recurse()
  29
+            }
  30
+        })
  31
+    }
  32
+})
29  examples/simple.js
... ...
@@ -0,0 +1,29 @@
  1
+var uuid = require("node-uuid")
  2
+var assert = require("assert")
  3
+var fold = require("reducers/fold")
  4
+var expand = require("reducers/expand")
  5
+
  6
+var mongo = require("..")
  7
+var insert = require("../insert")
  8
+var findOne = require("../findOne")
  9
+var close = require("../close")
  10
+
  11
+var client = mongo("mongodb://localhost:27017/mongo-client-example")
  12
+var collection = client(uuid())
  13
+
  14
+var insertResult = insert(collection, {
  15
+    hello: "world"
  16
+})
  17
+
  18
+var findResult = expand(insertResult, function (item) {
  19
+    assert.equal(item.hello, "world")
  20
+    return findOne(collection, {
  21
+        hello: "world"
  22
+    })
  23
+})
  24
+
  25
+fold(findResult, function (value) {
  26
+    assert.equal(value.hello, "world")
  27
+    console.log("value", value)
  28
+    close(collection)
  29
+})
12  find.js
... ...
@@ -0,0 +1,12 @@
  1
+var expand = require("reducers/expand")
  2
+var callback = require("callback-reduce")
  3
+
  4
+var cursor = require("./cursor")
  5
+
  6
+module.exports = find
  7
+
  8
+function find(collection, selector, options) {
  9
+    return expand(collection, function (collection) {
  10
+        return collection.find(selector || {}, options || {})
  11
+    })
  12
+}
11  findOne.js
... ...
@@ -0,0 +1,11 @@
  1
+var take = require("reducers/take")
  2
+var concat = require("reducers/concat")
  3
+var find = require("./find")
  4
+
  5
+module.exports = findOne
  6
+
  7
+function findOne(collection, selector, options) {
  8
+    var items = find(collection, selector, options)
  9
+
  10
+    return take(concat(items, [null]), 1)
  11
+}
20  index.js
... ...
@@ -0,0 +1,20 @@
  1
+var mongodb = require("mongodb")
  2
+var map = require("reducers/map")
  3
+var fold = require("reducers/fold")
  4
+var callback = require("callback-reduce")
  5
+var cache = require("cache-reduce")
  6
+var MongoClient = mongodb.MongoClient
  7
+
  8
+module.exports = mongo
  9
+
  10
+function mongo(uri, options) {
  11
+    var mongoClient = cache(callback(MongoClient.connect, uri, options || {}))
  12
+
  13
+    return client
  14
+
  15
+    function client(collectionName) {
  16
+        return map(mongoClient, function (db) {
  17
+            return db.collection(collectionName)
  18
+        })
  19
+    }
  20
+}
11  insert.js
... ...
@@ -0,0 +1,11 @@
  1
+var expand = require("reducers/expand")
  2
+var callback = require("callback-reduce")
  3
+
  4
+module.exports = insert
  5
+
  6
+function insert(collection, doc, options) {
  7
+    return expand(collection, function (collection) {
  8
+        return callback.call(collection, collection.insert
  9
+            , doc, options || {})
  10
+    })
  11
+}
11  mapReduce.js
... ...
@@ -0,0 +1,11 @@
  1
+var callback = require("callback-reduce")
  2
+var expand = require("reducers/expand")
  3
+
  4
+module.exports = mapReduce
  5
+
  6
+function mapReduce(collection, map, reduce, options) {
  7
+    return expand(collection, function (collection) {
  8
+        return callback.call(collection, collection.mapReduce
  9
+            , map, reduce, options || {})
  10
+    })
  11
+}
72  package.json
... ...
@@ -0,0 +1,72 @@
  1
+{
  2
+  "name": "mongo-client",
  3
+  "version": "0.1.0",
  4
+  "description": "No bullshit mongo wrapper",
  5
+  "keywords": [],
  6
+  "author": "Raynos <raynos2@gmail.com>",
  7
+  "repository": "git://github.com/Raynos/mongo-client.git",
  8
+  "main": "index",
  9
+  "homepage": "https://github.com/Raynos/mongo-client",
  10
+  "contributors": [
  11
+    {
  12
+      "name": "Jake Verbaten"
  13
+    }
  14
+  ],
  15
+  "bugs": {
  16
+    "url": "https://github.com/Raynos/mongo-client/issues",
  17
+    "email": "raynos2@gmail.com"
  18
+  },
  19
+  "dependencies": {
  20
+    "testem": "~0.2.52",
  21
+    "reducers": "~2.0.0",
  22
+    "mongodb": "~1.2.7",
  23
+    "callback-reduce": "~1.0.0",
  24
+    "reducible": "~1.0.5",
  25
+    "cache-reduce": "~0.1.1"
  26
+  },
  27
+  "devDependencies": {
  28
+    "tape": "0.1.5",
  29
+    "browserify": "https://github.com/raynos/node-browserify/tarball/master",
  30
+    "testem": "0.2.52",
  31
+    "node-uuid": "~1.4.0"
  32
+  },
  33
+  "licenses": [
  34
+    {
  35
+      "type": "MIT",
  36
+      "url": "http://github.com/Raynos/mongo-client/raw/master/LICENSE"
  37
+    }
  38
+  ],
  39
+  "scripts": {
  40
+    "test": "node ./test",
  41
+    "build": "browserify test/index.js -o test/bundle.js",
  42
+    "testem": "testem",
  43
+    "postinstall": "npm dedup"
  44
+  },
  45
+  "testling": {
  46
+    "files": "test/*.js",
  47
+    "browsers": {
  48
+      "ie": [
  49
+        "8",
  50
+        "9",
  51
+        "10"
  52
+      ],
  53
+      "firefox": [
  54
+        "16",
  55
+        "17",
  56
+        "nightly"
  57
+      ],
  58
+      "chrome": [
  59
+        "22",
  60
+        "23",
  61
+        "canary"
  62
+      ],
  63
+      "opera": [
  64
+        "12",
  65
+        "next"
  66
+      ],
  67
+      "safari": [
  68
+        "5.1"
  69
+      ]
  70
+    }
  71
+  }
  72
+}
11  remove.js
... ...
@@ -0,0 +1,11 @@
  1
+var expand = require("reducers/expand")
  2
+var callback = require("callback-reduce")
  3
+
  4
+module.exports = remove
  5
+
  6
+function remove(collection, selector, options) {
  7
+    return expand(collection, function (collection) {
  8
+        return callback.call(collection, collection.remove
  9
+            , selector, options)
  10
+    })
  11
+}
28  test/client.js
... ...
@@ -0,0 +1,28 @@
  1
+var test = require("tape")
  2
+var mongo = require("..")
  3
+var close = require("../close")
  4
+
  5
+test("mongo is a function", function (assert) {
  6
+    assert.equal(typeof mongo, "function")
  7
+    assert.end()
  8
+})
  9
+
  10
+test("mongo returns a function", function (assert) {
  11
+    var client = mongo("mongodb://localhost:27017/mongo-client-test")
  12
+    assert.equal(typeof client, "function")
  13
+    close(client("tests"), function (err) {
  14
+        assert.ifError(err)
  15
+        assert.end()
  16
+    })
  17
+})
  18
+
  19
+test("client returns a collection", function (assert) {
  20
+    var client = mongo("mongodb://localhost:27017/mongo-client-test")
  21
+    var collection = client("tests")
  22
+
  23
+    assert.equal(typeof collection, "object")
  24
+    close(collection, function (err) {
  25
+        assert.ifError(err)
  26
+        assert.end()
  27
+    })
  28
+})
58  test/find.js
... ...
@@ -0,0 +1,58 @@
  1
+var test = require("tape")
  2
+var fold = require("reducers/fold")
  3
+var take = require("reducers/take")
  4
+var uuid = require("node-uuid")
  5
+
  6
+var find = require("../find")
  7
+var insert = require("../insert")
  8
+var close = require("../close")
  9
+var Collection = require("./util/collection")
  10
+
  11
+var collection = Collection()
  12
+var value = uuid()
  13
+
  14
+test("insert document", function (assert) {
  15
+    var result = insert(collection, {
  16
+        foo: value
  17
+    })
  18
+
  19
+    fold(result, function (result) {
  20
+        assert.equal(result.foo, value)
  21
+        assert.ok(result._id)
  22
+        assert.end()
  23
+    })
  24
+})
  25
+
  26
+test("find the document", function (assert) {
  27
+    var doc = take(find(collection, {
  28
+        foo: value
  29
+    }), 1)
  30
+
  31
+    fold(doc, function (doc) {
  32
+        assert.equal(doc.foo, value)
  33
+        assert.end()
  34
+    })
  35
+})
  36
+
  37
+test("find no documents", function (assert) {
  38
+    var docs = find(collection, {
  39
+        foo: value + "1"
  40
+    })
  41
+    var called = false
  42
+
  43
+    var result = fold(docs, function () {
  44
+        called = true
  45
+    }, {})
  46
+
  47
+    fold(result, function end() {
  48
+        assert.equal(called, false)
  49
+        assert.end()
  50
+    })
  51
+})
  52
+
  53
+test("close", function (assert) {
  54
+    close(collection, function (err) {
  55
+        assert.ifError(err)
  56
+        assert.end()
  57
+    })
  58
+})
52  test/findOne.js
... ...
@@ -0,0 +1,52 @@
  1
+var test = require("tape")
  2
+var fold = require("reducers/fold")
  3
+var uuid = require("node-uuid")
  4
+
  5
+var findOne = require("../findOne")
  6
+var insert = require("../insert")
  7
+var close = require("../close")
  8
+var Collection = require("./util/collection")
  9
+
  10
+var collection = Collection()
  11
+var value = uuid()
  12
+
  13
+test("insert document", function (assert) {
  14
+    var result = insert(collection, {
  15
+        foo: value
  16
+    })
  17
+
  18
+    fold(result, function (result) {
  19
+        assert.equal(result.foo, value)
  20
+        assert.ok(result._id)
  21
+        assert.end()
  22
+    })
  23
+})
  24
+
  25
+test("find the document", function (assert) {
  26
+    var doc = findOne(collection, {
  27
+        foo: value
  28
+    })
  29
+
  30
+    fold(doc, function (doc) {
  31
+        assert.equal(doc.foo, value)
  32
+        assert.end()
  33
+    })
  34
+})
  35
+
  36
+test("find no documents", function (assert) {
  37
+    var docs = findOne(collection, {
  38
+        foo: value + "1"
  39
+    })
  40
+
  41
+    fold(docs, function (result) {
  42
+        assert.equal(result, null)
  43
+        assert.end()
  44
+    })
  45
+})
  46
+
  47
+test("close", function (assert) {
  48
+    close(collection, function (err) {
  49
+        assert.ifError(err)
  50
+        assert.end()
  51
+    })
  52
+})
8  test/index.js
... ...
@@ -0,0 +1,8 @@
  1
+require("../examples/simple")
  2
+
  3
+require("./client")
  4
+require("./find")
  5
+require("./update")
  6
+require("./findOne")
  7
+require("./remove")
  8
+require("./mapReduce")
74  test/mapReduce.js
... ...
@@ -0,0 +1,74 @@
  1
+var test = require("tape")
  2
+var fold = require("reducers/fold")
  3
+var take = require("reducers/take")
  4
+var uuid = require("node-uuid")
  5
+
  6
+var find = require("../find")
  7
+var insert = require("../insert")
  8
+var mapReduce = require("../mapReduce")
  9
+var close = require("../close")
  10
+var toArray = require("./util/toArray")
  11
+var Collection = require("./util/collection")
  12
+
  13
+var collection = Collection()
  14
+var value = uuid()
  15
+
  16
+test("insert some documents", function (assert) {
  17
+    var result = insert(collection, [{
  18
+        foo: value
  19
+        , count: 1
  20
+    }, {
  21
+        foo: value
  22
+        , count: 2
  23
+    }, {
  24
+        foo: value
  25
+        , count: 3
  26
+    }])
  27
+
  28
+    toArray(result, function (err, list) {
  29
+        assert.equal(list.length, 3)
  30
+        assert.deepEqual(list.map(function (v) {
  31
+            return v.count
  32
+        }), [1, 2, 3])
  33
+        assert.end()
  34
+    })
  35
+})
  36
+
  37
+test("map reduce them", function (assert) {
  38
+    var results = take(mapReduce(collection, map, reduce, {
  39
+        out: {
  40
+            inline: 1
  41
+        }
  42
+        , query: {
  43
+            foo: value
  44
+        }
  45
+    }), 1)
  46
+
  47
+    fold(results, function (list) {
  48
+        assert.equal(list.length, 1)
  49
+        assert.equal(list[0].value, 6)
  50
+        assert.end()
  51
+    })
  52
+
  53
+    function map(item) {
  54
+        /*global emit*/
  55
+        emit(this.foo, this.count)
  56
+    }
  57
+
  58
+    function reduce(key, values) {
  59
+        var count = 0
  60
+
  61
+        values.forEach(function (v) {
  62
+            count += v
  63
+        })
  64
+
  65
+        return count
  66
+    }
  67
+})
  68
+
  69
+test("close", function (assert) {
  70
+    close(collection, function (err) {
  71
+        assert.ifError(err)
  72
+        assert.end()
  73
+    })
  74
+})
51  test/remove.js
... ...
@@ -0,0 +1,51 @@
  1
+var test = require("tape")
  2
+var fold = require("reducers/fold")
  3
+var uuid = require("node-uuid")
  4
+
  5
+var remove = require("../remove")
  6
+var findOne = require("../findOne")
  7
+var insert = require("../insert")
  8
+var close = require("../close")
  9
+var Collection = require("./util/collection")
  10
+
  11
+var collection = Collection()
  12
+var value = uuid()
  13
+
  14
+test("insert document", function (assert) {
  15
+    var result = insert(collection, {
  16
+        foo: value
  17
+    })
  18
+
  19
+    fold(result, function (result) {
  20
+        assert.equal(result.foo, value)
  21
+        assert.ok(result._id)
  22
+        assert.end()
  23
+    })
  24
+})
  25
+
  26
+test("remove document", function (assert) {
  27
+    var result = remove(collection, {
  28
+        foo: value
  29
+    })
  30
+
  31
+    fold(result, function (result) {
  32
+        assert.equal(result, 1)
  33
+        assert.end()
  34
+    })
  35
+})
  36
+
  37
+test("document was removed", function (assert) {
  38
+    fold(findOne(collection, {
  39
+        foo: value
  40
+    }), function (result) {
  41
+        assert.equal(result, null)
  42
+        assert.end()
  43
+    })
  44
+})
  45
+
  46
+test("close", function (assert) {
  47
+    close(collection, function (err) {
  48
+        assert.ifError(err)
  49
+        assert.end()
  50
+    })
  51
+})
57  test/update.js
... ...
@@ -0,0 +1,57 @@
  1
+var test = require("tape")
  2
+var uuid = require("node-uuid")
  3
+var fold = require("reducers/fold")
  4
+var take = require("reducers/take")
  5
+
  6
+var find = require("../find")
  7
+var update = require("../update")
  8
+var close = require("../close")
  9
+var Collection = require("./util/collection")
  10
+
  11
+var collection = Collection()
  12
+var value = uuid()
  13
+
  14
+test("can upsert document", function (assert) {
  15
+    var result = update(collection, {
  16
+        foo: value
  17
+    }, {
  18
+        $set: {
  19
+            foo: value
  20
+        }
  21
+    }, {
  22
+        upsert: true
  23
+    })
  24
+
  25
+    fold(result, function (value) {
  26
+        assert.equal(value, 1)
  27
+        assert.end()
  28
+    })
  29
+})
  30
+
  31
+test("can update a document", function (assert) {
  32
+    var result = update(collection, {
  33
+        foo: value
  34
+    }, {
  35
+        $set: {
  36
+            foo: value + value
  37
+        }
  38
+    })
  39
+
  40
+    fold(result, function (value) {
  41
+        assert.equal(value, 1)
  42
+        assert.end()
  43
+    })
  44
+})
  45
+
  46
+test("correctly updated", function (assert) {
  47
+    fold(take(find(collection, {
  48
+        foo: value + value
  49
+    }), 1), function (result) {
  50
+        assert.equal(result.foo, value + value)
  51
+
  52
+        close(collection, function (err) {
  53
+            assert.ifError(err)
  54
+            assert.end()
  55
+        })
  56
+    })
  57
+})
9  test/util/collection.js
... ...
@@ -0,0 +1,9 @@
  1
+var mongo = require("../..")
  2
+
  3
+module.exports = collection
  4
+
  5
+function collection() {
  6
+    var client = mongo("mongodb://localhost:27017/mongo-client-test")
  7
+
  8
+    return client("test23")
  9
+}
32  test/util/toArray.js
... ...
@@ -0,0 +1,32 @@
  1
+var reduce = require("reducible/reduce")
  2
+var end = require("reducible/end")
  3
+var isError = require("reducible/is-error")
  4
+
  5
+module.exports = toArray
  6
+
  7
+function toArray(source, callback) {
  8
+    var ended = false
  9
+
  10
+    reduce(source, function(value, result) {
  11
+        if (ended) {
  12
+            return result
  13
+        }
  14
+
  15
+        // If source is `end`-ed deliver accumulated `state`.
  16
+        if (value === end) {
  17
+            ended = true
  18
+
  19
+            return callback(null, result)
  20
+        }
  21
+        // If is source has an error, deliver that.
  22
+        else if (isError(value)) {
  23
+            ended = true
  24
+
  25
+            return callback(value)
  26
+        }
  27
+
  28
+        result.push(value)
  29
+
  30
+        return result
  31
+    }, [])
  32
+}
12  update.js
... ...
@@ -0,0 +1,12 @@
  1
+var expand = require("reducers/expand")
  2
+var take = require("reducers/take")
  3
+var callback = require("callback-reduce")
  4
+
  5
+module.exports = update
  6
+
  7
+function update(collection, selector, doc, options) {
  8
+    return take(expand(collection, function (collection) {
  9
+        return callback.call(collection, collection.update
  10
+            , selector, doc, options || {})
  11
+    }), 1)
  12
+}

0 notes on commit 986d2b5

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