How to get previous version of the document on update?

Expected behavior

I would like (on update) to get both the updates (OK) and the previous values of the document (which are getting changed)

Actual behavior

I can’t find the current value of the document that will get updated (I need some of them to extend background route, but since I couldn’t find anything, I am making an extra request to MongoDb to get the data I need)

// Update a Outfit
router.put('/OutfitsCollection/:recordId', permissionMiddlewareCreator.update(), async (request, response, next) => {
    // Learn what this route does here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#update-a-record

    let userId = ""

    await OutfitsCollection.findOne({
        '_id': request.params.recordId
    }, function (err, doc) {
        if (err) {
            throw err
        } else {
            userId = doc._doc.user_id
        }
    })

    // ...

});

Context

I need to compare some values (previous and new ones) for somùe required behavior

Thanks

Hello @Thomas :wave:,

I’m not sure to completely get your request.

In order to update your record, you’ll have to fetch it first. The request will always contains the edited parameters (updated), and the fetched (previous) record will always be contains values before update.

Once you get your sequelize/mongoose object, you can do any type of logic before actually updating the record.

Let me know if that helps

Hey :slight_smile:

Well, as from your doc: https://docs.forestadmin.com/documentation/reference-guide/routes/default-routes#update-a-record

...

const {
  PermissionMiddlewareCreator,
  RecordUpdater,
} = require('forest-express-mongoose');
const { companies } = require('../models');

const permissionMiddlewareCreator = new PermissionMiddlewareCreator('companies');

...

// Update a Company - Check out our documentation for more details: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#update-a-record
router.put('/companies/:recordId', permissionMiddlewareCreator.update(), (request, response, next) => {
  const recordUpdater = new RecordUpdater(companies);
  recordUpdater.deserialize(request.body)
    .then(recordToUpdate => recordUpdater.update(recordToUpdate, request.params.recordId))
    .then(record => recordUpdater.serialize(record))
    .then(recordSerialized => response.send(recordSerialized))
    .catch(next);
});
...

Now, before getting to this point of the code, before, as mentioned in the question itself, I have an operation to run. However, within the request parameter I can’t find my current object value, I can only find the updated fields and their new value.

Based on the above statement, I have to make a request to MongoDb in order to get the value of my current object that will be updated

Example:

  • I want to update the name of one of my documents (current name is A). I update it to B

I am getting in the request “Attributes” set with some “name: B” value, but where is the statement that the current value was A until now?

Hi,

What I mean with my previous message is that what you are currently doing is actually the safest (And only) way to deal with what you are trying to achieve.
If you need to update a record based on its current value in database, you must do a database call.

The following snippet will give you the “previous” (The one in database) record, but is pretty similar to what you were doing:

...

const {
  PermissionMiddlewareCreator,
  RecordUpdater,
  RecordGetter,
} = require('forest-express-mongoose');
const { companies } = require('../models');
const permissionMiddlewareCreator = new PermissionMiddlewareCreator('companies');

...

// Update a Company - Check out our documentation for more details: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#update-a-record
router.put('/companies/:recordId', permissionMiddlewareCreator.update(), async (request, response, next) => {
  try {
    const recordGetter = new RecordGetter(companies);
    const recordUpdater = new RecordUpdater(companies);
    // Here is the old record that will be modified
    const recordOld = await recordGetter.get(request.params.recordId);
    // Here is the modification you want to put on the record come from your request
    const recordToUpdate = await recordUpdater.deserialize(request.body);
    // You can easily put your logic you want to do with old record and the value come from request
    // Here is the mechanic to send the updated record
    const recordUpdated = await recordUpdater.update(recordToUpdate, request.params.recordId);
    const recordSerialized = await recordUpdater.serialize(record);
    response.send(recordSerialized);
  } catch(error) {
    next(error);
  }
});

...
2 Likes