diff options
author | 2021-09-09 23:23:53 +0200 | |
---|---|---|
committer | 2021-09-09 23:23:53 +0200 | |
commit | a863c77ea12a4c7060b9c496e5df81bbdcca2374 (patch) | |
tree | 8d38ebc21928fa973235d8829bf1c6f7d65028df /models/NoteList.js | |
parent | 569bdb8c5d7538fa0ea8a99ff2f8376f7cbfa51a (diff) | |
download | my_apps-a863c77ea12a4c7060b9c496e5df81bbdcca2374.tar.gz my_apps-a863c77ea12a4c7060b9c496e5df81bbdcca2374.tar.bz2 my_apps-a863c77ea12a4c7060b9c496e5df81bbdcca2374.zip |
add db transactions, move db models
Diffstat (limited to 'models/NoteList.js')
-rw-r--r-- | models/NoteList.js | 66 |
1 files changed, 0 insertions, 66 deletions
diff --git a/models/NoteList.js b/models/NoteList.js deleted file mode 100644 index bf7b155..0000000 --- a/models/NoteList.js +++ /dev/null @@ -1,66 +0,0 @@ -const {encrypt, decrypt} = require('helpers/crypt') -const mongoose = require("mongoose") - -const decryptTitles = l => ({notes: l.notes.map(n => ({ ...n, title: decrypt(n.title)}))}) - -const noteListSchema = new mongoose.Schema({ - notes: [{ - title: { - type: String, - maxlength: 1000, - required: true, - }, - noteId: { - type: mongoose.Schema.Types.ObjectId, - ref: "Note", - required: true, - }, - created_at: {type: Date, default: Date.now}, - updated_at: {type: Date, default: Date.now} - }] -}) - -noteListSchema.statics.getList = async (id) => { - const newList = await NoteList.findById(id).lean() - - return {notes: newList.notes.map(n => ({ ...n, title: decrypt(n.title)}))} -} - -noteListSchema.statics.getNoteId = async (_id, id) => { - const noteList = await NoteList.findById(_id).lean() - return noteList.notes - .find(n => n._id.toString() === id.toString()).noteId -} - -noteListSchema.statics.addNote = async (_id, noteId, title) => { - const noteList = await NoteList.findOneAndUpdate( - {_id}, {$push: {notes: {title: encrypt(title ? title : 'no title'), noteId}}}, {new: true} - ).lean() - - return decryptTitles(noteList) -} - -noteListSchema.statics.removeNote = async (_id, id) => { - const noteList = await NoteList.findOneAndUpdate( - {_id}, {$pull: {notes: {_id: id}}} - ).lean() - - return decryptTitles(noteList) -} - -noteListSchema.statics.updateList = async (_id, noteId, title) => { - const noteList = await NoteList.findOneAndUpdate( - {_id, "notes.noteId": noteId}, - {$set: { - "notes.$.title": encrypt(title ? title : 'No title'), - "notes.$.updated_at": Date.now(), - }}, - {new: true} - ).lean() - - return decryptTitles(noteList) -} - -const NoteList = mongoose.models.NoteList || mongoose.model('NoteList', noteListSchema) - -export default NoteList |