Skip to content

feat: internal to Standard JSON Schema conversion COMPASS-8700 #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
10 changes: 7 additions & 3 deletions src/schema-convertors/internalToMongoDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ async function parseType(type: SchemaType, signal?: AbortSignal): Promise<MongoD
return schema;
}

function isPlainTypesOnly(types: MongoDBJSONSchema[]): types is { bsonType: string }[] {
return types.every(definition => !!definition.bsonType && Object.keys(definition).length === 1);
}

async function parseTypes(types: SchemaType[], signal?: AbortSignal): Promise<MongoDBJSONSchema> {
await allowAbort(signal);
const definedTypes = types.filter(type => type.bsonType.toLowerCase() !== 'undefined');
Expand All @@ -72,13 +76,13 @@ async function parseTypes(types: SchemaType[], signal?: AbortSignal): Promise<Mo
return parseType(definedTypes[0], signal);
}
const parsedTypes = await Promise.all(definedTypes.map(type => parseType(type, signal)));
if (definedTypes.some(type => ['Document', 'Array'].includes(type.bsonType))) {
if (isPlainTypesOnly(parsedTypes)) {
return {
anyOf: parsedTypes
bsonType: parsedTypes.map(({ bsonType }) => bsonType)
};
}
return {
bsonType: definedTypes.map((type) => convertInternalType(type.bsonType))
anyOf: parsedTypes
};
}

Expand Down
Loading