Related data deleting and creating error when flattened fields is working

Feature(s) impacted

deleting and creating new records in related data when none existing

Observed behavior

when any fields are flattened, related data uses an object instead of an array and causes an error

Expected behavior

deleting and creating related data as usual

Context

  • Project name: WescoverContentAdmin
  • Team name: Content Admin
  • Environment name: Development | nimrodmaltz
  • Agent type & version: “forest-express-mongoose”: “8.7.6”
  • Recent changes made on your end if any: no recent changes

hello again,

we have a found a bug on the related data section, we are unable to delete records and neither add new records when none existing. this bug is observed whenever there are fields to flatten.
we observed that the mongoose call to set is occurring with an object instead of an array:

'$set': { 'images.0.publicId': 'hello', 'images.0.type': 'world', 'images.1.publicId': 'hello2', 'images.1.type': 'world2', modifiedAt: new Date("Tue, 20 Dec 2022 10:55:40 GMT") }}

where it should occur using an array:

 '$set': { images: [ { publicId: 'hello', type: 'world' } ], modifiedAt: new Date("Tue, 20 Dec 2022 10:51:17 GMT") }}

the second quote is the right way, but occurs only when not using any fields to flatten.
the first one, is what happens when using fields to flatten in the liana collection.
this call doesnt achieve the goal, it doesnt delete nor add new ones correctly.
id be happy to elaborate if needed.

in order to reproduce, just create a collection with related data and some nested fields flattened using the fieldsToFlatten attribute in the liana collection and try deleting a record from related data, it should not work.

thanks in advance,
kind regards,
nimrod

Hello @nmz,

I have some difficulties to reproduce the bug on my side. Can you send me your mongoose schema and an example of data that doesn’t work ( in public on this thread if you don’t mind or in private message ) :pray:.

Kind regards,

Florian

@Florian_Gonzales Hi!
Here is a basic schema + repro that we double-checked just now:

mongoose Schema:

import { Schema } from 'mongoose';

const ProductImageSchema = new Schema(
  {
    publicId: String,
    type: String,
  },
  { _id: false }
);

const schema = Schema(
  {
    deepField: {
      someVal: String
    },
    images: [ProductImageSchema],
  },
  {
    timestamps: { updatedAt: 'modifiedAt' },
  }
);

export default schema;

“Forest” Liana Collections config:

import Liana from 'forest-express-mongoose';

Liana.collection('Product', {
  fieldsToFlatten: ['deepField']
});

Output of deleting a single image from the images field (at the related data view) - not working as expected:

'$set': { modifiedAt: new Date("Wed, 21 Dec 2022 07:17:51 GMT") }

Note - this does not clear the single image - the logic is wrong

And if we had 2 images - trying to delete one of them:

Would produce this (wrong) output:

'$set': {
  'images.0.publicId': 'img2', // Causes the 2nd image to fill the place of the 1st image duplicating it!!
  'images.0.type': 'alone',
  modifiedAt: new Date("Wed, 21 Dec 2022 07:26:12 GMT")
}

Avoiding the problem - not using flatten

If we change the config of the Liana collection to this:

import Liana from 'forest-express-mongoose';

Liana.collection('Product', {
  //fieldsToFlatten: ['deepField']
});

Output of deleting a single images from the images field is the correct expected behavior:

'$set': { images: [], modifiedAt: new Date("Wed, 21 Dec 2022 07:18:25 GMT") }

And if we had 2 images - trying to delete one of them produces this correct result:

'$set': {
  images: [ { publicId: 'img2', type: 'alone' } ], // This is great!
  modifiedAt: new Date("Wed, 21 Dec 2022 07:28:46 GMT")
}

Hopefully eng. can take it from here.

2 Likes