-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathseed.ts
58 lines (50 loc) · 1.35 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
import "dotenv/config";
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import { users } from "./src/db/schema";
import bcrypt from "bcrypt";
import { v4 as uuidv4 } from "uuid";
const SALT_ROUNDS = 10;
// Create MySQL Connection Pool
const pool = mysql.createPool({
uri: process.env.DATABASE_URL!,
connectionLimit: 10,
});
const db = drizzle(pool);
async function main() {
try {
console.log("Seeding database...");
// Hash passwords
const hashedPassword1 = await bcrypt.hash("123", SALT_ROUNDS);
const hashedPassword2 = await bcrypt.hash("456", SALT_ROUNDS);
// Insert users with UUIDs
await db.insert(users).values([
{
id: uuidv4(),
username: "abc",
passwordHash: hashedPassword1,
title: "Mr",
firstName: "John",
lastName: "Doe",
admin: true,
},
{
id: uuidv4(),
username: "def",
passwordHash: hashedPassword2,
title: "Ms",
firstName: "Jane",
lastName: "Smith",
admin: false,
},
]);
console.log("Database seeding completed successfully.");
} catch (error) {
console.error("Error seeding database:", error);
} finally {
await pool.end();
}
}
main().catch(console.error);