keystonejs/keystone-classic

findOneAndUpdate does not update tracked fields if upserted

Open

#4442 opened on Sep 25, 2017

View on GitHub
 (4 comments) (0 reactions) (0 assignees)JavaScript (14,656 stars) (2,288 forks)batch import
4.x candidatebughelp wanted

Description

Expected behavior

Calling findOneAndUpdate and creating a new document (while upsert is set to true) as a result of the function should also update the tracked createdAt and createdBy fields

Actual/Current behavior

It does not update createdAt or createdBy. They are both undefined.

Steps to reproduce the actual/current behavior

  1. Implement a findOneAndUpdate function call on a Keystone model.
  2. Pass it a first argument that represents a document that does not exist.
  3. Pass it a second argument that represents what data the document should have.
  4. Let the function run.
  5. Look at the document in the Admin panel, or retrieve the document some other way.
  6. createdAt and createdBy are both undefined.

I can manually instantiate these fields by passing createdAt and createdBy to the second argument of findOneAndUpdate, but the createdAt date will always update to be whenever the document was updated, rather than being maintained. Using findOneAndUpdate, I've no way of knowing whether this document exists before including this condition.

Environment

Software Version
Keystone 4.0.0-beta.5
Node 6.9.1

Contributor guide