是否可以从 Meteor 读取所有数据库操作?
Is it possible to read all database operations from Meteor?
如果我有这样设置的数据库:
@Apples = new Meteor.Collection 'apples'
有没有办法设置一个函数,每次对apple进行数据库操作时调用一个函数?例如
Apple.update {name: 'Frank'}
导致这样的事情被称为:
appleOperation = (operation, parameter) ->
# Log the operation in another database
# Continue with the database operation
我相信您可能正在寻找 cursor.observe and/or cursor.observeChanges。不好意思没用Coffeescript
Apples = new Meteor.Collection("apples");
Apples.find().observe({
added: function(document) {
// Do stuff with the added document
},
changed: function(newDoc, oldDoc) {
// Do stuff with the old and new document
},
removed: function(document) {
// Do stuff with the removed document
}
});
如果您现在添加文档:
Apples.insert({name: "Frank"});
将使用 Frank 插入时使用的任何 ID 调用添加的函数,并将 {name: "Frank"} 作为字段参数。
与 observeChanges 类似,它只为您提供更改的字段
Apples.find().observeChanges({
added: function(id, fields) {
// Do stuff with the added document
},
changed: function(id, fields) {
// Do stuff with the changed fields
},
removed: function(id) {
// Do stuff with the id of the removed document
}
});
是的,还有一个很好的包来处理所有细节问题:
如果我有这样设置的数据库:
@Apples = new Meteor.Collection 'apples'
有没有办法设置一个函数,每次对apple进行数据库操作时调用一个函数?例如
Apple.update {name: 'Frank'}
导致这样的事情被称为:
appleOperation = (operation, parameter) ->
# Log the operation in another database
# Continue with the database operation
我相信您可能正在寻找 cursor.observe and/or cursor.observeChanges。不好意思没用Coffeescript
Apples = new Meteor.Collection("apples");
Apples.find().observe({
added: function(document) {
// Do stuff with the added document
},
changed: function(newDoc, oldDoc) {
// Do stuff with the old and new document
},
removed: function(document) {
// Do stuff with the removed document
}
});
如果您现在添加文档:
Apples.insert({name: "Frank"});
将使用 Frank 插入时使用的任何 ID 调用添加的函数,并将 {name: "Frank"} 作为字段参数。
与 observeChanges 类似,它只为您提供更改的字段
Apples.find().observeChanges({
added: function(id, fields) {
// Do stuff with the added document
},
changed: function(id, fields) {
// Do stuff with the changed fields
},
removed: function(id) {
// Do stuff with the id of the removed document
}
});
是的,还有一个很好的包来处理所有细节问题: