@@ -30,11 +30,35 @@ const _BaseSchema: DB.Schema = {
3030} ;
3131
3232export class DDL {
33+ static EXTENSIONS = [ "as" , "constraint" , "dateOn" , "fullText" , "index" , "primaryKey" , "relations" , "unique" , "table" ] ;
34+ static TS_OPTIONS = { lib : [ "es2022" ] , module : "es2022" , target : "es2022" } ;
35+ static TJS_OPTIONS = { required : true , ignoreErrors : true , defaultNumberType : "integer" , validationKeywords : DDL . EXTENSIONS } ;
36+
3337 static padWidth = 4 ;
3438 static defaultWidth = 128 ;
3539 static textWidth = 2048 ;
3640
37- static async ensureSchemas ( schemasFile : string , classFiles : Record < string , string > , base = "" , enhance = false ) : Promise < Record < string , Schema > > {
41+ /**
42+ * Generator function that creates a map of schemas from class files
43+ * @param classFiles - a map of class names to file paths
44+ * @param base - the base directory where the files are located
45+ * @param extensions - additional extensions to be used by the generator
46+ * @example
47+ *
48+ * Below is an example of how to define the generator function using :
49+ *
50+ * ```ts
51+ * DDL.generator = async function(classFiles: Record<string, string>, base?: string) {
52+ * const TJS = (await import("npm:typescript-json-schema@0.65.1")).default;
53+ * const program = TJS.getProgramFromFiles(Object.values(classFiles), DDL.TS_OPTIONS, base);
54+ * const entries = Object.keys(classFiles).map((c) => [c, TJS.generateSchema(program, c, DDL.TJS_OPTIONS)]);
55+ * return Object.fromEntries(entries);
56+ * };
57+ * ```
58+ */
59+ static generator : ( classFiles : Record < string , string > , base ?: string , extensions ?: string [ ] ) => Promise < Record < string , Schema > > ;
60+
61+ static async ensureSchemas ( schemasFile : string , classFiles : Record < string , string > , base ?: string , enhance = false ) : Promise < Record < string , Schema > > {
3862 const sfn = base + schemasFile ;
3963
4064 // Try reading schemas from file
@@ -48,23 +72,15 @@ export class DDL {
4872 return schemas ;
4973 }
5074
51- static async generateSchemas ( classFiles : Record < string , string > , base ?: string , enhance = false ) : Promise < Record < string , Schema > > {
52- const TJS = ( await import ( "npm:typescript-json-schema@0.65.1" ) ) . default ;
53-
54- // Parameters for TypeScript JSON Schema
55- const validationKeywords = [ "as" , "constraint" , "dateOn" , "fullText" , "index" , "primaryKey" , "relations" , "unique" , "table" ] ;
56- const settings = { required : true , ignoreErrors : true , defaultNumberType : "integer" , validationKeywords } ;
57- const compilerOptions = { lib : [ "es2022" ] , module : "es2022" , target : "es2022" } ;
58-
59- // Get current set of schemas and find out which ones are outdated
60- const schemas = { } as Record < string , Schema > ;
75+ static async generateSchemas ( classFiles : Record < string , string > , base ?: string , enhance ?: boolean ) : Promise < Record < string , Schema > > {
76+ // If DDL has no generator, throw an error
77+ if ( ! DDL . generator ) throw new Error ( "DDL.generator must be set to a function that generates schemas from class files" ) ;
6178
62- // Run schema generation
63- const program = TJS . getProgramFromFiles ( Object . values ( classFiles ) , compilerOptions , base ) ;
79+ // Generate schemas and clean them and enhance them
80+ const schemas = await DDL . generator ( classFiles , base ) ;
6481 for ( const [ c , f ] of Object . entries ( classFiles ) ) {
6582 const etag = await eTag ( await Deno . stat ( f ) ) ;
66- // deno-lint-ignore no-explicit-any
67- schemas [ c ] = DDL . #cleanSchema( TJS . generateSchema ( program , c , settings as any ) as Schema , c , undefined , "file://./" + f , etag ) ;
83+ schemas [ c ] = DDL . #cleanSchema( schemas [ c ] , c , undefined , "file://./" + f , etag ) ;
6884 if ( enhance ) schemas [ c ] = DDL . enhanceSchema ( schemas [ c ] ) ;
6985 }
7086
0 commit comments