-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Schema as InternalSchema } from './schema-analyzer'; | ||
import { internalSchemaToExtended, internalSchemaToMongodb, internalSchemaToStandard } from './schema-convertors'; | ||
import { ExtendedJSONSchema, MongodbJSONSchema, StandardJSONSchema } from './types'; | ||
|
||
export interface SchemaAccessor { | ||
getStandardJsonSchema: () => Promise<StandardJSONSchema>; | ||
getMongodbJsonSchema: () => Promise<MongodbJSONSchema>; | ||
getExtendedJsonSchema: () => Promise<ExtendedJSONSchema>; | ||
getInternalSchema: () => Promise<InternalSchema>; | ||
} | ||
|
||
/** | ||
* Accessor for different schema formats. | ||
* Internal schema is provided at initialization, | ||
* the others are converted lazily and memoized. | ||
* Conversion can be aborted. | ||
*/ | ||
export class InternalSchemaBasedAccessor implements SchemaAccessor { | ||
private internalSchema: InternalSchema; | ||
private standardJSONSchema?: StandardJSONSchema; | ||
private mongodbJSONSchema?: MongodbJSONSchema; | ||
private extendedJSONSchema?: ExtendedJSONSchema; | ||
private signal?: AbortSignal; | ||
|
||
constructor(internalSchema: InternalSchema, signal?: AbortSignal) { | ||
this.signal = signal; | ||
this.internalSchema = internalSchema; | ||
} | ||
|
||
async getInternalSchema(): Promise<InternalSchema> { | ||
return this.internalSchema; | ||
} | ||
|
||
async getStandardJsonSchema(): Promise<StandardJSONSchema> { | ||
if (this.standardJSONSchema) return this.standardJSONSchema; | ||
return this.standardJSONSchema = await internalSchemaToStandard(this.internalSchema, { signal: this.signal }); | ||
} | ||
|
||
async getMongodbJsonSchema(): Promise<MongodbJSONSchema> { | ||
if (this.mongodbJSONSchema) return this.mongodbJSONSchema; | ||
return this.mongodbJSONSchema = await internalSchemaToMongodb(this.internalSchema, { signal: this.signal }); | ||
} | ||
|
||
async getExtendedJsonSchema(): Promise<ExtendedJSONSchema> { | ||
if (this.extendedJSONSchema) return this.extendedJSONSchema; | ||
return this.extendedJSONSchema = await internalSchemaToExtended(this.internalSchema, { signal: this.signal }); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Schema as InternalSchema } from './schema-analyzer'; | ||
import { ExtendedJSONSchema, MongodbJSONSchema, StandardJSONSchema } from './types'; | ||
|
||
export function internalSchemaToStandard( | ||
internalSchema: InternalSchema, | ||
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 18.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 20.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 16.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 20.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 18.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 16.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 20.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 18.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 16.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 20.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 16.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 18.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 18.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 20.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 16.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 20.x)
Check warning on line 5 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 18.x)
|
||
options: { | ||
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 18.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 20.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 16.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 20.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 18.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 16.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 20.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 18.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 16.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 20.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 16.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 18.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 18.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 20.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 16.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 20.x)
Check warning on line 6 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 18.x)
|
||
signal?: AbortSignal | ||
}): StandardJSONSchema { | ||
// TODO: COMPASS-8700 | ||
return {}; | ||
} | ||
|
||
export function internalSchemaToMongodb( | ||
internalSchema: InternalSchema, | ||
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 18.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 20.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 16.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 20.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 18.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 16.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 20.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 18.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 16.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 20.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 16.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 18.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 18.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 20.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 16.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 20.x)
Check warning on line 14 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 18.x)
|
||
options: { | ||
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 18.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 20.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 16.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 20.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 18.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 16.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 20.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 18.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 16.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 20.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 16.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 18.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 18.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 20.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 16.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 20.x)
Check warning on line 15 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 18.x)
|
||
signal?: AbortSignal | ||
}): MongodbJSONSchema { | ||
// TODO: COMPASS-8701 | ||
return {} as MongodbJSONSchema; | ||
} | ||
|
||
export function internalSchemaToExtended( | ||
internalSchema: InternalSchema, | ||
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 18.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 20.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 16.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 20.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 18.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 16.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 20.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 18.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 16.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 20.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 16.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 18.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 18.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 20.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 16.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 20.x)
Check warning on line 23 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 18.x)
|
||
options: { | ||
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 18.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 20.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 16.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 20.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 18.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 16.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 20.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 18.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 16.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 20.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (macos-latest, 16.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (ubuntu-latest, 18.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 18.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 20.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 16.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 20.x)
Check warning on line 24 in src/schema-convertors.ts GitHub Actions / Test (windows-latest, 18.x)
|
||
signal?: AbortSignal | ||
}): ExtendedJSONSchema { | ||
// TODO: COMPASS-8702 | ||
return {} as ExtendedJSONSchema; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { JSONSchema4 } from 'json-schema'; | ||
|
||
export type StandardJSONSchema = JSONSchema4; | ||
|
||
export type MongodbJSONSchema = Pick<StandardJSONSchema, 'title' | 'required' | 'description'> & { | ||
bsonType: string; | ||
properties?: Record<string, MongodbJSONSchema>; | ||
items?: MongodbJSONSchema[]; | ||
anyOf?: MongodbJSONSchema[]; | ||
} | ||
|
||
export type ExtendedJSONSchema = StandardJSONSchema & { | ||
['x-bsonType']: string; | ||
['x-metadata']: { | ||
hasDuplicates: boolean; | ||
probability: number; | ||
count: number; | ||
}; | ||
['x-sampleValues']: any[]; | ||
} | ||
|
||
export type AnyIterable<T = any> = Iterable<T> | AsyncIterable<T>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { SchemaAnalyzer, SchemaParseOptions } from './schema-analyzer'; | ||
import { AnyIterable } from './types'; | ||
|
||
export function verifyStreamSource( | ||
source: AnyIterable | ||
): AnyIterable { | ||
if (!(Symbol.iterator in source) && !(Symbol.asyncIterator in source)) { | ||
throw new Error( | ||
'Unknown input type for `docs`. Must be an array, ' + | ||
'stream or MongoDB Cursor.' | ||
); | ||
} | ||
|
||
return source; | ||
} | ||
|
||
export async function getCompletedSchemaAnalyzer( | ||
source: AnyIterable, | ||
options?: SchemaParseOptions | ||
): Promise<SchemaAnalyzer> { | ||
const analyzer = new SchemaAnalyzer(options); | ||
for await (const doc of verifyStreamSource(source)) { | ||
if (options?.signal?.aborted) throw options.signal.aborted; | ||
analyzer.analyzeDoc(doc); | ||
} | ||
return analyzer; | ||
} |