import { Document, Schema, Model } from "mongoose"; import { OnDeleteFuncOptions } from "../plugins/onDelete"; import { PrivacyFuncOptions } from "../plugins/privacy"; import { ProtectFuncOptions } from "../plugins/protect"; export declare type QueryMethod = string; export declare type GlobalFilter = { [key in QueryMethod]?: (docs: T[]) => void; }; export declare type MoongooseModelParams = { /** * Model name */ name: string; /** * Collection name (optional, induced from model name) */ collection?: string; /** * The plugin automatically added when true is Mongoose Paginate * * @default true * * https://www.npmjs.com/package/mongoose-paginate */ paginate?: boolean; /** * The plugin automatically added when true is Mongoose Aggregate Paginate V2 * * @default true * * https://www.npmjs.com/package/mongoose-aggregate-paginate-v2 */ aggregatePaginate?: boolean; /** * Always populate() certain fields in your Mongoose schemas. * * Only apply this plugin to top-level schemas. Don't apply this plugin to child schemas. * * @default true * * https://www.npmjs.com/package/mongoose-autopopulate * * ``` * const schema = new mongoose.Schema({ * populatedField: { * type: mongoose.Schema.Types.ObjectId, * ref: 'ForeignModel', * // The below option tells this plugin to always call `populate()` on * // `populatedField` * autopopulate: true * } * }); * ``` */ autopopulate?: boolean; /** * Attach virtuals to the results of Mongoose queries when using .lean(). * * @default true * * https://plugins.mongoosejs.io/plugins/lean-virtuals * * * ``` * // Define virtual * userSchema.virtual('lowercase').get(function() { * return this.name.toLowerCase(); * }); * * // You **must** pass `virtuals: true` to `lean()`, otherwise `lowercase` * // won't be in `res` * const res = await UserModel.find().lean({ virtuals: true }); * ``` */ leanVirtuals?: boolean; /** * Unique validation custom message * * @default true * * https://www.npmjs.com/package/mongoose-unique-validator */ uniqueValidator?: boolean; /** * Unique validator message * * You can pass through a custom error message as part of the optional options argument: * * You have access to all of the standard Mongoose error message templating: * * {PATH} * {VALUE} * {TYPE} * * @default "Expected {PATH} to be unique." * * https://www.npmjs.com/package/mongoose-unique-validator */ uniqueValidatorMessage?: string; /** * Add globaly a plugin to a mongoose schema * https://mongoosejs.com/docs/plugins.html#global */ plugins?: (schema: Schema) => void; /** * Mongoose schema defining the model */ schema: Schema; /** * Makes the indexes in MongoDB match the indexes defined in this model's schema. This function will drop any indexes that are not defined in the model's schema except the _id index, and build any indexes that are in your schema but not in MongoDB. * * @default true */ syncIndexes?: boolean; /** * syncIndexes options to pass to ensureIndexes() */ syncIndexesOptions?: Record; /** * syncIndexes optional callback */ syncIndexesCallback?: (err: any) => void; /** * Configure the schema created by adding methods, middlewares, virtuals and many other things provided by Mongoose * Methods - https://mongoosejs.com/docs/guide.html#methods * Middlewares - https://mongoosejs.com/docs/middleware.html * Virtuals - https://mongoosejs.com/docs/guide.html#virtuals * * ``` * externalConfig: function (schema) { * // Method example * schema.methods = { * verifyPassword: function (value: string) { * // your code here * }, * }; * * // Middleware example * schema.pre("save", function () { * // thing to do before save the object * }); * * // Virtual example * schema * .virtual("customColumn") * .get(() => { * return "Hey" + "Hello"; * }) * .set((value: string) => { * // your code here * }); * } * ``` */ externalConfig?: (schema: Schema) => void; /** * Define virtuals field * * @description For more flexibility define your virtuals in externalConfig method */ virtuals?: [ { fieldName: string; options?: any; get: (this: T) => any | Promise; set?: (this: T) => void | Promise; } ]; /** * Define model methods * * @description For more flexibility define your virtuals in externalConfig method */ methods?: { [K in keyof Partial]: (this: T, ...rest: any[]) => any | Promise; }; /** * Configure soft delete * https://github.com/dsanel/mongoose-delete * * @default true */ softDelete?: boolean; /** * Soft delete options */ softDeleteOptions?: { /** * Who has deleted the data? */ deletedBy?: boolean; /** * Save time of deletion */ deletedAt?: boolean; /** * We have the option to override all standard methods or only specific methods */ overrideMethods?: boolean | string | string[]; /** * Disable model validation on delete */ validateBeforeDelete?: boolean; /** * Create index on fields */ indexFields?: boolean | string | string[]; /** * Disable use of $ne operator */ use$neOperator?: boolean; [key: string]: any; }; /** * Protect attributes from mass assignment */ protectOptions?: ProtectFuncOptions; /** * Display or hide attributes */ privacyOptions?: PrivacyFuncOptions; /** * Query global filters */ postFilters?: GlobalFilter; /** * On delete options */ onDeleteOptions?: OnDeleteFuncOptions; }; /** * Create a mongoose model * @param params model definition parameters */ export default function mongooseModel(params: MoongooseModelParams): Model;