@@ -4,63 +4,59 @@ import { InternalTypeToStandardTypeMap, RELAXED_EJSON_DEFINITIONS } from './inte
4
4
import { InternalTypeToBsonTypeMap } from './internalToMongoDB' ;
5
5
import { allowAbort } from './util' ;
6
6
7
- export class InternalToExpandedConverter {
8
- private usedDefinitions = new Set < string > ( ) ;
7
+ export const convertInternalToExpanded = ( function ( ) {
8
+ const usedDefinitions = new Set < string > ( ) ;
9
9
10
- private clearUsedDefintions ( ) {
11
- this . usedDefinitions . clear ( ) ;
10
+ function clearUsedDefinitions ( ) {
11
+ usedDefinitions . clear ( ) ;
12
12
}
13
13
14
- private getUsedDefinitions ( ) {
14
+ function getUsedDefinitions ( ) {
15
15
const filteredDefinitions = Object . fromEntries (
16
- Object . entries ( RELAXED_EJSON_DEFINITIONS )
17
- . filter ( ( [ key ] ) => this . usedDefinitions . has ( key ) )
16
+ Object . entries ( RELAXED_EJSON_DEFINITIONS ) . filter ( ( [ key ] ) => usedDefinitions . has ( key ) )
18
17
) ;
19
18
return Object . freeze ( filteredDefinitions ) ;
20
19
}
21
20
22
- private markUsedDefinition ( ref : string ) {
23
- this . usedDefinitions . add ( ref . split ( '/' ) [ 2 ] ) ;
21
+ function markUsedDefinition ( ref : string ) {
22
+ usedDefinitions . add ( ref . split ( '/' ) [ 2 ] ) ;
24
23
}
25
24
26
- private getStandardType ( internalType : string ) {
27
- const type = { ... InternalTypeToStandardTypeMap [ internalType ] } ;
25
+ function getStandardType ( internalType : string ) {
26
+ const type = InternalTypeToStandardTypeMap [ internalType ] ;
28
27
if ( ! type ) throw new Error ( `Encountered unknown type: ${ internalType } ` ) ;
29
- return type ;
28
+ return { ... type } ;
30
29
}
31
30
32
- private getBsonType ( internalType : string ) {
31
+ function getBsonType ( internalType : string ) {
33
32
const type = InternalTypeToBsonTypeMap [ internalType ] ;
34
33
if ( ! type ) throw new Error ( `Encountered unknown type: ${ internalType } ` ) ;
35
34
return type ;
36
35
}
37
36
38
- private async parseType ( type : SchemaType , signal ?: AbortSignal ) : Promise < ExpandedJSONSchema > {
37
+ async function parseType ( type : SchemaType , signal ?: AbortSignal ) : Promise < ExpandedJSONSchema > {
39
38
await allowAbort ( signal ) ;
40
39
const schema : ExpandedJSONSchema = {
41
- ...this . getStandardType ( type . bsonType ) ,
42
- 'x-bsonType' : this . getBsonType ( type . bsonType ) ,
43
- 'x-metadata' : this . getMetadata ( type )
40
+ ...getStandardType ( type . bsonType ) ,
41
+ 'x-bsonType' : getBsonType ( type . bsonType ) ,
42
+ 'x-metadata' : getMetadata ( type )
44
43
} ;
45
- if ( 'values' in type && ! ! type . values ) {
44
+ if ( 'values' in type && type . values ) {
46
45
schema [ 'x-sampleValues' ] = type . values ;
47
46
}
48
- if ( schema . $ref ) this . markUsedDefinition ( schema . $ref ) ;
47
+ if ( schema . $ref ) markUsedDefinition ( schema . $ref ) ;
49
48
switch ( type . bsonType ) {
50
49
case 'Array' :
51
- schema . items = await this . parseTypes ( ( type as ArraySchemaType ) . types ) ;
50
+ schema . items = await parseTypes ( ( type as ArraySchemaType ) . types , signal ) ;
52
51
break ;
53
52
case 'Document' :
54
- Object . assign ( schema ,
55
- await this . parseFields ( ( type as DocumentSchemaType ) . fields , signal )
56
- ) ;
53
+ Object . assign ( schema , await parseFields ( ( type as DocumentSchemaType ) . fields , signal ) ) ;
57
54
break ;
58
55
}
59
-
60
56
return schema ;
61
57
}
62
58
63
- private getMetadata < TType extends SchemaField | SchemaType > ( {
59
+ function getMetadata < TType extends SchemaField | SchemaType > ( {
64
60
hasDuplicates,
65
61
probability,
66
62
count
@@ -72,50 +68,49 @@ export class InternalToExpandedConverter {
72
68
} ;
73
69
}
74
70
75
- private async parseTypes ( types : SchemaType [ ] , signal ?: AbortSignal ) : Promise < ExpandedJSONSchema > {
71
+ async function parseTypes ( types : SchemaType [ ] , signal ?: AbortSignal ) : Promise < ExpandedJSONSchema > {
76
72
await allowAbort ( signal ) ;
77
73
const definedTypes = types . filter ( type => type . bsonType . toLowerCase ( ) !== 'undefined' ) ;
78
74
const isSingleType = definedTypes . length === 1 ;
79
75
if ( isSingleType ) {
80
- return this . parseType ( definedTypes [ 0 ] , signal ) ;
76
+ return parseType ( definedTypes [ 0 ] , signal ) ;
81
77
}
82
- const parsedTypes = await Promise . all ( definedTypes . map ( type => this . parseType ( type , signal ) ) ) ;
78
+ const parsedTypes = await Promise . all ( definedTypes . map ( type => parseType ( type , signal ) ) ) ;
83
79
return {
84
80
anyOf : parsedTypes
85
81
} ;
86
82
}
87
83
88
- private async parseFields ( fields : DocumentSchemaType [ 'fields' ] , signal ?: AbortSignal ) : Promise < {
89
- required : ExpandedJSONSchema [ 'required '] ,
90
- properties : ExpandedJSONSchema [ 'properties' ] ,
91
- } > {
84
+ async function parseFields (
85
+ fields : DocumentSchemaType [ 'fields '] ,
86
+ signal ?: AbortSignal
87
+ ) : Promise < { required : ExpandedJSONSchema [ 'required' ] ; properties : ExpandedJSONSchema [ 'properties' ] } > {
92
88
const required = [ ] ;
93
89
const properties : ExpandedJSONSchema [ 'properties' ] = { } ;
94
90
for ( const field of fields ) {
95
91
if ( field . probability === 1 ) required . push ( field . name ) ;
96
92
properties [ field . name ] = {
97
- ...await this . parseTypes ( field . types , signal ) ,
98
- 'x-metadata' : this . getMetadata ( field )
93
+ ...await parseTypes ( field . types , signal ) ,
94
+ 'x-metadata' : getMetadata ( field )
99
95
} ;
100
96
}
101
97
102
98
return { required, properties } ;
103
99
}
104
100
105
- public async convert (
101
+ return async function convert (
106
102
internalSchema : InternalSchema ,
107
- options : {
108
- signal ?: AbortSignal
109
- } = { } ) : Promise < ExpandedJSONSchema > {
110
- this . clearUsedDefintions ( ) ;
111
- const { required, properties } = await this . parseFields ( internalSchema . fields , options . signal ) ;
103
+ options : { signal ?: AbortSignal } = { }
104
+ ) : Promise < ExpandedJSONSchema > {
105
+ clearUsedDefinitions ( ) ;
106
+ const { required, properties } = await parseFields ( internalSchema . fields , options . signal ) ;
112
107
const schema : ExpandedJSONSchema = {
113
108
type : 'object' ,
114
109
'x-bsonType' : 'object' ,
115
110
required,
116
111
properties,
117
- $defs : this . getUsedDefinitions ( )
112
+ $defs : getUsedDefinitions ( )
118
113
} ;
119
114
return schema ;
120
- }
121
- }
115
+ } ;
116
+ } ) ( ) ;
0 commit comments