-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from qvantor/feature/components
Feature/components
- Loading branch information
Showing
96 changed files
with
4,698 additions
and
257 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
apps/backend/prisma/migrations/20231009083704_components/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
}); |
Oops, something went wrong.