-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: send schema to the model for better results VSCODE-581 (#792)
* feat: send schema to the model for better results VSCODE-581 * refactor: change state in fetchCollectionSchema
- Loading branch information
1 parent
16277cb
commit 7a9ba2a
Showing
4 changed files
with
233 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import type { | ||
SimplifiedSchema, | ||
SimplifiedSchemaArrayType, | ||
SimplifiedSchemaDocumentType, | ||
SimplifiedSchemaType, | ||
} from 'mongodb-schema'; | ||
|
||
const PROPERTY_REGEX = '^[a-zA-Z_$][0-9a-zA-Z_$]*$'; | ||
|
||
export class SchemaFormatter { | ||
static getSchemaFromTypes(pInput: SimplifiedSchema): string { | ||
return new SchemaFormatter().format(pInput); | ||
} | ||
|
||
schemaString = ''; | ||
|
||
format(pInitial: SimplifiedSchema): string { | ||
this.processDocumentType('', pInitial); | ||
return this.schemaString; | ||
} | ||
|
||
private processSchemaTypeList( | ||
prefix: string, | ||
pTypes: SimplifiedSchemaType[] | ||
) { | ||
if (pTypes.length !== 0) { | ||
this.processSchemaType(prefix, pTypes[0]); | ||
} | ||
} | ||
|
||
private processSchemaType(prefix: string, pType: SimplifiedSchemaType) { | ||
const bsonType = pType.bsonType; | ||
if (bsonType === 'Document') { | ||
const fields = (pType as SimplifiedSchemaDocumentType).fields; | ||
|
||
if (Object.keys(fields).length === 0) { | ||
this.addToFormattedSchemaString(prefix + ': Document'); | ||
return; | ||
} | ||
|
||
this.processDocumentType(prefix, fields); | ||
return; | ||
} | ||
|
||
if (bsonType === 'Array') { | ||
const types = (pType as SimplifiedSchemaArrayType).types; | ||
|
||
if (types.length === 0) { | ||
this.addToFormattedSchemaString(prefix + ': ' + 'Array'); | ||
return; | ||
} | ||
|
||
const firstType = types[0].bsonType; | ||
if (firstType !== 'Array' && firstType !== 'Document') { | ||
this.addToFormattedSchemaString( | ||
prefix + ': ' + 'Array<' + firstType + '>' | ||
); | ||
return; | ||
} | ||
|
||
// Array of documents or arrays. | ||
// We only use the first type. | ||
this.processSchemaType(prefix + '[]', types[0]); | ||
return; | ||
} | ||
|
||
this.addToFormattedSchemaString(prefix + ': ' + bsonType); | ||
} | ||
|
||
private processDocumentType(prefix: string, pDoc: SimplifiedSchema) { | ||
if (!pDoc) { | ||
return; | ||
} | ||
|
||
Object.keys(pDoc).forEach((key) => { | ||
const keyAsString = this.getPropAsString(key); | ||
this.processSchemaTypeList( | ||
prefix + (prefix.length === 0 ? '' : '.') + keyAsString, | ||
pDoc[key]?.types | ||
); | ||
}); | ||
} | ||
|
||
getPropAsString(pProp: string): string { | ||
if (pProp.match(PROPERTY_REGEX)) { | ||
return pProp; | ||
} | ||
|
||
try { | ||
return JSON.stringify(pProp); | ||
} catch (e) { | ||
return pProp; | ||
} | ||
} | ||
|
||
addToFormattedSchemaString(fieldAndType: string) { | ||
if (this.schemaString.length > 0) { | ||
this.schemaString += '\n'; | ||
} | ||
this.schemaString += fieldAndType; | ||
} | ||
} |
Oops, something went wrong.