Skip to content

Commit

Permalink
add types to entity editor
Browse files Browse the repository at this point in the history
rename generateTypes to getColumns
  • Loading branch information
TimurRin committed Nov 21, 2024
1 parent 37b1983 commit 36d0463
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
6 changes: 2 additions & 4 deletions src/config/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from "fs/promises";

import knex from "../config/database.js";
import { Config } from "../types/config.js";
import { generateTypes } from "../utils/db.js";
import { getColumns } from "../utils/db.js";

let config: Config;
// eslint-disable-next-line sonarjs/no-unused-collection
Expand All @@ -30,7 +30,6 @@ export function getCli() {
*
*/
export async function getConfig(): Promise<Config> {
console.log("config", config);
if (config != null) {
return config;
}
Expand Down Expand Up @@ -60,8 +59,7 @@ export async function getConfig(): Promise<Config> {
);

for (const tableName of tablesList) {
console.log(tableName);
const columns = await generateTypes(knex, tableName);
const columns = await getColumns(knex, tableName);
if (!columns) {
continue;
}
Expand Down
6 changes: 2 additions & 4 deletions src/services/getTableEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
GetTableEntitiesParams,
GetTableEntitiesQuery,
} from "../types/openapi.js";
import { generateTypes, tableCheck } from "../utils/db.js";
import { getColumns, tableCheck } from "../utils/db.js";
import { getHtmlPageTemplate } from "../utils/html.js";

/**
Expand All @@ -22,7 +22,7 @@ export async function getTableEntities(
const limit = parseInt(query.limit) || 30;
const offset = parseInt(query.offset) || 0;

const columns = await generateTypes(knex, tableName || "table");
const columns = await getColumns(knex, tableName || "table");

if (!columns) {
throw new Error(`failed to get columns in '${tableName}'`);
Expand All @@ -38,8 +38,6 @@ export async function getTableEntities(
)
.map((value) => value.Field);

console.log("primaryKeys", primaryKeys);

const entities = await knex(tableName)
.select("*")
.limit(limit)
Expand Down
60 changes: 55 additions & 5 deletions src/services/getTableEntity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import knex from "../config/database.js";
import { GetTableEntityParams, GetTableEntityQuery } from "../types/openapi.js";
import { generateTypes, tableCheck } from "../utils/db.js";
import { getColumns, tableCheck } from "../utils/db.js";
import { getHtmlPageTemplate } from "../utils/html.js";

/**
Expand All @@ -16,7 +16,7 @@ export async function getTableEntity(

const tableColumns = await tableCheck(tableName);

const columns = await generateTypes(knex, tableName || "table");
const columns = await getColumns(knex, tableName || "table");

if (!columns) {
throw new Error("no such columns");
Expand Down Expand Up @@ -55,9 +55,59 @@ export async function getTableEntity(
${visibleColumns
.filter((column) => tableColumns.includes(column))
.map((column) => {
return `<div>
<p><label for="${column}">${column}</label></p>
<input type="text" id="${column}" name="${column}" value="${entity[column]}" autocomplete="off" ${primaryKeys.includes(column) ? "disabled" : ""}></div>
const columnType = columns.find((c) => c.Field === column)?.Type;
let inputType = "text";
let inputAttributes = "";
let columnValue = entity[column];
if (columnType == null) {
inputType = "text";
} else if (
columnType.includes("int") ||
columnType.includes("tinyint")
) {
inputType = "number";
} else if (
columnType.includes("datetime") ||
columnType.includes("timestamp")
) {
const date = columnValue as Date;
if (date && date.toISOString) {
columnValue = date.toISOString().slice(0, 16);
}
inputType = "datetime-local";
inputAttributes = `value="${columnValue}"`;
return `
<div>
<p><label for="${column}">${column}</label></p>
<input type="${inputType}" id="${column}" name="${column}" ${inputAttributes} ${primaryKeys.includes(column) ? "disabled" : ""}>
</div>
`;
} else if (columnType.includes("enum")) {
const enumValues = columnType
.replace("enum(", "")
.replace(")", "")
.split(",");
inputAttributes = `value="${entity[column]}"`;
return `
<div>
<p><label for="${column}">${column}</label></p>
<select id="${column}" name="${column}" ${primaryKeys.includes(column) ? "disabled" : ""}>
${enumValues.map((value) => `<option value="${value}" ${entity[column] === value ? "selected" : ""}>${value}</option>`).join("")}
</select>
</div>
`;
} else if (columnType.includes("bool")) {
inputType = "checkbox";
inputAttributes = `value="${entity[column]}" ${entity[column] === 1 ? "checked" : ""}`;
}
return `
<div>
<p><label for="${column}">${column}</label></p>
<input type="${inputType}" id="${column}" name="${column}" value="${columnValue}" autocomplete="off" ${primaryKeys.includes(column) ? "disabled" : ""} ${inputAttributes}>
</div>
`;
})
.join("")}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface GeneratedResultItem {
* @param knex
* @param tableName
*/
export async function generateTypes(
export async function getColumns(
knex: Knex,
tableName: string,
): Promise<GeneratedResultItem[] | undefined> {
Expand Down

0 comments on commit 36d0463

Please sign in to comment.