手动设置bookshelf.js中的时间戳值

5

我已经设置了一个表格,用于存储时间戳,并配置了书架以使用它们。通常情况下,一切都按照预期进行,书架会处理时间戳,但我有一个需要指定时间戳的情况,但当我尝试这样做时,值被忽略,当前日期被使用。

我试图将我的用例简化到必要的内容:

var Author = Bookshelf.Model.extend({
  tableName: 'authors',
  hasTimestamps: ['created_at', 'updated_at'],

  bookAuthors: function(){
    return this.hasMany(require('.book_authors'));
  },

  associateBookWithAuthor(bookId,timestamp) {
    return self.related('bookAuthors').create({
      book_id: bookId,
      updated_at: timestamp, // ignored by bookshelf
      created_at: timestamp  // also ignored
    })
  }
}

我仍希望在常规用例下保留hasTimestamps配置。是否有可能让Bookshelf允许我覆盖时间戳行为?

2个回答

5

1

我更新了这个问题的正确答案,以便新来访者能够参考。最新版本的书架允许覆盖时间戳值。

在此处查看官方文档和示例

https://bookshelfjs.org/api.html#Model-instance-hasTimestamps

myModel.save({created_at: new Date(2015, 5, 2)}).then(function(updatedModel) {
  // {
  //   name: 'blah',
  //   created_at: 'Tue Jun 02 2015 00:00:00 GMT+0100 (WEST)',
  //   updated_at: 'Sun Mar 25 2018 15:07:11 GMT+0100 (WEST)'
  // }
})

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接