Technology News

CSS Database Queries? Sure We Can!

Kinda silly sounding, isn’t it? CSS database queries. But, hey, CSS is capable of talking to other languages in the sense that it can set the values of things that they can read. Plus, CSS can request other files, and I suppose a server could respond to that request with something it requested from a database.

But I’m getting ahead of myself. The idea of CSS database queries was a joke tweet going around the other day about recruiters looking for a developer who can connect to a database with CSS. Lee Meichin wrote “Yes, I can connect to a DB in CSS” as an equally funny retort.

Screenshot of a tweet sarcastically looking for someone who can do CSS database queries.

What’s the trick behind CSS database queries?

It’s nicely elaborate:

  1. Use a hand-modified-to-ESM version of SQL.js, which is SQLite in JavaScript.
  2. Get a database ready that SQL.js can query.
  3. Build a Houdini PaintWorklet that executes queries in JavaScript and paints the results back to the screen in that <canvas>-y way that PaintWorklets do.
  4. Pass the query you want to run into the worklet by way of a CSS custom property.

So, the usage is like this in the end:

<script> CSS.paintWorklet.addModule('./cssdb.js')
</script>
<style> main { --sql-query: SELECT name FROM test; background: paint(sql-db); }
</style>

Which, you gotta admit, is connecting and querying a database in CSS.

This reminds me that Simon Willison did this last year with a totally different approach. His concept was that you have RESTful endpoints, like /api/roadside_attractions, that return JSON data. But then as an alternative endpoint, you could make that /api/roadside_attractions.css which would return a valid CSS file with all the data as CSS custom properties.

So, instead it looks like this:

<link rel="stylesheet" href="/api/roadside_attractions.css"> <style> .attraction-name:after { content: var(--name); } .attraction-address:after { content: var(--address); }
</style> <p class="attraction-name">Attraction name: </p>
<p class="attraction-address">Address: </p>

Which, again, is essentially connecting to a database in CSS (with HTML required, though). You can literally see it work.

Apple’s new Focus tool is painfully distracting
Mark Zuckerberg wants to build a voice assistant that blows Alexa and Siri away

Related Articles

Scam Alert: You’re Next!

Exploring the Potential of the Metaverse: A New World Awaits
“I’m not important, why would anyone target me?” The internet is a war zone filled with people actively trying to scrape all of your data and attack you with scams.…

How to Set Up Your WFH Space

how-to-set-up-your-wfh-space
The pandemic changed the way we work. Office doors closed and workers shifted to working from home. The truth is, even when the pandemic is over, many people will continue…

One Year Later: Reflecting on Building Resilience by Design

Exploring the Potential of the Metaverse: A New World Awaits
CrowdStrike’s Falcon platform is a cloud-native, AI-powered platform that protects customers through a combination of on-device prevention engines and real-time intelligence from the CrowdStrike Security Cloud. Our detection capabilities are…

My petite-vue review

Exploring the Potential of the Metaverse: A New World Awaits
Road testing petite-vue with a simple category filter July 21, 2021 petite-vue is a new cut of the Vue project specifically built with progressive enhancement in mind. At 5kb, petite-vue…