UNPKG

@onn-software/ddl-to-gql

Version:

Convert a SQL DDL to a GraphQL implementation with all relations.

146 lines (145 loc) 7.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Executor = void 0; const fs_1 = __importDefault(require("fs")); const globals_1 = require("./globals"); const util_1 = require("./util"); class Executor { constructor(ddlInterpreter, heuristicEngine, modelGenerator, repoGenerator, resolverGenerator, schemaGenerator, mainGenerator) { this.ddlInterpreter = ddlInterpreter; this.heuristicEngine = heuristicEngine; this.modelGenerator = modelGenerator; this.repoGenerator = repoGenerator; this.resolverGenerator = resolverGenerator; this.schemaGenerator = schemaGenerator; this.mainGenerator = mainGenerator; } execute(options) { console.log(`Options`, options); this.assertOptions(options); if (options.phases.length === 0 || options.phases.indexOf('ddl') >= 0) { console.log(`Phase: ddl`); const overrides = options.ddlOverridesPath && fs_1.default.existsSync(options.ddlOverridesPath) ? JSON.parse(fs_1.default.readFileSync(options.ddlOverridesPath, 'utf-8')) : {}; this.executeDdl(options.ddlPath, options.defPath, overrides); } const sourceTableDefs = JSON.parse(fs_1.default.readFileSync(options.defPath, 'utf-8')); if (options.phases.length === 0 || options.phases.indexOf('heuristics') >= 0) { console.log(`Phase: heuristics`); this.executeHeuristics(sourceTableDefs, options.heurPath, options.heurSuffixes?.split(',') ?? [], options.heurEnableAll); } const heuristics = options.heurPath && fs_1.default.existsSync(options.heurPath) ? JSON.parse(fs_1.default.readFileSync(options.heurPath, 'utf-8')) : []; const tableDefs = Executor.mergeTableDefs(sourceTableDefs, heuristics); if (options.phases.length === 0 || options.phases.indexOf('model') >= 0) { console.log(`Phase: model`); const res = this.modelGenerator.execute(tableDefs); fs_1.default.writeFileSync(`${options.tsFolder}/model.ts`, res); } if (options.phases.length === 0 || options.phases.indexOf('repo') >= 0) { console.log(`Phase: repo`); const res = this.repoGenerator.execute(tableDefs); fs_1.default.writeFileSync(`${options.tsFolder}/repos.ts`, res); } if (options.phases.length === 0 || options.phases.indexOf('resolver') >= 0) { console.log(`Phase: resolver`); const res = this.resolverGenerator.execute(tableDefs, options.gqlNoRoot); fs_1.default.writeFileSync(`${options.tsFolder}/resolvers.ts`, res); } if (options.phases.length === 0 || options.phases.indexOf('schema') >= 0) { console.log(`Phase: schema`); const res = this.schemaGenerator.execute(tableDefs, options.gqlNoRoot, options.gqlNoMutations); fs_1.default.writeFileSync(`${options.gqlFolder}/onn-ddl-to-gql.graphql`, res); } if (options.phases.length === 0 || options.phases.indexOf('main') >= 0) { console.log(`Phase: main`); const res = this.mainGenerator.execute(options.sqlFactory, options.gqlNoRoot); fs_1.default.writeFileSync(`${options.tsFolder}/index.ts`, res); } console.log(`Success`); } assertOptions(options) { if ((options.phases.length === 0 || options.phases.indexOf('ddl') >= 0) && !options.ddlPath) { throw Error('The option ddlPath is mandatory when ddl interpretation is enabled'); } if (options.phases.length === 0 || options.phases.indexOf('model') >= 0 || options.phases.indexOf('repo') >= 0 || options.phases.indexOf('resolver') >= 0 || options.phases.indexOf('main') >= 0) { if (!options.tsFolder) { throw Error('The option destFolder is mandatory when typescript generation is enabled'); } else { if (!fs_1.default.existsSync(options.tsFolder)) { fs_1.default.mkdirSync(options.tsFolder, { recursive: true }); } else if (fs_1.default.readdirSync(options.tsFolder).length > 0) { if (options.override) { fs_1.default.rmSync(options.tsFolder, { recursive: true }); fs_1.default.mkdirSync(options.tsFolder, { recursive: true }); } else { throw Error('The destFolder is not empty. Use --override to automatically clear the destFolder.'); } } } } if (options.phases.length === 0 || options.phases.indexOf('schema') >= 0) { if (!options.gqlFolder) { throw Error('The option gqlFolder is mandatory when GQL generation is enabled'); } else { if (!fs_1.default.existsSync(options.gqlFolder)) { fs_1.default.mkdirSync(options.gqlFolder); } else if (fs_1.default.readdirSync(options.gqlFolder).length > 0) { if (options.override) { fs_1.default.rmSync(options.gqlFolder, { recursive: true }); fs_1.default.mkdirSync(options.gqlFolder); } else { throw Error('The gqlFolder is not empty. Use --override to automatically clear the gqlFolder.'); } } } } if (options.tsPrefix) { globals_1.Globals.TS_PREFIX = options.tsPrefix; } if (options.gqlPrefix) { globals_1.Globals.GQL_PREFIX = options.gqlPrefix; } } executeDdl(ddlPath, tDefPath, overrides) { const ddl = fs_1.default.readFileSync(ddlPath, 'utf-8'); const tableDefs = this.ddlInterpreter.execute(ddl, overrides); fs_1.default.writeFileSync(tDefPath, JSON.stringify(tableDefs, undefined, 2)); } executeHeuristics(sourceTableDefs, heurPath, suffixes, heurEnableAll) { const heurs = this.heuristicEngine.execute(sourceTableDefs, { suffixes, heurEnableAll }); fs_1.default.writeFileSync(heurPath, JSON.stringify(heurs, undefined, 2)); } static mergeTableDefs(sourceTableDefs, ...partialTableDefs) { const tableDefsRecord = (0, util_1.associateBy)(sourceTableDefs, (td) => td.tableName); partialTableDefs.forEach((tableDefs) => tableDefs.forEach((def) => { tableDefsRecord[def.tableName].columns = [ ...tableDefsRecord[def.tableName].columns, ...def.columns, ]; tableDefsRecord[def.tableName].relations = [ ...tableDefsRecord[def.tableName].relations, ...def.relations.filter(r => r.enabled), ]; })); const res = Object.values(tableDefsRecord); res.sort((l, r) => l.tableName.localeCompare(r.tableName)); return res; } } exports.Executor = Executor;