← 全部点子
Developer tools· 开发者工具2026年10月26日约 14 天可做出来

Type-safe SQL imports for TypeScript without codegen无需代码生成的 TypeScript 类型安全 SQL 导入

A small tool that lets TypeScript developers import .sql files and get full type inference, with no codegen step and no ORM lock-in.一个小工具,让 TypeScript 开发者可以导入 .sql 文件并获得完整的类型推断,无需代码生成步骤,也不锁定 ORM。

用户与问题

面向谁: TypeScript backend developers and small teams who write raw SQL and want type safety without adopting an ORM or running a codegen step.使用原生 SQL 编写、希望在不引入 ORM 或运行代码生成步骤的前提下获得类型安全的 TypeScript 后端开发者和小型团队。

TypeScript developers who write raw SQL lose type safety. Their query results are typed as any, so column names and types are only checked at runtime. The usual fixes are codegen tools like sqlc or full ORMs like Prisma, both of which add a build step or lock you into a framework.编写原生 SQL 的 TypeScript 开发者会失去类型安全。他们的查询结果被类型化为 any,因此列名和类型只能在运行时检查。常见的解决方案是 sqlc 这类代码生成工具,或 Prisma 这类完整 ORM,但两者都会增加构建步骤,或让你被框架锁定。

可核实的证据

1 个来源 · 每个链接都能打开

为什么是现在

TypeScript type-level programming has matured enough to infer SQL schemas without a codegen step, so a small tool can now do what used to need a build pipeline.TypeScript 的类型级编程已经足够成熟,可以在没有代码生成步骤的情况下推断 SQL schema,因此一个小工具现在就能做到过去需要构建流水线才能完成的事。

市场空白与主要竞品

Existing options force a trade-off: sqlc needs a codegen step, Prisma needs ORM lock-in. A zero-codegen, no-lock-in import path for plain .sql files is still thin, especially for small teams that want to keep raw SQL.现有方案都迫使你做出取舍:sqlc 需要代码生成步骤,Prisma 需要接受 ORM 锁定。针对纯 .sql 文件的零代码生成、无锁定的导入路径仍然很少,尤其是对于希望保留原生 SQL 的小型团队而言。

竞品价格短板
sqlcFree, open source免费、开源Requires a codegen step and a config file before you get types.在获得类型之前需要代码生成步骤和配置文件。
PrismaFree core, paid cloud plans免费核心版,付费云计划Full ORM lock-in; you write in its schema language, not plain SQL.完全被 ORM 锁定;你要用它的 schema 语言编写,而不是原生 SQL。

首版核心功能

  1. Import a .sql file in TypeScript and get inferred types for the query result在 TypeScript 中导入 .sql 文件,并获得查询结果的推断类型
  2. Support SELECT queries against a declared schema with column name and type checking支持针对已声明 schema 的 SELECT 查询,并进行列名和类型检查
  3. Clear error messages when a query does not match the schema当查询与 schema 不匹配时给出清晰的错误信息

前 100 个用户

  1. Post in TypeScript and Node.js communities on Reddit and Discord在 Reddit 和 Discord 的 TypeScript 与 Node.js 社区发帖
  2. Share a short demo on X and Hacker News Show HN在 X 和 Hacker News 的 Show HN 上分享一个简短演示
  3. Write one blog post comparing the zero-codegen approach to sqlc and Prisma写一篇博客文章,比较零代码生成方案与 sqlc 和 Prisma

启动包

需求文档、技术栈和逐步构建的提示词。都可以复制。

TypeScriptNode.jsVitesttsx

Overview

A TypeScript library that lets developers import .sql files and get typed query results with no codegen step and no ORM.

Tech stack

  • TypeScript
  • Node.js
  • A test runner such as Vitest
  • A small demo app

Features

  1. Import a .sql file and infer the result type.
  2. Check SELECT queries against a declared schema.
  3. Show clear errors when a query does not match the schema.

Don'ts

  • Do not add a codegen or build step.
  • Do not require an ORM or a schema language.
  • Do not support writes or migrations in the first version.

Done when

  • A demo app imports a .sql file and gets correct types.
  • Wrong column names fail at compile time.
  • Tests cover at least three query shapes.

概述

一个 TypeScript 库,让开发者可以导入 .sql 文件并获得带类型的查询结果,无需代码生成步骤,也无需 ORM。

技术栈

  • TypeScript
  • Node.js
  • 测试运行器,例如 Vitest
  • 一个小型演示应用

功能

  1. 导入 .sql 文件并推断结果类型。
  2. 针对已声明的 schema 检查 SELECT 查询。
  3. 当查询与 schema 不匹配时显示清晰的错误。

不要做

  • 不要添加代码生成或构建步骤。
  • 不要要求使用 ORM 或 schema 语言。
  • 第一版不要支持写入或迁移。

完成标准

  • 演示应用导入 .sql 文件并获得正确的类型。
  • 错误的列名在编译时失败。
  • 测试覆盖至少三种查询形态。

第 1 步

Set up the project· 搭建项目

Create a new TypeScript Node.js project with strict mode on. Add Vitest for tests and tsx for running scripts. Add a src folder and a tests folder. Show me the package.json and tsconfig.json.

创建一个开启严格模式的 TypeScript Node.js 新项目。添加 Vitest 用于测试,添加 tsx 用于运行脚本。添加 src 文件夹和 tests 文件夹。向我展示 package.json 和 tsconfig.json。

第 2 步

Define the schema type· 定义 schema 类型

In src/schema.ts, define a TypeScript type that describes a database schema as a map of table names to column names and column types. Keep it simple: string, number, boolean, and null.

在 src/schema.ts 中,定义一个 TypeScript 类型,将数据库 schema 描述为表名到列名和列类型的映射。保持简单:string、number、boolean 和 null。

第 3 步

Parse a .sql file· 解析 .sql 文件

Write a function that reads a .sql file as a string and extracts the SELECT column list and the FROM table name. Return a plain object with those fields. Add tests for a simple SELECT and a SELECT with aliases.

编写一个函数,将 .sql 文件作为字符串读取,并提取 SELECT 列列表和 FROM 表名。返回包含这些字段的普通对象。为简单 SELECT 和带别名的 SELECT 添加测试。

第 4 步

Infer result types· 推断结果类型

Using the parsed query and the schema type, write a TypeScript type that maps the selected columns to their types from the schema. Add a test file that checks the inferred type with type-level assertions.

使用解析后的查询和 schema 类型,编写一个 TypeScript 类型,将选中的列映射到 schema 中对应的类型。添加一个测试文件,用类型级断言检查推断出的类型。

第 5 步

Add compile-time errors· 添加编译时错误

Make the inferred type fail to compile when a selected column does not exist in the schema. Show a short example of the error message a developer would see.

当选中的列在 schema 中不存在时,让推断出的类型编译失败。展示一个开发者会看到的错误信息的简短示例。

第 6 步

Build a demo app· 构建一个演示应用

Create a small demo app that imports a users.sql file, runs the query against an in-memory array, and prints typed results. Show the full demo code.

创建一个小型演示应用,导入 users.sql 文件,对内存中的数组运行查询,并打印带类型的结果。展示完整的演示代码。

第 7 步

Write the README· 编写 README

Write a README with a short intro, a quick start example, and a section comparing this to sqlc and Prisma in plain words. No hype.

编写一个 README,包含简短介绍、快速开始示例,以及一个用平实语言将其与 sqlc 和 Prisma 进行对比的章节。不要夸大宣传。

上线前检查

  • Strict TypeScript is on已开启严格 TypeScript
  • A .sql file can be imported可以导入 .sql 文件
  • Result types are inferred from the schema结果类型从 schema 推断得出
  • Wrong column names fail at compile time错误的列名会在编译时失败
  • Demo app runs演示应用可以运行
  • Tests pass测试通过
  • README has a quick startREADME 包含快速开始
  • Repo is public with a runnable example仓库公开,并带有可运行的示例

今日雷达发现了 113 个机会

登录后可查看前 10 个。

登录查看 10 条

每天早上一个带证据的点子。