Skip to content

Commit 2350b28

Browse files
committed
pr suggestions
1 parent 1249f8b commit 2350b28

File tree

5 files changed

+48
-47
lines changed

5 files changed

+48
-47
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {
1818
SimplifiedSchema
1919
} from './schema-analyzer';
2020
import * as schemaStats from './stats';
21-
import { AnyIterable, StandardJSONSchema, MongodbJSONSchema, ExtendedJSONSchema } from './types';
21+
import { AnyIterable, StandardJSONSchema, MongoDBJSONSchema, ExtendedJSONSchema } from './types';
2222
import { getCompletedSchemaAnalyzer } from './utils';
2323

2424
/**
@@ -77,7 +77,7 @@ export type {
7777
SimplifiedSchemaField,
7878
SimplifiedSchema,
7979
StandardJSONSchema,
80-
MongodbJSONSchema,
80+
MongoDBJSONSchema,
8181
ExtendedJSONSchema
8282
};
8383

src/schema-accessor.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import { Schema as InternalSchema } from './schema-analyzer';
22
import convertors from './schema-convertors';
3-
import { ExtendedJSONSchema, MongodbJSONSchema, StandardJSONSchema } from './types';
3+
import { ExtendedJSONSchema, MongoDBJSONSchema, StandardJSONSchema } from './types';
44

55
export interface SchemaAccessor {
66
getStandardJsonSchema: () => Promise<StandardJSONSchema>;
7-
getMongodbJsonSchema: () => Promise<MongodbJSONSchema>;
7+
getMongoDBJsonSchema: () => Promise<MongoDBJSONSchema>;
88
getExtendedJsonSchema: () => Promise<ExtendedJSONSchema>;
99
getInternalSchema: () => Promise<InternalSchema>;
1010
}
1111

12+
type Options = {
13+
signal?: AbortSignal;
14+
}
15+
1216
/**
1317
* Accessor for different schema formats.
1418
* Internal schema is provided at initialization,
@@ -18,31 +22,29 @@ export interface SchemaAccessor {
1822
export class InternalSchemaBasedAccessor implements SchemaAccessor {
1923
private internalSchema: InternalSchema;
2024
private standardJSONSchema?: StandardJSONSchema;
21-
private mongodbJSONSchema?: MongodbJSONSchema;
25+
private mongodbJSONSchema?: MongoDBJSONSchema;
2226
private extendedJSONSchema?: ExtendedJSONSchema;
23-
private signal?: AbortSignal;
2427

25-
constructor(internalSchema: InternalSchema, signal?: AbortSignal) {
26-
this.signal = signal;
28+
constructor(internalSchema: InternalSchema) {
2729
this.internalSchema = internalSchema;
2830
}
2931

30-
async getInternalSchema(): Promise<InternalSchema> {
32+
async getInternalSchema(options?: Options): Promise<InternalSchema> {
3133
return this.internalSchema;
3234
}
3335

34-
async getStandardJsonSchema(): Promise<StandardJSONSchema> {
36+
async getStandardJsonSchema(options: Options = {}): Promise<StandardJSONSchema> {
3537
if (this.standardJSONSchema) return this.standardJSONSchema;
36-
return this.standardJSONSchema = await convertors.internalSchemaToStandard(this.internalSchema, { signal: this.signal });
38+
return this.standardJSONSchema = await convertors.internalSchemaToStandard(this.internalSchema, options);
3739
}
3840

39-
async getMongodbJsonSchema(): Promise<MongodbJSONSchema> {
41+
async getMongoDBJsonSchema(options: Options = {}): Promise<MongoDBJSONSchema> {
4042
if (this.mongodbJSONSchema) return this.mongodbJSONSchema;
41-
return this.mongodbJSONSchema = await convertors.internalSchemaToMongodb(this.internalSchema, { signal: this.signal });
43+
return this.mongodbJSONSchema = await convertors.internalSchemaToMongoDB(this.internalSchema, options);
4244
}
4345

44-
async getExtendedJsonSchema(): Promise<ExtendedJSONSchema> {
46+
async getExtendedJsonSchema(options: Options = {}): Promise<ExtendedJSONSchema> {
4547
if (this.extendedJSONSchema) return this.extendedJSONSchema;
46-
return this.extendedJSONSchema = await convertors.internalSchemaToExtended(this.internalSchema, { signal: this.signal });
48+
return this.extendedJSONSchema = await convertors.internalSchemaToExtended(this.internalSchema, options);
4749
}
4850
}

src/schema-analyzer.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from 'bson';
1818

1919
import semanticTypes from './semantic-types';
20+
import { AnyIterable } from './types';
2021

2122
type TypeCastMap = {
2223
Array: unknown[];
@@ -586,3 +587,28 @@ export class SchemaAnalyzer {
586587
return simplifiedSchema(this.schemaAnalysisRoot.fields);
587588
}
588589
}
590+
591+
export function verifyStreamSource(
592+
source: AnyIterable
593+
): AnyIterable {
594+
if (!(Symbol.iterator in source) && !(Symbol.asyncIterator in source)) {
595+
throw new Error(
596+
'Unknown input type for `docs`. Must be an array, ' +
597+
'stream or MongoDB Cursor.'
598+
);
599+
}
600+
601+
return source;
602+
}
603+
604+
export async function getCompletedSchemaAnalyzer(
605+
source: AnyIterable,
606+
options?: SchemaParseOptions
607+
): Promise<SchemaAnalyzer> {
608+
const analyzer = new SchemaAnalyzer(options);
609+
for await (const doc of verifyStreamSource(source)) {
610+
if (options?.signal?.aborted) throw options.signal.aborted;
611+
analyzer.analyzeDoc(doc);
612+
}
613+
return analyzer;
614+
}

src/schema-convertors.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Schema as InternalSchema } from './schema-analyzer';
2-
import { ExtendedJSONSchema, MongodbJSONSchema, StandardJSONSchema } from './types';
2+
import { ExtendedJSONSchema, MongoDBJSONSchema, StandardJSONSchema } from './types';
33

44
function internalSchemaToStandard(
55
internalSchema: InternalSchema,
@@ -10,13 +10,13 @@ function internalSchemaToStandard(
1010
return {};
1111
}
1212

13-
function internalSchemaToMongodb(
13+
function internalSchemaToMongoDB(
1414
internalSchema: InternalSchema,
1515
options: {
1616
signal?: AbortSignal
17-
}): MongodbJSONSchema {
17+
}): MongoDBJSONSchema {
1818
// TODO: COMPASS-8701
19-
return {} as MongodbJSONSchema;
19+
return {} as MongoDBJSONSchema;
2020
}
2121

2222
function internalSchemaToExtended(
@@ -30,6 +30,6 @@ function internalSchemaToExtended(
3030

3131
export default {
3232
internalSchemaToStandard,
33-
internalSchemaToMongodb,
33+
internalSchemaToMongoDB,
3434
internalSchemaToExtended
3535
};

src/utils.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)