forked from bro-students/multy-stub
28 lines
649 B
JavaScript
28 lines
649 B
JavaScript
|
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)
|