Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit 94ff659d7ac21d4b9ea347f0ce53c0bd0ef37b4c @jamescarr committed
Showing with 189 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +28 −0 index.js
  3. +24 −0 package.json
  4. +59 −0 readme.md
  5. +76 −0 test/plugin-test.js
2 .gitignore
@@ -0,0 +1,2 @@
+node_modules
+
28 index.js
@@ -0,0 +1,28 @@
+var amqp = require('amqp')
+
+
+module.exports = exports = function amqpPlugin(schema, options){
+ connect(options, function(ex){
+ schema.post('save', function(){
+ ex.publish(ex.name+'.update', this)
+ });
+ schema.post('remove', function(){
+ ex.publish(ex.name+'.remove', this)
+ });
+ });
+};
+
+
+function connect(options, callback){
+ conn = amqp.createConnection()
+ conn.on('error', function(){
+ console.log("unable to connect to rabbitmq.Retrying in 5000ms");
+ setTimeout(function(){
+ connect(options, callback);
+ }, 5000);
+ });
+ conn.on('ready', function(){
+ var ex = conn.exchange(options.exchange)
+ callback(ex);
+ });
+}
24 package.json
@@ -0,0 +1,24 @@
+{
+ "author": "James R. Carr <james.r.carr@gmail.com> (http://blog.james-carr.org)",
+ "name": "mongoose-amqp-plugin",
+ "description": "A plugin that publishes mongoose models over amqp on save",
+ "version": "0.0.1",
+ "repository": {
+ "url": ""
+ },
+ "scripts": {
+ "test": "mocha"
+ },
+ "engines": {
+ "node": "~0.6.6"
+ },
+ "dependencies": {
+ "mongoose":"2.5.13"
+ , "amqp":"0.1.2"
+ },
+ "devDependencies": {
+ "mocha": "0.14.1"
+ , "should": "0.6.0"
+ },
+ "optionalDependencies": {}
+}
59 readme.md
@@ -0,0 +1,59 @@
+# mongoose-amqp-plugin
+This is a handy pluigin that makes mongoose models publish to an AMQP
+exchange on save. This can be handy if you want to update other systems
+when a change happens to one of your models.
+
+
+## Example Usage
+
+```javascript
+var mongoose = require('mongoose')
+ , mongoose_amqp = require('mongoose-amqp')
+ , Schema = mongoose.Schema
+
+var User = new Schema({
+ name: String
+ , email: String
+})
+
+User.plugin(mongoose_amqp, {exchange:'users'})
+
+
+```
+
+### Model.plugin(mongoose_amqp, options)
+
+The first argument is obviously the plugin. The second argument is an
+options object, all of which one (exchange) are optional but contain smart defaults.
+
+ * `exchange` - the exchange to publish the model to on save.
+ * `host` - host of the amqp broker. Defaults to localhost
+ * `port` - port of the amqp broker. Defaults to 5672
+ * `username` - the username to login with. Defaults to guest.
+ * `password` - the password to login with. Defaults to guest.
+ * `vhost` - the vhost to connect to. Defaults to '/'
+
+cense
+
+(The MIT License)
+
+Copyright (c) 2012-2012 James R. Carr &lt;james.r.carr@gmail.com&gt;
+
+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, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
76 test/plugin-test.js
@@ -0,0 +1,76 @@
+var should = require('should')
+ , mongoose = require('mongoose')
+ , Schema = mongoose.Schema
+ , mongoose_amqp = require('../index')
+ , amqp = require('amqp')
+
+var UserSchema = new Schema({
+ name: String
+ , email: String
+})
+
+UserSchema.plugin(mongoose_amqp, {exchange:'user'});
+var User = mongoose.model('user', UserSchema)
+
+describe('Mongoose AMQP', function(){
+ var queue = null, connection = null;
+ before(function(done){
+ mongoose.connect('mongodb://localhost/mongoose-amqp-test', function(){
+ User.remove(function(){
+ createAmqpConnection(function(conn, q){
+ queue = q;
+ connection = conn
+ done();
+ });
+ });
+ });
+ });
+ after(function(){
+ mongoose.disconnect();
+ connection.end();
+ });
+
+ it('should publish the model on save', function(done){
+ queue.subscribe(function(user, dunno, props){
+ user.name.should.eql(james.name)
+ user.email.should.eql(james.email)
+ user._id.should.eql(james._id.toString())
+ queue.unsubscribe(props.consumerTag)
+ done()
+ });
+ var james = new User({name:'james', email:'jc@example.com'});
+ james.save()
+ });
+
+ it('should publish deletions', function(done){
+ connection.queue('mongoose-amqp-test-deletions', function(q){
+ q.bind('user', 'user.remove');
+ q.subscribe(function(user){
+ user._id.should.eql(james._id.toString());
+ done()
+ });
+
+ var james = new User({name:'james', email:'jc@example.com'});
+ james.save(function(){
+ james.remove()
+ });
+ });
+ });
+});
+
+
+
+function createAmqpConnection(callback){
+ var conn = amqp.createConnection()
+ conn.on('ready', function(){
+ var exchange = conn.exchange('user')
+ var queue = conn.queue('mongoose-amqp-test-queue', function(q){
+ q.bind('user', 'user.update');
+ callback(conn, q);
+ });
+ });
+ conn.on('error', function(){
+ console.log("unable to connect to rabbitmq server on localhost. Is it running?");
+ });
+}
+

0 comments on commit 94ff659

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