Allow replacing nested fields
See original GitHub issueDue to the use of moredots
in modifyObject
, it is currently impossible to remove properties of a nested object via the API (talking about this line: https://github.com/florianholzapfel/express-restify-mongoose/blob/master/src/operations.js#L204)
so if you have a schema:
User = mongoose.model('User', new mongoose.Schema({
name: String,
info: Object,
})
User.info
is a generic object, so one would assume doing a PATCH /User/1 {info: {bmi: 33}}
would update the info
prop to the given value. But because moredots
has no concept of the schema the above query gets converted to:
{ 'info.bmi': 33 }
This is a problem because if you have the following document:
{
name: 'bob',
info: {
bmi: 33,
weight: 100,
}
}
There is no way to remove info.weight
since posting with {info: {bmi: 22}}
will just update the bmi
instead of overwriting the entire info
Not sure if this was the intention or not, but it seems to me that it makes more sense to have PATCH
be update only at the root level, and not recursive.
The current workaround is to specifically post with {info: {bmi: 22, weight: null}}
which will overwrite that property to null
which is not really ideal since it requires prior knowledge about the document and the schema
EDIT: Another workaround is if you make a middleware that does the above and sets unwanted values to undefined
, since sending the object {info: {bmi: 22, weight: undefined}}
does remove weight
but you can’t json serialize undefined
and you still have the issue of having to know about the structure of the data before updating
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:13 (4 by maintainers)
Thank you, this worked for me. Can you elaborate on the
nested protected and protected fields
topic and what exactly needs to be done in order to get this merged? I’m very interested in having this feature in the officialexpress-restify-mongoose
repo.Actually, rather than implementing yet another flag, I think we should perhaps be stricter and more “correct” in our use of HTTP verbs.
PATCH
POST
PATCH
, whether we keep this or not is up for debatePUT
PATCH
, but it should entirely replace a resource insteadThis is where most of the work is, which mostly boils down to:
moredots
$set
I think that’s pretty much it, let me know if you want to give it a shot! If you’re not sure about the automated tests, send a PR and we’ll go from there 👍