2024-12-23 18:42:18 +00:00
|
|
|
import { MongoClient } from 'mongodb';
|
|
|
|
|
|
|
|
|
|
if (!process.env.MONGODB_URI) {
|
|
|
|
|
throw new Error('Please add your Mongo URI to .env');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const uri = process.env.MONGODB_URI;
|
|
|
|
|
let client: MongoClient;
|
|
|
|
|
let clientPromise: Promise<MongoClient>;
|
|
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
let globalWithMongo = global as typeof globalThis & {
|
|
|
|
|
_mongoClientPromise?: Promise<MongoClient>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!globalWithMongo._mongoClientPromise) {
|
|
|
|
|
client = new MongoClient(uri);
|
|
|
|
|
globalWithMongo._mongoClientPromise = client.connect();
|
|
|
|
|
}
|
|
|
|
|
clientPromise = globalWithMongo._mongoClientPromise;
|
|
|
|
|
} else {
|
|
|
|
|
client = new MongoClient(uri);
|
|
|
|
|
clientPromise = client.connect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function connectToDatabase() {
|
|
|
|
|
const client = await clientPromise;
|
|
|
|
|
const db = client.db();
|
|
|
|
|
return { db, client };
|
|
|
|
|
}
|
2025-01-05 01:43:34 +00:00
|
|
|
|
|
|
|
|
export async function initMongoDB() {
|
|
|
|
|
try {
|
|
|
|
|
const { db } = await connectToDatabase();
|
|
|
|
|
|
|
|
|
|
await db.collection('favorites').createIndex(
|
|
|
|
|
{ userId: 1, mediaId: 1, mediaType: 1 },
|
|
|
|
|
{ unique: true }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error initializing MongoDB:', error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function resetIndexes() {
|
|
|
|
|
const { db } = await connectToDatabase();
|
|
|
|
|
|
|
|
|
|
await db.collection('favorites').dropIndexes();
|
|
|
|
|
|
|
|
|
|
await db.collection('favorites').createIndex(
|
|
|
|
|
{ userId: 1, mediaId: 1, mediaType: 1 },
|
|
|
|
|
{ unique: true }
|
|
|
|
|
);
|
|
|
|
|
}
|