-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseed.ts
140 lines (127 loc) · 4.04 KB
/
seed.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { faker } from '@faker-js/faker'
import { createId } from '@paralleldrive/cuid2'
import { PrismaClient, type User } from '@prisma/client'
import { CATEGORIES } from '../src/lib/constants'
import {
ARCTIC_GOODS,
ENBESA_GOODS,
NEW_WORLD_GOODS,
OLD_WORLD_GOODS,
REGIONS_1800,
} from '../src/lib/constants/1800/data'
const prisma = new PrismaClient()
const categories = Object.values(CATEGORIES)
const region = Object.values(REGIONS_1800)
const goods = [OLD_WORLD_GOODS, NEW_WORLD_GOODS, ENBESA_GOODS, ARCTIC_GOODS]
export const generateUserData = (
length: number,
): Omit<User, 'emailVerified' | 'image'>[] =>
Array.from({ length }, () => {
const firstName = faker.person.firstName()
const lastName = faker.person.lastName()
const username = faker.internet
.username({ firstName, lastName })
.replace(/\./g, '_')
return {
biography: faker.person.bio(),
email: faker.internet.email({
firstName,
lastName,
provider: 'example.fakerjs.dev',
}),
id: createId(),
name: faker.person.fullName({ firstName, lastName }),
username,
usernameURL: username.toLowerCase(),
}
})
export const generateStampData = (
length: number,
users: Omit<User, 'emailVerified' | 'image'>[],
) =>
Array.from({ length: length }, (_, index) => {
const categoryIdx = Math.floor(Math.random() * categories.length)
const rndCategory = categories[categoryIdx]
const regionIdx = Math.floor(Math.random() * region.length)
const rndRegion = region[regionIdx]
const getGood = () => {
const goodsInRegion = goods[regionIdx]
const goodIdx = Math.floor(Math.random() * goodsInRegion.length)
const rndGood = goodsInRegion[goodIdx]
return rndGood.toLowerCase()
}
const getCapital = () => {
if (rndCategory === 'island' && rndRegion === 'new world') {
return 'manola'
}
return null
}
const timestamp = faker.date.past()
return {
capital: getCapital(),
category: rndCategory,
changedAt: timestamp,
createdAt: timestamp,
downloads: faker.number.int({ max: 1000000, min: 0 }),
game: '1800',
good: rndCategory === 'production' ? getGood() : null,
id: createId(),
markdownDescription: `<h2> Stamp-${index} </h2>`,
modded: index % 20 === 0 ? true : false,
region: rndRegion,
stampFileUrl: 'http://localhost:3000/test-stamp.zip',
title: `Stamp-${index}-${faker.lorem.words({ max: 12, min: 0 })}`,
unsafeDescription: `## Stamp-${index}`,
updatedAt: timestamp,
userId: users[index % users.length].id,
}
})
export const generatePlaceHoldImages = () => {
return [1, 2].map((image) => {
const id = createId()
return {
id,
largeUrl: `https://placehold.co/1024x576.png?text=Large${image}\\n${id}`,
mediumUrl: `https://placehold.co/750x421.png?text=Medium${image}\\n${id}`,
originalUrl: `https://placehold.co/2000x2000.png?text=Original${image}\\n${id}`,
smallUrl: `https://placehold.co/500x281.png?text=Small${image}\\n${id}`,
thumbnailUrl: `https://placehold.co/250x250.png?text=Thumbnail${image}\\n${id}`,
}
})
}
async function seed() {
console.time('Database seed time elapsed')
const userData = generateUserData(100)
const stampData = generateStampData(1000, userData)
await prisma.user.createMany({ data: userData })
await prisma.stamp.createMany({
data: stampData,
})
for (const stamp of stampData) {
const selectUser = userData[Math.floor(Math.random() * userData.length)]
await prisma.stamp.update({
data: {
images: {
createMany: {
data: generatePlaceHoldImages(),
},
},
likedBy: {
connect: { id: selectUser.id },
},
},
where: {
id: stamp.id,
},
})
}
console.timeEnd('Database seed time elapsed')
console.log('Seed completed successfully')
}
seed()
.catch((error) => {
console.error(error)
})
.finally(async () => {
await prisma.$disconnect()
})