@autobe/agent
Version:
AI backend server code generator
52 lines (51 loc) • 2.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutoBeDatabaseComponentProgrammer = void 0;
const utils_1 = require("@autobe/utils");
const pluralize_1 = require("pluralize");
const tstl_1 = require("tstl");
var AutoBeDatabaseComponentProgrammer;
(function (AutoBeDatabaseComponentProgrammer) {
AutoBeDatabaseComponentProgrammer.validate = (props) => {
// pluralize table names in designs
for (const design of props.tables)
design.name = (0, pluralize_1.plural)(design.name);
if (props.prefix === null)
return;
// validate prefix
const prefix = props.prefix + "_";
props.tables.forEach((design, i) => {
if (design.name.startsWith(prefix) === false)
props.errors.push({
path: `${props.path}[${i}].name`,
expected: `${prefix}${design.name} | \`${prefix}\${string}\``,
value: design.name,
description: utils_1.StringUtil.trim `
Table "${design.name}" does not start with required prefix "${prefix}".
Fix: Rename the table to "${prefix}${design.name}" or use appropriate
prefix that matches the configured naming convention.
`,
});
});
};
AutoBeDatabaseComponentProgrammer.removeDuplicatedTable = (components) => {
// 1. First, remove duplicates within each component
const deduplicatedComponents = components.map((c) => (Object.assign(Object.assign({}, c), { tables: c.tables.filter((table, index, self) => self.findIndex((t) => t.name === table.name) === index) })));
// 2. Then, remove duplicates across components
const tableSet = new Set(deduplicatedComponents.flatMap((c) => c.tables.map((t) => t.name)));
const sorted = deduplicatedComponents
.map((c, i) => new tstl_1.Pair(c, i))
.sort((a, b) => a.first.tables.length - b.first.tables.length);
return sorted
.map((p) => new tstl_1.Pair(Object.assign(Object.assign({}, p.first), { tables: p.first.tables.filter((t) => {
if (tableSet.has(t.name) === false)
return false;
tableSet.delete(t.name);
return true;
}) }), p.second))
.sort((a, b) => a.second - b.second)
.map((p) => p.first)
.filter((c) => c.tables.length !== 0);
};
})(AutoBeDatabaseComponentProgrammer || (exports.AutoBeDatabaseComponentProgrammer = AutoBeDatabaseComponentProgrammer = {}));
//# sourceMappingURL=AutoBeDatabaseComponentProgrammer.js.map