Datasets:
Download ysnrfd_messenger/BACKUP2/backend/scripts/initDB.js from ysn-rfd/text-dataset-tiny-code-script-py-format: direct link, hf CLI and curl.
- Browser
- Download file 3.53 kB
-
https://huggingface.co/datasets/ysn-rfd/text-dataset-tiny-code-script-py-format/resolve/main/ysnrfd_messenger/BACKUP2/backend/scripts/initDB.js
- Command line
-
hf download hf://datasets/ysn-rfd/text-dataset-tiny-code-script-py-format/ysnrfd_messenger/BACKUP2/backend/scripts/initDB.js
-
curl -L -o initDB.js https://huggingface.co/datasets/ysn-rfd/text-dataset-tiny-code-script-py-format/resolve/main/ysnrfd_messenger/BACKUP2/backend/scripts/initDB.js
3.53 kB
| const mongoose = require('mongoose'); | |
| const bcrypt = require('bcryptjs'); | |
| require('dotenv').config(); | |
| // Import models | |
| const User = require('../src/models/User'); | |
| const Conversation = require('../src/models/Conversation'); | |
| const Message = require('../src/models/Message'); | |
| const initDB = async () => { | |
| try { | |
| // Connect to database | |
| await mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/ysnrfd-messenger'); | |
| console.log('β Connected to database for initialization'); | |
| // Clear existing data (optional - for development) | |
| if (process.argv.includes('--clean')) { | |
| await User.deleteMany({}); | |
| await Conversation.deleteMany({}); | |
| await Message.deleteMany({}); | |
| console.log('ποΈ Cleared existing data'); | |
| } | |
| // Create sample users | |
| 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`); | |
| // Create a sample conversation | |
| 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'); | |
| // Create sample messages | |
| 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'); | |
| } | |
| }; | |
| // Run initialization | |
| initDB(); |