Files
link-free/prisma/seed.ts

119 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-12-13 15:34:37 -03:00
import { PrismaClient } from '@prisma/client'
2021-12-15 14:05:27 -03:00
import { hashSync } from 'bcryptjs'
2021-12-18 00:50:54 -03:00
import cuid from 'cuid'
2021-12-15 14:05:27 -03:00
import dotenv from 'dotenv'
2021-12-13 15:34:37 -03:00
const prisma = new PrismaClient()
2021-12-15 14:05:27 -03:00
dotenv.config({
path: './.env'
})
2021-12-13 15:34:37 -03:00
async function main() {
const data = {
settings: {
avatar: '/avatar.jpeg',
name: 'John Doe',
description:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec euismod, nisl eget aliquam consectetur, nisl nunc aliquet nunc, euismod aliquam nunc nisl euismod.',
2021-12-19 20:03:59 -03:00
font: 'oswald',
2021-12-18 22:41:05 -03:00
buttonBorderRadius: '4',
2021-12-13 15:34:37 -03:00
colors: {
2021-12-18 22:41:05 -03:00
texts: '#e11d48',
icons: '#e11d48',
background: '#f8fafc',
buttonLabelColor: '#f8fafc',
buttonBackgroundColor: '#e11d48',
buttonBorderColor: '#e11d48'
2021-12-13 15:34:37 -03:00
}
},
socialLinks: [
{
label: 'Email',
href: '1'
},
{
label: 'GitHub',
href: '2'
},
{
label: 'LinkedIn',
href: '3'
},
{
label: 'Dev',
href: '4'
},
{
label: 'Instagram',
href: '5'
},
{
label: 'WhatsApp',
href: '6'
}
],
buttonLinks: [
{
2021-12-18 00:50:54 -03:00
id: cuid(),
2021-12-13 15:34:37 -03:00
label: 'Home',
href: '#'
},
{
2021-12-18 00:50:54 -03:00
id: cuid(),
2021-12-13 15:34:37 -03:00
label: 'About',
2021-12-18 00:50:54 -03:00
href: '#'
2021-12-13 15:34:37 -03:00
},
{
2021-12-18 00:50:54 -03:00
id: cuid(),
2021-12-13 15:34:37 -03:00
label: 'Contact',
2021-12-18 00:50:54 -03:00
href: '#'
2021-12-13 15:34:37 -03:00
}
]
}
2021-12-15 08:11:58 -03:00
const hasData = await prisma.data.findUnique({
2021-12-15 14:05:27 -03:00
where: { id: 1 }
2021-12-15 08:11:58 -03:00
})
2021-12-13 15:34:37 -03:00
if (!hasData) {
console.log(`Start seeding...`)
await prisma.data.create({
2021-12-15 08:11:58 -03:00
data: {
id: 1,
data: JSON.stringify(data)
}
2021-12-13 15:34:37 -03:00
})
}
2021-12-15 14:05:27 -03:00
const hasUser = await prisma.user.findFirst()
if (!hasUser) {
console.log(`Start seeding user...`)
const { USERNAME, PASSWORD } = process.env
if (!USERNAME || !PASSWORD) {
throw new Error('USERNAME and PASSWORD env variables are required')
}
await prisma.user.create({
data: {
username: USERNAME,
password: hashSync(PASSWORD, 10),
role: 'ADMIN'
}
})
}
2021-12-13 15:34:37 -03:00
}
main()
.catch(e => {
console.error(e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})