Skip to content

Commit 452d8f8

Browse files
committed
refactor: update MongoDBFieldType import paths and improve type usage across mock data generator components
1 parent d1989d9 commit 452d8f8

File tree

9 files changed

+29
-28
lines changed

9 files changed

+29
-28
lines changed

packages/compass-collection/src/components/mock-data-generator-modal/faker-mapping-selector.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '@mongodb-js/compass-components';
1111
import React from 'react';
1212
import { UNRECOGNIZED_FAKER_METHOD } from '../../modules/collection-tab';
13+
import type { MongoDBFieldType } from '@mongodb-js/compass-generative-ai';
1314

1415
const fieldMappingSelectorsStyles = css({
1516
width: '50%',
@@ -26,7 +27,7 @@ const labelStyles = css({
2627
interface Props {
2728
activeJsonType: string;
2829
activeFakerFunction: string;
29-
onJsonTypeSelect: (jsonType: string) => void;
30+
onJsonTypeSelect: (jsonType: MongoDBFieldType) => void;
3031
onFakerFunctionSelect: (fakerFunction: string) => void;
3132
}
3233

@@ -43,7 +44,7 @@ const FakerMappingSelector = ({
4344
label="JSON Type"
4445
allowDeselect={false}
4546
value={activeJsonType}
46-
onChange={onJsonTypeSelect}
47+
onChange={(value) => onJsonTypeSelect(value as MongoDBFieldType)}
4748
>
4849
{/* TODO(CLOUDP-344400) : Make the select input editable and render other options depending on the JSON type selected */}
4950
{[activeJsonType].map((type) => (

packages/compass-collection/src/components/mock-data-generator-modal/faker-schema-editor-screen.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import React from 'react';
1313
import FieldSelector from './schema-field-selector';
1414
import FakerMappingSelector from './faker-mapping-selector';
1515
import type { FakerSchema, MockDataGeneratorState } from './types';
16-
import type { MongoDBFieldType } from '../../schema-analysis-types';
16+
import type { MongoDBFieldType } from '@mongodb-js/compass-generative-ai';
1717

1818
const containerStyles = css({
1919
display: 'flex',
@@ -69,14 +69,14 @@ const FakerSchemaEditorContent = ({
6969
onSchemaConfirmed(false);
7070
};
7171

72-
const onJsonTypeSelect = (newJsonType: string) => {
72+
const onJsonTypeSelect = (newJsonType: MongoDBFieldType) => {
7373
const currentMapping = fakerSchemaFormValues[activeField];
7474
if (currentMapping) {
7575
setFakerSchemaFormValues({
7676
...fakerSchemaFormValues,
7777
[activeField]: {
7878
...currentMapping,
79-
mongoType: newJsonType as MongoDBFieldType,
79+
mongoType: newJsonType,
8080
},
8181
});
8282
resetIsSchemaConfirmed();

packages/compass-collection/src/components/mock-data-generator-modal/mock-data-generator-modal.spec.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe('MockDataGeneratorModal', () => {
8989
fields: [
9090
{
9191
fieldPath: 'name',
92-
mongoType: 'string',
92+
mongoType: 'String',
9393
fakerMethod: 'person.firstName',
9494
fakerArgs: [],
9595
},
@@ -318,25 +318,25 @@ describe('MockDataGeneratorModal', () => {
318318
fields: [
319319
{
320320
fieldPath: 'name',
321-
mongoType: 'string',
321+
mongoType: 'String',
322322
fakerMethod: 'person.firstName',
323323
fakerArgs: [],
324324
},
325325
{
326326
fieldPath: 'age',
327-
mongoType: 'int',
327+
mongoType: 'Int32',
328328
fakerMethod: 'number.int',
329329
fakerArgs: [],
330330
},
331331
{
332332
fieldPath: 'email',
333-
mongoType: 'string',
333+
mongoType: 'String',
334334
fakerMethod: 'internet',
335335
fakerArgs: [],
336336
},
337337
{
338338
fieldPath: 'username',
339-
mongoType: 'string',
339+
mongoType: 'String',
340340
fakerMethod: 'noSuchMethod',
341341
fakerArgs: [],
342342
},
@@ -390,21 +390,21 @@ describe('MockDataGeneratorModal', () => {
390390
});
391391
// the "name" field should be selected by default
392392
expect(screen.getByText('name')).to.exist;
393-
expect(screen.getByLabelText('JSON Type')).to.have.value('string');
393+
expect(screen.getByLabelText('JSON Type')).to.have.value('String');
394394
expect(screen.getByLabelText('Faker Function')).to.have.value(
395395
'person.firstName'
396396
);
397397
// select the "age" field
398398
userEvent.click(screen.getByText('age'));
399399
expect(screen.getByText('age')).to.exist;
400-
expect(screen.getByLabelText('JSON Type')).to.have.value('int');
400+
expect(screen.getByLabelText('JSON Type')).to.have.value('Int32');
401401
expect(screen.getByLabelText('Faker Function')).to.have.value(
402402
'number.int'
403403
);
404404
// select the "email" field
405405
userEvent.click(screen.getByText('email'));
406406
expect(screen.getByText('email')).to.exist;
407-
expect(screen.getByLabelText('JSON Type')).to.have.value('string');
407+
expect(screen.getByLabelText('JSON Type')).to.have.value('String');
408408
// the "email" field should have a warning banner since the faker method is invalid
409409
expect(screen.getByLabelText('Faker Function')).to.have.value(
410410
'Unrecognized'
@@ -418,7 +418,7 @@ describe('MockDataGeneratorModal', () => {
418418
// select the "username" field
419419
userEvent.click(screen.getByText('username'));
420420
expect(screen.getByText('username')).to.exist;
421-
expect(screen.getByLabelText('JSON Type')).to.have.value('string');
421+
expect(screen.getByLabelText('JSON Type')).to.have.value('String');
422422
expect(screen.getByLabelText('Faker Function')).to.have.value(
423423
'Unrecognized'
424424
);
@@ -432,15 +432,15 @@ describe('MockDataGeneratorModal', () => {
432432
fields: [
433433
{
434434
fieldPath: 'name',
435-
mongoType: 'string',
435+
mongoType: 'String',
436436
fakerMethod: 'person.firstName',
437437
fakerArgs: [],
438438
isArray: false,
439439
probability: 1.0,
440440
},
441441
{
442442
fieldPath: 'email',
443-
mongoType: 'string',
443+
mongoType: 'String',
444444
fakerMethod: 'internet.email',
445445
fakerArgs: [],
446446
isArray: false,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { MongoDBFieldType } from '../../schema-analysis-types';
1+
import type { MongoDBFieldType } from '@mongodb-js/compass-generative-ai';
22
import type { FakerFieldMapping } from './types';
33

44
export type FakerArg = string | number | boolean | { json: string };

packages/compass-collection/src/components/mock-data-generator-modal/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { MockDataSchemaResponse } from '@mongodb-js/compass-generative-ai';
2-
import type { MongoDBFieldType } from '../../schema-analysis-types';
2+
import type { MongoDBFieldType } from '@mongodb-js/compass-generative-ai';
33
import type { FakerArg } from './script-generation-utils';
44

55
export enum MockDataGeneratorStep {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
type SchemaAnalysisError,
2828
type SchemaAnalysisState,
2929
type FieldInfo,
30-
type MongoDBFieldType,
3130
} from '../schema-analysis-types';
3231
import { calculateSchemaDepth } from '../calculate-schema-depth';
3332
import {
@@ -710,7 +709,7 @@ function transformFakerSchemaToObject(
710709
const { fieldPath, ...fieldMapping } = field;
711710
result[fieldPath] = {
712711
...fieldMapping,
713-
mongoType: fieldMapping.mongoType as MongoDBFieldType,
712+
mongoType: fieldMapping.mongoType,
714713
};
715714
}
716715

packages/compass-collection/src/schema-analysis-types.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import type { MongoDBFieldType } from '@mongodb-js/compass-generative-ai';
12
import type { Document } from 'mongodb';
2-
import type { PrimitiveSchemaType } from 'mongodb-schema';
33

44
export const SCHEMA_ANALYSIS_STATE_INITIAL = 'initial';
55
export const SCHEMA_ANALYSIS_STATE_ANALYZING = 'analyzing';
@@ -30,11 +30,6 @@ export type SchemaAnalysisErrorState = {
3030
error: SchemaAnalysisError;
3131
};
3232

33-
/**
34-
* MongoDB schema type
35-
*/
36-
export type MongoDBFieldType = PrimitiveSchemaType['name'];
37-
3833
/**
3934
* Primitive values that can appear in sample_values after BSON-to-primitive conversion.
4035
* These are the JavaScript primitive equivalents of BSON values.

packages/compass-generative-ai/src/atlas-ai-service.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { SimplifiedSchema } from 'mongodb-schema';
1+
import type { PrimitiveSchemaType, SimplifiedSchema } from 'mongodb-schema';
22
import {
33
type PreferencesAccess,
44
isAIFeatureEnabled,
@@ -231,11 +231,16 @@ export interface MockDataSchemaRequest {
231231
signal: AbortSignal;
232232
}
233233

234+
/**
235+
* MongoDB schema type
236+
*/
237+
export type MongoDBFieldType = PrimitiveSchemaType['name'];
238+
234239
export const MockDataSchemaResponseShape = z.object({
235240
fields: z.array(
236241
z.object({
237242
fieldPath: z.string(),
238-
mongoType: z.string(),
243+
mongoType: z.string() as z.ZodType<MongoDBFieldType>,
239244
fakerMethod: z.string(),
240245
fakerArgs: z.array(
241246
z.union([

packages/compass-generative-ai/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ export type {
3535
MockDataSchemaRequest,
3636
MockDataSchemaRawField,
3737
MockDataSchemaResponse,
38+
MongoDBFieldType,
3839
} from './atlas-ai-service';

0 commit comments

Comments
 (0)