Skip to content

Instantly share code, notes, and snippets.

@maksadbek
Forked from hfcorriez/test.js
Created July 14, 2014 13:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maksadbek/68650097a9099203372b to your computer and use it in GitHub Desktop.
Save maksadbek/68650097a9099203372b to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
var db = mongoose.createConnection('127.0.0.1', 'test');
// Make schema
var schema = mongoose.Schema({ name: String });
// Create subscribers collection
var subscribers = [];
// Hook `save` post method
schema.post('save', function () {
console.log(this.name);
for (var i in subscribers) {
subscribers[i](this);
}
return true;
});
// Add static method for schema
schema.static('subscribe', function (subscriber) {
subscribers.push(subscriber);
});
// Create model from schema
var Cat = db.model('Cat', schema);
// Subscriber data changes
Cat.subscribe(function (data) {
console.log('receive data:');
console.log(data);
});
// Create new cat with name kitty.
new Cat({ name: 'kitty' }).save(function (err) {
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment