-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
236 lines (208 loc) · 7.27 KB
/
Copy pathsetup.js
File metadata and controls
236 lines (208 loc) · 7.27 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
require('dotenv').config();
// Define schemas
const userSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
dob: { type: Date },
contact: { type: String },
createdAt: { type: Date, default: Date.now },
lastLogin: { type: Date }
});
const orderSchema = new mongoose.Schema({
userId: { type: String, required: true },
userName: { type: String, required: true },
productId: { type: String, required: true },
productName: { type: String, required: true },
quantity: { type: Number, required: true },
status: { type: String, default: 'Pending' },
orderDate: { type: Date, default: Date.now },
lastUpdated: { type: Date, default: Date.now }
});
const notificationSchema = new mongoose.Schema({
userId: { type: String, required: true },
message: { type: String, required: true },
read: { type: Boolean, default: false },
createdAt: { type: Date, default: Date.now },
orderId: { type: String, required: true },
type: { type: String, enum: ['ORDER', 'SYSTEM', 'USER'], default: 'ORDER' },
status: { type: String, default: 'unread' }
});
const productSchema = new mongoose.Schema({
name: { type: String, required: true },
description: { type: String },
price: { type: Number, required: true },
stock: { type: Number, required: true },
createdAt: { type: Date, default: Date.now }
});
// Function to connect to database and create a new connection
async function connectToDb(url) {
try {
const connection = await mongoose.createConnection(url, {
useNewUrlParser: true,
useUnifiedTopology: true
});
console.log('Connected to database:', url);
return connection;
} catch (err) {
console.error('Database connection error:', err);
throw err;
}
}
// Function to safely drop collection if it exists
async function safeDropCollection(connection, collectionName) {
try {
// Check if the collection exists before attempting to drop it
const collections = await connection.db.listCollections().toArray();
const collectionExists = collections.some(col => col.name === collectionName);
if (collectionExists) {
await connection.db.collection(collectionName).drop();
console.log(`Dropped collection: ${collectionName}`);
}
} catch (err) {
console.log(`Collection ${collectionName} does not exist or could not be dropped`);
}
}
// Function to close database connection
async function closeConnection(connection) {
try {
await connection.close();
console.log('Database connection closed');
} catch (err) {
console.error('Error closing database connection:', err);
throw err;
}
}
async function createData() {
const defaultPassword = await bcrypt.hash('password123', 10);
const services = [
{
name: 'user-service',
port: 3001,
model: 'User',
schema: userSchema,
collection: 'users',
data: [
{
name: 'John Doe',
email: 'john@example.com',
password: defaultPassword,
dob: new Date('1990-01-01'),
contact: '+1234567890'
},
{
name: 'Jane Smith',
email: 'jane@example.com',
password: defaultPassword,
dob: new Date('1992-05-15'),
contact: '+1987654321'
}
]
},
{
name: 'product-service',
port: 3004,
model: 'Product',
schema: productSchema,
collection: 'products',
data: [
{
name: 'Laptop',
description: 'High-performance laptop',
price: 999.99,
stock: 50
},
{
name: 'Smartphone',
description: 'Latest smartphone model',
price: 699.99,
stock: 100
}
]
},
{
name: 'order-service',
port: 3002,
model: 'Order',
schema: orderSchema,
collection: 'orders',
data: [] // Will be populated after users and products are created
},
{
name: 'notification-service',
port: 3003,
model: 'Notification',
schema: notificationSchema,
collection: 'notifications',
data: [] // Will be populated after orders are created
}
];
let users, products, createdOrders;
try {
// First create users and products
for (const service of services) {
if (service.name === 'user-service' || service.name === 'product-service') {
const dbUrl = `mongodb://127.0.0.1:27017/${service.name}`;
console.log(`\n📦 Setting up ${service.name}...`);
const connection = await connectToDb(dbUrl);
// Safely drop collection if it exists
await safeDropCollection(connection, service.collection);
// Create model specific to this connection
const Model = connection.model(service.model, service.schema);
const created = await Model.create(service.data);
console.log(`✅ Created ${created.length} ${service.model} records`);
// Store created data for later use
if (service.name === 'user-service') {
users = created;
} else if (service.name === 'product-service') {
products = created;
}
await closeConnection(connection);
}
}
// Now create orders
const orderService = services.find(s => s.name === 'order-service');
const dbUrl = `mongodb://127.0.0.1:27017/${orderService.name}`;
console.log(`\n📦 Setting up ${orderService.name}...`);
const orderConn = await connectToDb(dbUrl);
await safeDropCollection(orderConn, 'orders');
const OrderModel = orderConn.model('Order', orderSchema);
const orderData = users.flatMap(user =>
products.map(product => ({
userId: user._id.toString(),
userName: user.name,
productId: product._id.toString(),
productName: product.name,
quantity: Math.floor(Math.random() * 3) + 1,
status: 'Pending'
}))
);
createdOrders = await OrderModel.create(orderData);
console.log(`✅ Created ${createdOrders.length} Order records`);
await closeConnection(orderConn);
// Finally, create notifications
const notificationService = services.find(s => s.name === 'notification-service');
console.log(`\n📦 Setting up ${notificationService.name}...`);
const notifConn = await connectToDb(`mongodb://127.0.0.1:27017/${notificationService.name}`);
await safeDropCollection(notifConn, 'notifications');
const NotificationModel = notifConn.model('Notification', notificationSchema);
const notificationData = createdOrders.map(order => ({
userId: order.userId,
message: `New order created for ${order.productName}`,
orderId: order._id.toString(),
type: 'ORDER'
}));
const createdNotifications = await NotificationModel.create(notificationData);
console.log(`✅ Created ${createdNotifications.length} Notification records`);
await closeConnection(notifConn);
console.log('\n🎉 All services setup completed successfully!');
} catch (error) {
console.error('\n❌ Error during setup:', error);
process.exit(1);
}
}
// Run the setup
console.log('🚀 Starting services setup...');
createData();