Compare commits

..

6 Commits

Author SHA1 Message Date
dswbx d2c75b1605 Merge pull request #26 from bknd-io/fix/mm-sync
fix: only sync config table once certain it has to happen
2024-12-11 18:29:22 +01:00
dswbx a474e3fe3a Merge pull request #28 from bknd-io/fix/admin-ui-fixes
Admin UI fixes
2024-12-11 18:29:05 +01:00
dswbx d81b3acb94 mm: disable console as it might error trying to fetch config 2024-12-11 18:25:31 +01:00
dswbx 2ca66f4fc9 admin ui: fix "enter" on create new modal (inside fields) 2024-12-11 17:46:15 +01:00
dswbx a8bbb6e760 admin ui: fix data erd layout 2024-12-11 17:30:33 +01:00
dswbx 2395d7fe97 fix: only sync config table once certain it has to happen 2024-12-11 15:51:00 +01:00
7 changed files with 84 additions and 88 deletions
+11
View File
@@ -5,10 +5,21 @@ const _oldConsoles = {
error: console.error error: console.error
}; };
export async function withDisabledConsole<R>(
fn: () => Promise<R>,
severities: ConsoleSeverity[] = ["log"]
): Promise<R> {
const enable = disableConsoleLog(severities);
const result = await fn();
enable();
return result;
}
export function disableConsoleLog(severities: ConsoleSeverity[] = ["log"]) { export function disableConsoleLog(severities: ConsoleSeverity[] = ["log"]) {
severities.forEach((severity) => { severities.forEach((severity) => {
console[severity] = () => null; console[severity] = () => null;
}); });
return enableConsoleLog;
} }
export function enableConsoleLog() { export function enableConsoleLog() {
+4 -1
View File
@@ -201,7 +201,10 @@ export class Repository<DB = any, TB extends keyof DB = any> implements EmitsEve
} }
}; };
} catch (e) { } catch (e) {
console.error("many error", e, compiled); if (e instanceof Error) {
console.error("[ERROR] Repository.performQuery", e.message);
}
throw e; throw e;
} }
} }
+38 -24
View File
@@ -7,9 +7,11 @@ import {
type Static, type Static,
StringEnum, StringEnum,
Type, Type,
mark,
objectEach, objectEach,
stripMark, stripMark,
transformObject transformObject,
withDisabledConsole
} from "core/utils"; } from "core/utils";
import { import {
type Connection, type Connection,
@@ -23,7 +25,7 @@ import {
} from "data"; } from "data";
import { TransformPersistFailedException } from "data/errors"; import { TransformPersistFailedException } from "data/errors";
import { Hono } from "hono"; import { Hono } from "hono";
import { type Kysely, sql } from "kysely"; import type { Kysely } from "kysely";
import { mergeWith } from "lodash-es"; import { mergeWith } from "lodash-es";
import { CURRENT_VERSION, TABLE_NAME, migrate } from "modules/migrations"; import { CURRENT_VERSION, TABLE_NAME, migrate } from "modules/migrations";
import { AppServer } from "modules/server/AppServer"; import { AppServer } from "modules/server/AppServer";
@@ -73,6 +75,7 @@ export type ModuleManagerOptions = {
) => Promise<void>; ) => Promise<void>;
// base path for the hono instance // base path for the hono instance
basePath?: string; basePath?: string;
trustFetched?: boolean;
}; };
type ConfigTable<Json = ModuleConfigs> = { type ConfigTable<Json = ModuleConfigs> = {
@@ -187,7 +190,10 @@ export class ModuleManager {
} }
async syncConfigTable() { async syncConfigTable() {
return await this.__em.schema().sync({ force: true }); this.logger.context("sync").log("start");
const result = await this.__em.schema().sync({ force: true });
this.logger.log("done").clear();
return result;
} }
private rebuildServer() { private rebuildServer() {
@@ -226,19 +232,23 @@ export class ModuleManager {
private async fetch(): Promise<ConfigTable> { private async fetch(): Promise<ConfigTable> {
this.logger.context("fetch").log("fetching"); this.logger.context("fetch").log("fetching");
const startTime = performance.now(); // disabling console log, because the table might not exist yet
const { data: result } = await this.repo().findOne( return await withDisabledConsole(async () => {
{ type: "config" }, const startTime = performance.now();
{ const { data: result } = await this.repo().findOne(
sort: { by: "version", dir: "desc" } { type: "config" },
} {
); sort: { by: "version", dir: "desc" }
if (!result) { }
throw BkndError.with("no config"); );
}
this.logger.log("took", performance.now() - startTime, "ms", result).clear(); if (!result) {
return result as ConfigTable; throw BkndError.with("no config");
}
this.logger.log("took", performance.now() - startTime, "ms", result.version).clear();
return result as ConfigTable;
}, ["log", "error", "warn"]);
} }
async save() { async save() {
@@ -359,9 +369,6 @@ export class ModuleManager {
configs = _configs; configs = _configs;
this.setConfigs(configs); this.setConfigs(configs);
/* objectEach(configs, (config, key) => {
this.get(key as any).setConfig(config);
}); */
this._version = version; this._version = version;
this.logger.log("migrated to", version); this.logger.log("migrated to", version);
@@ -395,10 +402,11 @@ export class ModuleManager {
return; return;
} }
this.logger.log("building");
const ctx = this.ctx(true); const ctx = this.ctx(true);
for (const key in this.modules) { for (const key in this.modules) {
this.logger.log(`building "${key}"`);
await this.modules[key].setContext(ctx).build(); await this.modules[key].setContext(ctx).build();
this.logger.log("built", key);
} }
this._built = true; this._built = true;
@@ -409,11 +417,6 @@ export class ModuleManager {
this.logger.context("build").log("version", this.version()); this.logger.context("build").log("version", this.version());
this.logger.log("booted with", this._booted_with); this.logger.log("booted with", this._booted_with);
// @todo: check this, because you could start without an initial config
if (this.version() !== CURRENT_VERSION) {
await this.syncConfigTable();
}
// if no config provided, try fetch from db // if no config provided, try fetch from db
if (this.version() === 0) { if (this.version() === 0) {
this.logger.context("no version").log("version is 0"); this.logger.context("no version").log("version is 0");
@@ -422,6 +425,16 @@ export class ModuleManager {
// set version and config from fetched // set version and config from fetched
this._version = result.version; this._version = result.version;
if (this.version() !== CURRENT_VERSION) {
await this.syncConfigTable();
}
if (this.options?.trustFetched === true) {
this.logger.log("trusting fetched config (mark)");
mark(result.json);
}
this.setConfigs(result.json); this.setConfigs(result.json);
} catch (e: any) { } catch (e: any) {
this.logger.clear(); // fetch couldn't clear this.logger.clear(); // fetch couldn't clear
@@ -431,6 +444,7 @@ export class ModuleManager {
// we can safely build modules, since config version is up to date // we can safely build modules, since config version is up to date
// it's up to date because we use default configs (no fetch result) // it's up to date because we use default configs (no fetch result)
this._version = CURRENT_VERSION; this._version = CURRENT_VERSION;
await this.syncConfigTable();
await this.buildModules(); await this.buildModules();
await this.save(); await this.save();
@@ -25,12 +25,6 @@ export const layoutWithDagre = ({ nodes, edges, graph }: LayoutProps) => {
const dagreGraph = new Dagre.graphlib.Graph(); const dagreGraph = new Dagre.graphlib.Graph();
dagreGraph.setDefaultEdgeLabel(() => ({})); dagreGraph.setDefaultEdgeLabel(() => ({}));
dagreGraph.setGraph(graph || {}); dagreGraph.setGraph(graph || {});
/*dagreGraph.setGraph({
rankdir: "LR",
align: "UR",
nodesep: NODE_SEP,
ranksep: RANK_SEP
});*/
nodes.forEach((node) => { nodes.forEach((node) => {
dagreGraph.setNode(node.id, { dagreGraph.setNode(node.id, {
@@ -48,7 +42,11 @@ export const layoutWithDagre = ({ nodes, edges, graph }: LayoutProps) => {
return { return {
nodes: nodes.map((node) => { nodes: nodes.map((node) => {
const position = dagreGraph.node(node.id); const position = dagreGraph.node(node.id);
return { ...node, x: position.x, y: position.y }; return {
...node,
x: position.x - (node.width ?? 0) / 2,
y: position.y - (node.height ?? 0) / 2
};
}), }),
edges edges
}; };
@@ -69,15 +69,13 @@ export function DataSchemaCanvas() {
const nodeLayout = layoutWithDagre({ const nodeLayout = layoutWithDagre({
nodes: nodes.map((n) => ({ nodes: nodes.map((n) => ({
id: n.id, id: n.id,
...EntityTableNode.getSize(n) ...EntityTableNode.getSize(n.data)
})), })),
edges, edges,
graph: { graph: {
rankdir: "LR", rankdir: "LR",
//align: "UR", marginx: 50,
ranker: "network-simplex", marginy: 50
nodesep: 350,
ranksep: 50
} }
}); });
@@ -88,12 +86,6 @@ export function DataSchemaCanvas() {
} }
}); });
/*const _edges = edges.map((e) => ({
...e,
source: e.source + `-${e.target}_id`,
target: e.target + "-id"
}));*/
return ( return (
<ReactFlowProvider> <ReactFlowProvider>
<Canvas <Canvas
@@ -1,6 +1,7 @@
import { typeboxResolver } from "@hookform/resolvers/typebox"; import { typeboxResolver } from "@hookform/resolvers/typebox";
import type { Static } from "core/utils";
import { type TAppDataEntityFields, entitiesSchema } from "data/data-schema"; import { type TAppDataEntityFields, entitiesSchema } from "data/data-schema";
import { useRef, useState } from "react"; import { useRef } from "react";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { MantineSelect } from "ui/components/form/hook-form-mantine/MantineSelect"; import { MantineSelect } from "ui/components/form/hook-form-mantine/MantineSelect";
import { useEvent } from "ui/hooks/use-event"; import { useEvent } from "ui/hooks/use-event";
@@ -11,31 +12,33 @@ import {
import { ModalBody, ModalFooter, type TCreateModalSchema, useStepContext } from "./CreateModal"; import { ModalBody, ModalFooter, type TCreateModalSchema, useStepContext } from "./CreateModal";
const schema = entitiesSchema; const schema = entitiesSchema;
type Schema = Static<typeof schema>;
export function StepEntityFields() { export function StepEntityFields() {
const { nextStep, stepBack, state, setState } = useStepContext<TCreateModalSchema>(); const { nextStep, stepBack, state, setState } = useStepContext<TCreateModalSchema>();
const entity = state.entities?.create?.[0]!; const entity = state.entities?.create?.[0]!;
const defaultFields = { id: { type: "primary", name: "id" } } as const; const defaultFields = { id: { type: "primary", name: "id" } } as const;
const ref = useRef<EntityFieldsFormRef>(null); const ref = useRef<EntityFieldsFormRef>(null);
const initial = entity
? entity
: {
fields: defaultFields,
config: {
sort_field: "id",
sort_dir: "asc"
}
};
const { const {
control, control,
formState: { isValid, errors }, formState: { isValid, errors },
getValues, getValues,
handleSubmit, handleSubmit,
watch, watch,
register,
setValue setValue
} = useForm({ } = useForm({
mode: "onTouched", mode: "onTouched",
resolver: typeboxResolver(schema), resolver: typeboxResolver(schema),
defaultValues: { defaultValues: initial as NonNullable<Schema>
...entity,
fields: defaultFields,
config: {
sort_field: "id",
sort_dir: "asc"
}
}
}); });
const values = watch(); const values = watch();
@@ -74,7 +77,11 @@ export function StepEntityFields() {
Add fields to <strong>{entity.name}</strong>: Add fields to <strong>{entity.name}</strong>:
</p> </p>
<div className="flex flex-col gap-1"> <div className="flex flex-col gap-1">
<EntityFieldsForm ref={ref} fields={defaultFields} onChange={updateListener} /> <EntityFieldsForm
ref={ref}
fields={initial.fields as any}
onChange={updateListener}
/>
</div> </div>
</div> </div>
<div className="flex flex-col gap-3"> <div className="flex flex-col gap-3">
@@ -82,7 +89,7 @@ export function StepEntityFields() {
<div className="flex flex-row gap-2"> <div className="flex flex-row gap-2">
<MantineSelect <MantineSelect
label="Field" label="Field"
data={Object.keys(values.fields).filter((name) => name.length > 0)} data={Object.keys(values.fields ?? {}).filter((name) => name.length > 0)}
placeholder="Select field" placeholder="Select field"
name="config.sort_field" name="config.sort_field"
allowDeselect={false} allowDeselect={false}
@@ -102,7 +109,7 @@ export function StepEntityFields() {
<div> <div>
{Object.entries(errors).map(([key, value]) => ( {Object.entries(errors).map(([key, value]) => (
<p key={key}> <p key={key}>
{key}: {value.message} {key}: {(value as any).message}
</p> </p>
))} ))}
</div> </div>
@@ -72,22 +72,16 @@ export const EntityFieldsForm = forwardRef<
name, name,
field field
})); }));
/*const entityFields = entity.fields.map((field) => ({
name: field.name,
field: field.toJSON()
}));*/
const { const {
control, control,
formState: { isValid, errors }, formState: { isValid, errors },
getValues, getValues,
handleSubmit,
watch, watch,
register, register,
setValue, setValue,
setError, setError,
reset, reset
clearErrors
} = useForm({ } = useForm({
mode: "all", mode: "all",
resolver: typeboxResolver(schema), resolver: typeboxResolver(schema),
@@ -114,7 +108,6 @@ export const EntityFieldsForm = forwardRef<
props?.onChange?.(toCleanValues(data)); props?.onChange?.(toCleanValues(data));
}); });
} }
//props?.onChange?.()
}, []); }, []);
useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
@@ -122,25 +115,9 @@ export const EntityFieldsForm = forwardRef<
getValues: () => getValues(), getValues: () => getValues(),
getData: () => { getData: () => {
return toCleanValues(getValues()); return toCleanValues(getValues());
/*return Object.fromEntries(
getValues().fields.map((field) => [field.name, objectCleanEmpty(field.field)])
);*/
}, },
isValid: () => isValid isValid: () => isValid
})); }));
console.log("errors", errors.fields);
/*useEffect(() => {
console.log("change", values);
onSubmit(values);
}, [values]);*/
function onSubmit(data: TFieldsFormSchema) {
console.log("submit", isValid, data, errors);
}
function onSubmitInvalid(a, b) {
console.log("submit invalid", a, b);
}
function handleAppend(_type: keyof typeof fieldsSchemaObject) { function handleAppend(_type: keyof typeof fieldsSchemaObject) {
const newField = { const newField = {
@@ -151,7 +128,6 @@ export const EntityFieldsForm = forwardRef<
config: {} config: {}
} }
}; };
console.log("handleAppend", _type, newField);
append(newField); append(newField);
} }
@@ -165,10 +141,7 @@ export const EntityFieldsForm = forwardRef<
}; };
return ( return (
<> <>
<form <div className="flex flex-col gap-6">
onSubmit={handleSubmit(onSubmit as any, onSubmitInvalid)}
className="flex flex-col gap-6"
>
<div className="flex flex-col gap-3"> <div className="flex flex-col gap-3">
<div className="flex flex-col gap-4"> <div className="flex flex-col gap-4">
{sortable ? ( {sortable ? (
@@ -220,9 +193,7 @@ export const EntityFieldsForm = forwardRef<
</Popover> </Popover>
</div> </div>
</div> </div>
<button type="submit" className="hidden" /> </div>
{/*<Debug watch={watch} errors={errors} />*/}
</form>
</> </>
); );
}); });