UNPKG

@autobe/agent

Version:

AI backend server code generator

79 lines (74 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AutoBeDatabaseSchemaProgrammer = void 0; const utils_1 = require("@autobe/utils"); const pluralize_1 = require("pluralize"); const AutoBeDatabaseModelProgrammer_1 = require("./AutoBeDatabaseModelProgrammer"); var AutoBeDatabaseSchemaProgrammer; (function (AutoBeDatabaseSchemaProgrammer) { AutoBeDatabaseSchemaProgrammer.validate = (props) => { // emend and fix table names AutoBeDatabaseModelProgrammer_1.AutoBeDatabaseModelProgrammer.emend(props.definition.model); for (const design of props.definition.newDesigns) design.name = AutoBeDatabaseModelProgrammer_1.AutoBeDatabaseModelProgrammer.fixName(design.name); // remove wrong named designs for (let i = props.definition.newDesigns.length - 1; i >= 0; i--) { const design = props.definition.newDesigns[i]; if (props.otherTables.includes(design.name) === false) { // to give validation feedback continue; } else if (design.name.startsWith((0, pluralize_1.singular)(props.definition.model.name))) { // correct and intended name continue; } props.definition.newDesigns.splice(i, 1); } // check whether target table exists if (props.targetTable !== props.definition.model.name) props.errors.push({ path: `${props.path}.model.name`, expected: `"${props.targetTable}"`, value: props.definition.model.name, description: utils_1.StringUtil.trim ` The model must be named exactly "${props.targetTable}", which is the target table assigned to this agent. Fix: Rename the model to "${props.targetTable}". `, }); const prefix = `${(0, pluralize_1.singular)(props.targetTable)}_`; props.definition.newDesigns.forEach((design, i) => { if (design.name.startsWith(prefix) === false) props.errors.push({ path: `${props.path}[${i}].name`, expected: `"${props.targetTable}" | \`${prefix}\${string}\``, value: design.name, description: utils_1.StringUtil.trim ` New design "${design.name}" violates the naming convention. Every newDesigns entry must be a 1NF child table whose name starts with the singular prefix "${prefix}" (e.g., "${prefix}items", "${prefix}details"). Reconsider whether "${design.name}" is truly needed, or rename it to start with "${prefix}" if it belongs to this target table. `, }); else if (props.otherTables.includes(design.name) === true) props.errors.push({ path: `${props.path}[${i}].name`, expected: `A name not colliding with neighboring tables`, value: design.name, description: utils_1.StringUtil.trim ` New design "${design.name}" collides with a table that already exists or is assigned to another agent. Tables you must not collide with: ${props.otherTables.map((t) => `- ${t}`).join("\n")} Reconsider whether this new design is necessary, or choose a different name with the "${prefix}" prefix that does not overlap with any of the above tables. `, }); }); }; })(AutoBeDatabaseSchemaProgrammer || (exports.AutoBeDatabaseSchemaProgrammer = AutoBeDatabaseSchemaProgrammer = {})); //# sourceMappingURL=AutoBeDatabaseSchemaProgrammer.js.map