RESTController.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. this.findOptions = {}
  28. this.findCriteria = {}
  29. this.extendData = (data) => data
  30. this.updateExtendedData = (data) => data
  31. debug('created RESTController', this.modelName)
  32. }
  33. /**
  34. * Creates a DB item.
  35. *
  36. * @param {object} data object
  37. * @return {Promise}
  38. */
  39. create (data) {
  40. debug('create data item', data)
  41. const newData = this.extendData(data)
  42. const p = this.model
  43. .create(newData)
  44. .then(instance => {
  45. debug('created instance', instance)
  46. return instance
  47. })
  48. // debug('create return value', p)
  49. return p
  50. }
  51. /**
  52. * Read a DB item.
  53. *
  54. * @param {String} id
  55. * @return {Promise}
  56. */
  57. read (id) {
  58. debug('reading data item', id)
  59. const filter = {}
  60. filter[this.key] = id
  61. const p = this.model
  62. .findOne(filter)
  63. .then(instance => {
  64. debug('read instance', instance)
  65. return instance
  66. })
  67. // debug('read return value', p)
  68. return p
  69. }
  70. /**
  71. * Get a list of all DB items.
  72. *
  73. * @return {Promise}
  74. */
  75. list () {
  76. debug('reading data list')
  77. const p = this.model
  78. .find({})
  79. .limit(MAX_RESULTS)
  80. .then(instance => {
  81. debug('read list instance', instance)
  82. return instance
  83. })
  84. // debug('read return value', p)
  85. return p
  86. }
  87. /**
  88. * Update a DB item.
  89. *
  90. * @param {String} id
  91. * @param {String} data
  92. * @return {Promise}
  93. */
  94. update (id, data) {
  95. debug('update data item', id, data)
  96. const filter = {}
  97. filter[this.key] = id
  98. const p = this.model
  99. .findOne(filter)
  100. .then(instance => {
  101. debug('found data instance', instance)
  102. for (let attribute in data) {
  103. if (data.hasOwnProperty(attribute) && attribute !== this.key && attribute !== '_id') {
  104. instance[attribute] = data[attribute]
  105. }
  106. }
  107. return instance.save()
  108. })
  109. .then(instance => {
  110. debug('updated data instance', instance)
  111. return instance
  112. })
  113. // debug('read return value', p)
  114. return p
  115. }
  116. /**
  117. * Delete a DB item.
  118. *
  119. * @param {String} id
  120. * @return {Promise}
  121. */
  122. delete (id) {
  123. debug('delete data item')
  124. const filter = {}
  125. filter[this.key] = id
  126. const p = this.model
  127. .remove(filter)
  128. .then(() => {
  129. return {}
  130. })
  131. // debug('read return value', p)
  132. return p
  133. }
  134. route () {
  135. const router = new Router()
  136. router.get('/', (req, res) => {
  137. this
  138. .list()
  139. .then(data => res.json(data))
  140. .catch(error => res.status(404).send(error))
  141. })
  142. router.post('/', (req, res) => {
  143. this
  144. .create(req.body)
  145. .then(data => res.json(data))
  146. .catch(error => res.status(404).send(error))
  147. })
  148. router.get('/:key', (req, res) => {
  149. this
  150. .read(req.params.key)
  151. .then(data => res.json(data))
  152. .catch(error => res.status(404).send(error))
  153. })
  154. router.put('/:key', (req, res) => {
  155. this
  156. .update(req.params.key, req.body)
  157. .then(data => res.json(data))
  158. .catch(error => res.status(404).send(error))
  159. })
  160. router.delete('/:key', (req, res) => {
  161. this
  162. .delete(req.params.key)
  163. .then(data => res.json(data))
  164. .catch(error => res.status(404).send(error))
  165. })
  166. return router
  167. }
  168. }
  169. export default RESTController