1
+ import { InternalSchemaBasedAccessor , SchemaAccessor } from './schema-accessor' ;
1
2
import { SchemaAnalyzer } from './schema-analyzer' ;
2
3
import type {
3
4
ArraySchemaType ,
@@ -6,7 +7,7 @@ import type {
6
7
DocumentSchemaType ,
7
8
PrimitiveSchemaType ,
8
9
SchemaType ,
9
- Schema ,
10
+ Schema as InternalSchema ,
10
11
SchemaField ,
11
12
SchemaParseOptions ,
12
13
SimplifiedSchemaBaseType ,
@@ -17,31 +18,18 @@ import type {
17
18
SimplifiedSchema
18
19
} from './schema-analyzer' ;
19
20
import * as schemaStats from './stats' ;
21
+ import { AnyIterable , StandardJSONSchema , MongodbJSONSchema , ExtendedJSONSchema } from './types' ;
22
+ import { getCompletedSchemaAnalyzer } from './utils' ;
20
23
21
- type AnyIterable < T = any > = Iterable < T > | AsyncIterable < T > ;
22
-
23
- function verifyStreamSource (
24
- source : AnyIterable
25
- ) : AnyIterable {
26
- if ( ! ( Symbol . iterator in source ) && ! ( Symbol . asyncIterator in source ) ) {
27
- throw new Error (
28
- 'Unknown input type for `docs`. Must be an array, ' +
29
- 'stream or MongoDB Cursor.'
30
- ) ;
31
- }
32
-
33
- return source ;
34
- }
35
-
36
- async function getCompletedSchemaAnalyzer (
24
+ /**
25
+ * Analyze documents - schema can be retrieved in different formats.
26
+ */
27
+ async function analyzeDocuments (
37
28
source : AnyIterable ,
38
29
options ?: SchemaParseOptions
39
- ) : Promise < SchemaAnalyzer > {
40
- const analyzer = new SchemaAnalyzer ( options ) ;
41
- for await ( const doc of verifyStreamSource ( source ) ) {
42
- analyzer . analyzeDoc ( doc ) ;
43
- }
44
- return analyzer ;
30
+ ) : Promise < SchemaAccessor > {
31
+ const internalSchema = ( await getCompletedSchemaAnalyzer ( source , options ) ) . getResult ( ) ;
32
+ return new InternalSchemaBasedAccessor ( internalSchema , options ?. signal ) ;
45
33
}
46
34
47
35
/**
@@ -51,7 +39,7 @@ async function getCompletedSchemaAnalyzer(
51
39
async function parseSchema (
52
40
source : AnyIterable ,
53
41
options ?: SchemaParseOptions
54
- ) : Promise < Schema > {
42
+ ) : Promise < InternalSchema > {
55
43
return ( await getCompletedSchemaAnalyzer ( source , options ) ) . getResult ( ) ;
56
44
}
57
45
@@ -78,19 +66,24 @@ export type {
78
66
DocumentSchemaType ,
79
67
PrimitiveSchemaType ,
80
68
SchemaType ,
81
- Schema ,
69
+ InternalSchema as Schema ,
70
+ InternalSchema ,
82
71
SchemaField ,
83
72
SchemaParseOptions ,
84
73
SimplifiedSchemaBaseType ,
85
74
SimplifiedSchemaArrayType ,
86
75
SimplifiedSchemaDocumentType ,
87
76
SimplifiedSchemaType ,
88
77
SimplifiedSchemaField ,
89
- SimplifiedSchema
78
+ SimplifiedSchema ,
79
+ StandardJSONSchema ,
80
+ MongodbJSONSchema ,
81
+ ExtendedJSONSchema
90
82
} ;
91
83
92
84
export {
93
85
parseSchema ,
86
+ analyzeDocuments ,
94
87
getSchemaPaths ,
95
88
getSimplifiedSchema ,
96
89
SchemaAnalyzer ,
0 commit comments