简体   繁体   English

架构从到关系NoSql

[英]Architectural from-to relation NoSql

I'm struggling with this architectural problem on my Parse.com document. 我在Parse.com文档中正在解决此体系结构问题。 I have the following "schema", called Debit : 我有以下“模式”,称为Debit:

"createdAt": "2014-12-12T02:31:06.026Z",
"creator": {
    "__type": "Pointer",
    "className": "_User",
    "objectId": "objectId"
},
"date": {
    "__type": "Date",
    "iso": "2014-12-12T02:31:14.437Z"
},
"debitStatus": 0,
"description": "Gdfvgg",
"from": {
    "__type": "Pointer",
    "className": "_User",
    "objectId": "Sd9B1XyZVL"
},
"has_accepted": false,
"objectId": "objectId",
"to": {
    "__type": "Pointer",
    "className": "_User",
    "objectId": "objectId"
},
"updatedAt": "2014-12-12T06:00:46.528Z",
"value": "6.48"

The from and to "columns" are pointers to _User table. 从和到“列”是指向_User表的指针。 The problem is, in my Android app I'd like to display both from and to debits according to the Logged User, but on my adapter only show the "not logged user" user. 问题是,在我的Android应用程序,我想都显示fromto根据登录的用户扣费,但我的适配器上仅显示“没有登录用户”用户。

I tried doing an or query on Parse to get all the debits queries but I can't include the User pointer because on or queries Parse doesn't support. 我尝试对Parse进行“ or查询以获取所有借方查询,但是由于“ Parse” or查询”不支持,所以无法包含User指针。

Like this: 像这样:

ParseQuery<ParseObject> query1 = new ParseQuery<ParseObject>("UserBalance");
query1.whereEqualTo("user_from", ParseUser.getCurrentUser());
ParseQuery<ParseObject> query2 = new ParseQuery<ParseObject>("UserBalance");
query2.whereEqualTo("user_to", ParseUser.getCurrentUser());

ParseQuery<ParseObject> query = ParseQuery.or(Arrays.asList(query1, query2));

But the problem is when showing the "not me" adapter: 但是问题是当显示“不是我”适配器时:

Adapter: 适配器:

 ParseUser user = null;
 ParseUser user1 = (ParseUser) object.get("user_to");
 if (user1 == ParseUser.getCurrentUser())
     user = (ParseUser) object.get("user_from");
 else
     user = user1;

Of course I need to fetch the user, which in the adapter is not a good idea. 当然,我需要获取用户,这在适配器中并不是一个好主意。 So, is there a better approach on the Database side to improve this ? 那么,在数据库方面是否有更好的方法可以改善这一点?

Thanks 谢谢

Parse is a database and the API will send you the records as they are in the database. 解析是一个数据库,API将向您发送数据库中的记录。 You could create a Parse.com cloud function if you want to compute the "other party" of your transactions on the Parse.com side of the line. 如果要在该行的Parse.com端计算交易的“另一方”,则可以创建Parse.com 云功能 The function will be provided with the current user and has the entire JavaScript API at its disposal so that should work. 该功能将由当前用户提供,并具有完整的JavaScript API,以便可以使用。

But it seems overkill to me. 但是对我来说似乎太过分了。 Their example aggregates information of many review rows into a single rating which should speed up the transmission of the result. 他们的示例将许多评论行的信息汇总到一个评分中,这将加快结果的传输速度。 In your case, you compute a new column which you can just as well compute on the client, just like you do. 在您的情况下,您将计算一个新列,就像在客户端上一样,它也可以在客户端上进行计算。

So I don't see anything wrong with your current approach. 因此,我认为您当前的方法没有任何问题。 I think it's not weird that it's your adapter's job to show the "other party". 我认为显示“另一方”是您的适配器的工作并不奇怪。 I'd call it view logic, a choice in how to display the data to the user. 我将其称为视图逻辑,这是如何向用户显示数据的一种选择。 So if the adapter needs to show the "other party", then the adapter needs to know which of the two users to filter out. 因此,如果适配器需要显示“另一方”,则适配器需要知道要过滤掉两个用户中的哪个。

To separate things a bit more, you could create a currentUser field in the adapter class and fill it in the constructor or use a setter. 为了进一步分开,您可以在适配器类中创建一个currentUser字段,并将其填充到构造函数中或使用setter。 Then the adapter need not call ParseUser.getCurrentUser() itself. 然后,适配器不需要调用ParseUser.getCurrentUser()本身。 This may for example help you create unit tests for the adapter. 例如,这可以帮助您为适配器创建单元测试。

If you want the rest of your code to be more independent of Parse.com, you can create some kind of Data Access Object that computes the "other party" column and also transforms the data to non-Parse.com objects. 如果希望其余代码更独立于Parse.com,则可以创建某种类型的数据访问对象,该对象将计算“另一方”列,并将数据转换为非Parse.com对象。 Seems like a different kind of overkill to me though. 不过,对我而言,这似乎是另一种过度的杀伤力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM