RestControllerVersioned.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @module RESTControllerVersionned
  3. *
  4. * RESTController with revision control for MongoDB.
  5. */
  6. import * as debugStuff from 'debug'
  7. const debug = debugStuff.debug('dbApiRESTCtrlVer')
  8. import RESTController from './RESTController'
  9. const MAX_RESULTS = 100
  10. /**
  11. * REST controller for MongoDB backend.
  12. */
  13. class RESTControllerVersioned extends RESTController {
  14. constructor (model, key) {
  15. super(model, key)
  16. this.filterOptions = { limit: MAX_RESULTS, skip: 0 }
  17. this.filterCriteria = { '_history.head': true }
  18. debug('created RESTControllerVersioned', this.modelName)
  19. }
  20. /**
  21. * Override create function to support versioning.
  22. *
  23. * @param {object} data object
  24. * @return {Promise}
  25. */
  26. create (data, author, version = '', comment = '') {
  27. debug('create data item', data)
  28. const history = {
  29. author,
  30. created: new Date(),
  31. version,
  32. comment,
  33. head: true,
  34. reference: []
  35. }
  36. const newData = { ...data, _history: history }
  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. * Override read to search by default for head version of given tag.
  48. *
  49. * @param {[type]}
  50. * @return {[type]}
  51. */
  52. read (tag) {
  53. if (typeof tag === 'string') {
  54. tag = { tag: tag }
  55. }
  56. debug('reading data item', tag)
  57. const filter = { ...this.filterCriteria, ...tag }
  58. const p = this.model
  59. .findOne(filter)
  60. .then(instance => {
  61. debug('read instance', instance)
  62. return instance
  63. })
  64. // debug('read return value', p)
  65. return p
  66. }
  67. /**
  68. * Get a list of all DB items.
  69. *
  70. * @return {Promise}
  71. */
  72. list (filter = this.filterCriteria) {
  73. debug('reading data list')
  74. const p = this.model
  75. .find(filter)
  76. .limit(MAX_RESULTS)
  77. .then(instance => {
  78. debug('read list instance', instance)
  79. return instance
  80. })
  81. // debug('read return value', p)
  82. return p
  83. }
  84. /**
  85. * Override update to support versioning.
  86. *
  87. * @param {String} id
  88. * @param {String} data
  89. * @return {Promise}
  90. */
  91. update (tag, data, author, version = '', comment = '') {
  92. if (typeof tag === 'string') {
  93. tag = { tag: tag }
  94. }
  95. debug('update data item', tag, data)
  96. const filter = { ...this.filterCriteria, ...tag }
  97. const p = this.model
  98. .findOne(filter)
  99. .then(instance => {
  100. debug('found data instance', instance)
  101. instance._history.head = false
  102. return instance.save()
  103. })
  104. .then(instance => {
  105. const { _id, __v, _history, ...oldData } = instance._doc
  106. debug('creating new data instance', oldData)
  107. const newData = { ...oldData, ...data }
  108. newData._history = {
  109. author,
  110. created: new Date(),
  111. version,
  112. comment,
  113. head: true,
  114. reference: [ _id, ..._history.reference ]
  115. }
  116. return this.model
  117. .create(newData)
  118. .then(instance => {
  119. debug('created instance', instance)
  120. return instance
  121. })
  122. })
  123. // debug('read return value', p)
  124. return p
  125. }
  126. /**
  127. * Override delete. We don't delete. Just not our thing...
  128. *
  129. * @param {String} id
  130. * @return {Promise}
  131. */
  132. delete (tag) {
  133. if (typeof tag === 'string') {
  134. tag = { tag: tag }
  135. }
  136. debug('delete data item', tag)
  137. const filter = { ...this.filterCriteria, ...tag }
  138. const p = this.model
  139. .findOne(filter)
  140. .then(instance => {
  141. instance._history.head = false
  142. return instance.save()
  143. })
  144. // debug('read return value', p)
  145. return p
  146. }
  147. }
  148. export default RESTControllerVersioned