[英]Supabase: Filter data with join and logical 'OR'
I'm using Supabase as a database and trying to implement a full-text search.我正在使用 Supabase 作为数据库并尝试实现全文搜索。
My example setup is quite simple, I have two tables:我的示例设置非常简单,我有两个表:
items项目
+----+-----------+-----------------+
| id | name | manufacturer_id |
+----+-----------+-----------------+
| 1 | Notebook | 1 |
+----+-----------+-----------------+
| 2 | Mouse | 2 |
+----+-----------+-----------------+
manufacturers制造商
+----+-----------+
| id | name |
+----+-----------+
| 1 | Apple |
+----+-----------+
| 2 | Microsoft |
+----+-----------+
My goal: search for item names or manufacturer names and always receive the respective items
.我的目标:搜索商品名称或制造商名称并始终收到相应的items
。 Searching for 'Apple' would return all items whose manufacturers name contain this phrase.搜索“Apple”将返回制造商名称包含此短语的所有项目。 Searching for 'Notebook' would return all items with this name.搜索“Notebook”将返回具有此名称的所有项目。
( I simplified this example by using exact matches because this is not the problem I'm stuck with ). (我通过使用精确匹配简化了这个例子,因为这不是我遇到的问题)。
My current approach is the following:我目前的方法如下:
let keyword = 'Apple';
supabase
.from('items')
.select('*, manufacturers!inner(*)')
.or(`name.eq.${term}`)
.or(`name.eq.${term}`, { foreignTable: 'manufacturers' })
Although this query does not return anything.尽管此查询不返回任何内容。
If I remove .or('name.eq.${term}')
it return the correct item.如果我删除.or('name.eq.${term}')
它返回正确的项目。 Same if I remove the second .or()
and use an item name as my keyword.如果我删除第二个.or()
并使用项目名称作为我的关键字,则相同。
I simply can't find a way to combine two OR operators.我根本找不到组合两个 OR 运算符的方法。 I also tried multiple filter()
queries but was not successfully.我也尝试了多个filter()
查询但没有成功。
EDIT: The (working) SQL syntax would be the following:编辑:(工作)SQL 语法如下:
SELECT * FROM items
JOIN manufacturers ON items.manufacturer_id = manufacturers.id
WHERE items.name = 'Apple' OR manufacturers.name = 'Apple'
Does anyone have an approach on how to do that with the Supabase JavaScript SDK?有没有人知道如何使用 Supabase JavaScript SDK 做到这一点? Thank you in advance!先感谢您!
You can't OR combine a foreign table( manufacturers
) condition with a parent table( items
) condition directly through the JS SDK.您不能直接通过 JS SDK 将外部表( manufacturers
)条件与父表( items
)条件或组合。
(More details about why here ) (有关原因的更多详细信息)
However you can create a function wrapping your query and use rpc to get it:但是,您可以创建一个 function 包装您的查询并使用 rpc 来获取它:
https://supabase.com/docs/reference/javascript/rpc https://supabase.com/docs/reference/javascript/rpc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.