Skip to content

submitOp support custom field? #414

@aeroyu

Description

@aeroyu

ex,i want to record who submit the op,pass the userId and save。
when I query by version,i can get who submit

Activity

alecgibson

alecgibson commented on Dec 7, 2020

@alecgibson
Collaborator

You can update the m metadata field in middleware, like so:

backend.use('connect', (request, callback) => {
  // request.agent.custom is a place for holding arbitrary data you attach
  // from the connection. It's available in all other middleware.
  // request.req comes from the second argument of backend.listen (see below)
  request.agent.custom = request.req;
});

backend.use('submit', (request, callback) => {
  // We can just attach the metadata from our agent.custom object to the
  // op.m metadata field
  request.op.m.userId = request.agent.custom.userId;
});

// Pass user information in when establishing their connection. This will come from
// a cookie or database, or whatever auth mechanism you've used
backend.listen(stream, {userId: '123'});

// Fetch ops from v5 (inclusive) to v10 (non-inclusive), along with the metadata,
// which includes the custom userId we set:
backend.getOps(agent, 'collection', 'id', 5, 10, {opsOptions: {metadata: true}}, (error, ops) => {
  // ops has the metadata attached to each op
});
aeroyu

aeroyu commented on Dec 8, 2020

@aeroyu
Author

req can pass in client submitOp method,in my case i need to related the op version and the user

alecgibson

alecgibson commented on Dec 8, 2020

@alecgibson
Collaborator

The op version is already stored on the op, which you'll have access to on the request object.

aeroyu

aeroyu commented on Dec 8, 2020

@aeroyu
Author

any example? thx,how can i stored userId to op

alecgibson

alecgibson commented on Dec 8, 2020

@alecgibson
Collaborator

I just gave you an example above.

aeroyu

aeroyu commented on Dec 8, 2020

@aeroyu
Author

i get the example from #224 and slove it
but another question is , i use client api fetchSnapshot and the result m is null

alecgibson

alecgibson commented on Dec 8, 2020

@alecgibson
Collaborator

Yes unfortunately the fetchSnapshot doesn't currently support any options (and hence can't send back metadata). You'll need to make a separate (server-side) call to backend.getOps (as outlined above), or we're more than happy to accept a Pull Request that adds this functionality!

aeroyu

aeroyu commented on Dec 8, 2020

@aeroyu
Author

:》

nieyuyao

nieyuyao commented on Dec 22, 2020

@nieyuyao

How can I add custom filed when submit op at client side? The methods mentioned above are all completed on the server side?:)

alecgibson

alecgibson commented on Dec 23, 2020

@alecgibson
Collaborator

@nieyuyao can you please provide a use-case example?

nieyuyao

nieyuyao commented on Dec 24, 2020

@nieyuyao

@nieyuyao can you please provide a use-case example?

Oh, my bad. I misunderstand!Thanks :)

listar

listar commented on Apr 28, 2022

@listar

client 的 sendOp函数源代码如下:

var message = {
    a: 'op',
    c: doc.collection,
    d: doc.id,
    v: doc.version,
    src: op.src,
    seq: op.seq,
    x: {}
  };
  if ('op' in op) message.op = op.op;
  if (op.create) message.create = op.create;
  if (op.del) message.del = op.del;
  if (doc.submitSource) message.x.source = op.source;
  this.send(message);

客户端 doc.submitSource = true

doc.submitOp(
	task,
	{
		source: {
			clientId: this.engineOt.clientId,
			reqId: this.sendId
		}
	},
	error => {
		error && this.submitOpErrorHandle(error, task)
	}
)

在source内补充你想要补充的数据

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @listar@aeroyu@alecgibson@nieyuyao

        Issue actions

          submitOp support custom field? · Issue #414 · share/sharedb