TailwindSQL – 类似于TailwindCSS,但用于React服务端组件中的SQL查询。
TailwindSQL – Like TailwindCSS, but for SQL Queries in React Server Components

原始链接: https://github.com/mmarinovic/tailwindsql

## TailwindSQL:具有类似 CSS 风格的 SQL 查询 TailwindSQL 将 Tailwind CSS 的简洁性带到 React Server Components 中的 SQL 查询中。它允许开发者使用熟悉的类名在 JSX 中直接编写数据库查询——无需客户端 JavaScript! 该库利用 SQLite 和自定义解析器/查询构建器,将 `className` 属性转换为在构建/渲染时执行的安全 SQL 语句。结果可以通过 `as` prop 渲染为文本、列表、表格或 JSON。 例如,`` 获取用户的姓名。该项目包含一个演示,并使用 Next.js 构建了一个交互式游乐场,以探索其功能。 TailwindSQL 是一个实验性项目,探索 CSS 驱动的数据库交互,并采用 MIT 许可(但不建议用于生产部署!)。你可以在 [GitHub](https://github.com/mmarinovic/tailwindsql) 上找到代码和说明。

一个名为TailwindSQL的新React库最近在Hacker News上分享,它旨在将TailwindCSS的实用优先CSS方法引入到React服务器组件中的SQL查询构建。该项目在GitHub上可用,引发了关于React相关框架激增以及网络开发中 perceived 的简单性下降的讨论。 评论者指出与较旧的技术(如Adobe ColdFusion)相似之处,后者提供类似的功能,并惊讶于ColdFusion仍在继续开发。一位用户指出在Hacker News等平台上活跃的ColdFusion社区,并强调它被用于构建成功的服务,如Distrokid。 值得注意的是,TailwindSQL项目本身被标记为“仅供娱乐”,不打算用于生产环境,这引发了一个幽默的预测,即“TailwindSyscall”将成为这一趋势的下一个迭代。
相关文章

原文

Like TailwindCSS, but for SQL queries in React Server Components.

GitHub

TailwindSQL lets you write SQL queries using Tailwind-style class names. Just use className to query your database directly in React Server Components!

// Fetch and render a user's name
<DB className="db-users-name-where-id-1" />
// Renders: "Ada Lovelace"

// Render products as a list
<DB className="db-products-title-limit-5" as="ul" />
// Renders: <ul><li>Mechanical Keyboard</li>...</ul>

// Order by price and show as table
<DB className="db-products-orderby-price-desc" as="table" />
  • 🎨 Tailwind-style syntax - Write SQL queries using familiar class names
  • React Server Components - Zero client-side JavaScript for queries
  • 🔒 SQLite - Built on better-sqlite3 for fast, local database access
  • 🎯 Zero Runtime - Queries are parsed and executed at build/render time
  • 🎭 Multiple Render Modes - Render as text, lists, tables, or JSON
db-{table}-{column}-where-{field}-{value}-limit-{n}-orderby-{field}-{asc|desc}
Class Name SQL Query
db-users SELECT * FROM users
db-users-name SELECT name FROM users
db-users-where-id-1 SELECT * FROM users WHERE id = 1
db-posts-title-limit-10 SELECT title FROM posts LIMIT 10
db-products-orderby-price-desc SELECT * FROM products ORDER BY price DESC
# Clone the repository
git clone https://github.com/mmarinovic/tailwindsql.git
cd tailwindsql

# Install dependencies
npm install

# Seed the database with demo data
npm run seed

# Start the development server
npm run dev

Open http://localhost:3000 to see the demo and interactive playground!

  1. Parser (src/lib/parser.ts) - Parses class names into query configurations
  2. Query Builder (src/lib/query-builder.ts) - Transforms configs into safe SQL queries
  3. DB Component (src/components/DB.tsx) - React Server Component that executes queries and renders results

The as prop controls how results are rendered:

Value Description
span Inline text (default)
div Block element
ul Unordered list
ol Ordered list
table HTML table
json JSON code block
tailwindsql/
├── src/
│   ├── app/              # Next.js app directory
│   │   ├── page.tsx      # Landing page
│   │   └── api/          # API routes
│   ├── components/        # React components
│   │   ├── DB.tsx        # Main DB component
│   │   ├── Example.tsx   # Example components
│   │   └── Playground.tsx # Interactive playground
│   └── lib/              # Core logic
│       ├── parser.ts     # Class name parser
│       ├── query-builder.ts # SQL query builder
│       └── db.ts         # Database connection
└── README.md

This project was built to explore css-driven database queries.

MIT - Do whatever you want with it (except deploy to production 😅)


Built with 💜 using Next.js, SQLite, and questionable decisions

联系我们 contact @ memedata.com