Skip to content
Browse files

added a one page example

  • Loading branch information...
1 parent 5a8daee commit 9988ba869f4efd0453a506f8774c1aa4994d1269 @jamescarr committed Mar 23, 2012
Showing with 160 additions and 0 deletions.
  1. +48 −0 examples/catch-all.js
  2. +1 −0 index.js
  3. +12 −0 test/examples/simple.js
  4. +18 −0 test/examples/url.js
  5. +12 −0 test/helpers/mongoose.js
  6. +69 −0 test/url-test.js
View
48 examples/catch-all.js
@@ -0,0 +1,48 @@
+var mongoose = require('mongoose')
+ , Schema = mongoose.Schema
+ , em = new(require('events')).EventEmitter
+ , monamqp = require('../index')
+ , amqp = require('amqp')
+ , UserSchema = new Schema({
+ name: String
+ , dob: Date
+ })
+ , messageCount = 0;
+
+UserSchema.plugin(monamqp, {exchange:'user'})
+
+var User = mongoose.model('user', UserSchema)
+
+var conn = amqp.createConnection()
+conn.on('ready', function(){
+ var ex = conn.exchange('user');
+ conn.queue('mongoose-amqp-example', function(q){
+ q.bind('user', '#')
+ em.emit('subscribed');
+ q.subscribe(function(user, headers, deliveryInfo){
+ console.log(user);
+ console.log("routing key: " + deliveryInfo.routingKey)
+ if(++messageCount >=2){
+ console.log("goodbye!")
+ conn.end()
+ process.exit();
+ }
+ });
+ });
+});
+
+
+em.on('subscribed', function(){
+ mongoose.connect('mongodb://localhost/example', function(){
+ var user = new User({name:'Homer Simpson', dob: new Date()});
+ user.save(function(){
+ console.log('taking a nap for 2 seconds then deleting');
+ setTimeout(function(){
+ user.remove();
+ }, 2000);
+ });
+ });
+
+});
+
+
View
1 index.js
@@ -3,6 +3,7 @@ var amqp = require('amqp')
module.exports = exports = function amqpPlugin(schema, options){
connect(options, function(ex){
schema.post('save', function(){
+ console.log('saving')
ex.publish(ex.name+'.update', this)
});
schema.post('remove', function(){
View
12 test/examples/simple.js
@@ -0,0 +1,12 @@
+var mongoose = require('mongoose')
+ , Schema = mongoose.Schema
+ , mongoose_amqp = require('../../index')
+
+ var UserSchema = new Schema({
+ name: String
+ , email: String
+})
+
+UserSchema.plugin(mongoose_amqp, {exchange:'user'});
+module.exports = mongoose.model('user', UserSchema)
+
View
18 test/examples/url.js
@@ -0,0 +1,18 @@
+var mongoose = require('mongoose')
+ , Schema = mongoose.Schema
+ , mongoose_amqp = require('../../index')
+ , URL = 'amqp://guest:guest@localhost:5672'
+ , UserSchema = new Schema({
+ name: String
+ , email: String
+})
+
+UserSchema.plugin(mongoose_amqp, {
+ exchange:'user'
+ , url: URL
+});
+module.exports = {
+ User: mongoose.model('user', UserSchema)
+ , url: URL
+}
+
View
12 test/helpers/mongoose.js
@@ -0,0 +1,12 @@
+var mongoose = require('mongoose')
+
+module.exports = {
+ before: function(done){
+ mongoose.connect('mongodb://localhost/mongoose-amqp-test', done)
+ },
+ after: function(done){
+ mongoose.disconnect();
+ if(done) done()
+ }
+}
+
View
69 test/url-test.js
@@ -0,0 +1,69 @@
+var should = require('should')
+ , amqp = require('amqp')
+ , example = require('./examples/url')
+ , helper = require('./helpers/mongoose')
+ , User = example.User
+
+describe('Mongoose URL Based AMQP Example', function(){
+ var queue = null, connection = null;
+ before(function(done){
+ helper.before(function(){
+ User.remove(function(){
+ createAmqpConnection(function(conn, q){
+ queue = q;
+ connection = conn
+ done();
+ });
+ });
+ });
+ });
+ after(function(){
+ helper.after()
+ 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, dontcate, props){
+ user._id.should.eql(james._id.toString());
+ q.unsubscribe(props.consumerTag);
+ done()
+ });
+
+ var james = new User({name:'james', email:'jc@example.com'});
+ james.save(function(){
+ james.remove()
+ });
+ });
+ });
+});
+
+
+
+function createAmqpConnection(callback){
+ var conn = amqp.createConnection({url:example.url})
+ 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 9988ba8

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