mirror of
https://github.com/34892002/edgeKey.git
synced 2026-05-06 23:33:10 +08:00
fix: 一键部署
This commit is contained in:
41
README.md
41
README.md
@@ -6,20 +6,10 @@ EdgeKey 是一套有vike框架开发,可直接部署到 Cloudflare 的一体
|
||||
|
||||
[](https://deploy.workers.cloudflare.com/?url=https://github.com/34892002/edgeKey)
|
||||
|
||||
首次部署后还需要初始化线上 D1(只需执行一次)与配置 `AUTH_SECRET`(否则应用会拒绝启动):
|
||||
|
||||
```powershell
|
||||
bunx wrangler login
|
||||
|
||||
# 按文件名顺序执行所有迁移(使用 DB 绑定名,避免数据库名称不同导致失败)
|
||||
Get-ChildItem ./prisma/migrations/*.sql | Sort-Object Name | ForEach-Object { bunx wrangler d1 execute DB --remote --file=$_.FullName }
|
||||
|
||||
# 初始化管理员账号与种子数据(可重复执行)
|
||||
bunx wrangler d1 execute DB --remote --file="./scripts/seed.sql"
|
||||
|
||||
# 配置 AUTH_SECRET
|
||||
bunx wrangler secret put AUTH_SECRET
|
||||
```
|
||||
> **注:**
|
||||
> 1. 点击按钮后,Cloudflare 会引导你自动配置应用环境变量(包括 `AUTH_SECRET`)并绑定 D1 数据库。
|
||||
> 2. Cloudflare 默认执行 `npm run deploy` 脚本,它会自动完成数据库建表与初始种子数据(管理员账号等)的操作,无需手动干预。
|
||||
> 3. 线上默认管理员账号为 `admin / admin123456`,首次登录后请务必在后台修改密码!
|
||||
|
||||
## 技术栈
|
||||
|
||||
@@ -101,11 +91,7 @@ bun install
|
||||
bunx prisma generate
|
||||
|
||||
# 2. 按顺序将所有迁移脚本应用到本地 Wrangler 模拟器
|
||||
bunx wrangler d1 execute edgekey-db --local --file="./prisma/migrations/0001_init.sql"
|
||||
# 2.1 在版本更新过程中,可能会有新的数据库结构调整。
|
||||
# 如果目录存在新的迁移脚本,需要按照序号手动执行,本地开发D1数据库使用 --local 参数。
|
||||
bunx wrangler d1 execute edgekey-db --local --file="./prisma/migrations/0002_xxx.sql"
|
||||
bunx wrangler d1 execute edgekey-db --local --file="./prisma/migrations/0003_xxx.sql"
|
||||
bunx wrangler d1 migrations apply DB --local
|
||||
|
||||
# 3. 初始化管理员账号与初始化种子数据
|
||||
bun run db:seed
|
||||
@@ -133,13 +119,12 @@ bun run dev
|
||||
|
||||
3. **按顺序初始化云端表结构**
|
||||
```bash
|
||||
bunx wrangler d1 execute edgekey-db --remote --file="./prisma/migrations/0001_init.sql"
|
||||
bunx wrangler d1 execute edgekey-db --remote --file="./prisma/migrations/0002_xxx.sql"
|
||||
bunx wrangler d1 migrations apply DB --remote
|
||||
```
|
||||
|
||||
4. **初始化管理员账号与初始化种子数据**
|
||||
```bash
|
||||
bunx wrangler d1 execute edgekey-db --remote --file="./scripts/seed.sql"
|
||||
bunx wrangler d1 execute DB --remote --file="./scripts/seed.sql"
|
||||
```
|
||||
初始化后默认管理员账号为 `admin / admin123456`,首次登录后请立即修改密码。
|
||||
|
||||
@@ -152,10 +137,10 @@ bun run dev
|
||||
6. **生成 Prisma Client 并一键部署**
|
||||
```bash
|
||||
bunx prisma generate
|
||||
bun run deploy
|
||||
bun run up
|
||||
```
|
||||
|
||||
`bun run deploy` 等价于先构建再发布:
|
||||
`bun run up` 等价于先构建再发布:
|
||||
- `vike build`
|
||||
- `wrangler deploy`
|
||||
|
||||
@@ -175,7 +160,7 @@ bun run dev
|
||||
### 当前运行方式
|
||||
|
||||
- `bun dev` 运行在 Cloudflare 风格的本地开发环境中,Prisma 会通过 `env.DB` 连接到**本地 D1 模拟器**。
|
||||
- `bun run deploy` 部署后,Prisma 会通过同一个 `env.DB` 绑定连接到**远程 D1**。
|
||||
- `bun run up` 部署后,Prisma 会通过同一个 `env.DB` 绑定连接到**远程 D1**。
|
||||
- `.env` 中的 `DATABASE_URL` 仅用于 Prisma CLI / 配置层,不参与当前应用运行时的数据库连接。
|
||||
- 当前 `prisma/schema.prisma` 仅保留 Cloudflare client generator,运行时统一使用 `generated/prisma/client`。
|
||||
- 因此,本项目当前的数据库运行模式是:**开发环境用本地 D1,生产环境用远程 D1**。
|
||||
@@ -203,7 +188,7 @@ bunx prisma migrate diff \
|
||||
**第二步:将迁移同步到本地 D1 模拟器(用于本地开发/测试)**
|
||||
|
||||
```bash
|
||||
bun run db:d1 -- --local --file=./prisma/migrations/0002_xxx.sql
|
||||
bunx wrangler d1 migrations apply DB --local
|
||||
```
|
||||
|
||||
如果不执行这一步,运行 `bun dev` 访问页面时会报错 `no such table`。
|
||||
@@ -211,10 +196,10 @@ bun run db:d1 -- --local --file=./prisma/migrations/0002_xxx.sql
|
||||
**第三步:将迁移同步到 Cloudflare 线上(发布前)**
|
||||
|
||||
```bash
|
||||
bun run db:d1 -- --remote --file=./prisma/migrations/0002_xxx.sql
|
||||
bunx wrangler d1 migrations apply DB --remote
|
||||
```
|
||||
|
||||
将文件名替换成对应的增量迁移 SQL 文件即可。本地和线上需要分别执行一次。
|
||||
本地和线上需要分别执行一次。
|
||||
|
||||
### 日常开发命令
|
||||
|
||||
|
||||
@@ -3,13 +3,16 @@
|
||||
"dev": "vike dev",
|
||||
"build": "vike build",
|
||||
"preview": "vike build && vike preview",
|
||||
"db:d1": "bunx wrangler d1 execute edgekey-db",
|
||||
"db:seed": "bunx wrangler d1 execute edgekey-db --local --file=./scripts/seed.sql",
|
||||
"db:d1": "bunx wrangler d1 execute DB",
|
||||
"db:seed": "bunx wrangler d1 execute DB --local --file=./scripts/seed.sql -y",
|
||||
"db:generate": "prisma generate",
|
||||
"db:studio": "prisma studio",
|
||||
"db:migrations:apply": "wrangler d1 migrations apply DB --remote",
|
||||
"db:seed:remote": "wrangler d1 execute DB --remote --file=./scripts/seed.sql -y",
|
||||
"verify:payments": "bun run scripts/verify-payment-adapters.ts",
|
||||
"verify:payment-notify": "bun run scripts/verify-payment-notify.ts",
|
||||
"deploy": "vike build && wrangler deploy",
|
||||
"deploy": "bun run db:migrations:apply && bun run db:seed:remote && bun run up",
|
||||
"up": "vike build && wrangler deploy",
|
||||
"types": "wrangler types"
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
@@ -11,7 +11,8 @@
|
||||
"d1_databases": [
|
||||
{
|
||||
"binding": "DB",
|
||||
"database_name": "edgekey-db"
|
||||
"database_name": "edgekey-db",
|
||||
"migrations_dir": "prisma/migrations"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user