UNPKG

streamby-core

Version:

StreamBy middleware framework for media storage management

81 lines (80 loc) 4.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createExport = createExport; exports.updateExport = updateExport; exports.deleteExport = deleteExport; const manager_1 = require("../models/manager"); const mongodb_1 = require("mongodb"); const connectionManager_1 = require("../adapters/database/connectionManager"); const nosql_1 = require("../adapters/database/nosql"); const sql_1 = require("../adapters/database/sql"); async function createExport(config, projectId, exportName, collectionName, jsonData, dbType) { const targetDb = config.databases?.find(db => db.type === dbType && db.main) || config.databases?.find(db => db.type === dbType); if (!targetDb) { throw new Error(`Database connection not found for type ${dbType}`); } const connection = (0, connectionManager_1.getConnection)(targetDb.id); let result; if (dbType === 'nosql') { result = await (0, nosql_1.createNoSQLRawExportCollection)(connection.client, projectId, exportName, jsonData, 'GET'); } else if (dbType === 'sql') { result = await (0, sql_1.createSQLRawExportTable)(connection.client, projectId, exportName, jsonData); } else { throw new Error('Unsupported database type'); } // Update the project with the new export reference // Assuming project metadata is always in NoSQL (MongoDB) const NoSQLProject = (0, manager_1.getModel)('projects', 'nosql'); await NoSQLProject.update({ _id: projectId }, { $push: { exports: { id: dbType === 'nosql' ? new mongodb_1.ObjectId(result.exportId) : result.exportId, name: exportName, collectionName: result.collectionName, type: 'raw', method: 'GET' } } }); return { ...result, message: 'Raw export created successfully' }; } async function updateExport(config, projectId, exportId, exportName, collectionName, jsonData, dbType) { const targetDb = config.databases?.find(db => db.type === dbType && db.main) || config.databases?.find(db => db.type === dbType); if (!targetDb) { throw new Error(`Database connection not found for type ${dbType}`); } const connection = (0, connectionManager_1.getConnection)(targetDb.id); let result; if (dbType === 'nosql') { const db = connection.client.db(); const updateData = { json: jsonData, name: exportName, updatedAt: new Date() }; await db.collection(collectionName).updateOne({ _id: new mongodb_1.ObjectId(exportId) }, { $set: updateData }); result = { collectionName, exportId }; } else { throw new Error('Unsupported database type for update'); } const NoSQLProject = (0, manager_1.getModel)('projects', 'nosql'); await NoSQLProject.update({ _id: new mongodb_1.ObjectId(projectId), 'exports.id': { $in: [new mongodb_1.ObjectId(exportId), exportId] } }, { $set: { 'exports.$.name': exportName, 'exports.$.collectionName': collectionName } }); console.log(result); return { ...result, message: 'Raw export updated successfully' }; } async function deleteExport(config, projectId, exportId, dbType, collectionName) { const targetDb = config.databases?.find(db => db.type === dbType && db.main) || config.databases?.find(db => db.type === dbType); if (!targetDb) { throw new Error(`Database connection not found for type ${dbType}`); } const connection = (0, connectionManager_1.getConnection)(targetDb.id); if (dbType === 'nosql') { const db = connection.client.db(); await db.collection(collectionName).deleteOne({ _id: new mongodb_1.ObjectId(exportId) }); } else { throw new Error('Unsupported database type for delete'); } const NoSQLProject = (0, manager_1.getModel)('projects', 'nosql'); await NoSQLProject.update({ _id: new mongodb_1.ObjectId(projectId) }, { $pull: { exports: { id: new mongodb_1.ObjectId(exportId) } } }); return { message: 'Export deleted successfully' }; }