Solvedobjection.js Timestamps once again

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 do

People.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:

'use strict';

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:

'use strict'

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');
};
16 Answers

✔️Accepted Answer

Ah, it didn't work because in my BaseModel:

class BaseModel extends Model {
  $beforeInsert() {
    if (this.timestamps) { // <-- Should actually be `this.constructor.timestamps`
      const timestamp = new Date().toISOString()
      this.createdAt = timestamp
      this.updatedAt = timestamp
    }
  }

  $beforeUpdate() {
    if (this.timestamps) { // <-- Should actually be `this.constructor.timestamps`
      this.updatedAt = new Date().toISOString()
    }
  }
}

...and in my actual model:

class MyModel extends BaseModel {
  static get timestamps() {
    return true
  }
}

...in case anyone else finds their way to this issue.

Related Issues:

23
objection.js Support for knex 0.95.0
I'll soon release the first alpha version of objection 3 It supports the newest knex To install it r...
19
objection.js Class constructor Model cannot be invoked without 'new'
You can also make it so that babel is using your node version by doing something like this: ...
15
objection.js Typings don't allow custom query builders
Here you go: I'm trying to create Model which has extended query builder with .session() method ...
12
objection.js Need to explicit destroy knex in order to stop the script
add idleTimeoutMillis: 500 too if you want to use knex in jest Hi I initialize Model this way: The p...
10
objection.js Poll - What would you like to see in the future versions of objection?
Support for for await (...) loops would be nice Especially if it's backed by cursors This could fall...
5
objection.js Timestamps once again
Ah it didn't work because in my BaseModel: ...and in my actual model: ...in case anyone else finds t...