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. for (let attribute in data) {
  71. if (data.hasOwnProperty(attribute) && attribute !== this.key && attribute !== '_id') {
  72. instance[attribute] = data[attribute]
  73. }
  74. }
  75. return instance.save()
  76. })
  77. .then(instance => {
  78. const response = {}
  79. response[this.modelName] = instance
  80. return response
  81. })
  82. }
  83. delete (id) {
  84. const filter = {}
  85. filter[this.key] = id
  86. return this.model
  87. .remove(filter)
  88. .then(() => {
  89. return {}
  90. })
  91. }
  92. route () {
  93. const router = new Router()
  94. router.get('/', (req, res) => {
  95. this
  96. .list()
  97. .then(data => res.json(data))
  98. .then(null, error => res.status(404).send(error))
  99. })
  100. router.post('/', (req, res) => {
  101. this
  102. .create(req.body)
  103. .then(data => res.json(data))
  104. .then(null, error => res.status(404).send(error))
  105. })
  106. router.get('/:key', (req, res) => {
  107. this
  108. .read(req.params.key)
  109. .then(data => res.json(data))
  110. .then(null, error => res.status(404).send(error))
  111. })
  112. router.put('/:key', (req, res) => {
  113. this
  114. .update(req.params.key, req.body)
  115. .then(data => res.json(data))
  116. .then(null, error => res.status(404).send(error))
  117. })
  118. router.delete('/:key', (req, res) => {
  119. this
  120. .delete(req.params.key)
  121. .then(data => res.json(data))
  122. .then(null, error => res.status(404).send(error))
  123. })
  124. return router
  125. }
  126. }
  127. debug('Defined RESTController')
  128. export default RESTController