1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import mongoose, { Schema, Document, Model, Mongoose } from 'mongoose';
- import { AppMessageSchema } from '../../dependencies/logging/dependencies/msgutil/dependencies/fisappmessagejsdistribution/src/message/common/appmessageschema';
- import { Message } from '../../dependencies/logging/dependencies/msgutil/dependencies/fisappmessagejsdistribution/src/test/schema/fisappmessageschematest';
- import { BaseMessage, MessageHeader } from '../../dependencies/logging/dependencies/msgutil/dependencies/dependencies';
- // Define the User Service class
- export class MongoService {
- private MongooseConnection: Mongoose
- constructor(client: string) {
- this.connectDB(client)
- }
- // Connect to MongoDB
- private async connectDB(client: string): Promise<void> {
- try {
- const mongoURI = 'mongodb://localhost:27017/' + client // Replace with your MongoDB URI
- this.MongooseConnection = await mongoose.connect(mongoURI);
- console.log('MongoDB connected');
- } catch (err) {
- console.error('Error connecting to MongoDB:', err);
- }
- }
- // Store Message
- public async write(data: BaseMessage, requestID: string, err_func?: any) {
- let messageModel: Model<any> = this.MongooseConnection.model(requestID, MessageSchema)
- messageModel.create({ data: JSON.stringify(data) }).then(() => {
- // lol do nothing.
- }).catch((err) => {
- err_func(err)
- console.log(`MongoError: ${err.message}`)
- })
- }
- }
- // Define an interface for the document
- interface IMessage extends Document {
- data: string
- }
- // Define a schema
- const MessageSchema: Schema = new Schema({
- data: { type: String, required: true },
- });
|