admin管理员组

文章数量:1026989

I have a table set up to have timestamps and bookshelf configured to use them. Normally everything happens as it should and bookshelf takes care of the timestamps, but I have a case where I want to specify them, but when I try to do that the values are ignored and the current date is used.

I've tried to simplify my use case down to the essentials:

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
    })
  }
}

I still want to keep the hasTimestamps configured for regular use cases. Is it possible to get Bookshelf to let me override the timestamps behavior?

I have a table set up to have timestamps and bookshelf configured to use them. Normally everything happens as it should and bookshelf takes care of the timestamps, but I have a case where I want to specify them, but when I try to do that the values are ignored and the current date is used.

I've tried to simplify my use case down to the essentials:

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
    })
  }
}

I still want to keep the hasTimestamps configured for regular use cases. Is it possible to get Bookshelf to let me override the timestamps behavior?

Share Improve this question edited Jul 25, 2015 at 8:11 Gangstead asked Jul 25, 2015 at 7:23 GangsteadGangstead 4,18223 silver badges37 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

After looking at the Bookshelf source created_at and updated_at are set on create/insert as necessary and never read from the model you pass in: https://github./tgriesser/bookshelf/blob/c83ada77d5c670a773c0b47c604b452cd0e03e86/src/base/model.js#L413

This makes my goal impossible. Reflecting on that it seems like a good idea to not overload these columns and to create another column for the date I'm trying to store.

Just updating right answer for new visitors to this question. The latest is bookshelf allows overriding timestamp values.

See official documentation and example here

https://bookshelfjs/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)'
  // }
})

I have a table set up to have timestamps and bookshelf configured to use them. Normally everything happens as it should and bookshelf takes care of the timestamps, but I have a case where I want to specify them, but when I try to do that the values are ignored and the current date is used.

I've tried to simplify my use case down to the essentials:

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
    })
  }
}

I still want to keep the hasTimestamps configured for regular use cases. Is it possible to get Bookshelf to let me override the timestamps behavior?

I have a table set up to have timestamps and bookshelf configured to use them. Normally everything happens as it should and bookshelf takes care of the timestamps, but I have a case where I want to specify them, but when I try to do that the values are ignored and the current date is used.

I've tried to simplify my use case down to the essentials:

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
    })
  }
}

I still want to keep the hasTimestamps configured for regular use cases. Is it possible to get Bookshelf to let me override the timestamps behavior?

Share Improve this question edited Jul 25, 2015 at 8:11 Gangstead asked Jul 25, 2015 at 7:23 GangsteadGangstead 4,18223 silver badges37 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

After looking at the Bookshelf source created_at and updated_at are set on create/insert as necessary and never read from the model you pass in: https://github./tgriesser/bookshelf/blob/c83ada77d5c670a773c0b47c604b452cd0e03e86/src/base/model.js#L413

This makes my goal impossible. Reflecting on that it seems like a good idea to not overload these columns and to create another column for the date I'm trying to store.

Just updating right answer for new visitors to this question. The latest is bookshelf allows overriding timestamp values.

See official documentation and example here

https://bookshelfjs/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)'
  // }
})

本文标签: javascriptManually setting timestamp values in bookshelfjsStack Overflow