This commit is contained in:
Primakov Alexandr Alexandrovich
2025-01-18 17:45:16 +03:00
parent d2d8b63aba
commit 86b79a7f49
7 changed files with 118 additions and 69 deletions

View File

@@ -0,0 +1,27 @@
const { Schema, model } = require('mongoose')
const { TODO_AUTH_COMMENTS_MODEL_NAME, TODO_AUTH_USER_MODEL_NAME } = require('../../const')
const schema = new Schema({
text: String,
created: {
type: Date, default: () => new Date().toISOString(),
},
answerTo: { type: Schema.Types.ObjectId, ref: TODO_AUTH_COMMENTS_MODEL_NAME },
createdBy: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME },
author: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME },
})
schema.set('toJSON', {
virtuals: true,
versionKey: false,
transform: function (doc, ret) {
delete ret._id
}
})
schema.virtual('id').get(function () {
return this._id.toHexString()
})
exports.CommentModel = model(TODO_AUTH_COMMENTS_MODEL_NAME, schema)

View File

@@ -1,24 +1,37 @@
const { Schema, model } = require('mongoose')
const { Schema, model } = require("mongoose");
const { TODO_ITEM_MODEL_NAME, TODO_AUTH_USER_MODEL_NAME } = require('../../const')
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(),
},
createdBy: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME },
})
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.set("toJSON", {
virtuals: true,
versionKey: false,
});
schema.virtual('id').get(function () {
return this._id.toHexString()
})
schema.virtual("id").get(function () {
return this._id.toHexString();
});
exports.ItemModel = model(TODO_ITEM_MODEL_NAME, schema)
schema.method('addComment', async function (commentId) {
this.comments.push(commentId)
await this.save()
})
exports.ItemModel = model(TODO_ITEM_MODEL_NAME, schema);