Skip to content
Draft
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
2 changes: 1 addition & 1 deletion packages/frontend/src/stdlib/analyses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export function renderSQL(
help,
component: (props) => <SQLSchemaAnalysis title={name} render={render} {...props} />,
initialContent: () => ({
backend: SQLBackend.MySQL,
backend: SQLBackend.PostgresSQL,
filename: "schema.sql",
}),
};
Expand Down
35 changes: 34 additions & 1 deletion packages/frontend/src/stdlib/analyses/sql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import download from "js-file-download";
import CircleHelp from "lucide-solid/icons/circle-help";
import Copy from "lucide-solid/icons/copy";
import Download from "lucide-solid/icons/download";
import { For, Match, Show, Switch } from "solid-js";
import { useContext, For, Match, Show, Switch } from "solid-js";
import { createMemo, createEffect } from "solid-js";
import invariant from "tiny-invariant";

import { BlockTitle, ErrorAlert, IconButton } from "catcolab-ui-components";
import { elaborateModel, DblModelMap } from "catlog-wasm";
import type { ModelAnalysisProps } from "../../analysis";
import { type ModelLibrary, ModelLibraryContext } from "../../model";
import { getConfig } from "../../user/documents";
import * as SQL from "./sql_types.ts";

const copyToClipboard = (text: string) => navigator.clipboard.writeText(text);
Expand Down Expand Up @@ -67,13 +72,41 @@ export default function SQLSchemaAnalysis(
title: string;
},
) {

const configDoc = getConfig();
const models = useContext(ModelLibraryContext);
const otherModel = models?.useElaboratedModel(() => configDoc[0].refId);

const sqlBackend = createMemo(() => {
const entry = otherModel();
if (entry?.validatedModel.tag === "Valid") {
const gen = entry.validatedModel.model
.presentation()
.obGenerators.find((ob) => ob.label.includes("SQLBackend"));
if (!gen) return undefined;
const mor = entry.validatedModel.model
.presentation()
.morGenerators.find((m) => m.dom.content === gen.id);
return mor?.label?.join(".") as SQL.SQLBackend | undefined;
}
});

const sql_script = () => {
const model = props.liveModel.elaboratedModel();
if (model) {
return props.render(model, props.content.backend);
}
};

createEffect(() => {
const configBackend = sqlBackend();
if (configBackend) {
props.changeContent((content) => {
content.backend = configBackend as SQL.SQLBackend;
});
}
});

const BackendConfig = () => (
<div>
<span>Backend: </span>
Expand Down
12 changes: 12 additions & 0 deletions packages/frontend/src/user/documents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,15 @@ function DeleteButton(props: { doc: DocInfo & { refId: string } }) {
</div>
);
}

export function getConfig() {
const userState = useUserState();
const config = createMemo(() =>
filterDocuments(userState.documents, {
query: "config",
deleted: false,
}),
);

return config();
}
Loading