Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea3f711

Browse files
committed
Add new seeder and ranking from cli
Signed-off-by: Pascal Syma <[email protected]>
1 parent 9a1017f commit ea3f711

File tree

2 files changed

+222
-5
lines changed

2 files changed

+222
-5
lines changed

prisma/genRanking.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
const {PrismaClient} = require('@prisma/client')
2+
const prisma = new PrismaClient()
3+
4+
async function main() {
5+
6+
// calculate overall ratings
7+
const cityOverallRating = (await prisma.comment.groupBy({
8+
by: ['cityId'],
9+
_count: {
10+
_all: true
11+
},
12+
_sum: {
13+
rating: true
14+
}
15+
})).map(c => {
16+
return {
17+
cityId: c.cityId,
18+
value: c._sum.rating / c._count._all,
19+
basedOn: c._count._all
20+
}
21+
});
22+
23+
const cityRating = (await prisma.commentDisablilty.findMany({
24+
select: {
25+
comment: {
26+
select: {
27+
cityId: true
28+
}
29+
},
30+
rating: true,
31+
disabilityId: true
32+
}
33+
}));
34+
const ratings = []
35+
// group by disability, group by city, sum/count ratings
36+
cityRating.forEach(r => {
37+
const {rating, disabilityId} = r
38+
const cityId = r.comment.cityId
39+
40+
const dIndex = ratings.findIndex(p => p.disabilityId === disabilityId)
41+
if (dIndex === -1) {
42+
ratings.push({
43+
disabilityId,
44+
cities: [{
45+
cityId,
46+
sum: rating,
47+
count: 1
48+
}]
49+
})
50+
return
51+
}
52+
53+
const cIndex = ratings[dIndex].cities.findIndex(p => p.cityId === cityId)
54+
if (cIndex === -1) {
55+
ratings[dIndex].cities.push({
56+
cityId,
57+
sum: rating,
58+
count: 1
59+
})
60+
return
61+
}
62+
ratings[dIndex].cities[cIndex].sum += rating
63+
ratings[dIndex].cities[cIndex].count += 1
64+
})
65+
66+
ratings.forEach(r => {
67+
r.cities.forEach(c => {
68+
c.rating = c.sum / c.count
69+
})
70+
})
71+
72+
const creates = ratings.map(d => d.cities.map(c => {
73+
return {
74+
disabilityId: d.disabilityId,
75+
cityId: c.cityId,
76+
value: c.rating,
77+
basedOn: c.count
78+
}
79+
})).flat(1)
80+
81+
const result = await prisma.$transaction([
82+
prisma.ranking.deleteMany(),
83+
// prisma.$queryRaw`ALTER TABLE Ranking AUTO_INCREMENT = 1`,
84+
prisma.ranking.createMany({
85+
data: creates
86+
}),
87+
prisma.overallRanking.deleteMany(),
88+
prisma.overallRanking.createMany({
89+
data: cityOverallRating
90+
})
91+
])
92+
93+
console.log(result)
94+
}
95+
96+
main()
97+
.catch((e) => {
98+
console.error(e)
99+
process.exit(1)
100+
})
101+
.finally(async () => {
102+
await prisma.$disconnect()
103+
})

prisma/seed2.js

Lines changed: 119 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,60 @@
11
const {PrismaClient} = require('@prisma/client')
22
const prisma = new PrismaClient()
33

4+
const disabilityCount = 6
5+
const titles = [
6+
'Interesting City',
7+
'Special needs? More like special gots!',
8+
'Horrible',
9+
'Weird',
10+
'Its okay'
11+
]
12+
13+
const messages = [
14+
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.',
15+
'Eros adversarium vel ut. Ius oporteat dignissim ex, quo et constituam dissentiunt.',
16+
'Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates',
17+
'According to all known laws of aviation, there is no way a bee should be able to fly.\n' +
18+
'Its wings are too small to get its fat little body off the ground.\n' +
19+
'The bee, of course, flies anyway because bees don\'t care what humans think is impossible.\n' +
20+
'Yellow, black. Yellow, black. Yellow, black. Yellow, black.',
21+
'No, I\'m not going.\n' +
22+
'Everybody knows, sting someone, you die.\n' +
23+
'Don\'t waste it on a squirrel.\n' +
24+
'Such a hothead.',
25+
'Well, hello.\n' +
26+
'Ken!\n' +
27+
'Hello.\n' +
28+
'I didn\'t think you were coming.\n' +
29+
'No, I was just late I tried to call, but... the battery.\n' +
30+
'I didn\'t want all this to go to waste,\n' +
31+
'so I called Barry. Luckily, he was free.\n' +
32+
'Oh, that was lucky.',
33+
'Remember what Van said, why is your life more valuable than mine?\n' +
34+
'Funny, I just can\'t seem to recall that! I think something stinks in here!\n' +
35+
'I love the smell of flowers.\n' +
36+
'How do you like the smell of flames?!'
37+
]
438

539
const rand = (min = 1, max = 5) => Math.floor(Math.random() * (max - min + 1) + min)
640

7-
const comments = [...Array(100).keys()].map(i => {
8-
return {public: true, title: 'test', message: 'm', rating: rand(), authorId: 1, cityId: rand(1, 10000)}
9-
})
41+
const comments = [...Array(100000).keys()].map(() => ({
42+
public: true,
43+
title: titles[rand(0, titles.length - 1)],
44+
message: messages[rand(0, messages.length - 1)],
45+
rating: rand(),
46+
authorId: rand(1, 4),
47+
cityId: rand(1, 10000)
48+
}))
49+
50+
const commentDisabilities = comments.map((_, ii) => ([...Array(rand(1, disabilityCount)).keys()].map(i => ({
51+
disabilityId: rand(1, disabilityCount),
52+
commentId: ii + 1,
53+
rating: rand()
54+
})))).flat()
55+
56+
const commentDisabilitiesUniq = [...new Map(commentDisabilities.map(item => [`${item.commentId}-${item.disabilityId}`, item])).values()]
57+
1058

1159
async function main() {
1260
const result = await prisma.$transaction([
@@ -19,13 +67,79 @@ async function main() {
1967
2068
// secrettt
2169
password: '$argon2i$v=19$m=4096,t=3,p=1$ysfmBmKdGRIobckCuBWzWA$tftAYhZhhOwISlSGXS1wXBA7RE4qETEkC/LJBGsk0fc',
22-
role: 'ADMIN'
70+
role: 'ADMIN',
71+
disabilitys: {
72+
createMany: {
73+
data: [
74+
{disabilityId: 3, createdAt: new Date(), verified: true},
75+
{disabilityId: 5, createdAt: new Date(), verified: true},
76+
{disabilityId: 6, createdAt: new Date(), verified: false},
77+
]
78+
}
79+
}
80+
}
81+
}),
82+
prisma.user.create({
83+
data: {
84+
name: 'Pascal',
85+
86+
// secrettt
87+
password: '$argon2i$v=19$m=4096,t=3,p=1$ysfmBmKdGRIobckCuBWzWA$tftAYhZhhOwISlSGXS1wXBA7RE4qETEkC/LJBGsk0fc',
88+
role: 'ADMIN',
89+
disabilitys: {
90+
createMany: {
91+
data: [
92+
{disabilityId: 2, createdAt: new Date(), verified: true},
93+
{disabilityId: 5, createdAt: new Date(), verified: true},
94+
{disabilityId: 6, createdAt: new Date(), verified: false},
95+
]
96+
}
97+
}
98+
}
99+
}),
100+
prisma.user.create({
101+
data: {
102+
name: 'Adrian',
103+
104+
// secrettt
105+
password: '$argon2i$v=19$m=4096,t=3,p=1$ysfmBmKdGRIobckCuBWzWA$tftAYhZhhOwISlSGXS1wXBA7RE4qETEkC/LJBGsk0fc',
106+
role: 'USER',
107+
disabilitys: {
108+
createMany: {
109+
data: [
110+
{disabilityId: 1, createdAt: new Date(), verified: true},
111+
{disabilityId: 2, createdAt: new Date(), verified: true},
112+
{disabilityId: 4, createdAt: new Date(), verified: false},
113+
]
114+
}
115+
}
116+
}
117+
}),
118+
prisma.user.create({
119+
data: {
120+
name: 'John',
121+
122+
// secrettt
123+
password: '$argon2i$v=19$m=4096,t=3,p=1$ysfmBmKdGRIobckCuBWzWA$tftAYhZhhOwISlSGXS1wXBA7RE4qETEkC/LJBGsk0fc',
124+
role: 'USER',
125+
disabilitys: {
126+
createMany: {
127+
data: [
128+
{disabilityId: 1, createdAt: new Date(), verified: true},
129+
{disabilityId: 2, createdAt: new Date(), verified: true},
130+
{disabilityId: 3, createdAt: new Date(), verified: true},
131+
{disabilityId: 4, createdAt: new Date(), verified: true},
132+
{disabilityId: 6, createdAt: new Date(), verified: true},
133+
]
134+
}
135+
}
23136
}
24137
}),
25138
prisma.comment.deleteMany({}),
26139
prisma.$queryRaw`ALTER TABLE Comment
27140
AUTO_INCREMENT = 1`,
28-
prisma.comment.createMany({data: comments})
141+
prisma.comment.createMany({data: comments}),
142+
prisma.commentDisablilty.createMany({data: commentDisabilitiesUniq})
29143
])
30144

31145
console.log(result)

0 commit comments

Comments
 (0)