@@ -35,77 +35,81 @@ export const InternalTypeToBsonTypeMap: Record<
35
35
MaxKey : 'maxKey'
36
36
} ;
37
37
38
- const convertInternalType = ( type : string ) => {
39
- const bsonType = InternalTypeToBsonTypeMap [ type ] ;
40
- if ( ! bsonType ) throw new Error ( `Encountered unknown type: ${ type } ` ) ;
41
- return bsonType ;
42
- } ;
43
-
44
- async function parseType ( type : SchemaType , signal ?: AbortSignal ) : Promise < MongoDBJSONSchema > {
45
- await allowAbort ( signal ) ;
46
- const schema : MongoDBJSONSchema = {
47
- bsonType : convertInternalType ( type . bsonType )
48
- } ;
49
- switch ( type . bsonType ) {
50
- case 'Array' :
51
- schema . items = await parseTypes ( ( type as ArraySchemaType ) . types ) ;
52
- break ;
53
- case 'Document' :
54
- Object . assign ( schema ,
55
- await parseFields ( ( type as DocumentSchemaType ) . fields , signal )
56
- ) ;
57
- break ;
38
+ class InternalToMongoDBConvertor {
39
+ private convertInternalType ( type : string ) {
40
+ const bsonType = InternalTypeToBsonTypeMap [ type ] ;
41
+ if ( ! bsonType ) throw new Error ( `Encountered unknown type: ${ type } ` ) ;
42
+ return bsonType ;
58
43
}
59
44
60
- return schema ;
61
- }
45
+ private async parseType ( type : SchemaType , signal ?: AbortSignal ) : Promise < MongoDBJSONSchema > {
46
+ await allowAbort ( signal ) ;
47
+ const schema : MongoDBJSONSchema = {
48
+ bsonType : this . convertInternalType ( type . bsonType )
49
+ } ;
50
+ switch ( type . bsonType ) {
51
+ case 'Array' :
52
+ schema . items = await this . parseTypes ( ( type as ArraySchemaType ) . types ) ;
53
+ break ;
54
+ case 'Document' :
55
+ Object . assign ( schema ,
56
+ await this . parseFields ( ( type as DocumentSchemaType ) . fields , signal )
57
+ ) ;
58
+ break ;
59
+ }
62
60
63
- function isPlainTypesOnly ( types : MongoDBJSONSchema [ ] ) : types is { bsonType : string } [ ] {
64
- return types . every ( definition => ! ! definition . bsonType && Object . keys ( definition ) . length === 1 ) ;
65
- }
61
+ return schema ;
62
+ }
66
63
67
- async function parseTypes ( types : SchemaType [ ] , signal ?: AbortSignal ) : Promise < MongoDBJSONSchema > {
68
- await allowAbort ( signal ) ;
69
- const definedTypes = types . filter ( type => type . bsonType . toLowerCase ( ) !== 'undefined' ) ;
70
- const isSingleType = definedTypes . length === 1 ;
71
- if ( isSingleType ) {
72
- return parseType ( definedTypes [ 0 ] , signal ) ;
64
+ private isPlainTypesOnly ( types : MongoDBJSONSchema [ ] ) : types is { bsonType : string } [ ] {
65
+ return types . every ( definition => ! ! definition . bsonType && Object . keys ( definition ) . length === 1 ) ;
73
66
}
74
- const parsedTypes = await Promise . all ( definedTypes . map ( type => parseType ( type , signal ) ) ) ;
75
- if ( isPlainTypesOnly ( parsedTypes ) ) {
67
+
68
+ private async parseTypes ( types : SchemaType [ ] , signal ?: AbortSignal ) : Promise < MongoDBJSONSchema > {
69
+ await allowAbort ( signal ) ;
70
+ const definedTypes = types . filter ( type => type . bsonType . toLowerCase ( ) !== 'undefined' ) ;
71
+ const isSingleType = definedTypes . length === 1 ;
72
+ if ( isSingleType ) {
73
+ return this . parseType ( definedTypes [ 0 ] , signal ) ;
74
+ }
75
+ const parsedTypes = await Promise . all ( definedTypes . map ( type => this . parseType ( type , signal ) ) ) ;
76
+ if ( this . isPlainTypesOnly ( parsedTypes ) ) {
77
+ return {
78
+ bsonType : parsedTypes . map ( ( { bsonType } ) => bsonType )
79
+ } ;
80
+ }
76
81
return {
77
- bsonType : parsedTypes . map ( ( { bsonType } ) => bsonType )
82
+ anyOf : parsedTypes
78
83
} ;
79
84
}
80
- return {
81
- anyOf : parsedTypes
82
- } ;
83
- }
84
85
85
- async function parseFields ( fields : DocumentSchemaType [ 'fields' ] , signal ?: AbortSignal ) : Promise < {
86
- required : MongoDBJSONSchema [ 'required' ] ,
87
- properties : MongoDBJSONSchema [ 'properties' ] ,
88
- } > {
89
- const required = [ ] ;
90
- const properties : MongoDBJSONSchema [ 'properties' ] = { } ;
91
- for ( const field of fields ) {
92
- if ( field . probability === 1 ) required . push ( field . name ) ;
93
- properties [ field . name ] = await parseTypes ( field . types , signal ) ;
86
+ private async parseFields ( fields : DocumentSchemaType [ 'fields' ] , signal ?: AbortSignal ) : Promise < {
87
+ required : MongoDBJSONSchema [ 'required' ] ,
88
+ properties : MongoDBJSONSchema [ 'properties' ] ,
89
+ } > {
90
+ const required = [ ] ;
91
+ const properties : MongoDBJSONSchema [ 'properties' ] = { } ;
92
+ for ( const field of fields ) {
93
+ if ( field . probability === 1 ) required . push ( field . name ) ;
94
+ properties [ field . name ] = await this . parseTypes ( field . types , signal ) ;
95
+ }
96
+
97
+ return { required, properties } ;
94
98
}
95
99
96
- return { required, properties } ;
100
+ public async convert (
101
+ internalSchema : InternalSchema ,
102
+ options : {
103
+ signal ?: AbortSignal
104
+ } = { } ) : Promise < MongoDBJSONSchema > {
105
+ const { required, properties } = await this . parseFields ( internalSchema . fields , options . signal ) ;
106
+ const schema : MongoDBJSONSchema = {
107
+ bsonType : 'object' ,
108
+ required,
109
+ properties
110
+ } ;
111
+ return schema ;
112
+ }
97
113
}
98
114
99
- export default async function internalSchemaToMongodb (
100
- internalSchema : InternalSchema ,
101
- options : {
102
- signal ?: AbortSignal
103
- } = { } ) : Promise < MongoDBJSONSchema > {
104
- const { required, properties } = await parseFields ( internalSchema . fields , options . signal ) ;
105
- const schema : MongoDBJSONSchema = {
106
- bsonType : 'object' ,
107
- required,
108
- properties
109
- } ;
110
- return schema ;
111
- }
115
+ export default InternalToMongoDBConvertor ;
0 commit comments