work-timer/prisma/seed.ts

121 lines
2.8 KiB
TypeScript
Raw Normal View History

import { PrismaClient } from '@prisma/client';
import bcrypt from 'bcryptjs';
2023-02-11 03:14:14 +01:00
const prisma = new PrismaClient();
2023-02-11 03:14:14 +01:00
async function seed() {
2023-06-22 13:11:03 +02:00
const email = 'nicola@nzambello.dev';
const adminEmail = 'admin@nzambello.dev';
2023-02-11 03:14:14 +01:00
// cleanup the existing database
await prisma.user.delete({ where: { email } }).catch(() => {
// no worries if it doesn't exist yet
});
2023-02-11 03:14:14 +01:00
2023-02-23 14:17:29 +01:00
// cleanup the existing database
await prisma.user.delete({ where: { email: adminEmail } }).catch(() => {
// no worries if it doesn't exist yet
});
const hashedPassword = await bcrypt.hash('rawmaterial', 10);
2023-02-11 03:14:14 +01:00
2023-02-23 14:17:29 +01:00
const admin = await prisma.user.create({
data: {
email: adminEmail,
admin: true,
password: {
create: {
hash: hashedPassword
}
}
}
});
2023-02-11 03:14:14 +01:00
const user = await prisma.user.create({
data: {
email,
password: {
create: {
hash: hashedPassword
}
}
}
});
2023-02-11 03:14:14 +01:00
const project = await prisma.project.create({
data: {
name: 'RawMaterial',
description:
'Raw Material is a web app for managing your projects and tasks.',
color: 'green',
2023-02-11 03:14:14 +01:00
userId: user.id
}
});
const otherProject = await prisma.project.create({
data: {
name: 'Memori',
description: 'Memori is a web app for managing your memories.',
color: 'violet',
userId: user.id
}
});
new Array(10).fill(0).forEach(async (_, index) => {
await prisma.project.create({
data: {
name: `Project ${index}`,
description: `Project ${index} description`,
color: 'red',
userId: user.id
}
});
});
2023-02-11 03:14:14 +01:00
await prisma.timeEntry.create({
data: {
description: 'Initial setup',
startTime: new Date('2021-01-01T09:00:00.000Z'),
endTime: new Date('2021-01-01T12:00:00.000Z'),
projectId: project.id,
userId: user.id
}
});
2023-02-11 03:14:14 +01:00
await prisma.timeEntry.create({
data: {
description: 'Database setup same day',
2023-02-11 03:14:14 +01:00
startTime: new Date('2021-01-01T13:00:00.000Z'),
endTime: new Date('2021-01-01T19:00:00.000Z'),
projectId: otherProject.id,
userId: user.id
}
});
await prisma.timeEntry.create({
data: {
description: 'Database setup next day',
startTime: new Date('2021-01-02T13:00:00.000Z'),
endTime: new Date('2021-01-02T19:00:00.000Z'),
projectId: otherProject.id,
userId: user.id
}
});
await prisma.timeEntry.create({
data: {
description: 'Ongoing activity',
startTime: new Date('2021-01-02T13:00:00.000Z'),
2023-02-11 03:14:14 +01:00
projectId: project.id,
userId: user.id
}
});
2023-02-11 03:14:14 +01:00
console.log(`Database has been seeded. 🌱`);
2023-02-11 03:14:14 +01:00
}
seed()
.catch((e) => {
console.error(e);
process.exit(1);
2023-02-11 03:14:14 +01:00
})
.finally(async () => {
await prisma.$disconnect();
});