Skip to content

Instantly share code, notes, and snippets.

@rajagp
Last active January 3, 2022 20:33
Show Gist options
  • Save rajagp/2b9ddf4d56684673b1befc8f59ab71e4 to your computer and use it in GitHub Desktop.
Save rajagp/2b9ddf4d56684673b1befc8f59ab71e4 to your computer and use it in GitHub Desktop.
{
"interface":":4984",
"adminInterface":":4985",
"metricsInterface":":4986",
"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": true,
"bucket":"travel-sample",
"server": "couchbase://10.2.3.57",
"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": ["*"]},
"priya": {"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;
}
/* validation */
var username = getUserName();
if ((oldDoc != null) && (oldDoc.username)) {
// Verify the user making the request is the same as the one in oldDoc's username
requireUser(oldDoc.username);
}
if (!isDelete()) {
// Validate required fields.
validateNotEmpty("username", doc.username);
if (isCreate()) {
// Validate that the _id is prefixed by owner.
var expectedDocId = "user" + "::" + doc.username;
if (expectedDocId != doc._id) {
throw({forbidden: "user doc Id must be of form user:userId"});
}
} else {
// 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)
}
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