multy-stub/server/routers/todo/model/todo/item.js
Primakov Alexandr Alexandrovich 86b79a7f49 comments
2025-01-18 17:45:16 +03:00

38 lines
849 B
JavaScript

const { Schema, model } = require("mongoose");
const {
TODO_ITEM_MODEL_NAME,
TODO_AUTH_USER_MODEL_NAME,
TODO_AUTH_COMMENTS_MODEL_NAME,
} = require("../../const");
const schema = new Schema({
title: String,
done: { type: Boolean, default: false },
closed: Date,
created: {
type: Date,
default: () => new Date().toISOString(),
},
comments: [
{ type: Schema.Types.ObjectId, ref: TODO_AUTH_COMMENTS_MODEL_NAME },
],
createdBy: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME },
});
schema.set("toJSON", {
virtuals: true,
versionKey: false,
});
schema.virtual("id").get(function () {
return this._id.toHexString();
});
schema.method('addComment', async function (commentId) {
this.comments.push(commentId)
await this.save()
})
exports.ItemModel = model(TODO_ITEM_MODEL_NAME, schema);