将 Parse 与外部数据库连接?

2021-12-30 00:00:00 database nosql mysql parse-platform

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

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.

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

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

编辑

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

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

});

我遵循了 Luca Laco 向我解释的相同方式.但是当我从客户端 JS 调用函数时出现错误.这是我的客户端 JS

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)

错误是:{"code":141, "message":"function not found"}我在哪里失踪和做错了什么?

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

推荐答案

如果你的意思是像一个普通的 mysql 连接器,那么答案是否定的,你不能.目前,使解析和其他相关的唯一方法是从解析查询和向解析查询.说清楚:

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:

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

  • 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.

如果您想从 php 获取存储在解析数据库中的值,您可以按照解析网站上的规范从 php 运行 REST 调用( https://parse.com/docs/rest/guide/ ),或者简单地使用 php sdk ( https://github.com/ParsePlatform/parse-php-sdk).还可以查看 Parse Webhooks.

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.

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

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.

我个人的意见,还是留在Mysql上,特别是因为在Parse上我们对查询还有很多限制(没有group by,没有distinct,查询超时等),虽然看起来是一个非常好的服务用于推送通知.无论如何,这一切都取决于您的软件的复杂性,正如我所说,这只是我的意见.

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.

这里有一个例子:

在解析云代码中,让我们制作一个名为'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();

?>

希望能帮到你

相关文章