diff --git a/packages/datasource-mongoose/src/collection.ts b/packages/datasource-mongoose/src/collection.ts index b7d03f371..929bc53c3 100644 --- a/packages/datasource-mongoose/src/collection.ts +++ b/packages/datasource-mongoose/src/collection.ts @@ -104,9 +104,9 @@ export default class MongooseCollection extends BaseCollection { if (this.stack.length < 2) { const { insertedIds } = await this.model.insertMany(data, { rawResult: true }); - return flatData.map((record, index) => ({ + return flatData.map((flatRecord, index) => ({ _id: replaceMongoTypes(insertedIds[index]), - ...record, + ...flatRecord, })); } @@ -125,7 +125,8 @@ export default class MongooseCollection extends BaseCollection { const updates: Record = {}; const results = []; - for (const record of data) { + for (const [index, record] of data.entries()) { + const flatRecord = flatData[index]; const { parentId, ...rest } = record; if (!parentId) throw new ValidationError('Trying to create a subrecord with no parent'); @@ -138,7 +139,7 @@ export default class MongooseCollection extends BaseCollection { results.push({ _id: `${rootId}.${path}.${updates[rootIdString].records.length - 1}`, - ...record, + ...flatRecord, }); } diff --git a/packages/datasource-mongoose/test/integration/flattener/_build-models.ts b/packages/datasource-mongoose/test/integration/flattener/_build-models.ts index 3563747f1..79496de7d 100644 --- a/packages/datasource-mongoose/test/integration/flattener/_build-models.ts +++ b/packages/datasource-mongoose/test/integration/flattener/_build-models.ts @@ -21,7 +21,9 @@ export default async function setupFlattener(dbName = 'test') { }), horsePower: String, owner: { type: Schema.Types.ObjectId, ref: 'companies' }, - comments: [{ date: Date, comment: String }], + comments: [ + { date: Date, comment: String, nested: { subNested: String, subNested2: String } }, + ], companies: [{ type: Schema.Types.ObjectId, ref: 'companies' }], }, company: { type: Schema.Types.ObjectId, ref: 'companies' }, diff --git a/packages/datasource-mongoose/test/integration/flattener/collection_create.test.ts b/packages/datasource-mongoose/test/integration/flattener/collection_create.test.ts index 8adf9c94c..74c2f013f 100644 --- a/packages/datasource-mongoose/test/integration/flattener/collection_create.test.ts +++ b/packages/datasource-mongoose/test/integration/flattener/collection_create.test.ts @@ -131,6 +131,44 @@ describe('Complex flattening', () => { ); }); + /** @see https://community.forestadmin.com/t/bug-report-with-smart-models-on-node-js-agent/6480/6 */ + it('should work when creating a submodel with nested fields', async () => { + const flatRecord = { + date: '2010-01-01T00:00:00.000Z', + comment: 'hi!', + 'nested@@@subNested': 'hi!', + 'nested@@@subNested2': 'hi!', + }; + + connection = await setupFlattener('collection_flattener_create'); + + const dataSource = new MongooseDatasource(connection, { + flattenMode: 'manual', + flattenOptions: { + cars: { + asFields: ['engine.comments.nested'], + asModels: ['engine.comments'], + }, + }, + }); + + const [car] = await dataSource.getCollection('cars').create(caller, [{ name: 'my fiesta' }]); + const [carCommentFromApp] = await dataSource + .getCollection('cars_engine_comments') + .create(caller, [{ parentId: car._id, ...flatRecord }]); + + const carSeenFromDb = await connection.model('cars').findOne({ _id: car._id }); + + expect(carCommentFromApp).toMatchObject(flatRecord); + expect(carSeenFromDb.engine.comments[0]).toEqual( + expect.objectContaining({ + date: expect.any(Date), + comment: 'hi!', + nested: { subNested: 'hi!', subNested2: 'hi!' }, + }), + ); + }); + it('creating a subModel should fail', async () => { connection = await setupFlattener('collection_flattener_create');