Published
Edited
Feb 14, 2022
19 stars
Insert cell
Insert cell
Insert cell
mermaid`${td_mermaid_text}`
Insert cell
html`<pre>${td_mermaid_text}</pre>`
Insert cell
td_mermaid_text = `
graph TD;
${fks.map((r) => `${r.table_from}-->${r.table_to};`).join("\n ")}
`
Insert cell
md`## Now try an entity relationship diagram

https://mermaid-js.github.io/mermaid/#/entityRelationshipDiagram`
Insert cell
mermaid`
%%{config: { 'fontFamily': 'Menlo', 'fontSize': 18, 'fontWeight': 400} }%%

${er_mermaid_text}`
Insert cell
html`<pre>${er_mermaid_text}</pre>`
Insert cell
er_mermaid_text = `
erDiagram
${tables
.map(
(table) =>
`${fks
.filter((fk) => fk.table_from == table)
.map(
(fk) => ` ${fk.table_from} ||--o{ ${fk.table_to} : ${fk.column_from}`
)
.join("\n")}\n ${table} {\n ${columns
.filter((o) => o.table == table)
.map((o) => `column ${o.column}`)
.join("\n ")}\n }`
)
.join(" \n")}
`
Insert cell
mermaidJs = require("mermaid@8.14.0/dist/mermaid.min.js")
Insert cell
params = new URL(location).searchParams
Insert cell
initial_url = params.get('url') || 'https://github-to-sqlite.dogsheep.net/github'
Insert cell
tables = [
...new Set([...fks.map((x) => x.table_from), ...fks.map((x) => x.table_to)])
]
Insert cell
columns = fks.map(x => ({ table: x.table_from, column: x.column_from }))
Insert cell
sql = `
select
sqlite_master.name as table_from,
fk_info.[from] as column_from,
fk_info.[table] as table_to,
fk_info.[to] as column_to
from
sqlite_master
join pragma_foreign_key_list(sqlite_master.name) as fk_info
order by
sqlite_master.name
`;
Insert cell
fks = await fetch(
`${database_url}.json?sql=${encodeURIComponent(sql)}&_shape=array`
).then((r) => r.json())
Insert cell
import {form} from "@mbostock/form-input"
Insert cell
mermaid = function (...values) {
const div = viewof mermaidContainer; //document.getElementById('mermaid');
if (!div) {
throw 'Cannot find div with id "mermaid"';
}

const src = String.raw(...values).trim();
const id = "mmd" + Math.round(Math.random() * 10000);
div.append(html`<div id="${id}" />`);

try {
const result = mermaidJs.render(id, src, undefined);
return svg([result]);
} catch (ex) {
console.error(ex);
throw ex;
}
}
Insert cell
viewof mermaidContainer = html`<div id="mermaid" />`
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more