-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser-utilities.ts
103 lines (91 loc) · 2.48 KB
/
user-utilities.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
import type { Prisma, PrismaClient } from "@prisma/client";
import argon2 from "argon2";
import { differenceInSeconds } from "date-fns";
const hashPasswords = async (userList: Prisma.UserCreateInput[]) => {
const userPromises = userList.map(async (user) => {
const hashedPassword = await argon2.hash(user.password);
return {
...user,
password: hashedPassword,
};
});
return await Promise.all(userPromises);
};
const admins: Prisma.UserCreateInput[] = [
{
id: "cle2wz3id0000fxpb8bgt5f3f",
name: "anhkha",
image:
"https://tliacbojiuhirqtqavdn.supabase.co/storage/v1/object/public/icons/avatars/047-turkey.svg",
password: "hunter2",
session: 0,
},
{
id: "cle2wz3id0002fxpb0digx7tv",
name: "magnus",
image:
"https://tliacbojiuhirqtqavdn.supabase.co/storage/v1/object/public/icons/avatars/048-vulture.svg",
password: "holtet",
session: 0,
},
{
id: "cle78jrxx000708lgglob19m9",
name: "hilde",
image:
"https://tliacbojiuhirqtqavdn.supabase.co/storage/v1/object/public/icons/avatars/049-bird.svg",
password: "haug",
session: 0,
},
{
id: "cle8i7e0g000108mpaawh9cw0",
name: "oskar",
image:
"https://tliacbojiuhirqtqavdn.supabase.co/storage/v1/object/public/icons/avatars/049-bird.svg",
password: "olsen",
session: 0,
},
{
id: "cleidrxes000008lf5b55f45t",
name: "synne",
image:
"https://tliacbojiuhirqtqavdn.supabase.co/storage/v1/object/public/icons/avatars/049-bird.svg",
password: "markmanrud",
session: 0,
},
{
id: "clf6jygmz000008jn7l43hafx",
name: "pilot",
image:
"https://tliacbojiuhirqtqavdn.supabase.co/storage/v1/object/public/icons/avatars/046-toucan.svg",
password: "fly-tur",
session: 0,
},
];
interface CreateUsersOptions {
prismaClient: PrismaClient;
users: Prisma.UserCreateInput[];
withAdmins?: boolean;
}
export async function createUsers({
prismaClient,
users,
withAdmins,
}: CreateUsersOptions) {
const timer = new Date();
console.info(`\n🎓 Seeding users ${withAdmins ? "and admins " : ""}!`);
if (withAdmins) {
users.push(...admins);
}
const output = await hashPasswords(users);
const data = await prismaClient.$transaction(
output.map((user) => {
return prismaClient.user.upsert({
where: { name: user.name },
create: user,
update: user,
});
})
);
console.log(`Took ${differenceInSeconds(new Date(), timer)}s`);
return data;
}