使用Posgtres JSON函数从与其他表有关系的表中生成完整的JSON对象

时间:2019-04-24 19:50:02

标签: sql postgresql jsonb

我想使用Postgres从'user','user_role','role'和'permission'生成用户JSON对象,如下所示:

CREATE TABLE "user" (
        id SERIAL PRIMARY KEY,
        name TEXT,
        location TEXT
);
CREATE TABLE "role" (
        id INT NOT NULL PRIMARY KEY,
        name TEXT
);
CREATE TABLE "user_role" (
        id SERIAL PRIMARY KEY,
        user_id BIGINT,
        role_id INT,
        FOREIGN KEY ("user_id") REFERENCES "user"("id"),
        FOREIGN KEY ("role_id") REFERENCES "role"("id")
);
CREATE TABLE "permission" (
        id INT NOT NULL PRIMARY KEY,
        role_id INT,
        name TEXT,
        FOREIGN KEY ("role_id") REFERENCES "role"("id")
);

INSERT INTO "user" ("name", "location") VALUES ('Hamed', 'Berlin');
INSERT INTO "user" ("name", "location") VALUES ('Zhang', 'Shenzhen');
INSERT INTO "user" ("name", "location") VALUES ('Jake', 'Vancouver');
INSERT INTO "role" ("id", "name") VALUES (1, 'admin');
INSERT INTO "role" ("id", "name") VALUES (2, 'user');
INSERT INTO "permission" ("id", "role_id", "name") VALUES (1, 1, 'add');
INSERT INTO "permission" ("id", "role_id", "name") VALUES (2, 1, 'delete');
INSERT INTO "permission" ("id", "role_id", "name") VALUES (3, 1, 'update');
INSERT INTO "permission" ("id", "role_id", "name") VALUES (4, 2, 'read');
INSERT INTO "user_role" ("user_id", "role_id") VALUES (1, 1);
INSERT INTO "user_role" ("user_id", "role_id") VALUES (1, 2);
INSERT INTO "user_role" ("user_id", "role_id") VALUES (2, 1);

SELECT (
    json_build_object (
        'id', u.id,
        'roles', json_agg(
            r.id
        )
    )
) FROM "user" AS u
LEFT JOIN "user_role" AS r ON r.user_id = u.id
GROUP by u.id;

SELECT (
    json_build_object (
        'id', r.id,
        'permissions', json_agg(
            p.id
        )
    )
) FROM "role" AS r
LEFT JOIN "permission" AS p ON p.role_id = r.id
GROUP by r.id;

输出为:

--------------------------------
1  |  {"id" : 1, "roles" : [1, 2]}
2  |  {"id" : 2, "roles" : [3]}
3  |  {"id" : 3, "roles" : [null]}

--------------------------------
1  |  {"id" : 1, "permissions" : [1, 2, 3]}
2  |  {"id" : 2, "permissions" : [4]}

我有两个问题:

  1. 如何摆脱 [空] 。我们可以代替 [] 吗?
  2. 我想生成此结果:
--------------------------------
1  |  {"id" : 1, "roles" : [1, 2], "permissions": [1, 2, 3, 4]}
2  |  {"id" : 2, "roles" : [3]   , "permissions": [4]}
3  |  {"id" : 3, "roles" : [null], "permissions": []}

查询的外观如何? 您可以找到代码并也here in DB fiddle运行它。谢谢。

2 个答案:

答案 0 :(得分:1)

如果要返回每个用户的角色和权限,则可以执行以下操作:

select
    to_json(a)
from (
    select
        u.id,
        coalesce(json_agg(distinct r.role_id) filter (where r.role_id is not null), '[]'::json) as roles,
        coalesce(json_agg(distinct p.id) filter (where p.id is not null), '[]'::json) as permissions
    from "user" as u
        left join "user_role" as r on
            r.user_id = u.id
        left join "permission" as p on
            p.role_id = r.role_id
    group by
        u.id
) as a

db-fiddle demo

答案 1 :(得分:1)

首先,您应该从role表中获得用户的角色,user_role只是一个桥接表。查看查询结果:

select 
    u.id as user_id, 
    ur.id as user_role_id, 
    r.id as role_id, 
    p.id as permission_id
from "user" u
left join "user_role" ur on ur.user_id = u.id
left join "role" r on r.id = ur.role_id
left join "permission" p on p.role_id = r.id

 user_id | user_role_id | role_id | permission_id 
---------+--------------+---------+---------------
       1 |            1 |       1 |             3
       1 |            1 |       1 |             2
       1 |            1 |       1 |             1
       1 |            2 |       2 |             4
       2 |            3 |       1 |             3
       2 |            3 |       1 |             2
       2 |            3 |       1 |             1
       3 |              |         |              
(8 rows)

用户#2的角色为#1(不是#3)。

您可以轻松地转换查询以获取聚合数据:

select 
    jsonb_build_object(
        'id', u.id,
        'roles', jsonb_agg(distinct r.id order by r.id),
        'permissions', jsonb_agg(p.id order by p.id))
from "user" u
left join "user_role" ur on ur.user_id = u.id
left join "role" r on r.id = ur.role_id
left join "permission" p on p.role_id = r.id
group by u.id

                   jsonb_build_object                    
---------------------------------------------------------
 {"id": 1, "roles": [1, 2], "permissions": [1, 2, 3, 4]}
 {"id": 2, "roles": [1], "permissions": [1, 2, 3]}
 {"id": 3, "roles": [null], "permissions": [null]}
(3 rows)

没有标准功能来剥离jsonb数组的空元素。您可以创建自己的一个:

create or replace function jsonb_strip_null_elements(jsonb)
returns jsonb language sql immutable as $$
    select coalesce(jsonb_agg(e), '[]')
    from jsonb_array_elements($1) as e
    where e <> 'null'
$$;

Live demo in db-fiddle.