user1079877
user1079877

Reputation: 9368

Generate a complete JSON Object from a table with relations with other tables using Posgtres JSON functions

I want to use Postgres to generate the user JSON object from 'user', 'user_role', 'role' and 'permission' as follow:

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;

The outputs are:

--------------------------------
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]}

I have two issues:

  1. How to get rid of [null]. Can we have [] of instead?
  2. I want to generate this result:
--------------------------------
1  |  {"id" : 1, "roles" : [1, 2], "permissions": [1, 2, 3, 4]}
2  |  {"id" : 2, "roles" : [3]   , "permissions": [4]}
3  |  {"id" : 3, "roles" : [null], "permissions": []}

How the query should look like? You can find the code and run it here in DB fiddle also. Thanks.

Upvotes: 1

Views: 212

Answers (2)

roman
roman

Reputation: 117420

If you want to return roles and permissions per user, then you can do this:

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

Upvotes: 1

klin
klin

Reputation: 121644

First, you should get roles for a user from the role table, user_role is only a bridge table. Look at the results of the query:

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)

User #2 has a role #1 (not #3).

You can easily transform the query to get aggregated data:

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)

There is no standard function to strip null elements of a jsonb array. You can create your own one:

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.

Upvotes: 1

Related Questions