Skip to content

Instantly share code, notes, and snippets.

@areski
Created March 13, 2016 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save areski/b6ecafb20a2508230d18 to your computer and use it in GitHub Desktop.
Save areski/b6ecafb20a2508230d18 to your computer and use it in GitHub Desktop.
PLLUAU example calling API when new Customer are created
--
-- PLLUAU example calling API when new Customer are created
--
CREATE TABLE customer (
id bigserial primary key,
full_name varchar(200) NOT NULL,
email varchar(200) NOT NULL,
created_date timestamp default NOW()
);
--
-- Trigger
--
CREATE OR REPLACE FUNCTION customer_created() RETURNS trigger AS $$
local row, operation = trigger.row, trigger.operation
print(_VERSION)
if operation == "insert" then
local curl = require "cURL.safe"
local api_url = 'http://requestb.in/184vyco1'
-- Perform HTTP Post
curl.easy()
:setopt_url(api_url)
:setopt_writefunction(io.stderr)
:setopt_httppost(
curl.form()
:add_content("full_name", row.full_name)
:add_content("email", row.email)
)
:perform()
:close()
end
$$language plluau;
DROP TRIGGER customer_create_trigger ON customer;
CREATE TRIGGER customer_create_trigger AFTER INSERT ON customer
FOR EACH ROW EXECUTE PROCEDURE customer_created();
INSERT INTO customer (full_name, email) VALUES ('Areski Belaid', 'areski@gmail.com');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment