diff --git a/lib/modification.js b/lib/modification.js index cb8e6ae..36db3f6 100644 --- a/lib/modification.js +++ b/lib/modification.js @@ -17,6 +17,29 @@ const { let audit +const _applyTransitionRecursively = (transition, data, d) => { + for (const [k, v] of transition.mapping) { + if (v.transition) { + if (Array.isArray(data[k])) { + d[k] = [] + for (let i = 0; i < data[k].length; i++) { + d[k].push({ _op: data[k][i]._op }) + if (data[k][i]._old) d[k][i]._old = {} + _applyTransitionRecursively(v.transition, data[k][i], d[k][i]) + } + } else { + d[k] = { _op: data[k]._op } + if (data[k]._old) d[k]._old = {} + _applyTransitionRecursively(v.transition, data[k], d[k]) + } + continue + } else if (v.ref) { + if (v.ref[0] in data) d[k] = data[v.ref[0]] + if (data._old && v.ref[0] in data._old) d._old[k] = data._old[v.ref[0]] + } + } +} + // REVISIT: remove once old database impl is removed const _getDataWithAppliedTransitions = (data, req) => { let d @@ -26,10 +49,7 @@ const _getDataWithAppliedTransitions = (data, req) => { if (transition) { d = { _op: data._op } if (data._old) d._old = {} - for (const [k, v] of transition.mapping) { - if (v.ref[0] in data) d[k] = data[v.ref[0]] - if (data._old && v.ref[0] in data._old) d._old[k] = data._old[v.ref[0]] - } + _applyTransitionRecursively(transition, data, d) } return d || data }