MongoDB ile TV dizileri için CRUD işlemleri eklendi
This commit is contained in:
74
server/modules/tvDataStore.js
Normal file
74
server/modules/tvDataStore.js
Normal file
@@ -0,0 +1,74 @@
|
||||
import { connectMongo } from "./db.js";
|
||||
|
||||
const COLLECTION = "tv_data";
|
||||
|
||||
async function getCollection() {
|
||||
const { db } = await connectMongo();
|
||||
const col = db.collection(COLLECTION);
|
||||
await col.createIndex({ rootFolder: 1 });
|
||||
await col.createIndex({ tvdbId: 1 });
|
||||
await col.createIndex({ updatedAt: -1 });
|
||||
return col;
|
||||
}
|
||||
|
||||
function buildDocument(key, rootFolder, seriesData) {
|
||||
const tvdbId = seriesData?.id ?? seriesData?.tvdbId ?? null;
|
||||
return {
|
||||
_id: key,
|
||||
key,
|
||||
rootFolder,
|
||||
tvdbId,
|
||||
name: seriesData?.name || null,
|
||||
data: seriesData || {},
|
||||
updatedAt: Date.now()
|
||||
};
|
||||
}
|
||||
|
||||
export async function upsertTvSeries(key, rootFolder, seriesData) {
|
||||
const col = await getCollection();
|
||||
const doc = buildDocument(key, rootFolder, seriesData);
|
||||
await col.updateOne({ _id: key }, { $set: doc }, { upsert: true });
|
||||
return doc.data;
|
||||
}
|
||||
|
||||
export async function getTvSeriesByKey(key) {
|
||||
const col = await getCollection();
|
||||
const doc = await col.findOne({ _id: key });
|
||||
return doc?.data || null;
|
||||
}
|
||||
|
||||
export async function getTvSeriesByRoot(rootFolder) {
|
||||
const col = await getCollection();
|
||||
const docs = await col.find({ rootFolder }).toArray();
|
||||
return docs.map((doc) => ({
|
||||
key: doc.key,
|
||||
rootFolder: doc.rootFolder,
|
||||
data: doc.data
|
||||
}));
|
||||
}
|
||||
|
||||
export async function listAllTvSeries() {
|
||||
const col = await getCollection();
|
||||
const docs = await col.find({}).toArray();
|
||||
return docs.map((doc) => ({
|
||||
key: doc.key,
|
||||
rootFolder: doc.rootFolder,
|
||||
data: doc.data
|
||||
}));
|
||||
}
|
||||
|
||||
export async function listTvSeriesKeysForRoot(rootFolder) {
|
||||
const col = await getCollection();
|
||||
const docs = await col.find({ rootFolder }).project({ key: 1 }).toArray();
|
||||
return docs.map((d) => d.key).filter(Boolean);
|
||||
}
|
||||
|
||||
export async function removeTvSeriesByKey(key) {
|
||||
const col = await getCollection();
|
||||
await col.deleteOne({ _id: key });
|
||||
}
|
||||
|
||||
export async function removeTvSeriesByRoot(rootFolder) {
|
||||
const col = await getCollection();
|
||||
await col.deleteMany({ rootFolder });
|
||||
}
|
||||
Reference in New Issue
Block a user