multy-stub/server/data/model/todo/list.js

28 lines
649 B
JavaScript
Raw Permalink Normal View History

2024-10-16 11:06:23 +03:00
const { Schema, model } = require('mongoose')
const { TODO_LIST_MODEL_NAME, TODO_ITEM_MODEL_NAME } = require('../../const')
const schema = new Schema({
title: String,
created: {
type: Date, default: () => new Date().toISOString(),
},
items: [{ type: Schema.Types.ObjectId, ref: TODO_ITEM_MODEL_NAME }],
})
schema.set('toJSON', {
virtuals: true,
versionKey: false,
})
schema.virtual('id').get(function () {
return this._id.toHexString()
})
schema.method('addItem', async function (itemObjectId) {
this.items.push(itemObjectId)
await this.save()
})
exports.ListModel = model(TODO_LIST_MODEL_NAME, schema)