Skip to content

Commit

Permalink
Merge pull request #30 from qvantor/feature/components
Browse files Browse the repository at this point in the history
Feature/components
  • Loading branch information
qvantor authored Oct 10, 2023
2 parents 41a8f2a + 7dea89d commit cbb828b
Show file tree
Hide file tree
Showing 96 changed files with 4,698 additions and 257 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- CreateTable
CREATE TABLE "Component" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"userId" INTEGER NOT NULL,
"json" JSONB NOT NULL,
"previewHeight" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "Component_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Tag" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"userId" INTEGER NOT NULL,

CONSTRAINT "Tag_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "TagOnComponent" (
"tagId" INTEGER NOT NULL,
"componentId" INTEGER NOT NULL,
"assignedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "TagOnComponent_pkey" PRIMARY KEY ("tagId","componentId")
);

-- AddForeignKey
ALTER TABLE "Component" ADD CONSTRAINT "Component_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Tag" ADD CONSTRAINT "Tag_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TagOnComponent" ADD CONSTRAINT "TagOnComponent_tagId_fkey" FOREIGN KEY ("tagId") REFERENCES "Tag"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TagOnComponent" ADD CONSTRAINT "TagOnComponent_componentId_fkey" FOREIGN KEY ("componentId") REFERENCES "Component"("id") ON DELETE CASCADE ON UPDATE CASCADE;
32 changes: 32 additions & 0 deletions apps/backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ model User {
TemplateVersion TemplateVersion[]
role PRole @default(USER)
Provider Provider[]
Component Component[]
Tag Tag[]
}

model Group {
Expand All @@ -41,6 +43,26 @@ model Group {
userId Int
}

model Component {
id Int @id @default(autoincrement())
name String
creator User @relation(fields: [userId], references: [id])
userId Int
json Json
previewHeight Int
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
tags TagOnComponent[]
}

model Tag {
id Int @id @default(autoincrement())
name String
creator User @relation(fields: [userId], references: [id])
userId Int
components TagOnComponent[]
}

model Template {
id Int @id @default(autoincrement())
name String
Expand Down Expand Up @@ -76,6 +98,16 @@ model Provider {

// M-N Relations

model TagOnComponent {
tag Tag @relation(fields: [tagId], references: [id], onDelete: Cascade)
tagId Int
component Component @relation(fields: [componentId], references: [id], onDelete: Cascade)
componentId Int
assignedAt DateTime @default(now())
@@id([tagId, componentId])
}

model UserOnGroup {
group Group @relation(fields: [groupId], references: [id], onDelete: Cascade)
groupId Int
Expand Down
29 changes: 29 additions & 0 deletions apps/backend/prisma/seed/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { writeFileSync } from 'node:fs';
import { PrismaClient } from '@prisma/client';
import 'dotenv';

const prisma = new PrismaClient({});

const genSeed = async (dbRequest: Promise<unknown[]>, name: string) => {
const seed = await dbRequest;
writeFileSync(
`${__dirname}/seeds/${name}.ts`,
`export default ` + JSON.stringify(seed)
);
console.log(`seed with ${seed.length} ${name} generated in file ${name}.ts`);
};
const main = async () => {
await genSeed(
prisma.tag.findMany({ select: { id: true, name: true } }),
'tags'
);
await genSeed(prisma.component.findMany(), 'components');
await genSeed(
prisma.tagOnComponent.findMany({
select: { tagId: true, componentId: true },
}),
'tagOnComponent'
);
};

main();
45 changes: 45 additions & 0 deletions apps/backend/prisma/seed/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { PrismaClient } from '@prisma/client';
import { ADMIN_EMAILS } from '../../src/common/constants/env-const';
import TAGS from './seeds/tags';
import COMPONENTS from './seeds/components';
import TAG_ON_COMPONENTS from './seeds/tagOnComponent';

const admins = ADMIN_EMAILS.split(',');
const prisma = new PrismaClient();
const createTags = async (userId: number) => {
await prisma.tag.createMany({
data: TAGS.map((tag) => ({ ...tag, userId })),
});
console.log(`${TAGS.length} tags created`);
};

const createComponents = async (userId: number) => {
await prisma.component.createMany({
data: COMPONENTS.map((component) => ({
...component,
userId,
})),
});
console.log(`${COMPONENTS.length} components created`);
await prisma.tagOnComponent.createMany({ data: TAG_ON_COMPONENTS });
console.log(`${TAG_ON_COMPONENTS.length} tagOnComponent created`);
};

const main = async () => {
const admin =
(await prisma.user.findUnique({ where: { email: admins[0] } })) ??
(await prisma.user.create({
data: {
firstName: '',
lastName: '',
email: admins[0],
role: 'ADMIN',
},
}));
await createTags(admin.id);
await createComponents(admin.id);
};

main().then(() => {
console.log('DB seed success!');
});
Loading

0 comments on commit cbb828b

Please sign in to comment.