ForestAdmin/forest-express-mongoose

View on GitHub
src/services/belongs-to-updater.js

Summary

Maintainability
A
1 hr
Test Coverage
A
100%
import Flattener from './flattener';

class BelongsToUpdater {
  constructor(model, association, opts, params, data) {
    this._model = model;
    this._params = params;
    this._data = data;
  }

  async perform() {
    const updateParams = {};
    updateParams[
      Flattener.unflattenFieldName(this._params.associationName)
    ] = this._data.data ? this._data.data.id : null;

    return this._model
      .findByIdAndUpdate(this._params.recordId, {
        $set: updateParams,
      }, {
        new: true,
      })
      .lean()
      .exec();
  }
}

module.exports = BelongsToUpdater;