In the past, I've relied on frontmatter and MDX files to create the dataset for
my occupations and timeline. Since moving away from contentLayer
this setup
has made less sense and since I'm not displaying any MDX content for the
occupations it's felt necessary to change this data layer.
I got this idea the other day to use SQLite as a read-only database in my Next.js. I've had issues with this in the past since the edge-nature of Vercel deployments makes creating stateful databases difficult. However, I'm not looking to write to the database, only read from it. Maybe this will work.
Database Setup
Borrowing heavily from the T3 stack, I've setup a new
service for the db
and using drizzle +
@libsql/client to interact
with a SQLite database.
I've build a few other application this way and enjoy a setting up a database layer like this.
Probably the most important part of the setup is the /db/index.ts
file. This
is where the database is setup and the connection is established.
The part that makes this work with Vercel is the dbPath
variable. This is
where the database file is stored. I've found that using process.cwd()
is the
best way to get the root of the project. This way, the database file is stored
in the root of the project and can be accessed by the Next.js serverless
functions.
Note
While this works as expected when building out the application in Vercel for statically generated pages, I've been running into issues with the serverless functions. I'm not sure if this is a limitation of Vercel or if I'm doing something wrong. I'll need to investigate this further.
Schema Definition
The schema for the occupations is pretty simple. I borrowed from the original type definitions I had in the MDX files, but have added some relationships to make data normalization easier.
Building the schema in drizzle is nice and simple, but the important part is how the many-to-many relationships are setup.
The occupation_to_skill
table is the many-to-many relationship between
occupation
and skill
, and the occupationToSkillRelations
is the
relationship that drizzle uses to join the two tables. This is a very powerful
and makes creating the data layer very easy: