docs icon indicating copy to clipboard operation
docs copied to clipboard

Insert - conflict options with a function on nested data

Open jarekkowol opened this issue 8 years ago • 1 comments

r.table('table').insert({
    id: id,
    session: session,
    sockets: [{
        id: socket_id
    }]
}, {conflict: function(id, oldDoc, newDoc) {
    return newDoc.merge({
        session: session,
        sockets: sockets(oldDoc('sockets'), newDoc('sockets'))
    });
}})

Adding new objects works fine:

function sockets(oldDoc, newDoc) {
    return oldDoc.append({
        id: socket_id
   });
}

Is it possible to update / remove nested data using oldDoc object?

jarekkowol avatar May 07 '17 20:05 jarekkowol

@jarekkowol did you resolved your issue?

Conflict handling is made for updating or keeping the old version of a document. I'm not sure that your example with .append is actually working as expected. You probably wanted to do return oldDoc('sockets').append({id: socket_id}).

As for updating nested objects - it's possible. It is the example that you have with merge and then if you have an array you can use map, forEach and filter to construct the new array that you want to merge. Example:

return newDoc.merge({
  session: session,
  sockets: r.merge(newDoc('sockets'), oldDoc('sockets')).map((socket) => {
    // You do your stuff here
  })
});

thinklinux avatar May 18 '17 18:05 thinklinux