Skip to content

Commit 9fedf3f

Browse files
committed
Process schema
1 parent 6d9061c commit 9fedf3f

File tree

4 files changed

+649
-7
lines changed

4 files changed

+649
-7
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Reducer, AnyAction, Action } from 'redux';
2-
import { analyzeDocuments, type Schema } from 'mongodb-schema';
2+
import { analyzeDocuments } from 'mongodb-schema';
33

44
import type { CollectionMetadata } from 'mongodb-collection-model';
55
import type { ThunkAction } from 'redux-thunk';
@@ -19,8 +19,10 @@ import {
1919
SCHEMA_ANALYSIS_STATE_INITIAL,
2020
type SchemaAnalysisError,
2121
type SchemaAnalysisState,
22+
type FieldInfo,
2223
} from '../schema-analysis-types';
2324
import { calculateSchemaDepth } from '../calculate-schema-depth';
25+
import { processSchema } from '../transform-schema-to-field-info';
2426
import type { Document, MongoError } from 'mongodb';
2527

2628
const DEFAULT_SAMPLE_SIZE = 100;
@@ -106,7 +108,7 @@ interface SchemaAnalysisStartedAction {
106108

107109
interface SchemaAnalysisFinishedAction {
108110
type: CollectionActions.SchemaAnalysisFinished;
109-
schema: Schema;
111+
processedSchema: Record<string, FieldInfo>;
110112
sampleDocument: Document;
111113
schemaMetadata: {
112114
maxNestingDepth: number;
@@ -201,7 +203,7 @@ const reducer: Reducer<CollectionState, Action> = (
201203
...state,
202204
schemaAnalysis: {
203205
status: SCHEMA_ANALYSIS_STATE_COMPLETE,
204-
schema: action.schema,
206+
processedSchema: action.processedSchema,
205207
sampleDocument: action.sampleDocument,
206208
schemaMetadata: action.schemaMetadata,
207209
},
@@ -420,7 +422,9 @@ export const analyzeCollectionSchema = (): CollectionThunkAction<
420422
schema.fields = schema.fields.filter(
421423
({ path }) => !isInternalFieldPath(path[0])
422424
);
423-
// TODO: Transform schema to structure that will be used by the LLM.
425+
426+
// Transform schema to structure that will be used by the LLM
427+
const processedSchema = processSchema(schema);
424428

425429
const maxNestingDepth = await calculateSchemaDepth(schema);
426430
const { database, collection } = toNS(namespace);
@@ -432,7 +436,7 @@ export const analyzeCollectionSchema = (): CollectionThunkAction<
432436
};
433437
dispatch({
434438
type: CollectionActions.SchemaAnalysisFinished,
435-
schema,
439+
processedSchema,
436440
sampleDocument: sampleDocuments[0],
437441
schemaMetadata,
438442
});

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Document } from 'mongodb';
2-
import { type Schema } from 'mongodb-schema';
32

43
export const SCHEMA_ANALYSIS_STATE_INITIAL = 'initial';
54
export const SCHEMA_ANALYSIS_STATE_ANALYZING = 'analyzing';
@@ -30,9 +29,16 @@ export type SchemaAnalysisErrorState = {
3029
error: SchemaAnalysisError;
3130
};
3231

32+
export interface FieldInfo {
33+
type: string; // MongoDB type (eg. String, Double, Array, Document)
34+
sample_values: unknown[]; // Real sample values (empty array if none)
35+
isArray?: boolean; // For arrays. Denotes that the type field refers to the type of the array elements
36+
probability?: number; // 0.0 - 1.0 field frequency
37+
}
38+
3339
export type SchemaAnalysisCompletedState = {
3440
status: typeof SCHEMA_ANALYSIS_STATE_COMPLETE;
35-
schema: Schema;
41+
processedSchema: Record<string, FieldInfo>;
3642
sampleDocument: Document;
3743
schemaMetadata: {
3844
maxNestingDepth: number;

0 commit comments

Comments
 (0)