-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.js
More file actions
57 lines (52 loc) · 1.44 KB
/
Copy pathseed.js
File metadata and controls
57 lines (52 loc) · 1.44 KB
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
import sequelize from './src/db.js';
import User from './src/models/user.model.js';
import bcrypt from 'bcrypt';
const seedDatabase = async () => {
try {
await sequelize.authenticate();
console.log('Connected to database.');
const users = [
{
name: 'Maria Garcia',
email: 'maria@example.com',
password: 'password123',
phone: '555-0001'
},
{
name: 'Carlos Rodriguez',
email: 'carlos@example.com',
password: 'password123',
phone: '555-0002'
},
{
name: 'Ana Martinez',
email: 'ana@example.com',
password: 'password123',
phone: '555-0003'
}
];
for (const user of users) {
// Hash password
const hashedPassword = await bcrypt.hash(user.password, 10);
try {
await User.create({
...user,
password: hashedPassword
});
console.log(`User ${user.name} created.`);
} catch (error) {
if (error.name === 'SequelizeUniqueConstraintError') {
console.log(`User ${user.name} already exists.`);
} else {
console.error(`Error creating ${user.name}:`, error);
}
}
}
console.log('Seeding completed.');
process.exit(0);
} catch (error) {
console.error('Unable to connect to the database:', error);
process.exit(1);
}
};
seedDatabase();