Wednesday 6 December 2017

Objection.js and timestamps

I have just started using objection.js and I love it!However, I have a problem with timestamps. It is not about how to create them. I followed the docs and looked up examples and they are setup properly.The problem is that I want those fields to be set only by the db, and from none else.Instead, after the setup I did, I doPeople.query().insert({updated_at: '2017-01-01'}); I am able to explicitly set the value. Is there a way to avoid this?My people.js model file:const Model = require('objection').Model; class People extends Model { // Table name is the only required property. static get tableName() { return 'people'; } $beforeInsert() { this.created_at = new Date().toISOString(); } $beforeUpdate() { this.updated_at = new Date().toISOString(); } static get jsonSchema() { return { type: 'object', required: ['first_name', 'last_name'], properties: { id: { type: 'integer' }, first_name: { type: 'string', minLength: 1, maxLength: 255 }, last_name: { type: 'string', minLength: 1, maxLength: 255 }, bio: { type: 'string' } } } }; } module.exports = People; and my migration file for the people table:exports.up = function (knex, Promise) { return knex.schema .createTable('people', function (table) { table.increments('id').primary(); table.string('first_name').notNullable(); table.string('last_name').notNullable(); table.text('bio'); table.timestamps(true, true); }); }; exports.down = function (knex, Promise) { return knex.schema.dropTable('people'); }; How can I avoid this?

Submitted December 06, 2017 at 11:46AM by honestserpent

No comments:

Post a Comment