The Database Hub | Back4App
The Database Hub | Back4App
How it works
  • Log In
  • Sign Up

  1. davimacedo
  2. Import Example
Public

27
31
27
  • DatabaseDatabase
  • GraphQL API PlaygroundAPI Playground
  • Get Started GuideGet Started
  • IssuesIssues
  • ContributorsContributors
  • GraphQL API Playground
    API Playground
    Get Started Guide
    Get Started
    Issues
    Issues
    Contributors
    Contributors

  1. Import Example

This is a public database to demonstrate how to import a set of parse objects to a Parse Server database.

  • Example
  • App Templates

Get StartedFetch Data
ClassClassesDescription
ClassExampleClass
This is the class to where the data is uploaded when running the example. It shows the latest created objects in the beginning.
ClassRole
This is the Parse default role class.
ClassUser
This is the Parse default user class.
NotesNotes

Parse Server Import Objects Example


This is a live example for this Gist that shows how to import data to a Parse Server database.

In order to run this example, first create a data.json file like this:

{
	"className": "ExampleClass",
	"rows": [
		{ "ExampleColumnA": "row1columnA", "ExampleColumnB": "row1columnB" },
		{ "ExampleColumnA": "row2columnA", "ExampleColumnB": "row2columnB"}
	]
}

Then you just need to run a curl command like this:

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

You can see the imported rows by checking the ExampleClass.

Use the clone button on the top of this page to clone this example to your account and see how it works. Basically it is running Parse Server 3.9.0 and it was added a main.js file with the following cloud code function:

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`;
});