RESTControllerSubdoc.js 2.9 KB

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