From a863c77ea12a4c7060b9c496e5df81bbdcca2374 Mon Sep 17 00:00:00 2001 From: piotrruss Date: Thu, 9 Sep 2021 23:23:53 +0200 Subject: add db transactions, move db models --- models/Note.js | 37 ------------------------------------- 1 file changed, 37 deletions(-) delete mode 100644 models/Note.js (limited to 'models/Note.js') diff --git a/models/Note.js b/models/Note.js deleted file mode 100644 index d19eae8..0000000 --- a/models/Note.js +++ /dev/null @@ -1,37 +0,0 @@ -const {encrypt, decrypt} = require('helpers/crypt') -const mongoose = require('mongoose') - -const noteSchema = new mongoose.Schema({ - content: {type: String, required: true, minlength: 1}, -}) - -noteSchema.statics.getNote = async (id) => { - const note = await Note.findById(id) - if (!note) throw new Error('Could not fetch note') - - const content = decrypt(note.content) - - return {...note, content} -} - -noteSchema.statics.updateNote = async (id, content) => { - const note = await Note.findByIdAndUpdate(id, {content: encrypt(content)}, {new: true}) - - if (!note) throw new Error('Could not update note') - - return {...note, content} -} - -noteSchema.pre('save', async function(next){ - const note = this; - - if (note.isModified('content')) { - note.content = encrypt(note.content) - } - - next() -}) - -const Note = mongoose.models.Note || mongoose.model('Note', noteSchema) - -export default Note -- cgit v1.2.3