Skip to content

Commit e4db17c

Browse files
committed
Address comment
1 parent 476d003 commit e4db17c

File tree

3 files changed

+81
-37
lines changed

3 files changed

+81
-37
lines changed

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import type { Document } from 'mongodb';
22
import type { PrimitiveSchemaType } from 'mongodb-schema';
3-
import type {
4-
ObjectId,
5-
Binary,
6-
BSONRegExp,
7-
Code,
8-
Timestamp,
9-
MaxKey,
10-
MinKey,
11-
} from 'bson';
123

134
export const SCHEMA_ANALYSIS_STATE_INITIAL = 'initial';
145
export const SCHEMA_ANALYSIS_STATE_ANALYZING = 'analyzing';
@@ -49,17 +40,10 @@ export type MongoDBFieldType = PrimitiveSchemaType['name'];
4940
* These are the JavaScript primitive equivalents of BSON values.
5041
*/
5142
export type SampleValue =
52-
| string
53-
| number
43+
| string // String, Symbol, ObjectId, Binary, RegExp, Code, etc. (converted to string)
44+
| number // Number, Int32, Long, Double, Decimal128, Timestamp (converted via valueOf())
5445
| boolean
5546
| Date
56-
| ObjectId
57-
| Binary
58-
| BSONRegExp
59-
| Code
60-
| Timestamp
61-
| MaxKey
62-
| MinKey
6347
| null
6448
| undefined;
6549

packages/compass-collection/src/transform-schema-to-field-info.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -474,47 +474,47 @@ describe('processSchema', function () {
474474
expect(result).to.deep.equal({
475475
objectId: {
476476
type: 'ObjectId',
477-
sample_values: [new ObjectId('642d766b7300158b1f22e972')],
477+
sample_values: ['642d766b7300158b1f22e972'],
478478
probability: 1.0,
479479
},
480480
binary: {
481481
type: 'Binary',
482-
sample_values: [new Binary(Buffer.from('test'))],
482+
sample_values: ['dGVzdA=='],
483483
probability: 1.0,
484484
},
485485
regex: {
486486
type: 'RegExp',
487-
sample_values: [new BSONRegExp('pattern', 'i')],
487+
sample_values: ['pattern'],
488488
probability: 1.0,
489489
},
490490
code: {
491491
type: 'Code',
492-
sample_values: [new Code('function() {}')],
492+
sample_values: ['function() {}'],
493493
probability: 1.0,
494494
},
495495
long: {
496496
type: 'Long',
497-
sample_values: [Long.fromNumber(123456789)],
497+
sample_values: [123456789],
498498
probability: 1.0,
499499
},
500500
decimal: {
501501
type: 'Decimal128',
502-
sample_values: [Decimal128.fromString('123.456')],
502+
sample_values: [123.456],
503503
probability: 1.0,
504504
},
505505
timestamp: {
506506
type: 'Timestamp',
507-
sample_values: [new Timestamp({ t: 1, i: 1 })],
507+
sample_values: [4294967297],
508508
probability: 1.0,
509509
},
510510
maxKey: {
511511
type: 'MaxKey',
512-
sample_values: [new MaxKey()],
512+
sample_values: ['MaxKey'],
513513
probability: 1.0,
514514
},
515515
minKey: {
516516
type: 'MinKey',
517-
sample_values: [new MinKey()],
517+
sample_values: ['MinKey'],
518518
probability: 1.0,
519519
},
520520
symbol: {

packages/compass-collection/src/transform-schema-to-field-info.ts

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ import type {
88
ConstantSchemaType,
99
} from 'mongodb-schema';
1010
import type { FieldInfo, SampleValue } from './schema-analysis-types';
11+
import {
12+
ObjectId,
13+
Binary,
14+
BSONRegExp,
15+
Code,
16+
Timestamp,
17+
MaxKey,
18+
MinKey,
19+
BSONSymbol,
20+
Long,
21+
Decimal128,
22+
} from 'bson';
1123

1224
/**
1325
* This module transforms mongodb-schema output into a flat, LLM-friendly format using
@@ -32,6 +44,61 @@ import type { FieldInfo, SampleValue } from './schema-analysis-types';
3244
*/
3345
const MAX_SAMPLE_VALUES = 10;
3446

47+
/**
48+
* Converts a BSON value to its primitive JavaScript equivalent
49+
*/
50+
function convertBSONToPrimitive(value: unknown): SampleValue {
51+
// Handle null/undefined
52+
if (value === null || value === undefined) {
53+
return value;
54+
}
55+
56+
// Keep Date as-is
57+
if (value instanceof Date) {
58+
return value;
59+
}
60+
61+
// Convert BSON objects to primitives
62+
if (value instanceof ObjectId) {
63+
return value.toString();
64+
}
65+
if (value instanceof Binary) {
66+
return value.toString('base64');
67+
}
68+
if (value instanceof BSONRegExp) {
69+
return value.pattern;
70+
}
71+
if (value instanceof Code) {
72+
return value.code;
73+
}
74+
if (value instanceof Timestamp) {
75+
return value.toNumber();
76+
}
77+
if (value instanceof MaxKey) {
78+
return 'MaxKey';
79+
}
80+
if (value instanceof MinKey) {
81+
return 'MinKey';
82+
}
83+
if (value instanceof BSONSymbol) {
84+
return value.toString();
85+
}
86+
if (value instanceof Long) {
87+
return value.toNumber();
88+
}
89+
if (value instanceof Decimal128) {
90+
return parseFloat(value.toString());
91+
}
92+
93+
// Handle objects with valueOf method (numeric types)
94+
if (value && typeof value === 'object' && 'valueOf' in value) {
95+
const result = (value as { valueOf(): unknown }).valueOf();
96+
return result as SampleValue;
97+
}
98+
99+
return value as SampleValue;
100+
}
101+
35102
function isConstantSchemaType(type: SchemaType): type is ConstantSchemaType {
36103
return type.name === 'Null' || type.name === 'Undefined';
37104
}
@@ -129,16 +196,9 @@ function processType(
129196
// Primitive: Create entry
130197
const fieldInfo: FieldInfo = {
131198
type: type.name,
132-
sample_values: type.values.slice(0, MAX_SAMPLE_VALUES).map((value) => {
133-
// Convert BSON values to their primitive equivalents, but keep Date objects as-is
134-
if (value instanceof Date) {
135-
return value;
136-
}
137-
if (value && typeof value === 'object' && 'valueOf' in value) {
138-
return value.valueOf();
139-
}
140-
return value;
141-
}) as SampleValue[],
199+
sample_values: type.values
200+
.slice(0, MAX_SAMPLE_VALUES)
201+
.map(convertBSONToPrimitive),
142202
probability: fieldProbability,
143203
};
144204

0 commit comments

Comments
 (0)