0

I am currently working with sharedb (https://share.github.io/sharedb/) and the operationel transformation type json0 (https://github.com/ottypes/json0). I need to add a key (path) to a JSON object afterwards, which should be shared via sharedb.

For example, the key "key2" should be added to the following JSON object:

{
   key1: 'some_value',
}

Unfortunately, according to the documentation of json0 (https://github.com/ottypes/json0), there is no operation for this, which surprises me a lot. How can I add a new key that will be recognized by sharedb? Simply adding the key (path) locally makes sharedb not recognize it!

Florian3007
  • 87
  • 14

1 Answers1

2

The docs you shared say:

{p:[path,key], oi:obj} inserts the object obj into the object at [path] with key key.

So you'd want to use that oi ("object insert") op shape:

const op = [{p: ['key2'], oi: 'some_other_value'}]
doc.submitOp(op)
Alec
  • 2,432
  • 2
  • 18
  • 28
  • I was missing the `[]` around the path property value. Only giving the string. Thank you for the tip! BTW, you can do the same using the json1 class methods : `const json1 = require('ot-json1'); const operation = json1.replaceOp(['counter'], 'old', 'newValue'); doc.submitOp([operation]);` More on that here: https://github.com/ottypes/json1 – Joel Carneiro Jan 10 '22 at 15:41