Add database connection...

This commit is contained in:
Dieter Blocher
2025-01-29 16:12:24 +00:00
parent ca77e0842d
commit 826cfb48c9
6 changed files with 284 additions and 2 deletions

View File

@@ -0,0 +1,30 @@
const mongoose = require("mongoose");
const connectDB = async () => {
try {
const conn = await mongoose.connect('mongodb://mongodb:27017/tombola', {
});
console.log(`MongoDB Connected: ${conn.connection.host}:${conn.connection.port}`);
// Get and log database stats
const dbStats = await conn.connection.db.stats();
console.log("Database Stats:", dbStats);
// Get and log all collections in the database
const collections = await conn.connection.db.collections();
console.log("Collections:");
collections.forEach(collection => {
console.log(collection.collectionName);
});
// Get and log the indexes of a specific collection (optional)
//const collectionName = "tombola"; // Replace with your collection name
//const indexes = await conn.connection.db.collection(collectionName).indexes();
//console.log(`Indexes in ${collectionName}:`);
//console.log(indexes);
} catch (error) {
console.error(error.message);
process.exit(1);
}
}
module.exports = connectDB;

12
src/models/users.js Normal file
View File

@@ -0,0 +1,12 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
firstname: { type: String },
familyname: { type: String },
email: { type: String },
phonenumber: { type: String },
},
{
timestamps: true,
});
module.exports = mongoose.model('user', UserSchema, 'users');

View File

@@ -3,6 +3,7 @@ require('dotenv').config();
const path = require('path');
const livereload = require('livereload');
const connectLivereload = require('connect-livereload');
const connectDB = require('./middleware/database');
// Setup livereload
@@ -10,6 +11,8 @@ const liveReloadServer = livereload.createServer();
liveReloadServer.watch(__dirname + "public"); // Watch public directory
const app = express()
connectDB();
// Middleware to inject livereload script
app.use(connectLivereload());