Skip to content

Commit beecd6b

Browse files
committed
address: update FakerFieldMapping to use MongoDBFieldType and improve type safety
1 parent df1e8f8 commit beecd6b

File tree

5 files changed

+17
-18
lines changed

5 files changed

+17
-18
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +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';
1617

1718
const containerStyles = css({
1819
display: 'flex',
@@ -75,7 +76,7 @@ const FakerSchemaEditorContent = ({
7576
...fakerSchemaFormValues,
7677
[activeField]: {
7778
...currentMapping,
78-
mongoType: newJsonType,
79+
mongoType: newJsonType as MongoDBFieldType,
7980
},
8081
});
8182
resetIsSchemaConfirmed();

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { expect } from 'chai';
22
import { faker } from '@faker-js/faker/locale/en';
3-
import {
4-
generateScript,
5-
type FakerFieldMapping,
6-
} from './script-generation-utils';
3+
import { generateScript } from './script-generation-utils';
4+
import type { FakerFieldMapping } from './types';
75

86
/**
97
* Helper function to test that generated document code is executable

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import type { MongoDBFieldType } from '../../schema-analysis-types';
2+
import type { FakerFieldMapping } from './types';
23

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

56
const DEFAULT_ARRAY_LENGTH = 3;
67
const INDENT_SIZE = 2;
78

8-
export interface FakerFieldMapping {
9-
mongoType: MongoDBFieldType;
10-
fakerMethod: string;
11-
fakerArgs: FakerArg[];
12-
probability?: number; // 0.0 - 1.0 frequency of field (defaults to 1.0)
13-
}
14-
159
// Array length configuration for different array types
1610
export type ArrayLengthMap = {
1711
[fieldName: string]:

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { MockDataSchemaResponse } from '@mongodb-js/compass-generative-ai';
2+
import type { MongoDBFieldType } from '../../schema-analysis-types';
3+
import type { FakerArg } from './script-generation-utils';
24

35
export enum MockDataGeneratorStep {
46
SCHEMA_CONFIRMATION = 'SCHEMA_CONFIRMATION',
@@ -37,11 +39,11 @@ export type MockDataGeneratorState =
3739

3840
export type FakerSchemaMapping = MockDataSchemaResponse['fields'][number];
3941

40-
export type FakerFieldMapping = {
41-
mongoType: string;
42+
export interface FakerFieldMapping {
43+
mongoType: MongoDBFieldType;
4244
fakerMethod: string;
43-
fakerArgs: any[];
44-
probability: number;
45-
};
45+
fakerArgs: FakerArg[];
46+
probability?: number; // 0.0 - 1.0 frequency of field (defaults to 1.0)
47+
}
4648

4749
export type FakerSchema = Record<string, FakerFieldMapping>;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
type SchemaAnalysisError,
2828
type SchemaAnalysisState,
2929
type FieldInfo,
30+
MongoDBFieldType,
3031
} from '../schema-analysis-types';
3132
import { calculateSchemaDepth } from '../calculate-schema-depth';
3233
import {
@@ -708,7 +709,10 @@ function transformFakerSchemaToObject(
708709

709710
for (const field of fakerSchema) {
710711
const { fieldPath, ...fieldMapping } = field;
711-
result[fieldPath] = fieldMapping;
712+
result[fieldPath] = {
713+
...fieldMapping,
714+
mongoType: fieldMapping.mongoType as MongoDBFieldType,
715+
};
712716
}
713717

714718
return result;

0 commit comments

Comments
 (0)