Public
Edited
Sep 20, 2024
Importers
2 stars
Also listed in…
Databases
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
visualizeSqlite(db, { showAttributes, showFKs, returnStr: false })
Insert cell
db = SQLiteDatabaseClient.open(
file?.url() || FileAttachment("chinook.db").url()
)
Insert cell
db.describeTables()
Insert cell
Insert cell
Insert cell
async function visualizeSqlite(
db,
{ showAttributes = true, showFKs = true, returnStr = false } = {}
) {
const tables = (await db.describeTables()).map((d) => d.name);

const fks = (
await Promise.all(
tables.map(async (tableName) => {
const fk = await db.query(
`SELECT * FROM pragma_foreign_key_list('${tableName}')`
);

return fk.map((fkResult) => ({ tableFrom: tableName, ...fkResult }));
})
)
).flat();

const attribNames = await Promise.all(
tables.map(async (tableName) => {
const columns = await db.describeColumns({ table: tableName });

return { tableName, columns };
// return columns.map((c) => ({ tableName, ...c }));
})
);

// ${fks
// .map(({ tableFrom, table, from }) => `${tableFrom} --> ${table} : ${from}`)
// .join("\n")}
// ${tables.join("\n")}
// ${attribNames
// .map(({ tableName, name, type }) => `${tableName} : ${name} `)
// .join("\n")}

const str = `erDiagram ${
showFKs
? "\n " +
fks
.map(
({ tableFrom, table, from }) =>
`${tableFrom} }o--|| ${table} : ${from}`
)
.join("\n ")
: ""
}
${attribNames
.map(
({ tableName, columns }) => `${tableName} {
${
showAttributes
? "\n " +
columns
.map((c) => `${c.databaseType.split("(")[0]} ${c.name}`)
.join("\n ")
: ""
}
}`
)
.join("\n ")}
`;

return returnStr ? str : mermaid`${str}`;
}
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