feat(db): init mongo and passing komga conf

This commit is contained in:
Julien Froidefond
2025-02-14 14:23:30 +01:00
parent 08f6e6a264
commit 1f881ade26
11 changed files with 1047 additions and 446 deletions

View File

@@ -0,0 +1,35 @@
import mongoose from "mongoose";
const configSchema = new mongoose.Schema(
{
userId: {
type: String,
required: true,
unique: true,
},
url: {
type: String,
required: true,
},
username: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
},
{
timestamps: true,
}
);
// Middleware pour mettre à jour le champ updatedAt avant la sauvegarde
configSchema.pre("save", function (next) {
this.updatedAt = new Date();
next();
});
export const KomgaConfig =
mongoose.models.KomgaConfig || mongoose.model("KomgaConfig", configSchema);

51
src/lib/mongodb.ts Normal file
View File

@@ -0,0 +1,51 @@
import mongoose from "mongoose";
const MONGODB_URI = process.env.MONGODB_URI;
if (!MONGODB_URI) {
throw new Error(
"Veuillez définir la variable d'environnement MONGODB_URI dans votre fichier .env"
);
}
interface MongooseCache {
conn: typeof mongoose | null;
promise: Promise<typeof mongoose> | null;
}
declare global {
var mongoose: MongooseCache | undefined;
}
let cached: MongooseCache = global.mongoose || { conn: null, promise: null };
if (!global.mongoose) {
global.mongoose = { conn: null, promise: null };
}
async function connectDB(): Promise<typeof mongoose> {
if (cached.conn) {
return cached.conn;
}
if (!cached.promise) {
const opts = {
bufferCommands: false,
};
cached.promise = mongoose.connect(MONGODB_URI!, opts).then((mongoose) => {
return mongoose;
});
}
try {
cached.conn = await cached.promise;
} catch (e) {
cached.promise = null;
throw e;
}
return cached.conn;
}
export default connectDB;

View File

@@ -0,0 +1,62 @@
import { cookies } from "next/headers";
import connectDB from "@/lib/mongodb";
import { KomgaConfig } from "@/lib/models/config.model";
interface User {
id: string;
email: string;
}
interface KomgaConfigData {
url: string;
username: string;
password: string;
}
export class ConfigDBService {
private static async getCurrentUser(): Promise<User> {
const userCookie = cookies().get("stripUser");
if (!userCookie) {
throw new Error("Utilisateur non authentifié");
}
try {
return JSON.parse(atob(userCookie.value));
} catch (error) {
console.error("Erreur lors de la récupération de l'utilisateur depuis le cookie:", error);
throw new Error("Utilisateur non authentifié");
}
}
static async saveConfig(data: KomgaConfigData) {
const user = await this.getCurrentUser();
await connectDB();
const config = await KomgaConfig.findOneAndUpdate(
{ userId: user.id },
{
userId: user.id,
url: data.url,
username: data.username,
password: data.password,
},
{ upsert: true, new: true }
);
return config;
}
static async getConfig() {
const user = await this.getCurrentUser();
await connectDB();
const config = await KomgaConfig.findOne({ userId: user.id });
if (!config) {
throw new Error("Configuration non trouvée");
}
return config;
}
}