printf(Hello World!\n)
tech blog

views: 1114

Flat-file CMS operations

02-15-2022

Why use a database when one isn't needed?

I did some reading on mongodb, reviewed my past work with relational databases, and have come to a conclusion: outside of text search for articles with specific words, adding a database to this site to manage blogposts is probably overkill.

For the most part, everything I can do to manage articles and media with a database I can do using PHP's filesystem commands. Without the overhead of a database, it might even end up being faster (at least initially). I don't think I will end up having thousands of articles on this site, so scanning through a directory's contents should be no trouble for my server. And, since I'm not planning on allowing anyone else to edit content, I don't have to worry about write locking or other things that a DB does to keep content safe.

DB vs Filesystem operations

Action DB Filesystem
Add an article INSERT into DB, probably have to make a custom webform for consistency Save a file into the appropriate folder
Delete an article DELETE query, will need unique key for everything Remove a file from the folder
Edit an article UPDATE query with DB tool (or DELETE and INSERT), or write custom webform for ease of use vim. emacs if I'm desperate
Keyword search SELECT query, possibly over multiple tables Parse a CSV file, created to aid with searching and SEO
Pagination SELECT query with LIMIT scandir() and basic math
Assembling html SELECT webpage contents, use PHP to put into template div Probably file_get_contents(), same otherwise
Show recent articles SELECT query, date column filemtime() gets file modification time, though I might need to name files carefully to preserve orig publication date (or put in CSV)
Create menu/index pages SELECT query scandir() for file contents, read metadata from CSV if needed
Comments Add and manage comments table Maybe add a file or subdirectory for article comments, but I haven't decided if I want to allow comments on this site at all

Backup Plan

Rolling a CMS without a database should be pretty simple. If nothing else, this should be an interesting exercise. However, if I end up missing DB features, it will be pretty easy for me to install PostgreSQL or SQLite and write a quick script to import the content of all of my existing articles.

I might end up using a DB anyway for in-house site analytics, but for now, I must not lose sight of the needs of this project. I am a software engineer; I like to use new things and can think of many clever ways to approach a simple job. However, one must never add complications to a program for their own sake. Maintaining this site would probably be easier with a set of CSV files than standing up a set of tables.

Regardless of how much I actually like writing SQL queries.

(Yes, I'm strange).