Skip to content

Commit 2d53b85

Browse files
committed
type conversion
1 parent 94626d3 commit 2d53b85

File tree

2 files changed

+64
-30
lines changed

2 files changed

+64
-30
lines changed

src/schema-convertors/internalToMongoDB.test.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -898,82 +898,82 @@ describe('internalSchemaToStandard', function() {
898898
required: [],
899899
properties: {
900900
_id: {
901-
bsonType: 'objectid'
901+
bsonType: 'objectId'
902902
},
903903
array: {
904904
bsonType: 'array',
905905
items: {
906-
bsonType: 'number'
906+
bsonType: 'double'
907907
}
908908
},
909909
binData: {
910-
bsonType: 'binary'
910+
bsonType: 'binData'
911911
},
912912
binaries: {
913913
bsonType: 'object',
914914
properties: {
915915
binaryOld: {
916-
bsonType: 'binary'
916+
bsonType: 'binData'
917917
},
918918
compressedTimeSeries: {
919-
bsonType: 'binary'
919+
bsonType: 'binData'
920920
},
921921
custom: {
922-
bsonType: 'binary'
922+
bsonType: 'binData'
923923
},
924924
encrypted: {
925-
bsonType: 'binary'
925+
bsonType: 'binData'
926926
},
927927
functionData: {
928-
bsonType: 'binary'
928+
bsonType: 'binData'
929929
},
930930
generic: {
931-
bsonType: 'binary'
931+
bsonType: 'binData'
932932
},
933933
md5: {
934-
bsonType: 'binary'
934+
bsonType: 'binData'
935935
},
936936
uuid: {
937-
bsonType: 'binary'
937+
bsonType: 'binData'
938938
},
939939
uuidOld: {
940-
bsonType: 'binary'
940+
bsonType: 'binData'
941941
}
942942
},
943943
required: []
944944
},
945945
boolean: {
946-
bsonType: 'boolean'
946+
bsonType: 'bool'
947947
},
948948
date: {
949949
bsonType: 'date'
950950
},
951951
dbRef: {
952-
bsonType: 'dbref'
952+
bsonType: 'dbPointer'
953953
},
954954
decimal: {
955-
bsonType: 'decimal128'
955+
bsonType: 'decimal'
956956
},
957957
double: {
958958
bsonType: 'double'
959959
},
960960
int: {
961-
bsonType: 'int32'
961+
bsonType: 'int'
962962
},
963963
javascript: {
964-
bsonType: 'code'
964+
bsonType: 'javascript'
965965
},
966966
javascriptWithScope: {
967-
bsonType: 'code'
967+
bsonType: 'javascript'
968968
},
969969
long: {
970970
bsonType: 'long'
971971
},
972972
maxKey: {
973-
bsonType: 'maxkey'
973+
bsonType: 'maxKey'
974974
},
975975
minKey: {
976-
bsonType: 'minkey'
976+
bsonType: 'minKey'
977977
},
978978
null: {
979979
bsonType: 'null'
@@ -988,16 +988,16 @@ describe('internalSchemaToStandard', function() {
988988
required: []
989989
},
990990
objectId: {
991-
bsonType: 'objectid'
991+
bsonType: 'objectId'
992992
},
993993
regex: {
994-
bsonType: 'bsonregexp'
994+
bsonType: 'regex'
995995
},
996996
string: {
997997
bsonType: 'string'
998998
},
999999
symbol: {
1000-
bsonType: 'bsonsymbol'
1000+
bsonType: 'symbol'
10011001
},
10021002
timestamp: {
10031003
bsonType: 'timestamp'
@@ -1416,7 +1416,7 @@ describe('internalSchemaToStandard', function() {
14161416
values: [
14171417
'2'
14181418
],
1419-
bsonType: 'string'
1419+
bsonType: 'String'
14201420
}
14211421
],
14221422
totalCount: 3,
@@ -1437,7 +1437,7 @@ describe('internalSchemaToStandard', function() {
14371437
arrayMixedType: {
14381438
bsonType: 'array',
14391439
items: {
1440-
bsonType: ['int32', 'string']
1440+
bsonType: ['int', 'string']
14411441
}
14421442
}
14431443
}
@@ -1513,7 +1513,7 @@ describe('internalSchemaToStandard', function() {
15131513
required: [],
15141514
properties: {
15151515
mixedType: {
1516-
bsonType: ['int32', 'string']
1516+
bsonType: ['int', 'string']
15171517
}
15181518
}
15191519
});
@@ -1633,7 +1633,7 @@ describe('internalSchemaToStandard', function() {
16331633
{
16341634
bsonType: 'array',
16351635
items: {
1636-
bsonType: 'int32'
1636+
bsonType: 'int'
16371637
}
16381638
},
16391639
{

src/schema-convertors/internalToMongoDB.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
1+
import { BSONRegExp } from 'bson';
12
import { ArraySchemaType, DocumentSchemaType, Schema as InternalSchema, SchemaType } from '../schema-analyzer';
23
import { MongoDBJSONSchema } from '../types';
34

4-
const internalTypeToBsonType = (type: string) => type === 'Document' ? 'object' : type.toLowerCase();
5+
const InternalTypeToBsonTypeMap: Record<
6+
SchemaType['name'] | 'Double' | 'BSONSymbol',
7+
string
8+
> = {
9+
Double: 'double',
10+
Number: 'double',
11+
String: 'string',
12+
Document: 'object',
13+
Array: 'array',
14+
Binary: 'binData',
15+
Undefined: 'undefined',
16+
ObjectId: 'objectId',
17+
Boolean: 'bool',
18+
Date: 'date',
19+
Null: 'null',
20+
RegExp: 'regex',
21+
BSONRegExp: 'regex',
22+
DBRef: 'dbPointer',
23+
BSONSymbol: 'symbol',
24+
Symbol: 'symbol',
25+
Code: 'javascript',
26+
Int32: 'int',
27+
Timestamp: 'timestamp',
28+
Long: 'long',
29+
Decimal128: 'decimal',
30+
MinKey: 'minKey',
31+
MaxKey: 'maxKey'
32+
};
33+
34+
const convertInternalType = (type: string) => {
35+
const bsonType = InternalTypeToBsonTypeMap[type];
36+
if (!bsonType) throw new Error(`Encountered unknown type: ${type}`);
37+
return bsonType;
38+
};
539

640
function parseType(type: SchemaType, signal?: AbortSignal): MongoDBJSONSchema {
741
if (signal?.aborted) throw new Error('Operation aborted');
842
const schema: MongoDBJSONSchema = {
9-
bsonType: internalTypeToBsonType(type.bsonType)
43+
bsonType: convertInternalType(type.bsonType)
1044
};
1145
switch (type.bsonType) {
1246
case 'Array':
@@ -36,7 +70,7 @@ function parseTypes(types: SchemaType[], signal?: AbortSignal): MongoDBJSONSchem
3670
};
3771
}
3872
return {
39-
bsonType: definedTypes.map((type) => internalTypeToBsonType(type.bsonType))
73+
bsonType: definedTypes.map((type) => convertInternalType(type.bsonType))
4074
};
4175
}
4276

0 commit comments

Comments
 (0)