All files / server/src/db mongoosePlugins.ts

80% Statements 8/10
100% Branches 0/0
100% Functions 3/3
77.77% Lines 7/9

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      39x 904x                   39x     39x 39x     39x     776x  
import mongoose, { Schema } from "mongoose";
import mongooseLeanGetters from "mongoose-lean-getters";
 
mongoose.plugin((schema: Schema) => {
  schema.set("toJSON", {
    // toJSON() removes _id and __v
    transform(_document, returnedObject) {
      delete returnedObject._id;
      delete (returnedObject as Record<string, unknown>).__v;
    },
  });
});
 
// Enable lean() getters and always apply getters
mongoose.plugin(mongooseLeanGetters, { defaultLeanOptions: { getters: true } });
 
// Always apply getters
mongoose.set("toObject", { getters: true, virtuals: true });
mongoose.set("toJSON", { getters: true, virtuals: true });
 
// Always run schema validation
mongoose.set("runValidators", true);
 
// Allow empty strings to pass the `required` validator
mongoose.Schema.Types.String.checkRequired((v) => typeof v === "string");