将 Parse 与外部数据库连接?

Connect Parse with external database?

我正在开发使用 PHP、mysql 等传统技术构建的项目,它是一个 Web 应用程序。 现在我们想在像 Andoid 这样的平台上为移动用户构建一个应用程序,iOS。 所以我们正在考虑将 MySql 与 Parse.com 数据库连接起来。 我知道 parse 使用 NoSql 类型的数据库来存储对象。

所以我的问题是我们可以将解析数据库连接到任何其他 SQL 数据库吗? 如果是,那么我们该怎么做?

编辑

@Luca laco 我刚刚像你一样创建了一个新的云功能。在下面。

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

});

我按照 Luca Laco 向我解释的方式进行操作。 但是当我从客户端 JS 调用函数时出现错误。 这是我的客户 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>

在网络选项卡中我可以看到

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

错误是:{"code":141, "message":"function not found"} 我哪里遗漏了哪里做错了?

如果您的意思是类似于常见的 mysql 连接器,那么答案是否定的,您不能。现在,进行解析和其他相关操作的唯一方法是从 Parse 查询和向 Parse 查询。要明确:

  • 如果您想从 Parse 中获取存储在 mysql 中的值,您必须对存储在 php 中的特定模块使用 http 请求 php 网站(并由您实现)需要一些参数,并且 return 以特定方式获得结果,通常采用 json 格式,还使用 ​​http header application/json.

  • 如果您想从 php 获取存储在解析数据库中的值,您可以 运行 来自 php 的 REST 调用解析网站 ( https://parse.com/docs/rest/guide/ ), or simply using the php sdk ( https://github.com/ParsePlatform/parse-php-sdk ) 上的规范。另请查看 Parse Webhooks。

据我了解,您已经有一个可用的 Web 服务,因此这样做,您只需通过 Parse 将存储在 mysql 服务器上的资源代理到您的客户端。换句话说,您应该使用 Parse SDK(对于 iOS 或 Android)为要在客户端上检索的每种类型的信息创建一个 Parse Cloud 函数,并为您执行的每个操作创建另一个 Parse Colud 函数在您的设备上,并且您希望始终通过 Parse 系统保存在 mysql 数据库中。

我个人的意见是继续 Mysql,尤其是因为在 Parse 上我们仍然对查询有很多限制(没有分组依据,没有区别,查询超时等),虽然看起来成为推送通知的真正优质服务。无论如何,所有这一切都取决于您的软件的复杂性,正如我所说,这只是我的意见。

[编辑]

举个例子:

在 Parse 云代码中,我们创建一个名为 '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            
        }
    });

});

示例 'getUser.php' 模块可以是

<?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();

?>

希望对您有所帮助