Skip to content

Instantly share code, notes, and snippets.

@rajagp
Created June 3, 2019 23:11
Show Gist options
  • Save rajagp/6d32ac9b755224a3f426baf6b7dbddbc to your computer and use it in GitHub Desktop.
Save rajagp/6d32ac9b755224a3f426baf6b7dbddbc to your computer and use it in GitHub Desktop.
{
"interface":":4984",
"log": ["*"],
"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": {
"travel-sample": {
"import_docs": "continuous",
"bucket":"travel-sample",
"server": "http://cb:8091",
"enable_shared_bucket_access":true,
"delta_sync": {
"enabled":true
},
"import_filter": `
function(doc) {
return true;
}
`,
"username": "admin",
"password": "password",
"users":{
"admin": {"password": "password", "admin_channels": ["*"]},
"demo": {"password": "password"},
"tester": {"password": "password"}
},
"num_index_replicas":0,
"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