Why do these documents sync to mobile?

Thanks for the replies, @priya.rajagopal - and yes, I think the most important lesson (to me anyway) is to always look at the sync. function from the perspective of the sync.gateway!

Now, I’ve been playing with a few different scenarios today. I’ld like to have your input to a c0uple of situations:

First my import filter:

           function(doc) {
                if (doc.type == 'A1') {
                    return false;
                }
                return true;
            }

That basically just says: Don’t send A1 type docs to mobile… This seems to work fine.

Then here is the sync function:

			function (doc, oldDoc) {
                function _log(t) {
                    // Does not work - yet...
                    console.log('SG: ' + t);
                }
                function _getUserKey(d) {
                    var key = null;
                    if (d) {
                        if (d.type == 'User') {
                            key = d.key;
                        } else {
                            key = d.userkey;
                        }
                    }
                    return key;
                }
            
                if (doc && doc._deleted) {
                    // Doc. deleted -> if public then require
					if(oldDoc){
	                    var userkey = _getUserKey(oldDoc);
	                    _log('delete doc id: ' + doc._id + ', userkey=' + userkey);
    	                if (userkey != null) {
                    		channel('channel.' + userkey);
                    		access(userkey, 'channel.' + userkey);
		                    requireUser(userkey);
            	        } else {
                	        channel('!');
                    	    requireAdmin();
						}
                    }
                    _log('doc deleted, id: ' + (doc._id || 'no id!') + ', ' + (oldDoc ? ('old key=' + oldDoc.key + ', userkey=' + userkey) : 'no oldDoc'));
                    return;
                }
                _log('doc id: ' + (doc._id || 'no id!') + ', ispublic: ' + doc.ispublic + ', userkey=' + userkey + ', ' + (oldDoc ? (oldDoc._deleted ? 'oldDoc is deleted' : ('old key=' + oldDoc.key + ', oldDoc.userkey=' + oldDoc.userkey + ' update')) : ' creation'));
                // Document type is mandatory
                if (typeof doc.type === 'undefined') {
                    throw ({ forbidden: "Document type is required. id=" + JSON.stringify(doc) });
                }
                // Document key is mandatory
                if (typeof doc.key === 'undefined') {
                throw ({ forbidden: "Document key is required. id=" + doc._id });
                }
                // Allow anyone to create a A2 on the server - this may not be necessary?
                if (oldDoc == null && doc.type == 'A2') {
                    _log('Created A2: ' + (doc._id || 'no id!') + ', key: ' + doc.key + ', userkey: ' + doc.userkey);
                    //return;
                }
                // Document type A1 - sync disabled...?
                if (doc.type === 'A1' && doc.issyncdisabled) {
                    throw ({ forbidden: "Sync. disabled for id=" + JSON.stringify(doc) });
                }
            
                // All public docs are available in the app
                if (doc.ispublic) {
                    _log('public, id: ' + (doc._id || 'no id!'));
                    channel('!');
                }
            
                // Only non-public docs "owned" by user can be replicated
                var userkey = _getUserKey(doc);
                if (userkey != null) {
                    if (oldDoc != null && ! oldDoc._deleted) {
                        // Update
                        if (oldDoc.type != doc.type) {
                            throw ({ forbidden: "Can't change doc type" });
                        }
                        if (oldDoc.key != doc.key) {
                            throw ({ forbidden: "Can't change doc key" });
                        }
                        if (oldDoc.userkey && oldDoc.userkey != doc.userkey) {
                            throw ({ forbidden: "Can't change user key" });
                        }
                    }
                    _log('User owned, id: ' + (doc._id || 'no id!') + ', type: ' + doc.type + ', user: ' + userkey);
                    channel('channel.' + userkey);
                    access(userkey, 'channel.' + userkey);
					requireUser(userkey);
                }
             }

This basically controls that you cannot create/save docs without a key and type - and you cannot change those nor the userkey. I have a couple of questions in relation to this function:

  • Is there a better way to disable replication for a specific doc than I have done? The use case is that I have Images as base64 encoded text. These take up quite some space so once sent to the server I would like them to be “disappear” locally (and just read the photos via a url where they are cached locally). I could purge them - but then I cannot find a way to later delete an image. So by throwing an exception I can clean out the bulk data and leave a “stub” doc that I can activate by removing the disabled flag. This will not work for existing photos -but I cannot fix that.

  • When should I use the Channel and Access functions? _I have added them to the code where I delete a document (and when I save/create a document with a userkey). I think I need them in the latter case - but not in the first? _

And finally, thank you for being patient with me. I now know why I found it so difficult to get the concept fit into my mind - but I think it is way closer now (and its a major improvement that we can have our own log statements written to the SG log!)