我有一个Postgres DB设置,我试图从查询中返回一个特定的数据形状。
我的数据如下所示:
CREATE TABLE resource_actions(
resource TEXT NOT NULL,
actions TEXT NOT NULL,
entity TEXT NOT NULL,
UNIQUE(resource, actions, entity)
);
INSERT INTO resource_actions (resource, actions, entity) VALUES
('reading-list', 'read', 'Citizen'),
('reading-list', 'read', 'Employee'),
('books', 'read', 'Employee'),
('titles', 'update', 'Citizen'),
('titles', 'read', 'Citizen'),
('titles', 'update', 'Employee'),
('titles', 'read', 'Employee'),
('authors', 'update', 'Employee'),
('authors', 'read', 'Citizen'),
('reviews', 'read', 'Citizen'),
('reviews', 'read', 'Employee'),
('reviews', 'create', 'Citizen'),
('reviews', 'create', 'Employee'),
('employees', 'read', 'Employee'),
('employees', 'read', 'Boss'),
('employees', 'delete', 'Boss'),
('employees', 'create', 'Boss'),
('employees', 'update', 'Boss'),
('employee-schedule', 'read', 'Boss'),
('employee-schedule', 'delete', 'Boss'),
('employee-schedule', 'create', 'Boss'),
('employee-schedule', 'update', 'Boss');
我想像这样返回数据:
{
read:['Citizen':['reading-list', 'titles', 'authors','reviews'], 'Employee':['books','reading-list', 'titles', 'authors'], 'Boss':[]]
create:['Citizen':['reviews'], 'Employee':['reviews'], 'Boss':['employees','employee-schedule']],
update: ['Citizen':['titles'], 'Employee':['authors','titles'], 'Boss':['employees','employee-schedule']],
delete:['Citizen':[], 'Employee':[], 'Boss':['employees','employee-schedule']]
}
我有一个接近的SQL查询,我只是不能为我的生活弄清楚我需要改变什么来获得正确的数据形状。我的查询是在这里:
select actions,
JSON_AGG(JSON_BUILD_OBJECT(resource, entity)) AS groupedActions
FROM resource_actions
GROUP BY actions
我有一个SQL小提琴显示我到目前为止所拥有的:http://sqlfiddle.com/#!17/93bf5/1/0
它让我接近了,但我不明白如何通过entity
聚合数据并显示他们可以访问的资源的数组。
3条答案
按热度按时间5anewei61#
| json结果|
| - ------|
| {“create”:[{“公民”:[“reviews”]},{“Employee”:[“reviews”]},{“Boss”:[“employees”,“employee-schedule”]}]}|
| {“read”:[{“Boss”:[“employees”,“employee-schedule”]},{“Citizen”:[“reading-list”,“titles”,“authors”,“reviews”]},{“Employee”:[“阅读列表”,“书籍”,“标题”,“评论”,“员工”]}]}|
| {“update”:[{“Boss”:[“employees”,“employee-schedule”]},{“Citizen”:[“titles”]},{“Employee”:[“标题”、“作者”]}]|
| {“delete”:[{“Boss”:[“employees”,“employee-schedule”]}]}|
DBfiddle demo
全部为单个JSON:
DBFiddle demo
lawou6xi2#
若要聚合具有串联的操作,可以使用自定义聚合
db<>fiddle
lh80um4z3#
您可以在子查询/CTE s中构造对象,然后使用
jsonb_object_agg()
收集它们:demo如果可以,请使用
jsonb
:它是可索引的,更轻,更快,更灵活,提供更多的函数和操作符,包括jsonpath
交互,在输入上的处理成本很小。如果你不能,上面所有的jsonb
函数都有直接的json
等价物,它们可以被交换出来。除了下面的
jsonb_pretty()
,在这里你必须将json
转换为jsonb
。如果你想美化一些基于文本的json,你可以将它作为字符串文本输入-它将被检测为unknown
并自动转换为支持的类型。通过
jsonb_pretty()
输出: