0

I've a logs table, which is it contains all the actions (updated, created) taken by operators (admin users).

On this table 2 of these columns (indexed as hash) target_entity and target_id Which respectively stores

  • table name: table name of the action taken on.
  • record id added or updated record's id in the target table.

So, what I am trying to achieve; I would like to add a computed field named eg:logs which depends on a function;

    FUNCTION public."fetchLogs"(
        referenceId integer,
        referenceName TEXT
    ) 
  • First parameter is the current table's primary key.

    • I'm not sure if I can automatically send primary key as first argument
    • So probably it should be something like table_row table
  • Second parameter is a static value which is table's name, planning to statically pass as argument.

This function returns a JSON object;

    RETURNS json LANGUAGE plpgsql STABLE 
    AS $function$

It should return log records related to this record.

At this point I have 2 things needs to be tackled with;

  1. Since the first parameter is the reference (primary) key, I don't know If I can just use primary key as an argument. I'm guessing I need to use as table_row (anytable if that's a thing) then table_row.id

  2. In the Hasura console, Add Computed Field > Function Name selector does not list this function, guessing because it doesn't explicitly indicated in the function which table is this action for.

Answers I'm looking for if this is achievable or is there a better practice for this kind of things?

Maybe I need a encapsulating function for each table needs this computed column. But I'm not sure is it possible or how can be done.

P.S. In case if you are wondering all primary keys are same type and name yes. All tables (will be using this computed column) has a primary key named as id and type is integer.

siniradam
  • 2,727
  • 26
  • 37

0 Answers0