Skip to content

Instantly share code, notes, and snippets.

View shahidhk's full-sized avatar

Shahidh K Muhammed shahidhk

View GitHub Profile
CREATE FUNCTION update_last_seen_timestamp_func() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.last_seen_at := now();
RETURN NEW;
END;
$$;
CREATE TRIGGER update_last_seen_timestamp_trigger
CREATE VIEW poll_results AS
SELECT poll.id AS poll_id,
o.option_id,
count(*) AS votes
FROM (( SELECT vote.option_id,
option.poll_id,
option.text
FROM (vote
LEFT JOIN public.option ON ((option.id = vote.option_id)))) o
LEFT JOIN poll ON ((poll.id = o.poll_id)))
@shahidhk
shahidhk / realtime-poll-tables.sql
Created August 28, 2018 10:52
Schema for a realtime poll application
CREATE TABLE "user" (
id uuid DEFAULT gen_random_uuid() NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
online_ping boolean,
last_seen_at timestamp with time zone
);
CREATE TABLE poll (
id uuid DEFAULT gen_random_uuid() NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
CREATE TABLE book (
id SERIAL NOT NULL,
title TEXT NOT NULL,
author TEXT NOT NULL,
genre TEXT,
PRIMARY KEY (id)
);
CREATE TABLE author (
name TEXT NOT NULL,
CREATE TABLE address (
id SERIAL NOT NULL,
user_id INTEGER NOT NULL REFERENCES "user" (id),
street TEXT,
city TEXT,
country TEXT,
PRIMARY KEY (id)
);
CREATE TABLE "user" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE address (
id SERIAL NOT NULL,
user_id INTEGER NOT NULL REFERENCES "user" (id),
street TEXT,
CREATE VIEW laptop_listing AS
SELECT
name, price,
spec->>'processor' AS processor,
spec->>'ram' AS ram,
spec->>'disk' AS disk,
spec->>'display' AS display
FROM
product
WHERE
# Query
mutation {
  update_product(
    where: {
      uuid: {_eq: "09cf633b-a34c-45cc-ad59-3273e3ad65f3"}
    },
    _delete_at_path: {
      spec: ["frequency_response", "min"]
 }
# Query
mutation updateProductSpec ($data: jsonb) {
  update_product(
    where: {
      uuid: {_eq: "09cf633b-a34c-45cc-ad59-3273e3ad65f3"}
    },
    _append: { spec: $data }
  ) {
    affected_rows
# Query
mutation {
  update_product(
    where: {
      uuid: {_eq: "09cf633b-a34c-45cc-ad59-3273e3ad65f3"}
    },
    _delete_key: {
      spec: "driver_units"
 }