Data.js 902 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict'
  2. import DataModel from '../models/Data'
  3. import { dbLogger, TEST_MODE } from '../../../config'
  4. import { formatLog } from '../../functions'
  5. export default class Data {
  6. static get Model() {
  7. return DataModel
  8. }
  9. static log(data) {
  10. if (!TEST_MODE) dbLogger.info(formatLog(data))
  11. }
  12. static async add(dataObj) {
  13. const doc = await DataModel.create({ data: dataObj })
  14. this.log(`New document was added. id=${doc.id}`)
  15. return doc
  16. }
  17. static async del(dataId) {
  18. const doc = await DataModel.findByIdAndDelete(dataId)
  19. this.log(`A document was deleted. id=${doc.id}`)
  20. }
  21. static async update(dataId, newDataObj) {
  22. const doc = await DataModel.findByIdAndUpdate(dataId, { $set: { data: newDataObj } }, { new: true })
  23. this.log(`A document was updated. id=${doc.id}`)
  24. return doc
  25. }
  26. static find(dataId) {
  27. return DataModel.findById(dataId)
  28. }
  29. }