Skip to content

Commit e894acd

Browse files
jcobisCopilot
andauthored
feat(compass-collection): Always override _id to mongodbObjectId and type ObjectId MDG - CLOUDP-360163 (#7580)
* WIP * Update packages/compass-collection/src/modules/collection-tab.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * WIP * WIP * WIP --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 8ec8268 commit e894acd

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

packages/compass-collection/src/components/mock-data-generator-modal/script-generation-utils.spec.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from 'chai';
22
import { faker } from '@faker-js/faker/locale/en';
3+
import { ObjectId } from 'bson';
34
import { generateScript, generateDocument } from './script-generation-utils';
45
import type { FakerFieldMapping } from './types';
56
import { UNRECOGNIZED_FAKER_METHOD } from '../../modules/collection-tab';
@@ -26,17 +27,19 @@ function testDocumentCodeExecution(script: string): any {
2627
const returnExpression = returnMatch![1];
2728

2829
// Create a new function
29-
// This is equivalent to: function(faker) { return <returnExpression>; }
30+
// This is equivalent to: function(faker, ObjectId) { return <returnExpression>; }
3031
// The 'faker' parameter will receive the real faker.js library when we pass it in on call
32+
// The 'ObjectId' parameter will receive the BSON ObjectId constructor
3133
// eslint-disable-next-line @typescript-eslint/no-implied-eval
3234
const generateDocument = new Function(
3335
'faker', // Parameter name for the faker library
36+
'ObjectId', // Parameter name for the ObjectId constructor
3437
`return ${returnExpression};` // Function body: return the document structure
3538
);
3639

37-
// Execute the function with the real faker library
40+
// Execute the function with the real faker library and ObjectId constructor
3841
// This actually generates a document using faker methods and returns it
39-
return generateDocument(faker);
42+
return generateDocument(faker, ObjectId);
4043
}
4144

4245
describe('Script Generation', () => {
@@ -941,10 +944,49 @@ describe('Script Generation', () => {
941944

942945
expect(result.success).to.equal(true);
943946
if (result.success) {
944-
expect(result.script).to.contain('faker.database.mongodbObjectId()');
947+
expect(result.script).to.contain('new ObjectId()');
945948

946949
// Test that the generated document code is executable
947-
testDocumentCodeExecution(result.script);
950+
const document = testDocumentCodeExecution(result.script);
951+
expect(document).to.be.an('object');
952+
expect(document).to.have.property('unknownId');
953+
expect(document.unknownId).to.be.instanceOf(ObjectId);
954+
}
955+
});
956+
957+
it('should use direct new ObjectId() generation for ObjectId fields', () => {
958+
const schema = {
959+
_id: {
960+
mongoType: 'ObjectId' as const,
961+
fakerMethod: 'database.mongodbObjectId',
962+
fakerArgs: [],
963+
},
964+
movie_id: {
965+
mongoType: 'ObjectId' as const,
966+
fakerMethod: 'database.mongodbObjectId',
967+
fakerArgs: [],
968+
},
969+
};
970+
971+
const result = generateScript(schema, {
972+
databaseName: 'testdb',
973+
collectionName: 'test',
974+
documentCount: 1,
975+
});
976+
977+
expect(result.success).to.equal(true);
978+
if (result.success) {
979+
// Check that ObjectId fields use direct new ObjectId() generation
980+
expect(result.script).to.contain('_id: new ObjectId()');
981+
expect(result.script).to.contain('movie_id: new ObjectId()');
982+
983+
// Test that the generated document code is executable and produces ObjectId instances
984+
const document = testDocumentCodeExecution(result.script);
985+
expect(document).to.be.an('object');
986+
expect(document).to.have.property('_id');
987+
expect(document).to.have.property('movie_id');
988+
expect(document._id).to.be.instanceOf(ObjectId);
989+
expect(document.movie_id).to.be.instanceOf(ObjectId);
948990
}
949991
});
950992

packages/compass-collection/src/components/mock-data-generator-modal/script-generation-utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,11 @@ function generateFakerCall(mapping: FakerFieldMapping): string {
484484
? getDefaultFakerMethod(mapping.mongoType)
485485
: mapping.fakerMethod;
486486

487+
// Use direct ObjectId generation for MongoDB ObjectIds
488+
if (method === 'database.mongodbObjectId') {
489+
return 'new ObjectId()';
490+
}
491+
487492
const args = formatFakerArgs(mapping.fakerArgs);
488493
return `faker.${method}(${args})`;
489494
}

packages/compass-collection/src/modules/collection-tab.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,16 @@ const validateFakerSchema = (
961961
}
962962
}
963963

964+
// Always override _id field to use mongodbObjectId
965+
if (result._id) {
966+
result._id = {
967+
mongoType: 'ObjectId',
968+
fakerMethod: 'database.mongodbObjectId',
969+
fakerArgs: [],
970+
probability: result._id.probability ?? 1.0,
971+
};
972+
}
973+
964974
return result;
965975
};
966976

0 commit comments

Comments
 (0)