Skip to content

Commit 53384e8

Browse files
committed
refactor: update MongoDBFieldType import paths and improve type usage across mock data generator components
1 parent 57f9376 commit 53384e8

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
@@ -90,7 +90,7 @@ describe('MockDataGeneratorModal', () => {
9090
fields: [
9191
{
9292
fieldPath: 'name',
93-
mongoType: 'string',
93+
mongoType: 'String',
9494
fakerMethod: 'person.firstName',
9595
fakerArgs: [],
9696
isArray: false,
@@ -323,31 +323,31 @@ describe('MockDataGeneratorModal', () => {
323323
fields: [
324324
{
325325
fieldPath: 'name',
326-
mongoType: 'string',
326+
mongoType: 'String',
327327
fakerMethod: 'person.firstName',
328328
fakerArgs: [],
329329
isArray: false,
330330
probability: 1.0,
331331
},
332332
{
333333
fieldPath: 'age',
334-
mongoType: 'int',
334+
mongoType: 'Int32',
335335
fakerMethod: 'number.int',
336336
fakerArgs: [],
337337
isArray: false,
338338
probability: 1.0,
339339
},
340340
{
341341
fieldPath: 'email',
342-
mongoType: 'string',
342+
mongoType: 'String',
343343
fakerMethod: 'internet',
344344
fakerArgs: [],
345345
isArray: false,
346346
probability: 1.0,
347347
},
348348
{
349349
fieldPath: 'username',
350-
mongoType: 'string',
350+
mongoType: 'String',
351351
fakerMethod: 'noSuchMethod',
352352
fakerArgs: [],
353353
isArray: false,
@@ -406,21 +406,21 @@ describe('MockDataGeneratorModal', () => {
406406
});
407407
// the "name" field should be selected by default
408408
expect(screen.getByText('name')).to.exist;
409-
expect(screen.getByLabelText('JSON Type')).to.have.value('string');
409+
expect(screen.getByLabelText('JSON Type')).to.have.value('String');
410410
expect(screen.getByLabelText('Faker Function')).to.have.value(
411411
'person.firstName'
412412
);
413413
// select the "age" field
414414
userEvent.click(screen.getByText('age'));
415415
expect(screen.getByText('age')).to.exist;
416-
expect(screen.getByLabelText('JSON Type')).to.have.value('int');
416+
expect(screen.getByLabelText('JSON Type')).to.have.value('Int32');
417417
expect(screen.getByLabelText('Faker Function')).to.have.value(
418418
'number.int'
419419
);
420420
// select the "email" field
421421
userEvent.click(screen.getByText('email'));
422422
expect(screen.getByText('email')).to.exist;
423-
expect(screen.getByLabelText('JSON Type')).to.have.value('string');
423+
expect(screen.getByLabelText('JSON Type')).to.have.value('String');
424424
// the "email" field should have a warning banner since the faker method is invalid
425425
expect(screen.getByLabelText('Faker Function')).to.have.value(
426426
'Unrecognized'
@@ -434,7 +434,7 @@ describe('MockDataGeneratorModal', () => {
434434
// select the "username" field
435435
userEvent.click(screen.getByText('username'));
436436
expect(screen.getByText('username')).to.exist;
437-
expect(screen.getByLabelText('JSON Type')).to.have.value('string');
437+
expect(screen.getByLabelText('JSON Type')).to.have.value('String');
438438
expect(screen.getByLabelText('Faker Function')).to.have.value(
439439
'Unrecognized'
440440
);
@@ -448,15 +448,15 @@ describe('MockDataGeneratorModal', () => {
448448
fields: [
449449
{
450450
fieldPath: 'name',
451-
mongoType: 'string',
451+
mongoType: 'String',
452452
fakerMethod: 'person.firstName',
453453
fakerArgs: [],
454454
isArray: false,
455455
probability: 1.0,
456456
},
457457
{
458458
fieldPath: 'email',
459-
mongoType: 'string',
459+
mongoType: 'String',
460460
fakerMethod: 'internet.email',
461461
fakerArgs: [],
462462
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,12 +231,17 @@ 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
content: z.object({
236241
fields: z.array(
237242
z.object({
238243
fieldPath: z.string(),
239-
mongoType: z.string(),
244+
mongoType: z.string() as z.ZodType<MongoDBFieldType>,
240245
fakerMethod: z.string(),
241246
fakerArgs: z.array(
242247
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)