Comments v0.5.1
Comments
An abstraction layer for MongoDB that provides some commenting functionality.
Usage
new Comments(options)Creates a new Comments object. This means, a connection to MongoDB is set up.
optionsis an object that defines some DB connection parameters.The default options are as follows:
{ 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 }
comments.saveComment(res, 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.
resis a string defining the resource the comment belongs to.commentis an object that defines a comment. It is stored directly into the collection. Any prior parsing is up to you.comment.resshould be defined for the later use ofcomments.getComments.savedis a callback function that takes three arguments(error, comment, action).erroris anErrorobject, when an error occurred, otherwise it isnull.commentis the saved comment object.actionis a string. It can either be'create','update'ornull, if an error occured.
comments.getComments(res, [[properties,] options,] received)Gives access to the comments of a resource.
resis a string defining the resource that contains the comments that you are looking for. Ifresisnull, 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:
{ _id: true, author: true, website: true, created: true, message: true }options [optional]is an object that defines additional options according to section "Query options" like sorting or paging.The default options are as follows:
{ sort: "created" // sort results by date of creation in ascending order }receivedis a callback function that takes two arguments(error, results).erroris anErrorobject, when an error occurred, otherwise it isnull.resultsis a cursor to the result set of the query. Look at section "Cursors" for more information on how to use them.
comments.count(res, counted)Counts the comments of a resource or the complete collection.
resis a string defining the resource that contains the comments that you are looking for. Ifresisnull, all comments in the collection will be counted.countedis a callback function that takes two arguments(error, count).erroris anErrorobject, when an error occurred, otherwise it isnull.countis the number of comments for the resource or in the collection.
comments.close(done)Closes the connection to the database.
doneis a callback function.
Facilities for a JSON-REST API
comments.getCommentsJSON(res, resp, received)Writes a JSON object including the requested comments to a HTTP ServerResponse
object.
resis a string defining the resource that contains the comments that you are looking for. Ifresisnull, all comments in the collection will be returned.respis ahttp.ServerResponseobject. (Please refer to the version of Node.js that you are using.)receivedis a callback function that takes one argument(error).erroris anErrorobject, when an error occurred, otherwise it isnull.
comments.parseCommentPOST(res, req, parsed)Reads a url encoded string from a HTTP ServerRequest object.
resis a string defining the resource where the comment should be saved.reqis ahttp.ServerRequestobject. (Please refer to the version of Node.js that you are using.)parsedis a callback function that takes two arguments(error, comment).erroris anErrorobject, when an error occurred, otherwise it isnull.commentis the parsed comment object.
comments.setCommentJSON(res, comment, resp, saved)Saves or updates a comment for the defined resource and writes the corresponding
HTTP status code to the given ServerResponse object.
resis a string defining the resource where the comment should be saved.commentis an object that defines a comment.respis ahttp.ServerResponseobject. (Please refer to the version of Node.js that you are using.)savedis a callback function that takes one argument(error).erroris anErrorobject, when an error occurred, otherwise it isnull.
Installation
You need a running MongoDB before you can use Comments. On Debian
apt-get install mongodbThen install Comments with npm.
npm install -g CommentsExamples
For examples, look at the tests.
Bugs and Issues
If you encounter any bugs or issues, feel free to open an issue at github.
License
Copyright © 2011-2012 Paul Vorbach
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.