Skip to content

Commit b0706f2

Browse files
committed
cleanup
1 parent d6f30e6 commit b0706f2

File tree

5 files changed

+93
-83
lines changed

5 files changed

+93
-83
lines changed

src/schema-accessor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Schema as InternalSchema } from './schema-analyzer';
22
import InternalToExpandedConvertor from './schema-convertors/internalToExpanded';
3-
import internalSchemaToMongodb from './schema-convertors/internalToMongoDB';
3+
import InternalToMongoDBConvertor from './schema-convertors/internalToMongoDB';
44
import InternalToStandardConvertor from './schema-convertors/internalToStandard';
55
import { ExpandedJSONSchema, MongoDBJSONSchema, StandardJSONSchema } from './types';
66

@@ -28,11 +28,13 @@ export class InternalSchemaBasedAccessor implements SchemaAccessor {
2828
private ExpandedJSONSchema?: ExpandedJSONSchema;
2929
public internalToStandardConvertor: InternalToStandardConvertor;
3030
public internalToExpandedConvertor: InternalToExpandedConvertor;
31+
public internalToMongoDBConvertor: InternalToMongoDBConvertor;
3132

3233
constructor(internalSchema: InternalSchema) {
3334
this.internalSchema = internalSchema;
3435
this.internalToStandardConvertor = new InternalToStandardConvertor();
3536
this.internalToExpandedConvertor = new InternalToExpandedConvertor();
37+
this.internalToMongoDBConvertor = new InternalToMongoDBConvertor();
3638
}
3739

3840
async getInternalSchema(): Promise<InternalSchema> {
@@ -51,7 +53,7 @@ export class InternalSchemaBasedAccessor implements SchemaAccessor {
5153
* Get MongoDB's $jsonSchema
5254
*/
5355
async getMongoDBJsonSchema(options: Options = {}): Promise<MongoDBJSONSchema> {
54-
return this.mongodbJSONSchema ??= await internalSchemaToMongodb(this.internalSchema, options);
56+
return this.mongodbJSONSchema ??= await this.internalToMongoDBConvertor.convert(this.internalSchema, options);
5557
}
5658

5759
/**

src/schema-convertors/index.ts

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

src/schema-convertors/internalToExpanded.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import assert from 'assert';
22
import { RELAXED_EJSON_DEFINITIONS } from './internalToStandard';
33
import InternalToExpandedConvertor from './internalToExpanded';
4-
import { ObjectId } from 'bson';
54

65
describe('internalSchemaToExpanded', async function() {
76
describe('Converts: ', async function() {

src/schema-convertors/internalToMongoDB.test.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'assert';
2-
import internalSchemaToMongoDB from './internalToMongoDB';
2+
import InternalToMongoDBConvertor from './internalToMongoDB';
33

44
describe('internalSchemaToMongoDB', async function() {
55
describe('Converts: ', async function() {
@@ -892,8 +892,9 @@ describe('internalSchemaToMongoDB', async function() {
892892
}
893893
]
894894
};
895-
const standard = await internalSchemaToMongoDB(internal);
896-
assert.deepStrictEqual(standard, {
895+
const convertor = new InternalToMongoDBConvertor();
896+
const mongodb = await convertor.convert(internal);
897+
assert.deepStrictEqual(mongodb, {
897898
bsonType: 'object',
898899
required: [],
899900
properties: {
@@ -1105,8 +1106,9 @@ describe('internalSchemaToMongoDB', async function() {
11051106
}
11061107
]
11071108
};
1108-
const standard = await internalSchemaToMongoDB(internal);
1109-
assert.deepStrictEqual(standard, {
1109+
const convertor = new InternalToMongoDBConvertor();
1110+
const mongodb = await convertor.convert(internal);
1111+
assert.deepStrictEqual(mongodb, {
11101112
bsonType: 'object',
11111113
required: ['author'],
11121114
properties: {
@@ -1190,8 +1192,9 @@ describe('internalSchemaToMongoDB', async function() {
11901192
}
11911193
]
11921194
};
1193-
const standard = await internalSchemaToMongoDB(internal);
1194-
assert.deepStrictEqual(standard, {
1195+
const convertor = new InternalToMongoDBConvertor();
1196+
const mongodb = await convertor.convert(internal);
1197+
assert.deepStrictEqual(mongodb, {
11951198
bsonType: 'object',
11961199
required: [],
11971200
properties: {
@@ -1335,8 +1338,9 @@ describe('internalSchemaToMongoDB', async function() {
13351338
}
13361339
]
13371340
};
1338-
const standard = await internalSchemaToMongoDB(internal);
1339-
assert.deepStrictEqual(standard, {
1341+
const convertor = new InternalToMongoDBConvertor();
1342+
const mongodb = await convertor.convert(internal);
1343+
assert.deepStrictEqual(mongodb, {
13401344
bsonType: 'object',
13411345
required: [],
13421346
properties: {
@@ -1429,8 +1433,9 @@ describe('internalSchemaToMongoDB', async function() {
14291433
}
14301434
]
14311435
};
1432-
const standard = await internalSchemaToMongoDB(internal);
1433-
assert.deepStrictEqual(standard, {
1436+
const convertor = new InternalToMongoDBConvertor();
1437+
const mongodb = await convertor.convert(internal);
1438+
assert.deepStrictEqual(mongodb, {
14341439
bsonType: 'object',
14351440
required: ['arrayMixedType'],
14361441
properties: {
@@ -1507,8 +1512,9 @@ describe('internalSchemaToMongoDB', async function() {
15071512
}
15081513
]
15091514
};
1510-
const standard = await internalSchemaToMongoDB(internal);
1511-
assert.deepStrictEqual(standard, {
1515+
const convertor = new InternalToMongoDBConvertor();
1516+
const mongodb = await convertor.convert(internal);
1517+
assert.deepStrictEqual(mongodb, {
15121518
bsonType: 'object',
15131519
required: [],
15141520
properties: {
@@ -1623,8 +1629,9 @@ describe('internalSchemaToMongoDB', async function() {
16231629
}
16241630
]
16251631
};
1626-
const standard = await internalSchemaToMongoDB(internal);
1627-
assert.deepStrictEqual(standard, {
1632+
const convertor = new InternalToMongoDBConvertor();
1633+
const mongodb = await convertor.convert(internal);
1634+
assert.deepStrictEqual(mongodb, {
16281635
bsonType: 'object',
16291636
required: [],
16301637
properties: {
@@ -1757,7 +1764,8 @@ describe('internalSchemaToMongoDB', async function() {
17571764
]
17581765
};
17591766
const abortController = new AbortController();
1760-
const promise = internalSchemaToMongoDB(internal, { signal: abortController.signal });
1767+
const convertor = new InternalToMongoDBConvertor();
1768+
const promise = convertor.convert(internal, { signal: abortController.signal });
17611769
abortController.abort(new Error('Too long, didn\'t wait.'));
17621770
await assert.rejects(promise, {
17631771
name: 'Error',

src/schema-convertors/internalToMongoDB.ts

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -35,77 +35,81 @@ export const InternalTypeToBsonTypeMap: Record<
3535
MaxKey: 'maxKey'
3636
};
3737

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;
5843
}
5944

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+
}
6260

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+
}
6663

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);
7366
}
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+
}
7681
return {
77-
bsonType: parsedTypes.map(({ bsonType }) => bsonType)
82+
anyOf: parsedTypes
7883
};
7984
}
80-
return {
81-
anyOf: parsedTypes
82-
};
83-
}
8485

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 };
9498
}
9599

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+
}
97113
}
98114

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

Comments
 (0)