|
@@ -1,7 +1,5 @@
|
|
|
/**
|
|
|
- * @module RESTControllerVersionned
|
|
|
- *
|
|
|
- * RESTController with revision control for MongoDB.
|
|
|
+ * @module RESTControllerVersioned
|
|
|
*/
|
|
|
import * as debugStuff from 'debug'
|
|
|
const debug = debugStuff.debug('dbApiRESTCtrlVer')
|
|
@@ -13,8 +11,8 @@ const MAX_RESULTS = 100
|
|
|
* REST controller for MongoDB backend.
|
|
|
*/
|
|
|
class RESTControllerVersioned extends RESTController {
|
|
|
- constructor (model, key) {
|
|
|
- super(model, key)
|
|
|
+ constructor (model, key = '_id', subDocuments = {}) {
|
|
|
+ super(model, key, subDocuments)
|
|
|
this.filterOptions = { limit: MAX_RESULTS, skip: 0 }
|
|
|
this.filterCriteria = { '_history.head': true }
|
|
|
debug('created RESTControllerVersioned', this.modelName)
|
|
@@ -23,11 +21,15 @@ class RESTControllerVersioned extends RESTController {
|
|
|
/**
|
|
|
* Override create function to support versioning.
|
|
|
*
|
|
|
+ * This function generates a history object to allow revision control
|
|
|
+ * of the data elements.
|
|
|
+ *
|
|
|
* @param {object} data object
|
|
|
* @return {Promise}
|
|
|
*/
|
|
|
- create (data, author, version = '', comment = '') {
|
|
|
+ create (data) {
|
|
|
debug('create data item', data)
|
|
|
+ const { author, version, comment, ...newData } = data
|
|
|
const history = {
|
|
|
author,
|
|
|
created: new Date(),
|
|
@@ -36,7 +38,7 @@ class RESTControllerVersioned extends RESTController {
|
|
|
head: true,
|
|
|
reference: []
|
|
|
}
|
|
|
- const newData = { ...data, _history: history }
|
|
|
+ newData._history = history
|
|
|
const p = this.model
|
|
|
.create(newData)
|
|
|
.then(instance => {
|
|
@@ -95,7 +97,7 @@ class RESTControllerVersioned extends RESTController {
|
|
|
* @param {String} data
|
|
|
* @return {Promise}
|
|
|
*/
|
|
|
- update (tag, data, author, version = '', comment = '') {
|
|
|
+ update (tag, data) {
|
|
|
if (typeof tag === 'string') {
|
|
|
tag = { tag: tag }
|
|
|
}
|
|
@@ -112,7 +114,8 @@ class RESTControllerVersioned extends RESTController {
|
|
|
.then(instance => {
|
|
|
const { _id, __v, _history, ...oldData } = instance._doc
|
|
|
debug('creating new data instance', oldData)
|
|
|
- const newData = { ...oldData, ...data }
|
|
|
+ const { author, version, comment, ...newItems } = data
|
|
|
+ const newData = { ...oldData, ...newItems }
|
|
|
newData._history = {
|
|
|
author,
|
|
|
created: new Date(),
|