Skip to content

Instantly share code, notes, and snippets.

@loicknuchel
Last active July 13, 2022 20:50
Show Gist options
  • Save loicknuchel/c3bf157148978a18bdf7fa6db6456cf4 to your computer and use it in GitHub Desktop.
Save loicknuchel/c3bf157148978a18bdf7fa6db6456cf4 to your computer and use it in GitHub Desktop.
Generate AML from PostgreSQL documentation
// to execute in the console of https://www.postgresql.org/docs/current/catalog-pg-class.html
// and paste the copied result into https://azimutt.app ;)
function generateTableAml() {
const url = window.location.href
const table_schema = 'pg_catalog'
const table_name = document.querySelector('h2.title .structname').textContent
const table_description = document.querySelector('p').textContent
const column_rows = Array.from(document.querySelector('.table-contents').querySelectorAll('tbody tr'))
const columns = column_rows.map(r => {
return {
name: r.querySelector('.structfield').textContent,
type: r.querySelector('.type').textContent,
description: r.querySelectorAll('p')[1].textContent
}
})
const relations = column_rows.flatMap(r => {
const rel_table = r.querySelector('.column_definition .structname')?.textContent
if (rel_table) {
return [{
from: r.querySelector('.structfield').textContent,
to_table: rel_table,
to_column: r.querySelectorAll('.column_definition .structfield')[1].textContent
}]
} else {
return []
}
})
return `${table_schema}.${table_name} | ${table_description}\\n${url}`
+ `${columns.map(c => `\n ${c.name} ${c.type} | ${c.description}`).join('')}`
+ `${relations.map(r => `\nfk ${table_schema}.${table_name}.${r.from} -> ${table_schema}.${r.to_table}.${r.to_column}`).join('')}`
}
copy(generateTableAml())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment