-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.js
More file actions
94 lines (79 loc) · 2.58 KB
/
Copy pathsync.js
File metadata and controls
94 lines (79 loc) · 2.58 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
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
const sequelize = require('./config/database');
const User = require('./models/User');
const Product = require('./models/Product');
const Order = require('./models/Order');
const initialData = require('./config/initialData');
(async () => {
try {
await sequelize.sync({ force: true });
console.log('Database synchronized');
// Seed initial data
await seedInitialData();
console.log('Initial data seeded');
} catch (error) {
console.error('Error synchronizing database:', error);
}
})();
async function seedInitialData() {
try {
// Seed users in parallel
await Promise.all(initialData.users.map(userData => User.create(userData)));
// Seed products in parallel
await Promise.all(initialData.products.map(productData => Product.create(productData)));
// Seed orders in parallel
await Promise.all(initialData.orders.map(orderData => Order.create(orderData)));
console.log('Seeding initial data completed');
} catch (error) {
console.error('Error seeding initial data:', error);
throw error;
}
}
async function performServerRequests() {
try {
// Fetch all users
const users = await User.findAll();
console.log('Users:', users);
// Fetch all products
const products = await Product.findAll();
console.log('Products:', products);
// Fetch all orders
const orders = await Order.findAll();
console.log('Orders:', orders);
// Create a new product
const newProduct = await Product.create({
name: 'Sample Product',
category: 'Sample Category',
size: 'M',
quantity: 100,
price: 20.0,
});
console.log('New product created:', newProduct);
// Update the newly created product
await Product.update(
{ price: 25.0 },
{ where: { id: newProduct.id } }
);
// Fetch the updated product to confirm the changes
const updatedProduct = await Product.findByPk(newProduct.id);
console.log('Product updated:', updatedProduct);
// Delete the product
const deletedProduct = await Product.destroy({ where: { id: newProduct.id } });
if (deletedProduct) {
console.log('Product deleted:', newProduct.id);
} else {
console.log('Product deletion failed:', newProduct.id);
}
console.log('Server requests performed successfully');
} catch (error) {
console.error('Error performing server requests:', error);
throw error;
}
}
// Execute server requests for demonstration
(async () => {
try {
await performServerRequests();
} catch (error) {
console.error('Error in server request execution:', error);
}
})();