From eb580f72a4f25bd1bae8f5459ebadfad02c20f91 Mon Sep 17 00:00:00 2001 From: Michael Brusegard <56915010+michaelbrusegard@users.noreply.github.com> Date: Sun, 10 Nov 2024 14:56:24 +0100 Subject: [PATCH] feat: add skills to seeding --- src/server/db/seed.ts | 67 ++++++++++++++++++++++++++++++++-- src/server/db/tables/skills.ts | 4 ++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/server/db/seed.ts b/src/server/db/seed.ts index 854a063..02d1ce1 100644 --- a/src/server/db/seed.ts +++ b/src/server/db/seed.ts @@ -1,4 +1,12 @@ -import { type InsertUser, locales, users } from '@/server/db/tables'; +import { + type InsertSkill, + type InsertUser, + type InsertUserSkill, + locales, + skills, + users, + usersSkills, +} from '@/server/db/tables'; import { fakerEN, fakerNB_NO, fakerSV } from '@faker-js/faker'; import { routing } from '@/lib/locale'; @@ -24,14 +32,67 @@ const insertedLocales = await db .returning(); console.log('Locales inserted:', insertedLocales); +console.log('Inserting user...'); const user: InsertUser = { name: 'Frank Sinatra', username: 'fransin', passwordHash: await hashPassword('Password1!'), }; -console.log('Inserting user...'); -await db.insert(users).values(user); +const insertedUser = await db.insert(users).values(user).returning(); console.log('User inserted'); +console.log('Inserting skills...'); +const skillsdata: InsertSkill[] = [ + { + identifier: 'printing', + }, + { + identifier: 'unix', + }, + { + identifier: 'raspberry', + }, + { + identifier: 'laser', + }, + { + identifier: 'arduino', + }, + { + identifier: 'souldering', + }, + { + identifier: 'workshop', + }, +]; +const insertedSkills = await db.insert(skills).values(skillsdata).returning(); +console.log('Skills inserted'); + +if (insertedUser.length === 0 || insertedSkills.length < 5) { + console.error('Error: Inserted user or skills data is incomplete.'); + process.exit(1); +} + +console.log('Inserting userskills...'); +const usersSkillsData: InsertUserSkill[] = [ + { + userId: insertedUser[0]?.id ?? 0, + skillId: insertedSkills[0]?.id ?? 0, + }, + { + userId: insertedUser[0]?.id ?? 0, + skillId: insertedSkills[1]?.id ?? 0, + }, + { + userId: insertedUser[0]?.id ?? 0, + skillId: insertedSkills[2]?.id ?? 0, + }, + { + userId: insertedUser[0]?.id ?? 0, + skillId: insertedSkills[4]?.id ?? 0, + }, +]; +await db.insert(usersSkills).values(usersSkillsData); + process.exit(); diff --git a/src/server/db/tables/skills.ts b/src/server/db/tables/skills.ts index dcac406..86df66e 100644 --- a/src/server/db/tables/skills.ts +++ b/src/server/db/tables/skills.ts @@ -46,6 +46,8 @@ const usersSkillsRelations = relations(usersSkills, ({ one }) => ({ type SelectSkill = InferSelectModel; type InsertSkill = InferInsertModel; +type SelectUserSkill = InferSelectModel; +type InsertUserSkill = InferInsertModel; export { skills, @@ -54,4 +56,6 @@ export { usersSkillsRelations, type SelectSkill, type InsertSkill, + type SelectUserSkill, + type InsertUserSkill, };