Allow PUT /api/things/:id to create a thing resource if it doesn't exist
A PUT /api/things/:id request should behave in one of two ways:
1 - When a thing resource with the given id does not exist:
- It should create the thing resource with the given id
- It should return 201 (Created)
2 - When a thing resource with the given id already exists:
- It should update the existing thing resource
- It should return 200 (Ok)
Please see https://tools.ietf.org/html/rfc7231#section-4.3.4
What do you guys think?
@bajtos @raymondfeng @ritch PTAL, possible bug.
Relabelling as an enhancement.
@sam-github was recently asking for a similar feature in our chat.
At the moment, PUT /api/things/:id preforms updateAttributes, which allows the client to update only a subset of model properties. When the model is not found and a new instance is being created, this may be incorrect behaviour, because a new model may require additional properties.
Workaround: call findOrCreate() first and then if it found, do an updateAttributes().
Ideally, we should implement an atomic method updateOrCreateProperties(where, propsIfCreate, propsIfUpdate, cb).
From chat:
Sam Roberts: I pretty frequently have a situation where I want, for example, a Process I've just seen to exist, and to have some specific properties about it set to a particular value. It might be there already, or maybe not. If its there already, I don't want to change any properties, except for one specific one (like the cause of exit). If it isn't there, its OK to create it with all the defaul values (except the one property I want to set). Upsert overwrites all current values if it exists, so doesn't work well. I can do FindOne, then if it doesn't exist, try to create it. and if create fails (because perhaps a concurrent request also needed to create the Process), then I do a FindOne again, and do an updateProperties. It seems subtle to me.
[10:30 AM] Raymond Feng: @sam: Some DBs have atomic findOrCreate. We'll use it if a connector implements the method
[10:30 AM] Miroslav Bajtoš: @sam: I see. You basically need `upsertProperties(where, propsIfCreate, propsIfUpdate, cb)`