Sync Gateway Function Creating user

We have many, many devices.
Each device has it’s own admin user, with a specific admin channel.
When a document is uploaded to sync gateway I want to put the doc on the channel that the user has assigned to them (other than the public channel).
I cannot see in the documentation where I can get information on the uploading user. How can I, in the sync function, tag the doc as being under the channels that the uploading user has?


Accessing the user information from the sync function is not possible, since sync function may subsequently be rerun for the same doc in an admin context.

If there is only one channel assigned the user and it is same as username, you can add the owner field. Then inside the sync function you can add using channel(doc.owner), also use the built-in requireUser to validate the owner is the synchronizing user itself.

// sample doc 
doc {
  /// all your saved information
  owner: <username>

// inside the sync function 

Or else use channels property in the document, which you can use it to provide access to channels.

// sample doc
doc {
 /// all your saved information
 channels: [channel1, channel2, ..]

// inside sync function 

More about

  1. assigning to channels
  2. require user