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`; | |
}); |
Little heads up for anyone looking. I feel like there could be a small versioning issue between the function here and the one posted in the live example. The version on this page revised on Jan 5 2020 and current as of June 23 2021 works great for me and also employs the improvements the owner describes in his Jan 5 2020 comment. Whereas the example posted in the live example fails for me at the line Parse.Promise.when(promises).
Thanks for the heads up. I've just updated the live example code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Milad-abbasi I've just updated the example to be compliant with the latest version of Parse and also to use Parse.Object.saveAll(). It conveniently splits the requests in batches so you can make sure it works properly. I also added the link for a living example at the Back4App Database Hub so you can just run and see the imported rows or clone, check the cloud code out and play around.