couchbase / couchbase-lite-ios

Lightweight, embedded, syncable NoSQL database engine for iOS and MacOS apps.
Apache License 2.0
1.62k stars 297 forks source link

Sync via document IDs #1526

Closed SalmanZafar1105 closed 7 years ago

SalmanZafar1105 commented 7 years ago

Sync documents by specifying document IDs.

As of now, filter pull is done by specifying channels. We have to run a separate pull replication for one single document.

How to pull one document from sync gateway?


pasin commented 7 years ago

You can set documentIDs to the replicator. Thanks.

SalmanZafar1105 commented 7 years ago

I am using couchbase lite and not couchdb, mongodb, etc. Couchbase lite supports only channel based filter.

http://developer.couchbase.com/documentation/mobile/current/guides/couchbase-lite/native-api/replication/index.html#filtered-pull-from-sync-gateway

All my documents are in some channel(s). No document is in the public channel.

SO I CANNOT SYNC WITH documentIDs.

jamesnocentini commented 7 years ago

Have you tried using the documentIDs method on the replication object as Pasin pointed out? If that doesn't solve your query can you explain why?

SalmanZafar1105 commented 7 years ago

CBLHTTP[400, "bad_request", http://:*****@my:4990/db/_changes?feed=longpoll&heartbeat=300000&style=all_docs&since=1088&filter=_doc_ids]

I get this error.

It has something to do with channels. If a document is assigned a channel, I cannot pull it with documentIDs.

jamesnocentini commented 7 years ago

Can you try with a one-shot replication instead of continuous?

SalmanZafar1105 commented 7 years ago

No error, but document is not synced.

In couchbase lite documentation, it is written that to filter documents from sync gateway we need to specify channels, but if we are using couchdb, pouchdb or cloudant then there are other methods.

The word "documentID" is not written in the replication part of couchbase lite documentation. Link -

http://developer.couchbase.com/documentation/mobile/1.3/guides/couchbase-lite/native-api/replication/index.html

I am asking, is there a better alternative if I want to sync just a single document.

jamesnocentini commented 7 years ago

The last sentence in this paragraph mentions filtering by doc ID and I agree we need a bit more content on that (specifically that it's not supported for continuous replications). Per https://github.com/couchbase/sync_gateway/issues/1703 and https://github.com/couchbase/sync_gateway/issues/1712 it should work though. Are you sure that user has access to the document?

SalmanZafar1105 commented 7 years ago

I coordinated with the back-end team to check if I have access to the document. They said the document's channel should be in the user's admin channel.

The userId is 919711. The user document has the following channel -

"channels": { "ch-919711": null }

Also, user's admin channel contain this channel - "admin_channels":["ch-919711",...]

So, I have access to the document.

jamesnocentini commented 7 years ago

I just tried with the following and the replication pulled the doc successfully from SG:

let replication = database.createPullReplication(kSyncGatewayUrl)
replication.authenticator = CBLAuthenticator.basicAuthenticator(withName: "user1", password: "pass")
replication.documentIDs = ["456"]
replication.start()

after adding the doc:

curl -vX PUT 'http://user1:pass@localhost:4984/todo/456' -H 'Content-Type: application/json' -d '{}'

We'll update the documentation to make that clearer so thanks for the feedback!

pasin commented 7 years ago

@SalmanZafar1105, sorry about this. I don't know that SGW doesn't currently support docIDs in continuous replication. Meanwhile, you might need to setup the timer to periodically start a single shot pull replicator.

SalmanZafar1105 commented 7 years ago

It's OK. I have it working now.