Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cli/recipes/endpoint/template/schemas/post.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ const newPost{{entityName | toPascalCase}}Schema = yup.object().shape({
mixed().nullable().test('is-blob', 'Binary data', value => {return value instanceof Buffer || value instanceof Uint8Array || false;}),
{% elsif column.dataType == 'tinyblob'%}
mixed().nullable().test('is-blob', 'Binary data', value => {return value instanceof Buffer || value instanceof Uint8Array || false;}),
{% elsif column.dataType == 'enum' %}
string().oneOf([{{ column.columnnType | extractEnumValues }}], "The {{column.column | toCamelCase}} field must be one of: {{ column.columnnType | extractEnumValues }}").nullable().label('{{column.column | toCamelCase}}').typeError("The {{column.column | toCamelCase}} field must be one of: {{ column.columnnType | extractEnumValues }}"),
{% endif %}
{% else %}
{% if column.columnKey == 'MUL' %}
Expand Down Expand Up @@ -161,6 +163,8 @@ const newPost{{entityName | toPascalCase}}Schema = yup.object().shape({
mixed().required().test('is-blob', 'Binary data required', value => {return value instanceof Buffer || value instanceof Uint8Array || false;}),
{% elsif column.dataType == 'tinyblob'%}
mixed().required().test('is-blob', 'Binary data required', value => {return value instanceof Buffer || value instanceof Uint8Array || false;}),
{% elsif column.dataType == 'enum' %}
string().oneOf([{{ column.columnnType | extractEnumValues }}], "The {{column.column | toCamelCase}} field must be one of: {{ column.columnnType | extractEnumValues }}").required('The {{column.column | toCamelCase}} field is required').label('{{column.column | toCamelCase}}').typeError("The {{column.column | toCamelCase}} field must be one of: {{ column.columnnType | extractEnumValues }}"),
{% endif %}
{% endif %}
{% endif %}
Expand Down
4 changes: 4 additions & 0 deletions cli/recipes/endpoint/template/schemas/put.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ const newPut{{entityName | toPascalCase}}Schema = yup.object().shape({
mixed().nullable().test('is-blob', 'Binary data', value => {return value instanceof Buffer || value instanceof Uint8Array || false;}),
{% elsif column.dataType == 'tinyblob'%}
mixed().nullable().test('is-blob', 'Binary data', value => {return value instanceof Buffer || value instanceof Uint8Array || false;}),
{% elsif column.dataType == 'enum' %}
string().oneOf([{{ column.columnnType | extractEnumValues }}], "The {{column.column | toCamelCase}} field must be one of: {{ column.columnnType | extractEnumValues }}").nullable().label('{{column.column | toCamelCase}}').typeError("The {{column.column | toCamelCase}} field must be one of: {{ column.columnnType | extractEnumValues }}"),
{% endif %}
{% else %}
{% if column.columnKey == 'MUL' %}
Expand Down Expand Up @@ -188,6 +190,8 @@ const newPut{{entityName | toPascalCase}}Schema = yup.object().shape({
mixed().test('is-blob', 'Binary data required', value => {return value instanceof Buffer || value instanceof Uint8Array || false;}),
{% elsif column.dataType == 'tinyblob'%}
mixed().test('is-blob', 'Binary data required', value => {return value instanceof Buffer || value instanceof Uint8Array || false;}),
{% elsif column.dataType == 'enum' %}
string().oneOf([{{ column.columnnType | extractEnumValues }}], "The {{column.column | toCamelCase}} field must be one of: {{ column.columnnType | extractEnumValues }}").required('The {{column.column | toCamelCase}} field is required').label('{{column.column | toCamelCase}}').typeError("The {{column.column | toCamelCase}} field must be one of: {{ column.columnnType | extractEnumValues }}"),
{% endif %}
{% endif %}
{% endif %}
Expand Down
28 changes: 21 additions & 7 deletions cli/utils/templateRenderer/filters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const enrichEngine = (engine) => {
engine.registerFilter("toConstantCase", (v) =>
upperCase(v).replace(/ /g, "_")
);
engine.registerFilter("extractEnumValues", (input) => {
const match = input.match(/enum\((.+)\)/);
return match ? match[1] : "";
});
engine.registerFilter("toTypeScriptType", (v) => {
switch (v) {
case "int":
Expand Down Expand Up @@ -49,11 +53,22 @@ const enrichEngine = (engine) => {
});
engine.registerFilter("schemaImports", (v) => {
let imports = [];
const filteredSchema = v.filter((c) => (c.columnKey === 'MUL' || c.columnKey === 'PRI') && c.dataType === 'int');
const uniqueTargetTables = [...new Set(filteredSchema.map((c) => c.foreignKeyTo.targetTable))];
Promise.all(uniqueTargetTables.map(async (table) => {
imports.push(`const select${toPascalCase(table)}ById = require('./queries/select${toPascalCase(table)}ById');`);
}));
const filteredSchema = v.filter(
(c) =>
(c.columnKey === "MUL" || c.columnKey === "PRI") && c.dataType === "int"
);
const uniqueTargetTables = [
...new Set(filteredSchema.map((c) => c.foreignKeyTo.targetTable)),
];
Promise.all(
uniqueTargetTables.map(async (table) => {
imports.push(
`const select${toPascalCase(
table
)}ById = require('./queries/select${toPascalCase(table)}ById');`
);
})
);

return imports.join("\n");
});
Expand Down Expand Up @@ -167,7 +182,7 @@ const enrichEngine = (engine) => {
}

let fieldList = uniq(fields);
const isUsingDotNotation = fields.find((field) => field.includes("."))
const isUsingDotNotation = fields.find((field) => field.includes("."));

if (isUsingDotNotation) {
return `${fieldList
Expand Down Expand Up @@ -234,7 +249,6 @@ const enrichEngine = (engine) => {
});
return joins.join(", ");
});

};

module.exports = enrichEngine;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xest",
"version": "0.0.0-alpha.23",
"version": "0.0.0-alpha.24",
"description": "Xest CLI for managing projects",
"main": "index.js",
"bin": {
Expand Down