Spaces:
Sleeping
Sleeping
Download src/classes/core/MongoPoster.ts from lamsaoganh/teamta: direct link, hf CLI and curl.
- Browser
- Download file 3.28 kB
-
https://huggingface.co/spaces/lamsaoganh/teamta/resolve/main/src/classes/core/MongoPoster.ts
- Command line
-
hf download hf://spaces/lamsaoganh/teamta/src/classes/core/MongoPoster.ts
-
curl -L -o MongoPoster.ts https://huggingface.co/spaces/lamsaoganh/teamta/resolve/main/src/classes/core/MongoPoster.ts
3.28 kB
| import mongoose from 'mongoose'; | |
| import { IPayloadReference } from "../../interfaces/core/IPayloadReference"; | |
| import { IStoragePoster } from "../../interfaces/core/IStoragePoster"; | |
| import { StorageType } from "../../types/StorageType"; | |
| export class MongoPoster implements IStoragePoster { | |
| storageType: StorageType = "mongo"; | |
| async post(reference: IPayloadReference): Promise<IPayloadReference> { | |
| let connection: mongoose.Connection | null = null; | |
| try { | |
| // ๐ SECURITY NOTE: Only for external data operations | |
| // Message queue and worker registry operations are FORBIDDEN | |
| // Must use Noah API for those operations | |
| const uri = process.env.MONGO_URI || reference.uri; | |
| if (!uri) { | |
| throw new Error("MongoDB URI is required"); | |
| } | |
| // Create new connection for this operation | |
| connection = await mongoose.createConnection(uri); | |
| // Determine collection and database | |
| const dbName = 'default'; // Use default database | |
| const collectionName = reference.collectionName || 'default'; | |
| // ๐ซ FORBIDDEN: message queue and worker registry collections | |
| const forbiddenCollections = ['messages', 'messagequeue', 'workerregistry', 'workers']; | |
| if (forbiddenCollections.some(forbidden => | |
| collectionName.toLowerCase().includes(forbidden))) { | |
| throw new Error(`๐ซ Collection '${collectionName}' is forbidden. Use Noah API for message queue and worker registry operations.`); | |
| } | |
| // Create model for the collection | |
| const schema = new mongoose.Schema({}, { strict: false }); | |
| const Model = connection.model(collectionName, schema, collectionName); | |
| let result; | |
| if (reference.documentId) { | |
| // Update existing document | |
| result = await Model.findByIdAndUpdate( | |
| reference.documentId, | |
| reference.data, | |
| { new: true, upsert: false } | |
| ); | |
| } else { | |
| // Create new document | |
| const doc = new Model(reference.data); | |
| result = await doc.save(); | |
| } | |
| // Close connection after operation | |
| await connection.close(); | |
| return { | |
| ...reference, | |
| data: result, | |
| // Add success and error properties as extended interface | |
| } as IPayloadReference & { success: boolean; timestamp: Date }; | |
| } catch (error) { | |
| // Ensure connection is closed even on error | |
| try { | |
| if (connection) { | |
| await connection.close(); | |
| } | |
| } catch (closeError) { | |
| // Ignore close errors | |
| } | |
| return { | |
| ...reference, | |
| data: null, | |
| // Add error information as extended interface | |
| } as IPayloadReference & { success: boolean; error: string; timestamp: Date }; | |
| } | |
| } | |
| } |