Skip to content

Commit

Permalink
feat: use database for data
Browse files Browse the repository at this point in the history
  • Loading branch information
Rei-x committed Feb 14, 2023
1 parent 7f48368 commit 6d28695
Show file tree
Hide file tree
Showing 22 changed files with 1,167 additions and 157 deletions.
731 changes: 731 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
"dev": "cross-env FORCE_COLOR=1 npm-run-all -l -p dev:*",
"dev:app": "wait-on tcp:5432 && next dev",
"dev:db": "docker compose -f docker-compose.dev.yml up --force-recreate -V ",
"db:seed": "prisma db seed",
"postinstall": "prisma generate",
"lint": "next lint",
"format:check": "prettier --check src",
"format:fix": "prettier --write src",
"start": "next start",
"prepare": "husky install"
},
"prisma": {
"seed": "tsx prisma/seed.ts"
},
"dependencies": {
"@chakra-ui/icons": "^2.0.17",
"@chakra-ui/react": "^2.4.9",
Expand Down Expand Up @@ -62,6 +66,7 @@
"prettier": "^2.8.4",
"prisma": "^4.9.0",
"semantic-release": "^19.0.5",
"tsx": "^3.12.3",
"typescript": "^4.9.4",
"wait-on": "^7.0.1"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
Warnings:
- You are about to drop the column `contact_link` on the `ContactMethods` table. All the data in the column will be lost.
- You are about to drop the column `contact_type` on the `ContactMethods` table. All the data in the column will be lost.
- You are about to drop the column `organization_id` on the `ContactMethods` table. All the data in the column will be lost.
- You are about to drop the column `email_address` on the `Managers` table. All the data in the column will be lost.
- You are about to drop the column `organization_id` on the `Managers` table. All the data in the column will be lost.
- You are about to drop the column `foundation_date` on the `Organization` table. All the data in the column will be lost.
- You are about to drop the column `long_description` on the `Organization` table. All the data in the column will be lost.
- You are about to drop the column `number_of_users` on the `Organization` table. All the data in the column will be lost.
- You are about to drop the column `owner_id` on the `Organization` table. All the data in the column will be lost.
- You are about to drop the column `organization_id` on the `Projects` table. All the data in the column will be lost.
- A unique constraint covering the columns `[slug]` on the table `Organization` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[ownerId]` on the table `Organization` will be added. If there are existing duplicate values, this will fail.
- Added the required column `contactLink` to the `ContactMethods` table without a default value. This is not possible if the table is not empty.
- Added the required column `contactType` to the `ContactMethods` table without a default value. This is not possible if the table is not empty.
- Added the required column `organizationId` to the `ContactMethods` table without a default value. This is not possible if the table is not empty.
- Added the required column `organizationId` to the `Managers` table without a default value. This is not possible if the table is not empty.
- Added the required column `foundationDate` to the `Organization` table without a default value. This is not possible if the table is not empty.
- Added the required column `longDescription` to the `Organization` table without a default value. This is not possible if the table is not empty.
- Added the required column `numberOfUsers` to the `Organization` table without a default value. This is not possible if the table is not empty.
- Added the required column `ownerId` to the `Organization` table without a default value. This is not possible if the table is not empty.
- Added the required column `slug` to the `Organization` table without a default value. This is not possible if the table is not empty.
- Added the required column `organizationId` to the `Projects` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE "ContactMethods" DROP CONSTRAINT "ContactMethods_organization_id_fkey";

-- DropForeignKey
ALTER TABLE "Managers" DROP CONSTRAINT "Managers_organization_id_fkey";

-- DropForeignKey
ALTER TABLE "Organization" DROP CONSTRAINT "Organization_owner_id_fkey";

-- DropForeignKey
ALTER TABLE "Projects" DROP CONSTRAINT "Projects_organization_id_fkey";

-- DropIndex
DROP INDEX "Organization_owner_id_key";

-- AlterTable
ALTER TABLE "ContactMethods" DROP COLUMN "contact_link",
DROP COLUMN "contact_type",
DROP COLUMN "organization_id",
ADD COLUMN "contactLink" TEXT NOT NULL,
ADD COLUMN "contactType" TEXT NOT NULL,
ADD COLUMN "organizationId" TEXT NOT NULL;

-- AlterTable
ALTER TABLE "Managers" DROP COLUMN "email_address",
DROP COLUMN "organization_id",
ADD COLUMN "email" TEXT,
ADD COLUMN "organizationId" TEXT NOT NULL;

-- AlterTable
ALTER TABLE "Organization" DROP COLUMN "foundation_date",
DROP COLUMN "long_description",
DROP COLUMN "number_of_users",
DROP COLUMN "owner_id",
ADD COLUMN "foundationDate" TIMESTAMP(3) NOT NULL,
ADD COLUMN "longDescription" TEXT NOT NULL,
ADD COLUMN "numberOfUsers" INTEGER NOT NULL,
ADD COLUMN "ownerId" TEXT NOT NULL,
ADD COLUMN "slug" TEXT NOT NULL;

-- AlterTable
ALTER TABLE "Projects" DROP COLUMN "organization_id",
ADD COLUMN "organizationId" TEXT NOT NULL;

-- CreateTable
CREATE TABLE "Tag" (
"id" TEXT NOT NULL,
"text" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "_OrganizationToTag" (
"A" TEXT NOT NULL,
"B" TEXT NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "Tag_text_key" ON "Tag"("text");

-- CreateIndex
CREATE UNIQUE INDEX "_OrganizationToTag_AB_unique" ON "_OrganizationToTag"("A", "B");

-- CreateIndex
CREATE INDEX "_OrganizationToTag_B_index" ON "_OrganizationToTag"("B");

-- CreateIndex
CREATE UNIQUE INDEX "Organization_slug_key" ON "Organization"("slug");

-- CreateIndex
CREATE UNIQUE INDEX "Organization_ownerId_key" ON "Organization"("ownerId");

-- AddForeignKey
ALTER TABLE "ContactMethods" ADD CONSTRAINT "ContactMethods_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Projects" ADD CONSTRAINT "Projects_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Managers" ADD CONSTRAINT "Managers_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Organization" ADD CONSTRAINT "Organization_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_OrganizationToTag" ADD CONSTRAINT "_OrganizationToTag_A_fkey" FOREIGN KEY ("A") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_OrganizationToTag" ADD CONSTRAINT "_OrganizationToTag_B_fkey" FOREIGN KEY ("B") REFERENCES "Tag"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- DropForeignKey
ALTER TABLE "Managers" DROP CONSTRAINT "Managers_organizationId_fkey";

-- AddForeignKey
ALTER TABLE "Managers" ADD CONSTRAINT "Managers_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
14 changes: 14 additions & 0 deletions prisma/models/contactMethod.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { faker } from "@faker-js/faker/locale/pl";
import type { Prisma } from "@prisma/client";

const contactTypes = ["Facebook", "Instagram", "Website", "Email"];

export const contactMethodFactory = (
props?: Partial<Prisma.ContactMethodsCreateInput>
): Omit<Prisma.ContactMethodsCreateInput, "organization"> => {
return {
contactLink: faker.internet.url(),
contactType: faker.helpers.arrayElement(contactTypes),
...props,
};
};
15 changes: 15 additions & 0 deletions prisma/models/manager.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { faker } from "@faker-js/faker/locale/pl";
import type { Prisma } from "@prisma/client";

export const managerFactory = (
props?: Partial<Prisma.ManagersCreateInput>
): Omit<Prisma.ManagersCreateInput, "organization"> => {
const firstName = faker.name.firstName();
const lastName = faker.name.lastName();

return {
name: firstName + " " + lastName,
email: faker.internet.email(firstName, lastName),
...props,
};
};
71 changes: 71 additions & 0 deletions prisma/models/organization.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { faker } from "@faker-js/faker/locale/pl";
import type { Prisma } from "@prisma/client";
import slugify from "slugify";
import { contactMethodFactory } from "./contactMethod.factory";
import { managerFactory } from "./manager.factory";
import { projectFactory } from "./project.factory";
import { tagFactory } from "./tag.factory";
import { userFactory } from "./user.factory";

const departments = [
"W1",
"W2",
"W3",
"W4",
"W5",
"W6",
"W7",
"W8",
"W9",
"W10",
"W11",
"W12",
];

export const organizationFactory = (
props?: Partial<Prisma.OrganizationCreateInput>
): Prisma.OrganizationCreateInput => {
const name = faker.company.name();

const tags = Array.from({
length: faker.datatype.number({ min: 1, max: 6 }),
}).map(() => tagFactory());

return {
name,
description: faker.commerce.productDescription(),
longDescription: faker.lorem.paragraphs(9),
createdAt: faker.date.past(),
slug: slugify(name) + faker.random.numeric(4),
numberOfUsers: faker.datatype.number(100),
foundationDate: faker.date.past(),
residence: faker.helpers.arrayElement(departments),
owner: {
create: userFactory({ role: "OWNER" }),
},
Tags: {
connectOrCreate: tags.map((tag) => ({
where: {
text: tag.text,
},
create: tag,
})),
},
ContactMethods: {
create: Array.from({ length: 3 }).map(() => contactMethodFactory()),
},
Managers: {
createMany: {
data: Array.from({ length: 3 }).map(() => managerFactory()),
},
},
Projects: {
createMany: {
data: Array.from({
length: faker.datatype.number({ min: 0, max: 4 }),
}).map(() => projectFactory()),
},
},
...props,
};
};
12 changes: 12 additions & 0 deletions prisma/models/project.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { faker } from "@faker-js/faker/locale/pl";
import type { Prisma } from "@prisma/client";

export const projectFactory = (
props?: Partial<Prisma.ProjectsCreateInput>
): Omit<Prisma.ProjectsCreateInput, "organization"> => {
return {
name: faker.company.name(),
description: faker.commerce.productDescription(),
...props,
};
};
22 changes: 22 additions & 0 deletions prisma/models/tag.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { faker } from "@faker-js/faker/locale/pl";
import type { Prisma } from "@prisma/client";

const tags = [
"Druk3D",
"Piwo",
"Fotografia",
"Programowanie",
"Muzyka",
"Taniec",
"Kultura",
"Sport",
];

export const tagFactory = (
props?: Partial<Prisma.TagCreateInput>
): Prisma.TagCreateInput => {
return {
text: faker.helpers.arrayElement(tags),
...props,
};
};
16 changes: 16 additions & 0 deletions prisma/models/user.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { faker } from "@faker-js/faker/locale/pl";
import type { Prisma } from "@prisma/client";

export const userFactory = (
props?: Partial<Prisma.UserCreateInput>
): Prisma.UserCreateInput => {
const firstName = faker.name.firstName();
const lastName = faker.name.lastName();

return {
name: firstName + " " + lastName,
email: faker.internet.email(firstName, lastName),
role: "USER",
...props,
};
};
Loading

0 comments on commit 6d28695

Please sign in to comment.