All files / src/models company_model.ts

85.71% Statements 6/7
100% Branches 0/0
0% Functions 0/1
85.71% Lines 6/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78    7x                                                                                     7x                         7x 7x           7x                       7x
import mongoose, { Document, Schema } from 'mongoose';
 
const companySchema: Schema = new mongoose.Schema({
    company: {
        type: String,
        required: true,
    },
    company_he: {
        type: String,
        required: true,
    },
    tags: [
        {
            type: String,
            required: true,
        }
    ],
    quizzes: [
        {
            title: {
                type: String,
                required: true,
            },
            quiz_id: {
                type: Number,
                required: true,
            },
            tags: [
                {
                    type: String,
                    required: true,
                }
            ],
            content: {
                type: String,
                required: true,
            },
            forum_link: {
                type: String,
                required: false,
            },
        }
    ]
}, { timestamps: true, strict: true, versionKey: false });
 
companySchema.set('toJSON', {
    transform: (doc: Document, ret: Record<string, any>) => {
        return {
            id: ret._id,
            company: ret.company,
            company_he: ret.company_he,
            tags: ret.tags,
            quizzes: ret.quizzes,
        }
    }
});
 
// Add indexes to CompanySchema
companySchema.index({ tags: 1 }); // For efficient matching on company tags
companySchema.index({ 'quizzes.tags': 1 }); // For efficient matching on quiz tags in subdocuments
 
// For content fields, consider a text index if you want to search for *words* within them.
// If you create the text index, you must then change the search logic to use $text operator.
// A regular index (like { 'quizzes.content': 1 }) will NOT accelerate $regexMatch.
// For `$regexMatch` performance, only a collection scan or a dedicated full-text search works.
companySchema.index(
    {
      'quizzes.content': 'text',
    },
    {
      name: 'quiz_content_text_index',
      weights: {
        'quizzes.content': 10, // Give more weight to content
      }
    }
  );
 
export const CompanyModel = mongoose.model('Companies', companySchema);