how to incorporate postgresql functions into hasura

27 Views Asked by At

I know that hasura doesn't support recursions and that's why I have this function in postgresql that I need to convert into a hasura tracked function. So i could simply call that but it's quite complicated due to the fact that hsura forces the creation of a table and despite that I didn't succeed.

WITH RECURSIVE ink_sort_group_hierarchy AS (
    SELECT
        id,
        value,
        name,
        ARRAY[order_id] AS item
    FROM ink_sort_group
    WHERE superior_id IS NULL AND slot_ink_id = 51  -- specified slot_ink_id

    UNION ALL

    SELECT
        ig.id,
        ig.value,
        ig.name,
        ih.item || ig.order_id
    FROM ink_sort_group ig
    JOIN ink_sort_group_hierarchy ih ON ig.superior_id = ih.id AND ig.slot_ink_id = 51
)
SELECT
    id,
    value,
    name,
    item
FROM ink_sort_group_hierarchy
ORDER BY item;

And this is my try to make it work

CREATE TYPE ink_sort_group_result AS (
    id INT,
    value INT,
    name TEXT,
    item INT[]
);

CREATE OR REPLACE FUNCTION get_ink_sort_groups(p_superior_id INT, p_slot_ink_id INT)
RETURNS SETOF ink_sort_group_result STABLE AS $$
BEGIN

    CREATE TEMPORARY TABLE temp_ink_sort_group_hierarchy (
        id INT,
        value INT,
        name TEXT,
        item INT[]
    );

    WITH RECURSIVE ink_sort_group_hierarchy AS (
        SELECT
            id,
            value,
            name,
            ARRAY[order_id] AS item
        FROM ink_sort_group
        WHERE superior_id = p_superior_id AND slot_ink_id = p_slot_ink_id

        UNION ALL

        SELECT
            ig.id,
            ig.value,
            ig.name,
            ih.item || ig.order_id
        FROM ink_sort_group ig
        JOIN ink_sort_group_hierarchy ih ON ig.superior_id = ih.id AND ig.slot_ink_id = p_slot_ink_id
    )
    INSERT INTO temp_ink_sort_group_hierarchy
    SELECT * FROM ink_sort_group_hierarchy;

    RETURN QUERY SELECT * FROM temp_ink_sort_group_hierarchy;

    DROP TABLE IF EXISTS temp_ink_sort_group_hierarchy;
END;
$$ LANGUAGE plpgsql;
0

There are 0 best solutions below