Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with createdAt and updateAt timestamps when updating #20

Closed
donniefargo opened this issue Jul 14, 2018 · 2 comments
Closed

Problem with createdAt and updateAt timestamps when updating #20

donniefargo opened this issue Jul 14, 2018 · 2 comments

Comments

@donniefargo
Copy link

donniefargo commented Jul 14, 2018

Hi

I'm not able to implement timestamps properly when extending your service class.
After updating an entity the createdAt field is null. Looks like this can only be changed from
within the service itself or completely overwriting your update method.

const { Service } = require('feathers-objection');
const uuid = require('uuidv4');

module.exports = class BaseService extends Service {
  create(data, params) {
    if(!Array.isArray(data)) data = [data];

    for(let i = 0; i < data.length; i++) {
      data[i].id = uuid();
      data[i].createdAt = new Date().toISOString();
      delete data[i].updatedAt;
    }
    if(data.length === 1) data = data[0];
    return super.create(data, params);
  }

  update(id, data, params) {
    data.updatedAt = new Date().toISOString();
    delete data.createdAt;
    return super.update(id, data, params);
  }

  patch(id, data, params) {
    data.updatedAt = new Date().toISOString();
    delete data.createdAt;
    return super.patch(id, data, params);
  }
}
@donniefargo
Copy link
Author

donniefargo commented Jul 14, 2018

This is working for me. Now the createdAt timestamp is consistent.

update(id, data, params) {

    data.updatedAt = new Date().toISOString();
    delete data.createdAt;

    if (Array.isArray(data)) {
      return Promise.reject(new Error('Not replacing multiple records. Did you mean `patch`?'));
    }

    return this._get(id, params).then(oldData => {
      let newObject = { ...oldData, ...data };

      // NOTE (EK): Delete id field so we don't update it
      delete newObject[this.id];

      return this._createQuery(params).where(this.id, id).update(newObject).then(() => {
        // NOTE (EK): Restore the id field so we can return it to the client
        return { id, ...newObject};
      });
    }).catch(function(err) { throw Error(err) });
  }

@dekelev
Copy link
Member

dekelev commented Aug 24, 2018

Please try this example in your Model to automatically set the createdAt & updatedAt fields:

const { Model } = require('objection');

class User extends Model {

  static get tableName() {
    return 'user';
  }

  static get jsonSchema() {
    return {
      type: 'object',
      required: ['name'],

      properties: {
        id: { type: 'integer' },
        name: { type: 'string' }
      }
    };
  }

  $beforeInsert() {
    this.createdAt = new Date().toISOString();
    this.updatedAt = new Date().toISOString();
  }

  $beforeUpdate() {
    this.updatedAt = new Date().toISOString();
  }
}

module.exports = User;

@dekelev dekelev closed this as completed Sep 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants