const mongoose = require('mongoose') const ObjectID = mongoose.Schema.Types.ObjectId const transactionSchema = new mongoose.Schema( { type: { type: String, enum: ['withdraw', 'deposit'], required: true, }, status: { type: String, enum: ['failed', 'pending', 'finished'], required: true, }, source: { type: String, enum: ['normal'], }, value: { type: Number, required: true, }, date: { type: Date, default: Date.now(), }, description: String, userId: { type: ObjectID, ref: 'User', }, belongingId: { type: ObjectID, ref: 'Belonging', }, }, { toJSON: { virtuals: true }, toObject: { virtuals: true }, timestamps: true, } ) // Add individual indexes for frequently queried fields transactionSchema.index({ type: 1 }) transactionSchema.index({ status: 1 }) transactionSchema.index({ value: 1 }) // Add compound indexes for common query patterns in aggregation transactionSchema.index({ belongingId: 1, status: 1 }) transactionSchema.index({ belongingId: 1, type: 1, status: 1 }) const Transaction = mongoose.model('Transaction', transactionSchema) module.exports = Transaction