RESTController.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @module RESTController
  3. *
  4. * RESTController for MongoDB.
  5. */
  6. import * as debugStuff from 'debug'
  7. const debug = debugStuff.debug('dbApiRESTCtrl')
  8. import mongoose from 'mongoose'
  9. mongoose.Promise = Promise
  10. import { Router } from 'express'
  11. const MAX_RESULTS = 100
  12. /**
  13. * REST controller for MongoDB backend.
  14. */
  15. class RESTController {
  16. /**
  17. * Generates a controller for a model type.
  18. *
  19. * @param {mongoose.Model} mongoose model to use
  20. * @param {key} Key to use as index. Default is '_id'
  21. * @return {null}
  22. */
  23. constructor (model, key = '_id') {
  24. this.model = model
  25. this.modelName = model.modelName.toLowerCase()
  26. this.key = key
  27. }
  28. /**
  29. * Creates a DB item.
  30. *
  31. * @param {object} data object
  32. * @return {null}
  33. */
  34. create (data) {
  35. return this.model
  36. .create(data)
  37. .then(instance => {
  38. const response = {}
  39. response[this.modelName] = instance
  40. return response
  41. })
  42. }
  43. read (id) {
  44. const filter = {}
  45. filter[this.key] = id
  46. return this.model
  47. .findOne(filter)
  48. .then(instance => {
  49. const response = {}
  50. response[this.modelName] = instance
  51. return response
  52. })
  53. }
  54. list () {
  55. return this.model
  56. .find({})
  57. .limit(MAX_RESULTS)
  58. .then(instance => {
  59. const response = {}
  60. response[this.modelName] = instance
  61. return response
  62. })
  63. }
  64. update (id, data) {
  65. const filter = {}
  66. filter[this.key] = id
  67. return this.model
  68. .findOne(filter)
  69. .then(instance => {
  70. console.log(instance)
  71. for (let attribute in data) {
  72. if (data.hasOwnProperty(attribute) && attribute !== this.key && attribute !== '_id') {
  73. instance[attribute] = data[attribute]
  74. }
  75. }
  76. return instance.save()
  77. })
  78. .then(instance => {
  79. const response = {}
  80. response[this.modelName] = instance
  81. return response
  82. })
  83. }
  84. delete (id) {
  85. const filter = {}
  86. filter[this.key] = id
  87. return this.model
  88. .remove(filter)
  89. .then(() => {
  90. return {}
  91. })
  92. }
  93. route () {
  94. const router = new Router()
  95. router.get('/', (req, res) => {
  96. this
  97. .list()
  98. .then(data => res.json(data))
  99. .then(null, error => res.status(404).send(error))
  100. })
  101. router.post('/', (req, res) => {
  102. this
  103. .create(req.body)
  104. .then(data => res.json(data))
  105. .then(null, error => res.status(404).send(error))
  106. })
  107. router.get('/:key', (req, res) => {
  108. this
  109. .read(req.params.key)
  110. .then(data => res.json(data))
  111. .then(null, error => res.status(404).send(error))
  112. })
  113. router.put('/:key', (req, res) => {
  114. this
  115. .update(req.params.key, req.body)
  116. .then(data => res.json(data))
  117. .then(null, error => res.status(404).send(error))
  118. })
  119. router.delete('/:key', (req, res) => {
  120. this
  121. .delete(req.params.key)
  122. .then(data => res.json(data))
  123. .then(null, error => res.status(404).send(error))
  124. })
  125. return router
  126. }
  127. }
  128. debug('Defined RESTController')
  129. export default RESTController