UNPKG

@pikmeup/database-service

Version:

Shared database service for PikMeUp using Typegoose with MongoDB

246 lines (245 loc) 10.8 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("./index"); const User_1 = require("./models/User"); const Trip_1 = require("./models/Trip"); const readline = __importStar(require("readline")); // Create readline interface for user input const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // Available tables/collections const availableTables = [ 'User', 'Passenger', 'Driver', 'Supervisor', 'Cab', 'Route', 'Stop', 'Trip', 'Leave', 'CabAssignment', 'RouteSchedule' ]; // Function to prompt user for table selection function promptForTables() { return new Promise((resolve) => { console.log('\n=== Available Tables/Collections ==='); availableTables.forEach((table, index) => { console.log(`${index + 1}. ${table}`); }); console.log('0. All tables'); console.log('\nEnter the numbers of tables you want to test (comma-separated, e.g., 1,3,5):'); rl.question('Your selection: ', (answer) => { const selections = answer.split(',').map(s => s.trim()); if (selections.includes('0')) { resolve(availableTables); return; } const selectedTables = selections .map(s => parseInt(s)) .filter(num => num > 0 && num <= availableTables.length) .map(num => availableTables[num - 1]); resolve(selectedTables); }); }); } // Function to create test data for each table async function createTestData(selectedTables) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p; const createdData = {}; try { // Connect to MongoDB await (0, index_1.connectToDatabase)('mongodb://localhost:27017/pikmeup'); console.log('\n=== Creating Test Data ===\n'); // Create User first if needed (many other tables depend on it) if (selectedTables.includes('User') || selectedTables.includes('Passenger') || selectedTables.includes('Driver') || selectedTables.includes('Supervisor')) { const user = await index_1.models.User.create({ name: 'Test User', email: 'test@example.com', phoneNumber: '1234567890', firebaseId: 'test_firebase_id_123', password: 'test123', role: User_1.Role.PASSENGER, isActive: true }); createdData.User = user; console.log('✅ Created User:', user._id); } // Create Passenger if (selectedTables.includes('Passenger')) { const passenger = await index_1.models.Passenger.create({ userId: ((_a = createdData.User) === null || _a === void 0 ? void 0 : _a._id) || '507f1f77bcf86cd799439011' // fallback ObjectId }); createdData.Passenger = passenger; console.log('✅ Created Passenger:', passenger._id); } // Create Driver if (selectedTables.includes('Driver')) { const driver = await index_1.models.Driver.create({ userId: ((_b = createdData.User) === null || _b === void 0 ? void 0 : _b._id) || '507f1f77bcf86cd799439011' // fallback ObjectId }); createdData.Driver = driver; console.log('✅ Created Driver:', driver._id); } // Create Supervisor if (selectedTables.includes('Supervisor')) { const supervisor = await index_1.models.Supervisor.create({ userId: ((_c = createdData.User) === null || _c === void 0 ? void 0 : _c._id) || '507f1f77bcf86cd799439011' // fallback ObjectId }); createdData.Supervisor = supervisor; console.log('✅ Created Supervisor:', supervisor._id); } // Create Cab if (selectedTables.includes('Cab')) { const cab = await index_1.models.Cab.create({ number: 'KA01AB12345', capacity: 4, driver: ((_d = createdData.Driver) === null || _d === void 0 ? void 0 : _d._id) || '507f1f77bcf86cd799439011' // fallback ObjectId }); createdData.Cab = cab; console.log('✅ Created Cab:', cab._id); } // Create Route if (selectedTables.includes('Route')) { const route = await index_1.models.Route.create({ name: 'Test Route', supervisor: ((_e = createdData.Supervisor) === null || _e === void 0 ? void 0 : _e._id) || '507f1f77bcf86cd799439011', // fallback ObjectId cabs: ((_f = createdData.Cab) === null || _f === void 0 ? void 0 : _f._id) ? [createdData.Cab._id] : [] }); createdData.Route = route; console.log('✅ Created Route:', route._id); } // Create Stops if (selectedTables.includes('Stop')) { const stop = await index_1.models.Stop.create({ address: '123 Test Street', order: 1, route: ((_g = createdData.Route) === null || _g === void 0 ? void 0 : _g._id) || '507f1f77bcf86cd799439011' // fallback ObjectId }); createdData.Stop = stop; console.log('✅ Created Stop:', stop._id); } // Create Trip if (selectedTables.includes('Trip')) { const trip = await index_1.models.Trip.create({ startTimestamp: new Date(), endTimestamp: new Date(Date.now() + 3600000), // 1 hour later scheduledRoute: ((_h = createdData.RouteSchedule) === null || _h === void 0 ? void 0 : _h._id) || '507f1f77bcf86cd799439011', // fallback ObjectId passengerList: createdData.Passenger ? [createdData.Passenger._id] : [], driver: ((_j = createdData.Driver) === null || _j === void 0 ? void 0 : _j._id) || '507f1f77bcf86cd799439011', // fallback ObjectId cab: ((_k = createdData.Cab) === null || _k === void 0 ? void 0 : _k._id) || '507f1f77bcf86cd799439011', // fallback ObjectId type: Trip_1.TripType.TO_OFFICE, status: Trip_1.TripStatus.COMPLETED }); createdData.Trip = trip; console.log('✅ Created Trip:', trip._id); } // Create Leave if (selectedTables.includes('Leave')) { const leave = await index_1.models.Leave.create({ driver: ((_l = createdData.Driver) === null || _l === void 0 ? void 0 : _l._id) || '507f1f77bcf86cd799439011', // fallback ObjectId startDate: new Date(), endDate: new Date(Date.now() + 86400000), // 1 day later reason: 'Test leave request', status: 'pending' }); createdData.Leave = leave; console.log('✅ Created Leave:', leave._id); } // Create CabAssignment if (selectedTables.includes('CabAssignment')) { const cabAssignment = await index_1.models.CabAssignment.create({ cab: ((_m = createdData.Cab) === null || _m === void 0 ? void 0 : _m._id) || '507f1f77bcf86cd799439011', // fallback ObjectId driver: ((_o = createdData.Driver) === null || _o === void 0 ? void 0 : _o._id) || '507f1f77bcf86cd799439011', // fallback ObjectId startDate: new Date(), endDate: new Date(Date.now() + 86400000), // 1 day later status: 'active' }); createdData.CabAssignment = cabAssignment; console.log('✅ Created CabAssignment:', cabAssignment._id); } // Create RouteSchedule if (selectedTables.includes('RouteSchedule')) { const routeSchedule = await index_1.models.RouteSchedule.create({ route: ((_p = createdData.Route) === null || _p === void 0 ? void 0 : _p._id) || '507f1f77bcf86cd799439011', // fallback ObjectId dayOfWeek: 1, // Monday departureTime: '08:00', arrivalTime: '09:00', isActive: true }); createdData.RouteSchedule = routeSchedule; console.log('✅ Created RouteSchedule:', routeSchedule._id); } console.log('\n=== Test Data Creation Complete ==='); console.log('Created records for tables:', selectedTables); } catch (error) { console.error('❌ Error creating test data:', error); throw error; } finally { // Disconnect from database await (0, index_1.disconnectFromDatabase)(); rl.close(); } } // Main function async function main() { try { console.log('🚀 Interactive Database Test Script'); console.log('====================================='); const selectedTables = await promptForTables(); if (selectedTables.length === 0) { console.log('No tables selected. Exiting...'); rl.close(); return; } console.log(`\nSelected tables: ${selectedTables.join(', ')}`); await createTestData(selectedTables); } catch (error) { console.error('❌ Error in main:', error); process.exit(1); } } // Run the script main();