Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add functionality

  • Loading branch information...
commit 7884a75ca3172c1bcdc48fc277f4ddfb10861b34 1 parent 8dacd6e
@pvorb authored
View
1  .npmignore
@@ -0,0 +1 @@
+test/
View
136 README.mkd
@@ -1 +1,135 @@
-**cmts** is a RESTful comment server.
+# Comments
+
+**Commments** is an abstraction layer for MongoDB that allows for easily
+providing some commenting functionality.
+
+## Usage
+
+```javascript
+new Comments(options)
+```
+
+Creates a new Comments object. This means, a connection to MongoDB is set up.
+
+* `options` is an object that defines some DB connection parameters.
+
+ The default options are as follows:
+
+ ```javascript
+ {
+ host: 'localhost', // - hostname of the server where mongodb is
+ // running
+ port: 27017, // - port that is used by mongodb
+ name: 'website', // - name of the mongodb database
+ collection: 'comments', // - name of the collection that contains the
+ // comments
+ index: 'doc' // - fields to be indexed
+ }
+ ```
+
+ For more information on how to define `opt.index` look at
+ [the MongoDB documentation][mongodb-doc-indexes].
+
+```javascript
+comments.saveComment(comment, saved)
+```
+
+Adds a new or updates a comment in the collection depending on if there already
+is a comment with the same `_id` property. If `comment` does not define an
+`_id` property, a new comment is created.
+
+* `comment` is an object that defines a comment. It is stored directly into
+ the collection. Any prior parsing is up to you. `comment.doc` should be
+ defined for the later use of `comments.getComments`.
+* `saved` is a callback function that takes two arguments `(error, comment)`.
+ `error` is an `Error` object, when an error occurred, otherwise it is
+ `null`. `comment` is either the saved comment object or `1` if an existing
+ comment has been updated.
+
+```javascript
+comments.getComments(doc, [[properties,] options,] received)
+```
+
+Gives access to the comments of a document.
+
+* `doc` is a string defining the document that contains the comments that you
+ are looking for. If `doc` is `null`, all comments in the collection will be
+ found.
+* `properties [optional]` is an object that defines, which properties of the
+ comments shall be returned.
+
+ The default properties are as follows:
+
+ ```javascript
+ {
+ _id: true,
+ author: true,
+ website: true,
+ created: true,
+ message: true
+ }
+ ```
+* `options [optional]` is an object that defines additional options according
+ to [section "Query options"][mongodb-doc-queries] like _sorting_ or
+ _paging_.
+
+ The default options are as follows:
+
+ ```javascript
+ {
+ sort: "created" // sort results by date of creation in ascending order
+ }
+ ```
+* `received` is a callback function that takes two arguments
+ `(error, results)`. `error` is an `Error` object, when an error occurred,
+ otherwise it is `null`. `results` is a cursor to the result set of the
+ query. Look at [section "Cursors"][mongodb-doc-queries] for more
+ information on how to use them.
+
+```javascript
+comments.count(doc, counted)
+```
+
+Counts the comments of a document or the complete collection.
+
+* `doc` is a string defining the document that contains the comments that you
+ are looking for. If `doc` is `null`, all comments in the collection will be
+ counted.
+* `counted` is a callback function that takes two arguments `(error, count)`.
+ `error` is an `Error` object, when an error occurred, otherwise it is
+ `null`. `count` is the number of comments for the document or in the
+ collection.
+
+## Installation
+
+You need a running MongoDB before you can use **Comments**. On Debian
+
+```bash
+# apt-get install mongodb
+```
+
+Then install **Comments** with npm.
+
+```bash
+# npm install -g Comments
+```
+
+## Examples
+
+For examples, look at the [tests][test-dir].
+
+## Bugs and Issues
+
+If you encounter any bugs or issues, feel free to open an issue at
+[github][issues].
+
+## License
+
+This package is licensed under the
+[MIT license][license].
+
+[mongodb-doc-indexes]:https://github.com/christkv/node-mongodb-native/blob/master/docs/indexes.md
+[mongodb-doc-queries]:https://github.com/christkv/node-mongodb-native/blob/master/docs/queries.md
+[test-dir]:https://github.com/pvorb/node-comments/test
+[issues]:https://github.com/pvorb/node-comments/issues
+[license]:http://vorb.de/license/mit.html
View
0  cmts.js
No changes.
View
140 comments.js
@@ -0,0 +1,140 @@
+var append = require('append'),
+ mongodb = require('mongodb'),
+ MongoDB = mongodb.Db,
+ MongoServer = mongodb.Server;
+
+// constructor
+var Comments = module.exports = function(opt) {
+
+ // Default options
+ var defaultOpt = {
+ host: 'localhost',
+ port: 27017,
+ name: 'website',
+ collection: 'comments',
+ index: 'doc'
+ };
+
+ this.opt = append(defaultOpt, opt);
+};
+
+// method: connect
+Comments.prototype.connect = function(connected) {
+ var inst = this,
+ opt = this.opt;
+
+ // Server connection
+ inst.server = new MongoServer(opt.host, opt.port, { auto_reconnect: true });
+ // DB connection
+ var dbConnector = new MongoDB(opt.name, inst.server);
+
+ try {
+ dbConnector.open(function(err, db) {
+ if (err) throw err;
+
+ // DB connection
+ inst.db = db;
+
+ db.collection(opt.collection, function(err, col) {
+ if (err) throw err;
+
+ // save ref to collection
+ inst.collection = col;
+
+ col.ensureIndex(opt.index, function(err, index) {
+ if (err) throw err;
+
+ connected(null, col);
+ });
+ });
+ });
+ } catch(err) {
+ connected(err);
+ }
+};
+
+// method: getCollection
+Comments.prototype.getCollection = function(done) {
+ // If connection hasn't already been established
+ if (typeof this.collection == 'undefined')
+ // try to connect
+ try {
+ this.connect(done);
+ } catch(err) {
+ done(err);
+ }
+ // otherwise simply use existing collection
+ else
+ done(null, this.collection);
+};
+
+// method: saveComment
+Comments.prototype.saveComment = function(comment, saved) {
+ try {
+ // get collection and save comment
+ this.getCollection(function(err, col) {
+ col.save(comment, saved);
+ });
+ } catch(err) {
+ saved(err);
+ }
+};
+
+// method: getComments
+Comments.prototype.getComments = function(doc, props, opt, received) {
+ var defaultOpt = {
+ sort: "created"
+ };
+
+ var defaultProps = {
+ _id: true,
+ author: true,
+ website: true,
+ created: true,
+ message: true
+ };
+
+ // set properties and options
+ if (arguments.length == 2) {
+ received = props;
+ props = defaultProps;
+ opt = defaultOpt;
+ } else if (arguments.length == 3) {
+ received = opt;
+ opt = defaultOpt;
+ props = append(defaultProps, props);
+ } else {
+ opt = append(defaultOpt, opt);
+ props = append(defaultProps, props);
+ }
+
+ var query = {};
+ if (doc !== null)
+ query.doc = doc;
+
+ try {
+ // get collection and find comments
+ this.getCollection(function(err, col) {
+ col.find(query, props, opt, received);
+ });
+ } catch(err) {
+ received(err);
+ }
+};
+
+// method: count
+Comments.prototype.count = function(doc, counted) {
+ try {
+ this.getComments(doc, function(err, results) {
+ results.count(counted);
+ });
+ } catch(err) {
+ counted(err);
+ }
+};
+
+// method: close
+Comments.prototype.close = function(done) {
+ if (this.db)
+ this.db.close(done);
+};
View
18 package.json
@@ -1,20 +1,22 @@
{
- "author": "Paul Vorbach <paul@vorb.de> (http://vorb.de)",
- "name": "cmts",
- "description": "A RESTful comment server",
- "tags": [ "comment", "discussion", "REST" ],
+ "author": "Paul Vorbach <paul@vorb.de> (https://vorb.de)",
+ "name": "Comments",
+ "description": "a wrapper for storing comments in mongodb",
+ "tags": [ "comment", "discussion", "blog" ],
"version": "0.0.0",
"repository": {
"type": "git",
- "url": "git://github.com/pvorb/node-cmts.git"
+ "url": "git://github.com/pvorb/node-comments.git"
},
"bugs": {
- "web": "https://github.com/pvorb/node-cmts/issues"
+ "web": "https://github.com/pvorb/node-comments/issues"
},
- "main": "cmts.js",
+ "main": "comments.js",
"engines": {
- "node": "*"
+ "node": ">=0.4.0"
},
"dependencies": {
+ "append": ">=0.1.0",
+ "mongodb": ">=0.9.6-19"
}
}
View
25 test/add.js
@@ -0,0 +1,25 @@
+var Comments = require('../');
+
+var comments = new Comments();
+
+function log(err, comment) {
+ if (err) console.error(err);
+ else if (comment === 1)
+ console.log('updated');
+ else
+ console.log('created');
+
+ // close afterwards
+ comments.close(function() {
+ console.log('bye');
+ });
+}
+
+comments.saveComment({
+ doc: '/path/document',
+ message: 'this is the text of the comment',
+ author: 'paul',
+ website: 'https://vorb.de',
+ email: 'paul@vorb.de',
+ created: new Date()
+}, log);
View
13 test/count.js
@@ -0,0 +1,13 @@
+var Comments = require('../');
+
+var comments = new Comments();
+
+// list all comments
+comments.count(null, function(err, count) {
+ if (err) throw err;
+ console.log('There are '+count+' comments in this collection.');
+
+ comments.close(function() {
+ console.log('bye');
+ });
+});
View
19 test/list.js
@@ -0,0 +1,19 @@
+var Comments = require('../');
+
+var comments = new Comments();
+
+// list all comments
+comments.getComments('/path/document', function(err, results) {
+ results.each(function(err, comment) {
+ if (err) throw err;
+
+ if (comment)
+ console.log(comment);
+ else
+ comments.close(function(err) {
+ if (err) throw err;
+
+ console.log('bye');
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.