如何通过自定义类型字段值查询 Amplify 数据存储(Flutter)?

huangapple go评论66阅读模式
英文:

How to query Amplify datastore by custom type field value(Flutter)

问题

我最近正在写一个Flutter应用程序,我需要在Amplify数据存储中操作数据。我能够通过常规数据类型(例如字符串)查询数据,但我不确定如何按照自定义类型数据的数组进行查询。

以下是我的数据模型示例:

type Shop @model {
id: ID!
owner: String
item: [Item]
}

type Item {
name: String
color: String
}


我如何获取所有具有名称为“abc”的Item的Shop模型的列表,不管它是什么颜色。

我已经尝试过像这样做,使用谓词“contains”,但似乎这不是正确的方法:

Future<List> getShops(String itemName) async {
try {
final shops = await Amplify.Datastore.query(
Shop.classType,
where: Shop.ITEM.name.contains(itemName)
);
return shops;
} catch (e) {
throw e;
}
}

英文:

I'm recently writing a flutter app which I need to manipulate the data in Amplify datastore. I'm able to query data by regular data type(e.g. String), but I'm not sure how to query by an array of custom type data.

Here is an example of my data model

type Shop @model {
    id: ID!
    owner: String
    item: [Item]
}

type Item{
    name: String
    color: String
}

How can I get a list of all Shop Models that have an Item named "abc", no matter what color it is.

I have done something like this, using the predicate "contains", but seems that it's not the right way to do so:

Future &lt;List&lt;Shop&gt;&gt; getShops(String itemName) async{
    try{
        final shops = await Amplify.Datastore.query(
            Shop.classType,
            where: Shop.ITEM.name.contains(itemName)
        );
        return shops;
    } catch (e){
        throw e;
    }
}

答案1

得分: 1

以下是翻译好的部分:

I found another way to achieve this. I changed the database structure to many-to-many relationship as below.
我找到了另一种实现方式。我将数据库结构更改为如下的多对多关系。

This will automatically generate a table called "ShopItem" which tells you if the Shop model and Item model are related.
这将自动生成一个名为"ShopItem"的表,告诉您Shop模型和Item模型是否相关。

Then I just use the following code snippet to get a list of all Shop Models that have an Item named "abc":
然后,我只需使用以下代码片段来获取具有名称为"abc"的Item的所有Shop模型的列表:

Might not be the best way, but I find it clearer and simpler than what I did before. Just let me know if there is a more efficient way.
可能不是最佳方式,但我觉得它比之前的方法更清晰和简单。如果有更高效的方式,请告诉我。

英文:

I found another way to achieve this. I changed the database structure to many-to-many relationship as below.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

type Shop @model{
  id: ID!
  items: [Item] @manyToMany(relationName: &quot;ShopItem&quot;)
  owner: String
}

type Item @model{
  id: ID!
  shops: [Shop] @manyToMany(relationName: &quot;ShopItem&quot;)
  name: String
  color: String
}

<!-- end snippet -->

This will automatically generate a table called "ShopItem" which tells you if the Shop model and Item model are related.

Then I just use the following code snippet to get a list of all Shop Models that have an Item named "abc":

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

final dataList = await Amplify.DataStore.query(ShopItem.classType);

List&lt;Shop&gt; shopList = [];

for(var element in dataList){
  if(element.item.name == &quot;abc&quot;){
    shopList.add(element.shop);
  }
}

<!-- end snippet -->

Might not be the best way, but I find it clearer and simpler than what I did before. Just let me know if there is a more efficient way.

答案2

得分: 0

我将为您翻译代码部分:

// 将商品链接到商店:
final item = Item(name: "样本", color: "红色");
await Amplify.Datastore.save(item);

final shop = Shop(owner: "样本");
await Amplify.Datastore.save(shop);

final shopItem = ShopItem(Shop: shop, Item: item);
await Amplify.Datastore.save(shopItem); // 这将链接两个模型

// 取消链接:
await Amplify.Datastore.delete(shopItem);

参考链接: Amplify数据存储关系模型

英文:

I'll give some examples on how to manipulate data of relational models here in case someone is also learning it.

To link an item to a shop:

<!-- language: lang-html -->

final item = Item(name: &quot;sample&quot;, color: &quot;red&quot;);
await Amplify.Datastore.save(item);

final shop = Shop(owner: &quot;sample&quot;);
await Amplify.Datastore.save(shop);    

final shopItem = ShopItem(Shop: shop, Item: item);
await Amplify.Datastore.save(shopItem);    //this links the two models together

<!-- end snippet -->

To unlink:

<!-- language: lang-html -->

await Amplify.Datastore.delete(shopItem); 

<!-- end snippet -->

Reference: Amplify datastore relational models

huangapple
  • 本文由 发表于 2023年3月9日 23:05:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定