How to sync a specific document from couchbase server?

I am developing an application. In this application, I used couchbase Lite for mobile devices. I did with the synchronization of all documents in the database from the couchbase server. but the problem is that the database is large. I do not want to synchronize all documents from the couchbase server. I want to synchronize only certain data / documents from the server.

My question is: how can I sync a specific document related to that particular user?

+4
source share
1 answer

Access to documents for a specific user is performed in the synchronization function. This is a function written in JavaScript that is located in the Sync Gateway configuration file.

Methods available in the synchronization function:

  • channel(channelname): route the document to the channel.
  • access(username, channelname): provide access to the username for the channel (the ability to provide the role to the channel too, and as a result, all users with this role will gain access to the channel).
  • role(username, rolename): Assign a user with a role.
  • requireAccess(channelname): throws an error if the user in the context does not yet have access to the channel.
  • requireUser(username): throws an error if the user in the context is not the username.
  • requireRole(rolename): Throws and errors if the user in the context does not have the role of rolename.
  • throw({forbidden: "error message"}): Throw an exception for user verification.

Here is an example configuration file with inline comments:

{
  "log": ["REST", "CRUD"],
  "users": {
    "foo1": {"password": "letmein", "admin_roles": ["admin"]},
    "foo2": {"password": "letmein"}
  },
  "databases": {
    "quizz": {
      "sync": `function(doc, oldDoc) {
        // The owner field shouldn't change during updates
        if (doc.owner != oldDoc.owner) {
          throw({forbidden: "Can't change the owner field on existing documents"});
        }
        switch(doc.type) {
          case "list":
            // only users with admin role can create/update list documents
            requireRole("admin");
            break;
          case "todo":
           // only the owner of a todo document can create/update it
           require(doc.owner);
           break;
        }
      }`
    }
  }
}

. , , ( ) (.. /http, ). , .

. docs .

+3

Source: https://habr.com/ru/post/1619896/


All Articles