A small Fastify starter with:
- Fastify
- Scalar API docs
- Zod-based schemas
- Eta templates
- Static assets
- SQLite via better-sqlite3
- dotenv-based environment config
- Node.js 20 or newer
- npm
git clone git@github.com:linlinw5/fastify_starter.git
cd fastify_starter
npm install
## Environment
Copy [.env.example](.env.example) to `.env` if you want to override defaults:
```bash
cp .env.example .envAvailable variables:
NODE_ENV- runtime mode, defaults todevelopmentHOST- server host, defaults to0.0.0.0PORT- server port, defaults to5000DB_PATH- SQLite database file path, defaults todata/db.sqliteLOGGER_FASTIFY- enable Fastify built-in logger, defaults totrueLOGGER_DB- enable DB debug logs in custom logger, defaults totrue
## Development
```bash
npm run dev
npm run build
npm startnpm run dev- start the app in watch modenpm run build- build the production bundle intodist/npm run start- run the production buildnpm run typecheck- run TypeScript type checking
src/app.ts- application bootstrapsrc/routes/- route definitionssrc/db/- database access layerviews/- Eta templatespublic/- static assetsdata/- local SQLite database files
Drizzle Kit manages database schema changes.
npx drizzle-kit push # push schema changes to the database (executes DDL)
npx drizzle-kit pull # reverse-generate schema.ts from an existing database
npx drizzle-kit generate # generate migration files
npx drizzle-kit migrate # run migrations
npx drizzle-kit studio # open visual admin UI, similar to DataGrip- The generated SQLite database file is ignored by Git.
- After cloning, run
npm installbefore starting the app.
这是一个简洁的 Fastify 启动模板,包含:
- Fastify
- Scalar API 文档
- Zod Schema 校验
- Eta 模板引擎
- 静态资源支持
- 基于 better-sqlite3 的 SQLite
- 基于 dotenv 的环境变量配置
- Node.js 20+
- npm
git clone git@github.com:linlinw5/fastify_starter.git
cd fastify_starter
npm install如果你需要自定义配置,可复制 .env.example 为 .env:
cp .env.example .env支持的变量:
NODE_ENV:运行环境,默认developmentHOST:服务监听地址,默认0.0.0.0PORT:服务端口,默认5000DB_PATH:SQLite 文件路径,默认data/db.sqliteLOGGER_FASTIFY:是否开启 Fastify 内置日志,默认trueLOGGER_DB:是否开启 DB 调试日志,默认true
npm run devnpm run build
npm startnpm run dev:开发模式(watch)npm run build:构建产物到dist/npm run start:运行生产构建产物npm run typecheck:TypeScript 类型检查
src/app.ts:应用启动入口src/routes/:路由定义src/db/:数据库访问层views/:Eta 模板文件public/:静态资源data/:本地 SQLite 数据文件
Drizzle Kit 的核心作用是管理数据库 Schema 变更。
npx drizzle-kit push # 推送 schema 变更到数据库(执行 DDL)
npx drizzle-kit pull # 从现有数据库反向生成 schema.ts
npx drizzle-kit generate # 生成迁移文件
npx drizzle-kit migrate # 执行迁移
npx drizzle-kit studio # 打开可视化管理界面,类似 DataGrip- SQLite 数据文件默认已在 Git 忽略列表中。
- 克隆后请先执行
npm install再启动项目。