sql-export - create .md / .json files from database content

yaroslaff

Yaroslav Polyakov

Posted on November 25, 2022

sql-export - create .md / .json files from database content

Hugo (and other SSG, such as Gatsby, Next.js, and others) are great tools to generate static and very fast websites (which are highly rated by google and comfortable for users). But if you have source data in SQL database, you need to generate thousands of markdown files (e.g. one file for each item you are selling).

sql-export export database data into all-in-one JSON file or into many files (one file per SQL result row) in JSON, Markdown with YAML frontmatter or custom format (using text/template, same syntax as you use is HTML layouts in Hugo)

Very simple:

# Export query results to JSON file
./sql-export -q 'SELECT title, price FROM libro LIMIT 2' > /tmp/books.json

# Export to many JSON files
./sql-export -q 'SELECT id, title, price FROM libro' -f json -o '/tmp/libro/{{.id}}.json'
cat /tmp/libro/123.json 
{
    "id": 123,
    "price": 45,
    "title": "ALLGEMEINE GESCHICHTE DER HANDFEUERWAFFEN. Eine Übersicht ihrer Entwickelung. Mit 123 Abbildungen und 4 Ubersichtstafeln. - Günther Reinhold. - Reprint Verlag, - 2001"
}

# Export to markdown files with YAML frontmatter
./sql-export -q 'SELECT id, title, price FROM libro' -f md -o '/tmp/libro/{{.id}}.md'
cat /tmp/libro/123.md 
---
id: 123
price: 45
title: "ALLGEMEINE GESCHICHTE DER HANDFEUERWAFFEN. Eine Übersicht ihrer Entwickelung."
    Mit 123 Abbildungen und 4 Ubersichtstafeln. - Günther Reinhold. - Reprint Verlag,
    - 2001
---

# Export to any custom template
cat out-template.html 
id: {{.id}}
title: {{.title}}
price: {{.price}}

./sql-export -q 'SELECT id, title, price FROM libro' -o '/tmp/libro/{{.id}}.txt' --tpl out-template.html 

cat /tmp/libro/123.txt 
id: 123
title: ALLGEMEINE GESCHICHTE DER HANDFEUERWAFFEN. Eine Übersicht ihrer Entwickelung. Mit 123 Abbildungen und 4 Ubersichtstafeln. - Günther Reinhold. - Reprint Verlag, - 2001
price: 45
Enter fullscreen mode Exit fullscreen mode

Full documentation and git repo: https://github.com/yaroslaff/sql-export

💖 💪 🙅 🚩
yaroslaff
Yaroslav Polyakov

Posted on November 25, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related