Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add command to generate Markdown documentation from database schema #408

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
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
62 changes: 62 additions & 0 deletions frontend/packages/cli/src/cli/export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import path from 'node:path'
import { dbStructureSchema } from '@liam-hq/db-structure'
import { Command } from 'commander'
import fs from 'fs/promises'
import * as v from 'valibot'

const generateMdCommand = new Command('generate-md')
const schemaJsonPath = path.join(process.cwd(), 'dist', 'schema.json')

generateMdCommand.action(async () => {
const fileContent = await fs.readFile(schemaJsonPath, 'utf-8')
const json = JSON.parse(fileContent)
const dbStructure = v.parse(dbStructureSchema, json)
const markdownLines: string[] = []

markdownLines.push('# Database Structure')
markdownLines.push('## Tables')

for (const [tableName, table] of Object.entries(dbStructure.tables)) {
markdownLines.push(`### ${tableName}`)
if (table.comment !== null) markdownLines.push(table.comment)
markdownLines.push(`#### Columns`)
for (const [columnName, column] of Object.entries(table.columns)) {
markdownLines.push(`- ${columnName}: ${column.type}`)
if (column.default !== null && column.default !== '')
markdownLines.push(` - Default: ${column.default}`)
if (column.check !== null && column.check !== '')
markdownLines.push(` - Check: ${column.check}`)
if (column.primary) markdownLines.push(` - Primary Key`)
if (column.notNull) markdownLines.push(` - Not Null`)
if (column.comment !== null)
markdownLines.push(` - Comment: ${column.comment}`)
}
markdownLines.push(`#### Indices`)
for (const [indexName, index] of Object.entries(table.indices)) {
markdownLines.push(`- ${indexName}`)
if (index.unique) markdownLines.push(` - Unique`)
markdownLines.push(` - Columns: ${index.columns.join(', ')}`)
}
}

markdownLines.push(`## Relationships`)
for (const [relationshipName, relationship] of Object.entries(
dbStructure.relationships,
)) {
markdownLines.push(`- ${relationshipName}`)
markdownLines.push(` - Primary: ${relationship.primaryTableName}.${relationship.primaryColumnName}`)
markdownLines.push(` - Foreign: ${relationship.foreignTableName}.${relationship.foreignColumnName}`)
markdownLines.push(
` - Update Constraint: ${relationship.updateConstraint}`,
)
markdownLines.push(
` - Delete Constraint: ${relationship.deleteConstraint}`,
)
}

const markdownContent = markdownLines.join('\n')
const outputPath = path.join(process.cwd(), 'dist', 'schema.md')
await fs.writeFile(outputPath, markdownContent, 'utf-8')
})

export { generateMdCommand }
3 changes: 3 additions & 0 deletions frontend/packages/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createRequire } from 'node:module'
import { Command } from 'commander'
import { erdCommand } from './erdCommand/index.js'
import { generateMdCommand } from './export.js'
import { initCommand } from './initCommand/index.js'

const program = new Command()
Expand All @@ -11,4 +12,6 @@ const { version } = require('../../package.json')
program.name('liam').description('CLI tool for Liam').version(version)
program.addCommand(erdCommand)
program.addCommand(initCommand)
program.addCommand(generateMdCommand)

export { program }
Loading