Skip to content

Instantly share code, notes, and snippets.

@sahin
Last active February 3, 2017 22:13
Show Gist options
  • Save sahin/3b4c09cff2b090ddb5791bceaba5175d to your computer and use it in GitHub Desktop.
Save sahin/3b4c09cff2b090ddb5791bceaba5175d to your computer and use it in GitHub Desktop.
async function parseFind(className, identifyingValue, identifyingKey = 'id') {
const ParseObject = Parse.Object.extend(className);
const query = new Parse.Query(ParseObject);
query.equalTo(identifyingKey, identifyingValue);
const result = await query.first();;
return result;
}
async function parseSave(className, objectAttributes) {
const ParseObject = Parse.Object.extend(className);
const object = new ParseObject();
object.set(objectAttributes);
await object.save();
}
async function parseCreateOrUpdate(className, objectAttributes, identifyingKey = 'id') {
const identifyingValue = objectAttributes[identifyingKey];
const foundObject = await parseFind(className, identifyingValue, identifyingKey);
console.log({ foundObject });
if (foundObject) {
foundObject.set(objectAttributes);
await foundObject.save();
return;
}
await parseSave(className, objectAttributes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment