-
-
Save maksadbek/68650097a9099203372b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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