Skip to content

Commit 8b54337

Browse files
committed
feat: update seeding.
1 parent 8a22908 commit 8b54337

File tree

11 files changed

+292
-23890
lines changed

11 files changed

+292
-23890
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Warnings:
3+
4+
- A unique constraint covering the columns `[name]` on the table `Skill` will be added. If there are existing duplicate values, this will fail.
5+
- A unique constraint covering the columns `[skillForUserId,projectId]` on the table `SkillForProject` will be added. If there are existing duplicate values, this will fail.
6+
- A unique constraint covering the columns `[userId,skillId]` on the table `SkillForUser` will be added. If there are existing duplicate values, this will fail.
7+
8+
*/
9+
-- CreateIndex
10+
CREATE UNIQUE INDEX "Skill_name_key" ON "Skill"("name");
11+
12+
-- CreateIndex
13+
CREATE UNIQUE INDEX "SkillForProject_skillForUserId_projectId_key" ON "SkillForProject"("skillForUserId", "projectId");
14+
15+
-- CreateIndex
16+
CREATE UNIQUE INDEX "SkillForUser_userId_skillId_key" ON "SkillForUser"("userId", "skillId");

prisma/schema.prisma

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ model VerificationToken {
7272
// Not limited to technical skills. Can be used for any skill, e.g. "Public Speaking", "Leadership"
7373
model Skill {
7474
id String @id @default(cuid())
75-
name String
75+
name String @unique // Ensures skill names are unique
7676
icon String? // Optional icon, e.g., from Iconify.
7777
SkillForUser SkillForUser[] // One-to-many relationship with SkillForUser
7878
}
@@ -91,6 +91,8 @@ model SkillForUser {
9191
yearStarted Int? // Optional start year of using the skill
9292
totalYears Int? // Optional total years using the skill
9393
SkillForProject SkillForProject[]
94+
95+
@@unique([userId, skillId]) // Ensures that a user cannot repeat the same skill
9496
}
9597

9698
// SkillForProject is a specific skill that a user has for a specific project, e.g. "HTML", "CSS", "JavaScript",
@@ -103,6 +105,8 @@ model SkillForProject {
103105
description String? // Optional meta about the skill for this project. Overrides the description in SkillForUser if found.
104106
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
105107
projectId String
108+
109+
@@unique([skillForUserId, projectId]) // Ensures that a user cannot repeat the same skill for the same project
106110
}
107111

108112
// Project is a project that a user has worked on, e.g. "Personal Portfolio", "Company Website", and is specific

prisma/seed/education.mjs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* eslint-disable no-console */
2+
3+
import { PrismaClient } from "@prisma/client";
4+
import { getTestUserIds } from "./helpers/users.mjs";
5+
6+
const prisma = new PrismaClient();
7+
8+
async function main() {
9+
const testUserIds = await getTestUserIds();
10+
11+
const skills = await prisma.skill.findMany({
12+
where: {
13+
OR: [
14+
{ name: { contains: "HTML", mode: "insensitive" } },
15+
{ name: { contains: "CSS", mode: "insensitive" } },
16+
{ name: { contains: "JavaScript", mode: "insensitive" } },
17+
],
18+
},
19+
});
20+
21+
for (const userId of testUserIds) {
22+
for (const skill of skills) {
23+
const existingSkillForUser = await prisma.skillForUser.findFirst({
24+
where: {
25+
userId,
26+
skillId: skill.id,
27+
},
28+
});
29+
30+
if (existingSkillForUser) {
31+
console.log(`Skill ${skill.name} already exists for user ${userId}`);
32+
continue;
33+
}
34+
35+
const createdSkill = await prisma.skillForUser.create({
36+
data: {
37+
skillId: skill.id,
38+
userId: userId,
39+
},
40+
});
41+
console.log(`Created skill ${skill.name} for user ${userId} with id: ${createdSkill.id}`);
42+
}
43+
}
44+
}
45+
46+
main()
47+
.catch((e) => {
48+
throw e;
49+
})
50+
.finally(async () => {
51+
await prisma.$disconnect();
52+
});

prisma/seed/helpers/users.mjs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { PrismaClient } from "@prisma/client";
2+
3+
const prisma = new PrismaClient();
4+
5+
export const testUserEmails = ["[email protected]", "[email protected]"];
6+
7+
export const testUsers = [
8+
{
9+
10+
name: "John Doe",
11+
slug: "john-doe",
12+
displayEmail: "[email protected]",
13+
location: "Los Angeles, CA",
14+
siteTitle: "John Doe's OpenResume",
15+
title: "Designer",
16+
},
17+
{
18+
19+
name: "Jane Doe",
20+
slug: "jane-doe",
21+
displayEmail: "[email protected]",
22+
location: "New York, NY",
23+
siteTitle: "Jane Doe's OpenResume",
24+
title: "Senior Software Engineer",
25+
},
26+
];
27+
28+
export const getTestUserIds = async () => {
29+
const testUserIds = [];
30+
31+
for (const email of testUserEmails) {
32+
const user = await prisma.user.findFirst({
33+
where: {
34+
email,
35+
},
36+
});
37+
if (user) {
38+
testUserIds.push(user.id);
39+
}
40+
}
41+
};

prisma/seed/seedSkills.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)