aufgeteilt in Module
This commit is contained in:
30
db/mongo.js
Normal file
30
db/mongo.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import { MongoClient } from 'mongodb';
|
||||
|
||||
const MONGO_ROOT_USER = process.env.MONGO_ROOT_USER;
|
||||
const MONGO_ROOT_PASSWORD = process.env.MONGO_ROOT_PASSWORD;
|
||||
let MONGO_URI = process.env.MONGO_URI || 'mongodb://localhost:27017';
|
||||
|
||||
if (MONGO_ROOT_USER && MONGO_ROOT_PASSWORD) {
|
||||
const uriParts = MONGO_URI.split('://');
|
||||
if (uriParts.length === 2) {
|
||||
const protocol = uriParts[0];
|
||||
const rest = uriParts[1];
|
||||
MONGO_URI = `${protocol}://${encodeURIComponent(MONGO_ROOT_USER)}:${encodeURIComponent(MONGO_ROOT_PASSWORD)}@${rest}`;
|
||||
}
|
||||
}
|
||||
const DB_NAME = process.env.DB_NAME || 'espdb';
|
||||
|
||||
let db, entriesCollection, usersCollection;
|
||||
|
||||
export async function initMongo() {
|
||||
const client = new MongoClient(MONGO_URI);
|
||||
await client.connect();
|
||||
db = client.db(DB_NAME);
|
||||
entriesCollection = db.collection('entries');
|
||||
usersCollection = db.collection('users');
|
||||
return { db, entriesCollection, usersCollection };
|
||||
}
|
||||
|
||||
export function getCollections() {
|
||||
return { db, entriesCollection, usersCollection };
|
||||
}
|
||||
Reference in New Issue
Block a user