Skip to content

Instantly share code, notes, and snippets.

@quentar
Last active July 30, 2020 22:35
Show Gist options
  • Save quentar/9760161 to your computer and use it in GitHub Desktop.
Save quentar/9760161 to your computer and use it in GitHub Desktop.
parse.com db-rules to allow unique columns
////////////// contraining bits libarary
// parse.com CloudCode functions to allow kind-of unique column (when requests do not come for 1 object at the very same time)
// (that may happen with queue of commands, which can cause multiple objects to be created)
// Generic Functions, used as the sample at the end of file
// ALLOW NEW OBJECT, NEW WINNER TAKES IT ALL = OVERWRITE OLD OBJECT
function parseDBrequestUpdateUniqueOne(request,response,dbName,keyName) {
if (request.object.existed() ) {
response.success(); //if it is and update, allow all
}
if (!request.object.get(keyName)) {
response.error("Table: "+dbName+" must have key "+keyName+".");
} else {
var query = new Parse.Query(dbName);
var keyVal = request.object.get(keyName);
query.equalTo(keyName, keyVal);
query.first({
success: function(object) {
if (object) {
//update
object.destroy(); //delete old object - we will replace it with a new one
response.success();
} else {
response.success();
}
},
error: function(error) {
response.error("UNIQUE OCL: Could not validate uniqueness for this "+dbName+" object.");
}
});
}
}
// DO NOT ALLOW NEW OBJECT
function parseDBrequestForceUniqueOne(request,response,dbName,keyName) {
if (!request.object.get(keyName)) {
response.error("Table: "+dbName+" must have key "+keyName+".");
} else {
var query = new Parse.Query(dbName);
var keyVal = request.object.get(keyName);
query.equalTo(keyName, keyVal);
query.first({
success: function(object) {
if (object) {
response.error("UNIQUE OCL: "+dbName+" with this "+keyName+" ("+keyVal+") already exists.");
} else {
response.success();
}
},
error: function(error) {
response.error("UNIQUE OCL: Could not validate uniqueness for this "+dbName+" object.");
}
});
}
}
//MERGE OBJECT = unsent columns are kept from old objects !
function merge_options(obj1,obj2){
var obj3 = {};
for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
return obj3;
}
function parseDBrequestMergeUniqueOne(request,response,dbName,keyName) {
if (request.object.existed() ) {
response.success(); //if it is an update, allow all
}
if (!request.object.get(keyName)) {
response.error("Table: "+dbName+" must have key "+keyName+".");
} else {
var query = new Parse.Query(dbName);
var keyVal = request.object.get(keyName);
query.equalTo(keyName, keyVal);
query.first({
success: function(object) {
if (object) {
//update
var oldo = JSON.parse( JSON.stringify(object) );
var newo = JSON.parse ( JSON.stringify(request.object) );
var merge2 = merge_options (oldo,newo);
// console.log("merge2 :"+merge2);
for (var attrname in merge2) {
request.object.set(attrname, merge2[attrname]);
// console.log("m2: " + attrname + ": " + merge2[attrname]);
// console.log("ro: " + attrname + ": " + request.object[attrname]);
}
// console.log("result: " + JSON.stringify(request.object));
object.destroy(); //delete old object - we will replace it with a new one
response.success();
} else {
response.success();
}
},
error: function(error) {
response.error("UNIQUE OCL: Could not validate uniqueness for this "+dbName+" object.");
}
});
}
}
// ALLOW NEW OBJECT, NEW WINNER TAKES IT ALL = OVERWRITE OLD OBJECT , tsLock column requires bigger value or update is blocked
function parseDBrequestUpdateUniqueOneNewer(request,response,dbName,keyName,tslock) {
if (request.object.existed() ) {
// response.success(); //if it is and update, allow all
}
if (!request.object.get(keyName)) {
response.error("Table: "+dbName+" must have key "+keyName+".");
} else {
var query = new Parse.Query(dbName);
var keyVal = request.object.get(keyName);
query.equalTo(keyName, keyVal);
query.first({
success: function(object) {
if (object) {
//update
var doit = 1;
//now check for TS
var newTS = request.object.get(tslock);
var oldTS = object.get(tslock);
if (oldTS == null ) {
// response.success();
}
console.log("point B");
if ( (newTS > oldTS) || (oldTS == null) ) {
console.log("point C1");
doit = 1;
} else {
doit = 0;
response.error("UNIQUE OCL ("+dbName+") : UPDATE TS TOO OLD "+newTS+" < "+oldTS+" (old)");
}
if (doit > 0 ) {
if ( request.object.existed() ) {
//update operation = keep old object because its id is the same!
} else {
object.destroy(); //delete old object - we will replace it with a new one
}
response.success();
}
} else {
response.success();
}
},
error: function(error) {
response.error("UNIQUE OCL: Could not validate uniqueness for this "+dbName+" object.");
}
});
}
}
//////////////
/// use generic function to class test, unique column = uniqKey
Parse.Cloud.beforeSave("test", function(request, response) {
parseDBrequestMergeUniqueOne(request,response,"test","uniqKey");
});
@cloudjanak
Copy link

HOw to check unique key ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment