Last active
June 23, 2021 21:33
-
-
Save davimacedo/c7738203a2a616b48a22fdc7c00b37d2 to your computer and use it in GitHub Desktop.
Example of importing data with cloud functions. See a live example at https://www.back4app.com/database/davimacedo/parse-import-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -X POST \ | |
-H "X-Parse-Application-Id: LL9oIdzIkmwl5xyowQQu0fTmXyUWfet9RuAzwHfj" \ | |
-H "X-Parse-REST-API-Key: R3S8PYQKuzeV4c8MUeO5ved46C50MEp56boDHW1O" \ | |
-H "Content-Type: application/json" \ | |
-d @data.json \ | |
https://parseapi.back4app.com/functions/import |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"className": "ExampleClass", | |
"rows": [ | |
{ "ExampleColumnA": "row1columnA", "ExampleColumnB": "row1columnB" }, | |
{ "ExampleColumnA": "row2columnA", "ExampleColumnB": "row2columnB"} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Parse.Cloud.define('import', async request => { | |
const className = request.params.className; | |
const rows = request.params.rows; | |
const MyClass = Parse.Object.extend(className); | |
var myClassObjects = []; | |
for (let i = 0; i < rows.length; i++) { | |
const myClassObject = new MyClass(); | |
for (const column in rows[i]) { | |
myClassObject.set(column, rows[i][column]); | |
} | |
myClassObjects.push(myClassObject); | |
} | |
try { | |
await Parse.Object.saveAll(myClassObjects); | |
} catch (e) { | |
throw new Error(`Import failed: ${e}`); | |
} | |
return `Successfully imported ${myClassObjects.length} rows into ${className} class`; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the heads up. I've just updated the live example code.