sindri/source/outputs/typescript.ts

174 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-07-13 21:57:56 +02:00
namespace _sindri.outputs.typescript
{
/**
*/
function render_entity_type(
domain
) : string
{
return lib_plankton.string.coin(
"type type_{{name}} = {\n{{fields}}\n};\n",
{
"name": domain.name,
"fields": (
domain.data_fields
.map(
(data_field) => lib_plankton.string.coin(
"\t{{name}} : {{type}};{{macro_comment}}",
{
"name": data_field.name,
"type": lib_plankton.string.coin(
(data_field.nullable ? "(null | {{core}})" : "{{core}}"),
{
"core": {
"boolean": "boolean",
"integer": "number",
"float": "number",
"string_short": "string",
"string_medium": "string",
"string_long": "string",
}[data_field["type"]],
}
),
"macro_comment": (
(data_field.description !== null)
? lib_plankton.string.coin(
" // {{comment}}",
{
"comment": data_field.description,
}
)
: ""
),
}
)
)
// .map(x => ("\t" + x))
.join("\n")
),
}
);
}
/**
*/
function render_entity_collection(
domain
) : string
{
return lib_plankton.string.coin(
"let collection_{{name}} : {{collection_type}} = {{collection_value}};\n",
{
"name": domain.name,
"collection_type": (
(domain.key_field === null)
? lib_plankton.string.coin(
"Array<type_{{name}}>",
{
"name": domain.name,
}
)
: lib_plankton.string.coin(
"Record<number, type_{{name}}>",
{
"name": domain.name,
}
)
),
"collection_value": (
(domain.key_field === null)
? "[]"
: "{}"
),
}
);
}
/**
*/
function render_entity(
domain
) : string
{
return (
render_entity_type(domain)
+
render_entity_collection(domain)
);
}
/**
*/
function render_functions(
domain
) : string
{
return (
[
// list
"function {{name}}_list() : Promise<type_{{name}}> {return sql_query_get(\"SELECT * FROM {{name}};\");}",
// read
"",
// create
"",
// update
"",
// delete
"",
]
.join("\n\n")
);
}
/**
*/
function render_api_actions(
domain
) : string
{
return (
[
// list
"",
// read
"",
// create
"",
// update
"",
// delete
"",
]
.join("\n\n")
);
}
/**
*/
export function render(
input_data
) : string
{
return (
input_data["domains"]
.map(
2023-07-13 21:57:56 +02:00
(domain) => render_entity(domain)
)
.map(x => (x + "\n"))
.join("\n")
);
2023-07-13 21:57:56 +02:00
}
}
const output_typescript : type_output = {
"render": _sindri.outputs.typescript.render,
};