This PR patches #3131 after testing on staging exposed a regression.
Problem
Users were unable to create or edit articles as a bug in the save logic meant that documents were being persisted with a _id of null when upserted.
Solution
Change the implementation of the articles and sessions' save method to check for an existing _id of the object to be persisted, use updateOne when it exists, and use insertOne (instead of an upsert on a updateOne) when it doesn't.
Considerations
In the existing implementation (i.e. pre #3131), the save method accepts explicitly setting an _id value for documents rather than always relying on mongodb to generate them upon creation. So, for example, if I called Article.save while passing an object with a key-value pair of _id: 123, it would respect this and persist an article with 123 as the _id. It would also except null or undefined as values, which is what is causing the bug.
With refactors of this kind, I aim to not change test implementations as they can easily result in false positives, however, it felt warranted in this case. My read of the code is that the tests I have modified were relying on this behavior as a test convenience rather than an assertion that it would explicitly set the _id field with whatever value is passed in.
If I am missing something in the code, or using bad assumptions, then please let me know.
This PR patches #3131 after testing on staging exposed a regression.
Problem
Users were unable to create or edit articles as a bug in the save logic meant that documents were being persisted with a
_id
ofnull
whenupserted
.Solution
Change the implementation of the
articles
andsessions
'save
method to check for an existing_id
of the object to be persisted, useupdateOne
when it exists, and useinsertOne
(instead of anupsert
on aupdateOne
) when it doesn't.Considerations
_id
value for documents rather than always relying onmongodb
to generate them upon creation. So, for example, if I calledArticle.save
while passing an object with a key-value pair of_id: 123
, it would respect this and persist anarticle
with123
as the_id
. It would also exceptnull
orundefined
as values, which is what is causing the bug._id
field with whatever value is passed in.If I am missing something in the code, or using bad assumptions, then please let me know.