Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions libs/supabase/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- AlterTable
ALTER TABLE "McqQuestion" ADD COLUMN "createdById" UUID,
ADD COLUMN "updatedById" UUID;

-- AddForeignKey
ALTER TABLE "McqQuestion" ADD CONSTRAINT "McqQuestion_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "Profile"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "McqQuestion" ADD CONSTRAINT "McqQuestion_updatedById_fkey" FOREIGN KEY ("updatedById") REFERENCES "Profile"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
1 change: 1 addition & 0 deletions libs/supabase/prisma/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const allModels = ['Subject', 'Topic', 'McqQuestion']
19 changes: 13 additions & 6 deletions libs/supabase/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ model Profile {

createdTopics Topic[] @relation("TopicCreatedBy")
updatedTopics Topic[] @relation("TopicUpdatedBy")

createdMcqs McqQuestion[] @relation("McqCreatedBy")
updatedMcqs McqQuestion[] @relation("McqUpdatedBy")
}

model Course {
Expand Down Expand Up @@ -115,13 +118,17 @@ model McqQuestion {
explanation String
topic Topic @relation(fields: [topicId], references: [id])
topicId String @db.Uuid
createdBy Profile? @relation("McqCreatedBy", fields: [createdById], references: [id], onDelete: Restrict)
createdById String? @db.Uuid
updatedBy Profile? @relation("McqUpdatedBy", fields: [updatedById], references: [id], onDelete: Restrict)
updatedById String? @db.Uuid
}

model ContactUs {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
type String?
name String?
email String?
mobile String?
message String?
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
type String?
name String?
email String?
mobile String?
message String?
}
6 changes: 6 additions & 0 deletions libs/supabase/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import { PrismaClient } from '@prisma/client';
import { create_profile_on_signup } from './seeds/create_profile_on_signup';
import { create_check_mobile_exists } from './seeds/check_mobile_exists';
import { seedRLSPolicies } from './seeds/rls_policies';
import { create_createdBy } from './seeds/create_createdBy_trigger';
import { create_updatedBy } from './seeds/create_updatedBy_trigger';
import { create_updatedAt } from './seeds/create_updatedAt_trigger';

const prisma = new PrismaClient();

async function main() {
await create_profile_on_signup(prisma);
await create_check_mobile_exists(prisma);
await create_createdBy(prisma);
await create_updatedAt(prisma);
await create_updatedBy(prisma);
await seedRLSPolicies(prisma);
}

Expand Down
37 changes: 37 additions & 0 deletions libs/supabase/prisma/seeds/create_createdBy_trigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { PrismaClient } from '@prisma/client';
import { allModels } from '../models';

async function makeFunction(prisma) {
await prisma.$executeRaw`
CREATE OR REPLACE FUNCTION public.update_createdBy()
RETURNS trigger AS $$
BEGIN
NEW."createdById" := auth.uid();
RETURN NEW;
END;
$$
LANGUAGE plpgsql
SECURITY definer SET search_path = public;
`;
}

async function makeTrigger(prisma: PrismaClient, tableName: string) {
await prisma.$executeRawUnsafe(`
CREATE OR REPLACE TRIGGER update_createdBy_trigger
BEFORE INSERT ON public."${tableName}"
FOR EACH ROW
EXECUTE PROCEDURE public.update_createdBy();
`);
}

export async function create_createdBy(prisma: PrismaClient) {
await makeFunction(prisma);
await makeCreatedByTriggers(prisma)
}


async function makeCreatedByTriggers(prisma: PrismaClient) {
for (const entity of allModels) {
await makeTrigger(prisma, entity);
}
}
29 changes: 29 additions & 0 deletions libs/supabase/prisma/seeds/create_updatedAt_trigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { PrismaClient } from '@prisma/client';
import { allModels } from '../models';

async function makeFunction(prisma) {
await prisma.$executeRaw`
CREATE EXTENSION IF NOT EXISTS moddatetime SCHEMA public;
`;
}

async function makeTrigger(prisma: PrismaClient, tableName: string) {
await prisma.$executeRawUnsafe(`
CREATE OR REPLACE TRIGGER update_updatedAt_trigger
BEFORE UPDATE ON public."${tableName}"
FOR EACH ROW
EXECUTE PROCEDURE moddatetime("updatedAt");
`);
}

export async function create_updatedAt(prisma: PrismaClient) {
await makeFunction(prisma);
await makeUpdatedAtTriggers(prisma)
}


async function makeUpdatedAtTriggers(prisma: PrismaClient) {
for (const entity of allModels) {
await makeTrigger(prisma, entity);
}
}
37 changes: 37 additions & 0 deletions libs/supabase/prisma/seeds/create_updatedBy_trigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { PrismaClient } from '@prisma/client';
import { allModels } from '../models';

async function makeFunction(prisma) {
await prisma.$executeRaw`
CREATE OR REPLACE FUNCTION public.update_updated_by()
RETURNS trigger AS $$
BEGIN
NEW."updatedById" := auth.uid();
RETURN NEW;
END;
$$
LANGUAGE plpgsql
SECURITY definer SET search_path = public;
`;
}

async function makeTrigger(prisma: PrismaClient, tableName: string) {
await prisma.$executeRawUnsafe(`
CREATE OR REPLACE TRIGGER update_updatedBy_trigger
BEFORE UPDATE OR INSERT ON public."${tableName}"
FOR EACH ROW
EXECUTE PROCEDURE public.update_updated_by();
`);
}

export async function create_updatedBy(prisma: PrismaClient) {
await makeFunction(prisma);
await makeUpdatedByTriggers(prisma)
}


async function makeUpdatedByTriggers(prisma: PrismaClient) {
for (const entity of allModels) {
await makeTrigger(prisma, entity);
}
}