Created
December 18, 2025 12:47
-
-
Save AshKyd/9f8bed7f8b6a574333feee1922904f18 to your computer and use it in GitHub Desktop.
Print a markdown schema for a sqlite database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sqlite from 'node:sqlite'; | |
| const DB_PATH = "/home/ash/KoboReader.sqlite"; | |
| function printSchema() { | |
| try { | |
| const db = new sqlite.DatabaseSync(DB_PATH); | |
| // Query for all tables and their SQL creation statements | |
| const schema = db.prepare(` | |
| SELECT name, sql | |
| FROM sqlite_master | |
| WHERE type='table' | |
| AND name NOT LIKE 'sqlite_%' | |
| ORDER BY name ASC | |
| `).all(); | |
| console.log(`# Database Schema forKoboReader.sqlite\n\n`); | |
| schema.forEach(table => { | |
| console.log(`## ${table.name}`); | |
| const formattedSql = table.sql.replace('(','(\n ').replace(/\s+\n/,'\n').replace(/,/g,',\n ').replace(/\n\s+/g,'\n ') | |
| console.log(` | |
| ${'```'}sql | |
| ${formattedSql} | |
| ${'```'}`); | |
| console.log('-'.repeat(40)); | |
| }); | |
| } catch (err) { | |
| if (err.code === 'ENOENT') { | |
| console.error("Error: Database file not found at path:", DB_PATH); | |
| } else { | |
| console.error("Error reading schema:", err.message); | |
| } | |
| } | |
| } | |
| printSchema(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment