
Objection-timestamps
Automatically modify the created_at
and updated_at
columns on your models.
v0.x usage
v1.x and v2.x usage
Setup
Basic
The basic setup assumes you have the columns created_at
and updated_at
in your table.
let Model = require('objection').Model;
let timestampPlugin = require('objection-timestamps').timestampPlugin;
class Post extends timestampPlugin()(Model) {
static get tableName() {
return 'user';
}
static get timestamp() {
return true;
}
}
Post
.query()
.insertAndFetch({
firstName: 'John',
lastName: 'Doe'
})
.then(john => {
console.log(john.created_at);
console.log(john.updated_at);
});
Advanced
You can pass in an object to override the default settings
let Model = require('objection').Model;
let timestampPlugin = require('objection-timestamps').timestampPlugin;
let plugin = timestampPlugin({
createdAt: 'my_created_at',
updatedAt: 'my_updated_at',
genDate: function() {
return 'my date format';
}
});
class Post extends plugin(Model) {
static get tableName() {
return 'user';
}
static get timestamp() {
return true;
}
}
Post
.query()
.insertAndFetch({
firstName: 'John',
lastName: 'Doe'
})
.then(john => {
console.log(john.my_created_at);
console.log(john.my_updated_at);
});
If you provide custom values plugin won't override them
let Model = require('objection').Model;
let timestampPlugin = require('objection-timestamps').timestampPlugin;
class Post extends timestampPlugin()(Model) {
static get tableName() {
return 'user';
}
static get timestamp() {
return true;
}
}
Post
.query()
.insertAndFetch({
firstName: 'John',
lastName: 'Doe',
created_at: 'Foobar',
updated_at: 'Foobiz'
})
.then(john => {
console.log(john.created_at);
console.log(john.updated_at);
});