I have a html file and related js file. I want to import and export data with js. So I have researched references but it said you can do this with UI. But I didn't find anything about how to do this programatically.
There is no endpoint in Parse Server for importing and exporting data. Some Parse Server hosting providers, like Back4App, provide additional endpoints that you can use to import/export data programatically. Otherwise, the best way for you would be creating cloud code functions for importing/exporting the data and then call these functions. Here goes some examples.
For Importing, you can use this cloud code function:
Parse.Cloud.define("import", function (request, response) {
var className = request.params.className;
var rows = request.params.rows;
var MyClass = Parse.Object.extend(className);
var promises = [];
for (var i = 0; i < rows.length; i++) {
var myClassObject = new MyClass();
for (var column in rows[i]) {
myClassObject.set(column, rows[i][column]);
}
promises.push(myClassObject.save());
}
Parse.Promise
.when(promises)
.then(
function () {
response.success('Successfully imported ' + i + ' rows into ' + className + ' class');
},
function (error) {
response.error('Import failed: ' + error);
});
});
Then prepare a data.json
file like this:
{
"className": "ExampleClass",
"rows": [
{ "ExampleColumnA": "row1columnA", "ExampleColumnB": "row1columnB" },
{ "ExampleColumnA": "row2columnA", "ExampleColumnB": "row2columnB"}
]
}
Then call the function like this:
curl -X POST \
-H "X-Parse-Application-Id: YOUR_APP_ID" \
-H "X-Parse-REST-API-Key: YOUR_REST_KEY" \
-H "Content-Type: application/json" \
-d @data.json \
https://your.server.url/functions/import
For exporting the data, use this cloud code function:
Parse.Cloud.define("export", function(request, response) {
var ExportObject = Parse.Object.extend(request.params.exportClass);
var query = new Parse.Query(ExportObject);
query.find({ success: response.success, error: response.error });
});
And call this cloud code function like this:
curl -X POST \
-H "X-Parse-Application-Id: YOUR_APP_ID" \
-H "X-Parse-REST-API-Key: YOUR_REST_KEY" \
-H "Content-Type: application/json" \
-d '{"exportClass": "MyClass"}' \
https://your.server.url/functions/export > out.json