Questions about SG1.5 Beta

I am using couchbase-sync-gateway-enterprise_1.5.0-377 with couchbase-server-enterprise_5.0.0-beta for testing write data by N1QL and Sync Gateway in the same bucket.
my sync gateway looks like as following:

{
    "databases": {
        "sgdb": {
            "bucket": "my-bucket",
            "username": "my-user",
            "password": "my-password",
            "server": "http://localhost:8091",
            "unsupported": {
                "enable_extended_attributes": true
            },
            "import_docs": "continuous",
      "sync" : 
    `
      function(doc, oldDoc) {
        if(!doc.hasOwnProperty(name)){
            throw({ "forbidden"  : "Missing required properties: name"});
        }
        // some route and channel logic for create/update/delete operation
}
`
        }
    }
}

Question 1:
When I execute INSERT statement by Query Workbench with name property, it works fine.but when I INSERT document without name property,N1QL works successfully,but SG shows

2017-06-17T05:08:12.234+08:00 CRUD+: Failed to update document "doc:1" w/ xattr: 403 Missing required properties: name
2017-06-17T05:08:12.234+08:00 Import: Error importing doc "doc:1": 403 Missing required properties: name
2017-06-17T05:08:12.235+08:00 HTTP: #012:     --> 403 Missing required properties: name  (75.7 ms)

and when I get document by SG,I got the same error

{"error":"Forbidden","reason":"Missing required properties: name"}

can I filter the invalid documents by N1QL via some META() info, such as xattr?

Question 2

when I update some document by UPDATE statement via Query Workbench,I found that UPDATE will trigger SG Creation Processing (oldDoc == null && !doc._deleted ).
can any update cause SG update Processing (oldDoc != null && !doc._deleted )?

Question 3
when I delete document by DELETE statement via Query Workbench,I found DELETE will not trigger SG delete Processing (doc._deleted)
can any delete cause SG delete Processing (doc._deleted )?

As you’ve seen, any writes through Couchbase Server need to get run through the Sync Function to apply security before they can be accessed by Sync Gateway. Your N1QL updates can’t be imported for use by Sync Gateway because they are being rejected by your Sync Function.

In the upcoming Beta 2 of Sync Gateway 1.5 we’ll be adding some additional functionality to the Sync Function to give you more granular control over how imports are handled. We’re adding an ‘isImport’ built-in function that will let you identify whether the document being processed is SG attempting to import a document that was not written through Sync Gateway, and modify security rules as needed.

For your specific questions:

Question 1: can I filter the invalid documents by N1QL via some META() info, such as xattr?

N1QL doesn’t have the ability to query extended attributes in 5.0. It’s on the roadmap for a future release, but until then you’ll have to craft a N1QL query based on your sync function (e.g. based on the name property in this case).

Question 2: can any update cause SG update Processing (oldDoc != null && !doc._deleted )?

When a document is updated via N1QL (or the SDK) is imported by Sync Gateway, oldDoc will be empty (as the previous body no longer exists). Currently we don’t provide a way to identify in the Sync Function whether a document being imported is an insert or an update, but we could consider an enhancement to add that. If you have a use case for this functionality that’s not covered by the upcoming isImport function, please file a github issue in the Sync Gateway repo with some details and we can review for 1.5 GA.

Question 3: can any delete cause SG delete Processing (doc._deleted )?

This sounds like a bug - doc._deleted should be set prior to Sync Function execution. I’ve filed doc _deleted not set during import of SDK delete · Issue #2660 · couchbase/sync_gateway · GitHub for this fix.

Thanks,
Adam

Thank you very much.
Another question:
can I get doc’s rev by N1QL?
previous version, I can get doc’s rev by _sync.rev property.

If you are looking for an API to query the metadata associated with the doc, then you can use the SG Admin interface
{{adminurl}}/{{db}}/_raw/{{docid}}

Thank you very much, it works.I want to get the metadata associated with the doc by N1QL.
Now I can get rev by

SELECT result._rev
  FROM CURL("http://127.0.0.1:4985/sgdb/{{docid}}",
            {"header":"Content-Type: application/json",
             "request" : "GET" }) result;

only SG server is in the same machine with CB query server.
I want to know is there an other way to get rev info commonly.

1 Like

_rev is stored with the rest of Sync Gateway’s metadata (in a system extended attribute). As mentioned above, N1QL doesn’t yet support accessing this data.

I’d be interested in your use case for retrieving _rev - it may help get this work prioritized. Is it primarily for development time visibility into the doc properties, or do you also have a production use case for N1QL retrieval of _rev?

I need rev property to update document by SG public REST API.
and I’d like to query document via N1QL.

Now I can get doc’s rev by _sync.rev property via N1QL.

Hey Atom, Curious : Why do you want to update the document via the SG REST API when you can now do it directly via the CB server interface UPSERT and have the documents synced over to mobile ?

I want to use Couchbase Mobile (may be future for the couchbase lite or pouchdb) and Sync Gateway _changes API for documents update notification. and I also want to use N1QL to get flexibly query.so I need write document by SG and query document by N1QL.