You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求推荐:基于SQL查询结果生成TypeScript类型定义的CLI工具

Tools to Generate TypeScript Types from SQL Queries & Schemas

Absolutely! There are several reliable CLI tools that do exactly what you’re looking for—automatically generate TypeScript type definitions from SQL schemas and query results, mirroring the workflow you love with Apollo Codegen for GraphQL. These tools eliminate the risk of manual type errors and keep your types in sync with your database. Here are my top recommendations:

1. pgtyped (PostgreSQL-Focused)

If your team uses PostgreSQL, pgtyped is a fantastic choice built specifically for TypeScript. It parses your raw SQL queries and generates precise types that match the expected result set.

  • Setup: Install it as a dev dependency:
    npm install --save-dev pgtyped
    
  • Workflow:
    1. Write your SQL queries in dedicated .sql files (e.g., fetch-active-users.sql):
      SELECT id, username, email, created_at FROM users WHERE is_active = true;
      
    2. Create a config file (pgtyped.config.json) to point to your database and query files.
    3. Run the CLI to generate types:
      npx pgtyped -c pgtyped.config.json
      
    4. You’ll get a corresponding .ts file with a type like FetchActiveUsersResult that perfectly matches your query’s output.

2. sql-ts (Multi-Database Support)

For teams working with multiple databases (MySQL, SQL Server, SQLite, etc.), sql-ts is a flexible option. It can generate TypeScript interfaces from your entire database schema or target specific queries to generate result types.

  • Setup: Install globally or as a dev dependency:
    npm install -g sql-ts
    
  • Usage:
    Run the CLI with your database connection string to generate schema-based types, or use flags to specify query files. For example:
    sql-ts -c "mysql://user:pass@localhost/db" -o ./src/types/db.ts
    
    This outputs interfaces for every table in your schema, and you can extend it to generate query-specific types too.

3. Prisma Introspection (ORM + Type Generation)

While Prisma is primarily an ORM, its introspection feature is a powerful way to generate TypeScript types from an existing SQL schema. Even if you prefer writing raw SQL, you can use Prisma’s generated types to validate your query results.

  • Setup:
    1. Initialize Prisma in your project:
      npx prisma init
      
    2. Configure your database connection in prisma/schema.prisma.
  • Generate Types:
    Run the introspection command to pull your schema and generate types:
    npx prisma db pull
    
    This creates a fully typed @prisma/client package with models for all your tables. You can import these types to type-check raw SQL queries or use Prisma’s query builder for type-safe database interactions.

All these tools work similarly to Apollo Codegen—they keep your types in lockstep with your database and queries, so you never have to manually update types when your schema or queries change.

内容的提问来源于stack exchange,提问作者Gabe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:40:11