Skip to content

Instantly share code, notes, and snippets.

@rajagp
Created June 3, 2019 23:13
Show Gist options
  • Save rajagp/918baf21d320db2ed25bca93b49400c9 to your computer and use it in GitHub Desktop.
Save rajagp/918baf21d320db2ed25bca93b49400c9 to your computer and use it in GitHub Desktop.
{
"adminInterface": ":4985",
"logging": {
"log_file_path": "/var/tmp/sglogs",
"console": {
"log_level": "debug",
"log_keys": ["*"]
},
"error": {
"enabled": true,
"rotation": {
"max_size": 20,
"max_age": 180
}
},
"warn": {
"enabled": true,
"rotation": {
"max_size": 20,
"max_age": 90
}
},
"info": {
"enabled": false
},
"debug": {
"enabled": false
}
},
"databases": {
"db": {
"server": "cb-example-0000.cb-example.default.svc:8091",
"bucket": "default",
"username": "admin",
"password":"password",
"users":{
"admin": {"password": "password", "admin_channels": ["*"]},
"demo": {"password": "password"},
"tester": {"password": "password"}
},
"allow_conflicts": false,
"revs_limit": 20,
"enable_shared_bucket_access": true,
"delta_sync": {
"enabled": true
},
"sync": `
function sync(doc, oldDoc) {
/* sanity check */
// check if document was removed from server or via SDK
// In this case, just return
if (isRemoved()) {
return;
}
if (doc.type == "user") {
/* validation */
var username = getUserName();
if (!isDelete()) {
// Validate required fields.
validateNotEmpty("username", doc.username);
// Restrict access to "user" type docs
requireUser(doc.username);
if (!isCreate()) {
// Validate that the username hasn't changed.
validateReadOnly("username", doc.username, oldDoc.username);
}
}
/* Routing */
// Add doc to the user's channel.
channel("channel." + username);
// Give user read access to channel
if (!isDelete()) {
// Deletion of user document is essentially deletion of user
access(username,"channel." + username)
}
}
else {
// put the static travel sample docs in a public channel
channel("!");
}
function getType() {
return (isDelete() ? oldDoc.type : doc.type);
}
function getUserName() {
return (isDelete() ? oldDoc.username : doc.username);
}
function isCreate() {
// Checking false for the Admin UI to work
return ((oldDoc == false) || (oldDoc == null || oldDoc._deleted) && !isDelete());
}
function isUpdate() {
return (!isCreate() && !isDelete());
}
// This is when document is removed via SDK or directly on server
function isRemoved() {
return( isDelete() && oldDoc == null);
}
function isDelete() {
return (doc._deleted == true);
}
function validateNotEmpty(key, value) {
if (!value) {
throw({forbidden: key + " is not provided."});
}
}
function validateReadOnly(name, value, oldValue) {
if (value != oldValue) {
throw({forbidden: name + " is read-only."});
}
}
}
`
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment