-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.js
54 lines (48 loc) · 1.82 KB
/
seed.js
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
require('dotenv').config(); // Load environment variables
const { sequelize } = require('./config/database'); // Import sequelize instance
const Server = require('./models/Server'); // Import Server model
const Instance = require('./models/Instance');
const User = require('./models/userModel');
async function seedDatabase() {
try {
// Synchronize the database schema (optional, can be skipped if schema is already up-to-date)
// await sequelize.sync({ force: false });
await sequelize.sync({})
.then(() => {
console.log("Database & tables created!");
})
.catch(err => {
console.error('Unable to sync database:', err);
});
console.log('Database synchronized.');
// Define the sample server data
const serverData = {
region: 'us-east-3',
ip_address: 'http://192.168.219.221',
port: 4690,
available_instances: 5,
num_instances: 5,
ram: '64GB',
graphics_card: 'NVIDIA Tesla T4',
storage: '1TB SSD',
};
// Use findOrCreate to prevent duplicates
const [server, created] = await Server.findOrCreate({
where: { ip_address: serverData.ip_address, port: serverData.port },
defaults: serverData, // Set the data if no existing record is found
});
if (created) {
console.log('Sample server created:', server.toJSON());
} else {
console.log('Server already exists:', server.toJSON());
}
} catch (error) {
console.error('Error seeding the database:', error);
} finally {
// Close the database connection
await sequelize.close();
console.log('Database connection closed.');
}
}
// Execute the seed function
seedDatabase();