Skip to content

Commit 0c6a595

Browse files
committed
Refactoring to remove heavy dependencies
1 parent 969a977 commit 0c6a595

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

deno.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
"@std/http": "jsr:@std/http@^1",
2121
"hub": "jsr:@acr/hub@^0"
2222
},
23+
"publish": {
24+
"include": [
25+
"LICENSE",
26+
"README.md",
27+
"mod.ts",
28+
"src/**/*.ts"
29+
]
30+
},
2331
"tasks": {
2432
"check": "deno check **/*.ts && deno lint && deno fmt --check",
2533
"lint": "deno lint src test",

src/ddl.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,35 @@ const _BaseSchema: DB.Schema = {
3030
};
3131

3232
export 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

test/ddl.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ import { createTables, dbInit, getProvider } from "./helpers.ts";
1111
const CI = Deno.env.has("CI");
1212
const DB = await dbInit(getProvider());
1313

14+
// Generator function is declared here so that it does not go into the published module
15+
DDL.generator = async function(classFiles: Record<string, string>, base?: string) {
16+
const TJS = (await import("npm:typescript-json-schema@0.65.1")).default;
17+
const program = TJS.getProgramFromFiles(Object.values(classFiles), DDL.TS_OPTIONS, base);
18+
// deno-lint-ignore no-explicit-any
19+
const entries = Object.keys(classFiles).map((c) => [c, TJS.generateSchema(program, c, DDL.TJS_OPTIONS as any)]);
20+
return Object.fromEntries(entries);
21+
};
22+
1423
// Import the static schema from the JSON file
1524
import staticSchema from "../resources/account.json" with { type: "json" };
1625

0 commit comments

Comments
 (0)