notify wip
This commit is contained in:
1 parent
c205ae81e2
commit
358958c127
12 files changed
+539
-18
No files matched your search
@@ -0,0 +1,81 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "listings" (
|
||||
"id" BIGINT NOT NULL,
|
||||
"seller" BIGINT NOT NULL,
|
||||
"data" JSONB NOT NULL,
|
||||
"wantId" UUID NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "listings_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "users" (
|
||||
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||
"email" TEXT NOT NULL,
|
||||
"password" TEXT NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "users_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "wants" (
|
||||
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||
"release" BIGINT NOT NULL,
|
||||
"data" JSONB NOT NULL,
|
||||
"userId" UUID NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "wants_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "offers" (
|
||||
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||
"status" TEXT NOT NULL,
|
||||
"condition" TEXT NOT NULL DEFAULT '',
|
||||
"item" MONEY NOT NULL,
|
||||
"shipping" MONEY NOT NULL,
|
||||
"total" MONEY NOT NULL,
|
||||
"listingId" BIGINT NOT NULL,
|
||||
"userId" UUID NOT NULL,
|
||||
"wantId" UUID NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "offers_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "listings_seller_idx" ON "listings"("seller");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "wants_release_idx" ON "wants"("release");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "wants_userId_release_key" ON "wants"("userId", "release");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "offers_listingId_key" ON "offers"("listingId");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "listings" ADD CONSTRAINT "listings_wantId_fkey" FOREIGN KEY ("wantId") REFERENCES "wants"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "wants" ADD CONSTRAINT "wants_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "offers" ADD CONSTRAINT "offers_listingId_fkey" FOREIGN KEY ("listingId") REFERENCES "listings"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "offers" ADD CONSTRAINT "offers_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "offers" ADD CONSTRAINT "offers_wantId_fkey" FOREIGN KEY ("wantId") REFERENCES "wants"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,65 @@
|
||||
CREATE OR REPLACE FUNCTION notify_uuid() RETURNS TRIGGER AS $$
|
||||
DECLARE
|
||||
record RECORD;
|
||||
id UUID;
|
||||
BEGIN
|
||||
IF (TG_OP = 'DELETE') THEN
|
||||
id = OLD.id;
|
||||
ELSE
|
||||
id = NEW.id;
|
||||
END IF;
|
||||
|
||||
payload = json_build_object(
|
||||
'table', TG_TABLE_NAME,
|
||||
'action', TG_OP,
|
||||
'id', id
|
||||
);
|
||||
|
||||
PERFORM pg_notify('events', payload::text);
|
||||
|
||||
RETURN NULL;
|
||||
END;
|
||||
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE OR REPLACE FUNCTION notify_bigint() RETURNS TRIGGER AS $$
|
||||
DECLARE
|
||||
record RECORD;
|
||||
id int8;
|
||||
BEGIN
|
||||
IF (TG_OP = 'DELETE') THEN
|
||||
id = OLD.id;
|
||||
ELSE
|
||||
id = NEW.id;
|
||||
END IF;
|
||||
|
||||
payload = json_build_object(
|
||||
'table', TG_TABLE_NAME,
|
||||
'action', TG_OP,
|
||||
'id', id
|
||||
);
|
||||
|
||||
PERFORM pg_notify('events', payload::text);
|
||||
|
||||
RETURN NULL;
|
||||
END;
|
||||
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
|
||||
CREATE TRIGGER notify_listings_event
|
||||
AFTER INSERT OR UPDATE OR DELETE ON listings
|
||||
FOR EACH ROW EXECUTE PROCEDURE notify_bigint();
|
||||
|
||||
CREATE TRIGGER notify_wants_event
|
||||
AFTER INSERT OR UPDATE OR DELETE ON wants
|
||||
FOR EACH ROW EXECUTE PROCEDURE notify_bigint();
|
||||
|
||||
CREATE TRIGGER notify_offers_event
|
||||
AFTER INSERT OR UPDATE OR DELETE ON offers
|
||||
FOR EACH ROW EXECUTE PROCEDURE notify_uuid();
|
||||
|
||||
CREATE TRIGGER notify_users_event
|
||||
AFTER INSERT OR UPDATE OR DELETE ON users
|
||||
FOR EACH ROW EXECUTE PROCEDURE notify_uuid();
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "postgresql"
|
||||
Reference in new issue
Block a user