https://github.com/mediacomem/bookshelf-touch
Automatically update timestamps on your Bookshelf models when saving
https://github.com/mediacomem/bookshelf-touch
Last synced: about 1 year ago
JSON representation
Automatically update timestamps on your Bookshelf models when saving
- Host: GitHub
- URL: https://github.com/mediacomem/bookshelf-touch
- Owner: MediaComem
- License: mit
- Created: 2017-11-17T14:20:45.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-30T08:22:54.000Z (over 8 years ago)
- Last Synced: 2025-03-16T07:36:28.121Z (over 1 year ago)
- Language: JavaScript
- Size: 186 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# bookshelf-touch
This [Bookshelf.js](http://bookshelfjs.org) plugin automatically updates your timestamps when saving your Bookshelf models.
[](https://badge.fury.io/js/bookshelf-touch)
[](https://gemnasium.com/github.com/MediaComem/bookshelf-touch)
[](https://travis-ci.org/MediaComem/bookshelf-touch)
[](https://coveralls.io/github/MediaComem/bookshelf-touch?branch=master)
[](LICENSE.txt)
- [Usage](#usage)
- [Configuration](#configuration)
- [Timestamp configuration](#timestamp-configuration)
Developed at the [Media Engineering Institute](http://mei.heig-vd.ch) ([HEIG-VD](https://heig-vd.ch)).
## Usage
```js
const knex = require('knex')({ ... });
const bookshelf = require('bookshelf')(knex);
const touch = require('bookshelf-touch');
bookshelf.plugin(touch);
const MyModel = bookshelf.Model.extend({
tableName: 'my_table',
// Define which timestamps you want to be automatically updated
// (supports "created_at" and "updated_at" by default, but you
// can configure more).
timestamps: [ 'created_at', 'updated_at' ]
});
const model = new MyModel({
name: 'Awesome'
});
// Both timestamps are automatically set when the model is created.
model.save();
// Only the updated_at timestamp is updated when the model is saved.
model.name = 'Indeed';
model.save();
// You can trigger an update of the timestamps with the touch method
// (this does not save the model):
model.touch();
```
## Configuration
The `timestamps` property of your model can be:
* `true` to automatically update both `created_at` and `updated_at`.
* A string indicating which of `created_at` or `updated_at` should be updated.
* An array containing either `created_at`, `updated_at` or both to indicate which should be updated.
* An object mapping timestamp columns as keys to timestamp configurations as value. See [Timestamp configuration](#timestamp-configuration) below.
* Do not define it or set it to false to disable the plugin on that model.
### Timestamp configuration
You may configure the behavior of timestamps (and add timestamps) yourself.
This is what the default configuration looks like:
```js
const MyModel = bookshelf.Model.extend({
tableName: 'my_table',
timestamps: {
created_at: {}
updated_at: {
default: record => record.get('created_at'),
update: true
}
}
});
```
A timestamp configuration object has 2 options:
* `default` - A function that takes the record as an argument and should return a default value.
This function will be called if the record has no value for that timestamp, or when updating
the timestamp. If it returns a falsy value, a new date is created instead. If the default
function is not specified (like for `created_at`), a new date is also created as the default
value.
* `update` - Whether to update the timestamp when the record is touched. This defaults to false.
As expected, it is not set for `created_at` and set to true for `updated_at` in the default
configuration.
You may define new timestamps by adding them to the configuration.
Use true and false to enable/disable default timestamps:
```js
const MyModel = bookshelf.Model.extend({
tableName: 'my_table',
timestamps: {
created_at: true,
updated_at: false,
amended_at: {
default: record => new Date(11029388)
}
}
});
```