Connect Parse with external database?

2020-07-30 06:42发布

问题:

I am working on project where it is build using traditional technologies like PHP, mysql and it is a web application. Now we want to build an app for mobile users on platform like Andoid, iOS. So we are thinking to connect MySql with Parse.com database. I know parse uses NoSql kind of database for storing objects.

So my question is can we connect parse database to any other SQL database ? If yes then how we can do that ?

EDIT

@Luca laco I just created a new cloud function like you. which is below.

Parse.Cloud.define("get_parse4j_object", 

function(request,response){

    // Parameters from client (iOS/Android app)
    //var requestedObjectId = request.params.objectId;

    // Calling beckend service for getting user information
    Parse.Cloud.httpRequest({
        method: "GET",
        headers: {
            'Content-Type': 'application/json'
        },        
        url: "https://api.parse.com/1/parse4j/MLiOgxncUM", /* This could be your url for the proper php module */
        //body: { "objectId":requestedObjectId }, /* Here you compose the body request for the http call, passing the php parameters and their values */
        success: function(httpResponse) {

        /* We expect that the php response is a Json string, using the header('application/json'), so: */
        var jsonResponse = JSON.parse(httpResponse.text);

        /* sample structure in jsonResponse: { "name":"Joe", "surname":"Banana", "birth_date":"01-02-1999" } */

        /* Do any additional stuff you need... */

        /* return the result to your iOS/Android client */
        return response.success( { "myRequestedUserInfo" : jsonResponse } );
                alert(jsonResponse);
        },
        error: function(httpResponse) {            
                return response.error({ "msg":"Unable to fetch this user", "code":123456 }); // sample error response            
        }
    });

});

I followed the same way which Luca Laco explained me. But I am getting error when I am calling function from client JS. This is my client JS

<script type="text/javascript">
    Parse.initialize("APP_ID", "JAVASCRIPT_KEY");


    Parse.Cloud.run('get_parse4j_object', {}, {
            success: function(result) {
            alert(result);
        },
            error: function(error) {
            alert(JSON.stringify(error));
        }
    });
  </script>

In the network tab I can see

POST https://api.parse.com/1/functions/get_parse4j_object 400 (Bad Request)

and error is: {"code":141, "message":"function not found"} Where I am missing and doing wrong ?

回答1:

If you mean something like a common mysql connector, then the response is no, you can't. At now, The only way to make parse and something else in relation, is to query from and to Parse. To be clear:

  • If you want to get a value from Parse, that is stored in mysql, you have to use a http request to a specific php module stored on your php website ( and implemented by you ) that expect some paramenter, and return the result in a specific way, normally in json format, using also the http header application/json.

  • If you want to get a value from php, that is stored on the parse db, you can run a REST call from php following the spec on the parse website ( https://parse.com/docs/rest/guide/ ), or simply using the php sdk ( https://github.com/ParsePlatform/parse-php-sdk ). Take a look also to the Parse Webhooks.

From what i understood, you already have a working web service, so doing this, you would just proxy the resources stored on your server on mysql to your clients through Parse. In other words you should create a Parse Cloud function for each type of information you want to retrieve on the clients using the Parse SDK (for iOS or Android) and another Parse Colud function for each action you perform on your devices and you want to save on your mysql db, always through Parse system.

My personal opinion, is to stay on Mysql, especially because on Parse we still have a lot of limitation on the queries ( no group by, no distinct, query timeout, etc. ), while seems to be a really good service for the push notification. Anyway all this depends by the complexity of your software and as i said, is just my opinion.

[Edit]

Here an example:

In Parse cloud code, let's make a cloud function called 'get_user_info'

Parse.Cloud.define("get_user_info", 

function(request,response){

    // Parameters from client (iOS/Android app)
    var requestedUserId = request.params.user_id;

    // Calling beckend service for getting user information
    Parse.Cloud.httpRequest({
        method: "POST",        
        url: "https://www.yourPhpWebsite.com/getUser.php", /* This could be your url for the proper php module */
        body: { "php_param_user_id":requestedUserId }, /* Here you compose the body request for the http call, passing the php parameters and their values */
        success: function(httpResponse) {

        /* We expect that the php response is a Json string, using the header('application/json'), so: */
        var jsonResponse = JSON.parse(httpResponse.text);

        /* sample structure in jsonResponse: { "name":"Joe", "surname":"Banana", "birth_date":"01-02-1999" } */

        /* Do any additional stuff you need... */

        /* return the result to your iOS/Android client */
        return response.success( { "myRequestedUserInfo" : jsonResponse } );

        },
        error: function(httpResponse) {            
                return response.error({ "msg":"Unable to fetch this user", "code":123456 }); // sample error response            
        }
    });

});

The sample 'getUser.php' module could be

<?php

$expectedUserId = $_POST['php_param_user_id'];

// query your MySql db using passed user id
$query = "SELECT name,surname,birth_date FROM MyUserTable Where id = ".$expectedUserId;

// perform your query (the above one is just an example, would be better to use PDO and any other check, just to avoid SQL Injection)
// ...
// ..
// .

$resultQuery = row[0];

// sample json structure
$jsonResponseToParse = '{ "name":'.resultQuery["name"].', "surname":'.resultQuery["surname"].', "birth_date":'.resultQuery["birth_date"].' }';

header('application/json');

echo jsonResponseToParse;

exit();

?>

Hope it helps