Skip to content

Commit

Permalink
feat: add skills to seeding
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbrusegard committed Nov 10, 2024
1 parent 0999ed1 commit eb580f7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
67 changes: 64 additions & 3 deletions src/server/db/seed.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
4 changes: 4 additions & 0 deletions src/server/db/tables/skills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const usersSkillsRelations = relations(usersSkills, ({ one }) => ({

type SelectSkill = InferSelectModel<typeof skills>;
type InsertSkill = InferInsertModel<typeof skills>;
type SelectUserSkill = InferSelectModel<typeof usersSkills>;
type InsertUserSkill = InferInsertModel<typeof usersSkills>;

export {
skills,
Expand All @@ -54,4 +56,6 @@ export {
usersSkillsRelations,
type SelectSkill,
type InsertSkill,
type SelectUserSkill,
type InsertUserSkill,
};

0 comments on commit eb580f7

Please sign in to comment.