When a user logs into our solution from the web we create an “ActivityLog” document and updates the “User” document with latest login. When replicating to Couchbase Lite we really just want to ignore the ActivityLog documents. This is my sync_gateway.json config file:
{
"log": ["*"],
"adminInterface": "0.0.0.0:4985",
"interface": "0.0.0.0:4984",
"databases": {
"data": {
"use_views":false,
"num_index_replicas":0,
"bucket": "data",
"server": "http://ds9:8091",
"username": "xxxx",
"password": "yyyyyyy",
"enable_shared_bucket_access": true,
"import_docs": true,
"users": { "GUEST": { "disabled": true, "admin_channels": ["!"] } },
"sync": `function (doc, oldDoc) {
function _log(t){
console.log('SG: ' + t);
}
// Document deleted?
console.log("XX: sync...");
if (oldDoc && oldDoc._deleted){
// Server doc. deleted -> just propagate this!
_log('oldDoc deleted, id: ' + (oldDoc._id || 'no id!'));
channel('!');
return;
}
if (doc && doc._deleted){
// Server doc. deleted -> just propagate this on the public channel!
_log('doc deleted, id: ' + (doc._id || 'no id!') + ', ' + (oldDoc ? ('old key=' + oldDoc.key + ', userkey=' + oldDoc.userkey) : 'no oldDoc'));
channel('!');
return;
}
// Document type is mandatory
if (!doc.type){
throw({forbidden: "Document type is required."});
}
if ((doc.type == 'EnvLake' || doc.type == 'EnvMeasurement' || doc.type == 'Feedback' || doc.type == 'ActivityLog') && oldDoc){
throw({forbidden: "Document type not allowed to sync to mobile..."});
}
// All public docs are available in the app
if (doc.ispublic) {
_log('public, id: ' + (doc._id || 'no id!'));
channel('!');
// return;
}
// All non-club fishing trips and catches are available (for stats)
if ((doc.type == 'FishingTrip' || doc.type == 'Catch') && doc.clubonlykey == undefined) {
_log('non-club trips, id: ' + (doc._id || 'no id!'));
channel('!');
}
// All non-specific user info is available (for stats)
if (doc.type == 'User') {
_log('User doc, id: ' + (doc._id || 'no id!'));
channel('!');
}
// Only docs "owned" by user can be updated
var key;
if(doc.type == 'User'){
key = doc.key;
} else if(doc.type == 'FishingTrip' || doc.type == 'Catch' || doc.type == 'Photo' || doc.type == 'Private' ||Â doc.type == 'Image'){
key = doc.userkey;
}
if(key){
_log('User owned, id: ' + (doc._id || 'no id!') + ', type: ' + doc.type + ', user: ' + doc.userkey);
requireUser(key);
channel('channel.' + key);
access(key,'channel.' + key);
}
// Creation of new Feedback docs (perhaps without userkey)?
// Creation of new docs?
// Updates to existing docs?
// Readonly for all non-user specific docs...
}`,
"allow_conflicts": false,
"revs_limit": 20
}
}
}
As you can see I have tried to make “ActivityLog” a forbidden type. However, when I look in the sg_info.log
file I see:
2019-05-08T12:25:23.078+02:00 [INF] Import: Created new rev ID for doc "ActivityLog:359576920ABD3F6EC12583F40039412C" / "1-0ee5e9a08f9bfdc415ae63052cabff41"
2019-05-08T12:25:23.131+02:00 [INF] CRUD: Stored doc "ActivityLog:359576920ABD3F6EC12583F40039412C" / "1-0ee5e9a08f9bfdc415ae63052cabff41" as #289410
2019-05-08T12:25:23.132+02:00 [INF] Import: Created new rev ID for doc "User:BA171123846CEBF1C1257CB2002DA330" / "796-8193f650cd91eb97cd59f87c566c6d31"
2019-05-08T12:25:23.147+02:00 [INF] CRUD: Stored doc "User:BA171123846CEBF1C1257CB2002DA330" / "796-8193f650cd91eb97cd59f87c566c6d31" as #289411
2019-05-08T12:25:23.152+02:00 [INF] Cache: Received #289410 after 36ms ("ActivityLog:359576920ABD3F6EC12583F40039412C" / "1-0ee5e9a08f9bfdc415ae63052cabff41")
2019-05-08T12:25:23.152+02:00 [INF] Cache: Initialized cache for channel "*" with options: &{ChannelCacheMinLength:50 ChannelCacheMaxLength:500 ChannelCacheAge:1m0s}
2019-05-08T12:25:23.154+02:00 [INF] Cache: #289410 ==> channels {*}
2019-05-08T12:25:23.154+02:00 [INF] Cache: Received #289411 after 11ms ("User:BA171123846CEBF1C1257CB2002DA330" / "796-8193f650cd91eb97cd59f87c566c6d31")
So I can see that the sync gateway does do some work on the doc. Can/should I do anything to prevent this?
Oh, and by the way, my attempts of logging from my sync function does not create any log messages :
…so if you have an idea of how to make that work I would also appreciate that