0

I am using sails 0.12.0 with sails-mongo 0.12.3. I took over an existing application from "external" developers.

I noticed that the "createdAt" date in present all mongo collections is incorrect, whereas the updatedAt date is correct. The createdAt date should auto-generate as today's date and time when the document is created, however it seems to be set to some date in the past. The updatedAt date does appear to have the correct value for today's date.

Example:

"createdAt" : ISODate("2017-09-25T18:39:49.409Z"), "updatedAt" : ISODate("2017-09-25T18:39:59.021Z")

How can I resolve this issue? I noticed that the files in the "models" directory are all empty, so it does not appear to be a model issue as no explicit models exist.

Example:

module.exports = {
  attributes: {

  }
};

I tried explicitly setting the createdAt date in one of the model files (as suggested by similar questions) however that did not work.

Example:

  attributes: {
    createdAt: {
      type: 'datetime',
      defaultsTo: function() {return new Date();}
    }
  },
U Rogel
  • 1,893
  • 15
  • 30
pengz
  • 2,279
  • 3
  • 48
  • 91
  • Are you using blueprints? If so you might find something helpful in these github issues: https://github.com/balderdashy/sails/issues/4575 – Raqem Feb 05 '19 at 16:46

3 Answers3

2

You can try to define your self the createdAt.

In your single model (api/models) you dont need to write anything related to createdAt or updatedAt.

// api/models/SomeModel.js
module.exports = {
  attributes: {
    // createdAt: { type:'string' },  -> coment it out, or remove at all from here.
  }
};

add this to your config/models.js

// config/models.js

module.exports.models = {
  migrate: 'safe',
  attributes: {
    createdAt: { type: 'string' },
    updatedAt: { type: 'string' },
    id: { type: 'string', columnName: '_id' }
  },

  beforeUpdate: function(values, next) {
    if(!values.updatedAt) values.updatedAt = new Date().toISOString();
    // this is my feature i am using if i want to explicity not trigger updatedAt.
    else if(values.updatedAt == 'no_update') delete values.updatedAt;
    else values.updatedAt = new Date(values.updatedAt).toISOString();
    next();
  },

  beforeCreate: function(values, next) {
    if(!values.updatedAt) values.updatedAt = new Date().toISOString();
    else values.updatedAt = new Date(values.updatedAt).toISOString();
    if(!values.createdAt) values.createdAt = new Date().toISOString();
    else values.createdAt = new Date(values.createdAt).toISOString();
    next();
  },
}
mansim
  • 727
  • 7
  • 21
  • Thanks for your answer. I would add to it that I don't recommend to convert date to string with .toISOString() if you don't need to apply $gt,$lt like operations to query DB data; otherwise remove .toISOString() from the end of lines. – 1nstinct Apr 25 '22 at 21:44
1

In newer Sails.js versions (>1.0 I guess, tested it on 1.5) it works like this:

module.exports.models = {
    createdAt: { type: 'ref', autoCreatedAt: true },
    updatedAt: { type: 'ref', autoUpdatedAt: true },
}

If it's set up so then createdAt and updatedAt will be ISODate() objects in MongoDB database, as expected.

Sven
  • 856
  • 10
  • 10
0

For sails 1.n, in sails/config/models.js, you could try:

attributes: {
  createdAt: { type: 'string', columnType: 'datetime', autoCreatedAt: true, },
  updatedAt: { type: 'string', columnType: 'datetime', autoUpdatedAt: true, },
  // id: { type: 'number', autoIncrement: true, },
  id: { type: 'string', columnName: '_id' },
},
DerekC
  • 762
  • 5
  • 10