|
@@ -0,0 +1,141 @@
|
|
|
+/**
|
|
|
+ * @module RESTControllerSubdoc
|
|
|
+ */
|
|
|
+import * as debugStuff from 'debug'
|
|
|
+const debug = debugStuff.debug('dbApiRESTCtrlSubdoc')
|
|
|
+
|
|
|
+const MAX_RESULTS = 100
|
|
|
+
|
|
|
+/**
|
|
|
+ * REST controller for MongoDB sub-documents.
|
|
|
+ */
|
|
|
+class RESTControllerSubdoc {
|
|
|
+ /**
|
|
|
+ * Generates a controller for a model type.
|
|
|
+ *
|
|
|
+ * @param {mongoose.Model} mongoose model to use
|
|
|
+ * @param {key} Key to use as index. Default is '_id'
|
|
|
+ * @return {null}
|
|
|
+ */
|
|
|
+ constructor (model, key = '_id') {
|
|
|
+ this.model = model
|
|
|
+ this.modelName = model.modelName.toLowerCase()
|
|
|
+ this.key = key
|
|
|
+ this.findOptions = {}
|
|
|
+ this.findCriteria = {}
|
|
|
+ this.extendData = (data) => data
|
|
|
+ this.updateExtendedData = (data) => data
|
|
|
+ debug('created RESTController', this.modelName)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Creates a DB item.
|
|
|
+ *
|
|
|
+ * @param {object} data object
|
|
|
+ * @return {Promise}
|
|
|
+ */
|
|
|
+ create (data) {
|
|
|
+ debug('create data item', data)
|
|
|
+ const newData = this.extendData(data)
|
|
|
+ const p = this.model
|
|
|
+ .create(newData)
|
|
|
+ .then(instance => {
|
|
|
+ debug('created instance', instance)
|
|
|
+ return instance
|
|
|
+ })
|
|
|
+ // debug('create return value', p)
|
|
|
+ return p
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Read a DB item.
|
|
|
+ *
|
|
|
+ * @param {String} id
|
|
|
+ * @return {Promise}
|
|
|
+ */
|
|
|
+ read (id) {
|
|
|
+ debug('reading data item', id)
|
|
|
+ const filter = {}
|
|
|
+ filter[this.key] = id
|
|
|
+
|
|
|
+ const p = this.model
|
|
|
+ .findOne(filter)
|
|
|
+ .then(instance => {
|
|
|
+ debug('read instance', instance)
|
|
|
+ return instance
|
|
|
+ })
|
|
|
+ // debug('read return value', p)
|
|
|
+ return p
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get a list of all DB items.
|
|
|
+ *
|
|
|
+ * @return {Promise}
|
|
|
+ */
|
|
|
+ list () {
|
|
|
+ debug('reading data list')
|
|
|
+ const p = this.model
|
|
|
+ .find({})
|
|
|
+ .limit(MAX_RESULTS)
|
|
|
+ .then(instance => {
|
|
|
+ debug('read list instance', instance)
|
|
|
+ return instance
|
|
|
+ })
|
|
|
+ // debug('read return value', p)
|
|
|
+ return p
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Update a DB item.
|
|
|
+ *
|
|
|
+ * @param {String} id
|
|
|
+ * @param {String} data
|
|
|
+ * @return {Promise}
|
|
|
+ */
|
|
|
+ update (id, data) {
|
|
|
+ debug('update data item', id, data)
|
|
|
+ const filter = {}
|
|
|
+ filter[this.key] = id
|
|
|
+
|
|
|
+ const p = this.model
|
|
|
+ .findOne(filter)
|
|
|
+ .then(instance => {
|
|
|
+ debug('found data instance', instance)
|
|
|
+ for (let attribute in data) {
|
|
|
+ if (data.hasOwnProperty(attribute) && attribute !== this.key && attribute !== '_id') {
|
|
|
+ instance[attribute] = data[attribute]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return instance.save()
|
|
|
+ })
|
|
|
+ .then(instance => {
|
|
|
+ debug('updated data instance', instance)
|
|
|
+ return instance
|
|
|
+ })
|
|
|
+ // debug('read return value', p)
|
|
|
+ return p
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Delete a DB item.
|
|
|
+ *
|
|
|
+ * @param {String} id
|
|
|
+ * @return {Promise}
|
|
|
+ */
|
|
|
+ delete (id) {
|
|
|
+ debug('delete data item')
|
|
|
+ const filter = {}
|
|
|
+ filter[this.key] = id
|
|
|
+
|
|
|
+ const p = this.model
|
|
|
+ .remove(filter)
|
|
|
+ .then(() => {
|
|
|
+ return {}
|
|
|
+ })
|
|
|
+ // debug('read return value', p)
|
|
|
+ return p
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export default RESTControllerSubdoc
|