1

Context: On the client, a field is modified and that change is propagated to the server as a keypath-value pair. I start out with a keypath, something like foo.bar.baz = cat, which I can convert to {foo: {bar: {baz: "cat"}}} and then merge into my document (which might look something like {foo: {bar: {baz: "dog"}}}).

So far, this has worked fine for all objects, however it breaks down when I need to do something with an array.


Suppose my document in RethinkDB looked like this: {name: "Me", pets: [{name: "Shadow"}]}. The user decides to update the pet's name, so the keypath looks something like pets[0].name = Sparky. As far as I can tell, this can't trivially (or non-trivially, for that matter) be converted into something I can pass to merge. Or can it? Thoughts?

Max
  • 4,882
  • 2
  • 29
  • 43

1 Answers1

0

You'd have to write something like .update(function(row) { return {pets: row('pets').changeAt(0, row('pets')(0).merge({name: 'Sparky'}))};}). I would recommend instead making pets a map from names to pets (or from "numbers converted to strings" to pets if you really want indexing).

mlucy
  • 5,249
  • 1
  • 17
  • 21
  • Thanks mlucy. I'm going to give the map of numbers-as-strings to values a try. (I'd *like* to keep the "pets" ordered, and if I were to key by name [name-to-value mapping], I'd need to keep track of the order through another array...which I'd need to update if any of the names changed.) – Max Jun 12 '16 at 23:50
  • Alright, what I ended up doing was somewhat grotesque, but this is for a hobby project anyway. I just retrieved the entire object, made the changes I needed, then put the whole thing back (`replace`d the entire document). Simple, relatively easy, and no hacks...other than being non-atomic and rather inefficient. – Max Jun 13 '16 at 01:42