I’m taking this way more seriously than I probably should.

Styled Query Component

Like StyledComponents, but for SQL. `${template literal}` your way to database queries!

Server Componentsbetter-sqlite3Typed ParamsRender Modes

User by ID

Fetch a single user and review the query metadata.

Lookup User

Run a single query for user #1.

ReactUserById.tsx
type UserByIdParams = {   id: number };type UserByIdRow = {   id: number;   name: string }; const UserById = sqc.Select<  UserByIdParams,   UserByIdRow>({  from: "users",  columns: ["id", "name"],  where: { id: sqc.param("id") },}); <UserById  await={false}  id={1}  fulfilled={(rows, meta) => /* render output + meta */}  reject={(error) => /* handle error */}/>
Output

Rows

idname
1Sophie Lovelace

Meta

SQLSELECT id, name FROM users WHERE id = :id
Params{"id":1}
Rows1
Duration2.06ms

Create a Client

Connect createClient to better-sqlite3 with a lightweight adapter.

TypeScriptclient.ts
import { createClient } from "@lib";import db from "@lib/db"; const sqc = createClient({  adapter: async (sql, params) => {    const stmt = db.prepare(sql);    const rows = stmt.all(params) as Record<string, unknown>[];    const columns = rows.length > 0 ? Object.keys(rows[0]) : [];    return { rows, columns };  },});

Tagged Template (Await Mode)

Use sqc.sql with sqc.param for dynamic values and render results as a table.

User List

Fetch a range of users with a dynamic limit.

ReactUserList.tsx
type UserListParams = {   id: number;   limit: number };type UserListRow = {   id: number;   name: string }; const UserList = sqc.sql<UserListParams, UserListRow>`  select id, name  from users  where id >= ${sqc.param("id")}  limit ${sqc.param("limit")}`; <UserList await id={1} limit={5} as="table" />
Output

Rows

idname
1Sophie Lovelace
2Larry Thompson
3Donald Turing
4Susan Hopper
5Ryan Turing

Meta

SQL select id, name from users where id >= :id limit :limit
Params{"id":1,"limit":5}
Rows5
Duration1.50ms

Select API (Promise Mode)

Use sqc.Select and custom fulfilled/reject handlers for full control.

Products by Category

Handle results manually and inspect query metadata.

ReactProductsByCategory.tsx
type ProductsByCategoryParams = {   category: string;   limit: number };type ProductsByCategoryRow = {   id: number;   title: string;   price: number;   category: string }; const ProductsByCategory = sqc.Select<  ProductsByCategoryParams,   ProductsByCategoryRow>({  from: "products",  columns: ["id", "title", "price", "category"],  where: { category: sqc.param("category") },  orderBy: { field: "price", direction: "desc" },  limit: sqc.param("limit"),}); <ProductsByCategory  await={false}  category="electronics"  limit={3}  fulfilled={(rows, meta) => /* custom render */}  reject={(error) => /* custom render */}/>
Output

Rows

idtitlepricecategory
502Pro Chair 502993.68electronics
16Essential Headphones 16985.69electronics
536Ergonomic Tablet 536982.28electronics

Meta

SQLSELECT id, title, price, category FROM products WHERE category = :category ORDER BY price DESC LIMIT :limit
Params{"category":"electronics","limit":3}
Rows3
Duration1.13ms

Render Modes

Switch output formats with the as prop.

Reactrender.tsx
<UserList await id={1} limit={3} as="json" /><UserList await id={1} limit={3} as="table" />
Output

Rows

idname
1Sophie Lovelace
2Larry Thompson
3Donald Turing

Meta

SQL select id, name from users where id >= :id limit :limit
Params{"id":1,"limit":3}
Rows3
Duration0.71ms

Playground

Experiment with Styled Query Component examples, params, and output formats.

Interactive Query Builder

Pick an example, tweak params, and inspect the generated SQL.

User by ID

Tagged template with a single param.

sql
ReactUserById.tsx
type UserByIdParams = { id: number };type UserByIdRow = {  id: number;  name: string;  email: string;  role: string;  status: string;}; const UserById = sqc.sql<UserByIdParams, UserByIdRow>`  select id, name, email, role, status  from users  where id = ${sqc.param("id")}`; <UserById await id={1} as="table" />

Execution

Render As

Parameters

Result

Meta

Run a query to see SQL and execution metadata.