a postgraphile plugin to capture designated function calls as re-playable events
Branch: master
Clone or download
Latest commit e75a474 Mar 23, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dev init commit Jan 10, 2018
src/migrations note Jan 11, 2018
.gitignore init commit Jan 10, 2018
LICENSE Initial commit Jan 10, 2018
README.md Update README.md Mar 22, 2018
knexfile.js init commit Jan 10, 2018
package-lock.json ready Jan 10, 2018
package.json

README.md

event-phile

a postgraphile plugin to capture designated function calls as re-playable events

*** just an idea

git clone https://github.com/stlbucket/event-phile.git
npm install

you might need to:

npm install -g knex-migrate

edit knexfile.js to update your connection string to a test database

knex-migrate up --env dev
./dev/execute.sh events

a function recorded as an event

-- this function would really be in another schema
------------------
create or replace function event_phile.add_to_category(
  _name text,
  _amount NUMERIC(50,2)
) returns event_phile.category as $$
declare
  _event_log event_phile.event_log;  -- this really wouldn't be here.  it just makes the function work for this illustration
  _category event_phile.category;
begin


------------------- PRE-PROCESSING
-- this would happen via the plugin
-- doing this here for illustration
-- this is the sql that happens before the function is called

INSERT INTO event_phile.event_log(
  occurred_at,
  event_name,
  event_key,
  status,
  args_json
)
SELECT
  current_timestamp,                                                       -- if occurred_at is not with event, then current_timestamp
  'event_phile.add_to_category',                                           -- populated by plugin
  _name,                                                                   -- this would actually be populated dynamically in the plugin
  'Captured',                                                              -- so far this is just captured
  ('{"_name": "' || _name || '" ,"_amount": ' || _amount ||'}')::jsonb     -- but really the args jsonified
RETURNING *
INTO _event_log;
------------------- END PRE-PROCESSING

------------------- PROCESSING
  -- this would be the bulk of the actual contents of the event_phile.add_to_category function
  -- if the event_phile.event_log_config.processing_mode for this event is = 'Consume'
  -- then this function would actually be executed

  INSERT INTO
  event_phile.category(
    name,
    total_amount
  )
  SELECT
    _name,
    _amount
  ON CONFLICT (name)
  DO UPDATE
  SET total_amount = category.total_amount + _amount
  RETURNING *
  INTO _category
  ;
------------------- END PROCESSING


------------------- POST-PROCESSING
-- this would happen via the plugin
-- update the event_log record
UPDATE event_phile.event_log
SET
  status = 'Consumed',
  result_json = '{}' -- result would by default be empty.  only if there were an error or auditing were enabled would result be recorded
WHERE id = _event_log.id;
------------------- END POST-PROCESSING

------------------- RETURN RESULTS
  -- this would be part of the actual function contents
  return _category;
------------------- END RETURN RESULTS

  ---------------------EXCEPTION HANDLING
    -- this would happen via the plugin
    -- the stacked diagnostics would be recorded in result_json
    -- and the status would be 'Errored'
end;
$$ language plpgsql strict security definer;
--||--
GRANT execute ON FUNCTION event_phile.add_to_category(
  text,
  numeric
) TO public;