| | const mongoose = require('mongoose');
|
| | const bcrypt = require('bcryptjs');
|
| | require('dotenv').config();
|
| |
|
| |
|
| | const User = require('../src/models/User');
|
| | const Conversation = require('../src/models/Conversation');
|
| | const Message = require('../src/models/Message');
|
| |
|
| | const initDB = async () => {
|
| | try {
|
| |
|
| | await mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/ysnrfd-messenger');
|
| | console.log('β
Connected to database for initialization');
|
| |
|
| |
|
| | if (process.argv.includes('--clean')) {
|
| | await User.deleteMany({});
|
| | await Conversation.deleteMany({});
|
| | await Message.deleteMany({});
|
| | console.log('ποΈ Cleared existing data');
|
| | }
|
| |
|
| |
|
| | const sampleUsers = [
|
| | {
|
| | username: 'alice',
|
| | email: 'alice@example.com',
|
| | password: 'password123',
|
| | displayName: 'Alice Johnson',
|
| | bio: 'Hello! I love coding and chatting.',
|
| | status: 'online'
|
| | },
|
| | {
|
| | username: 'bob',
|
| | email: 'bob@example.com',
|
| | password: 'password123',
|
| | displayName: 'Bob Smith',
|
| | bio: 'Developer and tech enthusiast',
|
| | status: 'online'
|
| | },
|
| | {
|
| | username: 'charlie',
|
| | email: 'charlie@example.com',
|
| | password: 'password123',
|
| | displayName: 'Charlie Brown',
|
| | bio: 'Just here to chat with friends',
|
| | status: 'away'
|
| | }
|
| | ];
|
| |
|
| | const createdUsers = await User.insertMany(sampleUsers);
|
| | console.log(`π€ Created ${createdUsers.length} sample users`);
|
| |
|
| |
|
| | const sampleConversation = await Conversation.create({
|
| | type: 'group',
|
| | name: 'General Chat',
|
| | description: 'A place for general discussion',
|
| | createdBy: createdUsers[0]._id,
|
| | participants: [
|
| | { user: createdUsers[0]._id, role: 'owner' },
|
| | { user: createdUsers[1]._id, role: 'member' },
|
| | { user: createdUsers[2]._id, role: 'member' }
|
| | ]
|
| | });
|
| |
|
| | console.log('π¬ Created sample conversation');
|
| |
|
| |
|
| | const sampleMessages = [
|
| | {
|
| | conversation: sampleConversation._id,
|
| | sender: createdUsers[0]._id,
|
| | content: 'Hello everyone! Welcome to the chat! π',
|
| | type: 'text'
|
| | },
|
| | {
|
| | conversation: sampleConversation._id,
|
| | sender: createdUsers[1]._id,
|
| | content: 'Thanks for setting this up, Alice! π',
|
| | type: 'text'
|
| | },
|
| | {
|
| | conversation: sampleConversation._id,
|
| | sender: createdUsers[2]._id,
|
| | content: 'Hey guys! Looking forward to chatting with you all!',
|
| | type: 'text'
|
| | }
|
| | ];
|
| |
|
| | await Message.insertMany(sampleMessages);
|
| | console.log(`π Created ${sampleMessages.length} sample messages`);
|
| |
|
| | console.log('\nπ Database initialization completed successfully!');
|
| | console.log('\nπ Sample login credentials:');
|
| | console.log('Alice: alice@example.com / password123');
|
| | console.log('Bob: bob@example.com / password123');
|
| | console.log('Charlie: charlie@example.com / password123');
|
| |
|
| | } catch (error) {
|
| | console.error('β Initialization error:', error);
|
| | process.exit(1);
|
| | } finally {
|
| | await mongoose.connection.close();
|
| | console.log('\nπ Database connection closed');
|
| | }
|
| | };
|
| |
|
| |
|
| | initDB(); |