Untitled
unknown
typescript
a month ago
1.3 kB
1
Indexable
Never
import { model, PaginateModel, Schema } from "mongoose"; import paginate from "mongoose-paginate-v2"; import { IVersion, IVersionModel } from "../../interfaces"; import { Product } from "../product/model"; const schema = new Schema<IVersion, IVersionModel>({ version: { type: String, required: [true, 'Version is required'], trim: true, }, description: { type: String, required: [true, 'Description is required'], trim: true, }, changes: { type: String, required: [true, 'Changes are required'], trim: true, }, releaseDate: { type: Date, required: [true, 'Release Date is required'], validate: { validator: (value: Date) => { return new Date() <= value; }, message: 'Release Date must be in the past.' } }, }, { timestamps: true }); schema.post('remove', { document: true, query: false }, async function (doc, next) { await Product.updateMany({}, { $pull: { versions: doc._id } }); next(); }); schema.plugin(paginate); schema.methods.toJSON = function () { const version = this.toObject(); delete version.__v; delete version.createdAt; delete version.updatedAt; return JSON.parse(JSON.stringify(version).replace(/_id/g, 'id')); }; export const Version = model<IVersion, PaginateModel<IVersionModel>>('Version', schema, 'versions');
Leave a Comment