@autobe/agent
Version:
AI backend server code generator
116 lines (114 loc) • 5.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutoBeInterfaceSchemaProgrammer = void 0;
const utils_1 = require("@autobe/utils");
const utils_2 = require("@typia/utils");
const pluralize_1 = require("pluralize");
const AutoBeDatabaseModelProgrammer_1 = require("../../database/programmers/AutoBeDatabaseModelProgrammer");
const AutoBeJsonSchemaValidator_1 = require("../utils/AutoBeJsonSchemaValidator");
var AutoBeInterfaceSchemaProgrammer;
(function (AutoBeInterfaceSchemaProgrammer) {
AutoBeInterfaceSchemaProgrammer.getDatabaseSchemaName = (typeName) => (0, pluralize_1.plural)(utils_2.NamingConvention.snake(typeName.split(".")[0].substring(1)));
AutoBeInterfaceSchemaProgrammer.getNeighborDatabaseSchemas = (props) => {
const expected = AutoBeInterfaceSchemaProgrammer.getDatabaseSchemaName(props.typeName);
const found = props.application.files
.flatMap((f) => f.models)
.find((m) => m.name === expected);
if (found === undefined)
return;
return AutoBeDatabaseModelProgrammer_1.AutoBeDatabaseModelProgrammer.getNeighbors({
application: props.application,
model: found,
});
};
AutoBeInterfaceSchemaProgrammer.getDatabaseSchemaProperties = (props) => [
// Primary key
{
key: props.model.primaryField.name,
nullable: false,
type: "uuid",
},
// Plain fields (columns like title, description, created_at, etc.)
...props.model.plainFields.map((f) => ({
key: f.name,
nullable: f.nullable,
type: f.type,
})),
// Foreign key columns (e.g., todo_app_user_id)
...props.model.foreignFields.map((f) => ({
key: f.name,
nullable: f.nullable,
type: "uuid",
})),
// Foreign key relation names (e.g., user) — relations, not columns
...props.model.foreignFields.map((f) => ({
key: f.relation.name,
nullable: f.nullable,
type: null,
})),
// Opposite relations from other models — relations, not columns
...props.everyModels
.map((m) => m.foreignFields
.filter((f) => f.relation.targetModel === props.model.name)
.map((f) => ({
key: f.relation.oppositeName,
nullable: f.unique,
type: null,
})))
.flat(),
];
AutoBeInterfaceSchemaProgrammer.validate = (props) => {
AutoBeJsonSchemaValidator_1.AutoBeJsonSchemaValidator.validateSchema({
errors: props.errors,
operations: props.operations,
typeName: props.typeName,
schema: props.design.schema,
path: `${props.path}.schema`,
});
if (props.design.databaseSchema !== null &&
props.everyModels.some((m) => m.name === props.design.databaseSchema) ===
false)
props.errors.push({
path: `${props.path}.databaseSchema`,
expected: props.everyModels
.map((m) => JSON.stringify(m.name))
.join(" | "),
value: props.design.databaseSchema,
description: utils_1.StringUtil.trim `
You set "databaseSchema" to ${JSON.stringify(props.design.databaseSchema)},
but no database model with that name exists.
Available database models are:
${props.everyModels.map((m) => `- ${m.name}`).join("\n")}
Fix: Use one of the available model names above, or set to null
if this type does not map to a single database table (e.g.,
computed/aggregated types, types composed purely by business logic,
or embedded JSON structures).
`,
});
};
AutoBeInterfaceSchemaProgrammer.fixApplication = (props) => {
const func = props.application.functions[0];
fixDatabaseSchema({
$defs: func.parameters.$defs,
parameters: func.parameters,
everyModels: props.everyModels,
});
};
const fixDatabaseSchema = (props) => {
utils_2.LlmTypeChecker.visit({
$defs: props.$defs,
schema: props.parameters,
closure: (next) => {
if (utils_2.LlmTypeChecker.isObject(next) === false)
return;
const member = next.properties.databaseSchema;
if (member === undefined || utils_2.LlmTypeChecker.isAnyOf(member) === false)
return;
const value = member.anyOf.find((x) => utils_2.LlmTypeChecker.isString(x));
if (value === undefined)
return;
},
});
};
})(AutoBeInterfaceSchemaProgrammer || (exports.AutoBeInterfaceSchemaProgrammer = AutoBeInterfaceSchemaProgrammer = {}));
//# sourceMappingURL=AutoBeInterfaceSchemaProgrammer.js.map