admin管理员组文章数量:1387288
In my collection I'd like to have automatically generated createdAt
and updatedAt
fields that would contain the date of when the object was inserted / updated for the last time - kind of like it's happening in Ruby on Rails. Currently I'm doing this with an observer similar to this one:
MyCollection.find({}).observeChanges({
changed: function(id, changes) {
MyCollection.update(id, ...);
},
});
Is there a better / more efficient / more straightforward way?
In my collection I'd like to have automatically generated createdAt
and updatedAt
fields that would contain the date of when the object was inserted / updated for the last time - kind of like it's happening in Ruby on Rails. Currently I'm doing this with an observer similar to this one:
MyCollection.find({}).observeChanges({
changed: function(id, changes) {
MyCollection.update(id, ...);
},
});
Is there a better / more efficient / more straightforward way?
Share Improve this question asked Mar 18, 2014 at 9:08 Hubert OGHubert OG 19.6k7 gold badges47 silver badges73 bronze badges 2- 1 To catch client updates and inserts you can use deny() as remended in the Unofficial FAQ. – user728291 Commented Mar 18, 2014 at 9:31
-
I don't think the following meteorite package uses
observeChanges
to acplish similar functionality, so I guess their way is better than yours: github./matb33/meteor-collection-hooks – Peppe L-G Commented Mar 18, 2014 at 10:36
2 Answers
Reset to default 6I use Collection2. It supports autoValue
in the schema, a function that putes the forced value of a field. As these two fields are used in all collections, you can save them to a variable:
@SchemaHelpers =
createdAt:
type: Date
autoValue: ->
if @isInsert
return new Date
if @isUpsert
return $setOnInsert: new Date
if @isUpdate
@unset()
return
updatedAt:
type: Date
autoValue: ->
return new Date
And then in the collection:
Schema = {}
Posts = new Meteor.Collection("posts")
Schema.Posts = new SimpleSchema
createdAt: SchemaHelpers.createdAt
updatedAt: SchemaHelpers.updatedAt
title:
type: String
max: 30
body:
type: String
max: 3000
Posts.attachSchema(Schema.Posts)
This solution makes updatedAt
always present and its value will be very close to createdAt
when it is just inserted (not necessarily the same). If you need updatedAt
not to be set when inserting, you can use something like the example in the Collection2 readme:
updatedAt: {
type: Date,
autoValue: function() {
if (this.isUpdate) {
return new Date();
}
},
denyInsert: true,
optional: true
},
but this does not handle upserts. I don't know any good solution that handles upserts correctly and leaves the field empty at inserts.
I like https://github./matb33/meteor-collection-hooks
collection.before.insert (userId, doc) ->
doc.createdAt = new Date().valueOf #toISOString()
本文标签: javascriptAutomatic createdAt and updatedAt fields in MeteorStack Overflow
版权声明:本文标题:javascript - Automatic createdAt and updatedAt fields in Meteor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744510402a2609845.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论