最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Automatic createdAt and updatedAt fields in Meteor - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 6

I 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()
发布评论

评论列表(0)

  1. 暂无评论