temp-log-service.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import mongoose, { Schema, Document, Model, Mongoose } from 'mongoose';
  2. import { AppMessageSchema } from '../../dependencies/logging/dependencies/msgutil/dependencies/fisappmessagejsdistribution/src/message/common/appmessageschema';
  3. import { Message } from '../../dependencies/logging/dependencies/msgutil/dependencies/fisappmessagejsdistribution/src/test/schema/fisappmessageschematest';
  4. import { BaseMessage, MessageHeader } from '../../dependencies/logging/dependencies/msgutil/dependencies/dependencies';
  5. // Define the User Service class
  6. export class MongoService {
  7. private MongooseConnection: Mongoose
  8. constructor(client: string) {
  9. this.connectDB(client)
  10. }
  11. // Connect to MongoDB
  12. private async connectDB(client: string): Promise<void> {
  13. try {
  14. const mongoURI = 'mongodb://localhost:27017/' + client // Replace with your MongoDB URI
  15. this.MongooseConnection = await mongoose.connect(mongoURI);
  16. console.log('MongoDB connected');
  17. } catch (err) {
  18. console.error('Error connecting to MongoDB:', err);
  19. }
  20. }
  21. // Store Message
  22. public async write(data: BaseMessage, requestID: string, err_func?: any) {
  23. let messageModel: Model<any> = this.MongooseConnection.model(requestID, MessageSchema)
  24. messageModel.create({ data: JSON.stringify(data) }).then(() => {
  25. // lol do nothing.
  26. }).catch((err) => {
  27. err_func(err)
  28. console.log(`MongoError: ${err.message}`)
  29. })
  30. }
  31. }
  32. // Define an interface for the document
  33. interface IMessage extends Document {
  34. data: string
  35. }
  36. // Define a schema
  37. const MessageSchema: Schema = new Schema({
  38. data: { type: String, required: true },
  39. });