create-authhero 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,25 +15,69 @@ A single-tenant AuthHero authentication server using Cloudflare Workers and D1.
15
15
  npm install
16
16
  ```
17
17
 
18
- 2. Create a D1 database:
18
+ 2. Create a D1 database (if not using local mode):
19
19
 
20
20
  ```bash
21
21
  wrangler d1 create authhero-db
22
22
  ```
23
23
 
24
- 3. Update `wrangler.toml` with your database ID from the output above.
24
+ Update `wrangler.toml` with your database ID from the output above.
25
25
 
26
- 4. Run local database migrations:
26
+ 3. Run database migrations:
27
+
28
+ **For local development:**
29
+
30
+ ```bash
31
+ npm run migrate
32
+ ```
33
+
34
+ **For production:**
35
+
36
+ ```bash
37
+ npm run db:migrate:remote
38
+ ```
39
+
40
+ 4. Seed the database with an admin user:
41
+
42
+ **For local development:**
43
+
44
+ ```bash
45
+ ADMIN_EMAIL=admin@example.com ADMIN_PASSWORD=yourpassword npm run seed:local
46
+ ```
47
+
48
+ **For production:**
27
49
 
28
50
  ```bash
29
- npm run db:migrate
51
+ ADMIN_EMAIL=admin@example.com ADMIN_PASSWORD=yourpassword npm run seed:remote
30
52
  ```
31
53
 
32
54
  5. Start the development server:
55
+
56
+ **For local mode (local D1 database):**
57
+
58
+ ```bash
59
+ npm run dev:local
60
+ ```
61
+
62
+ **For remote mode (production D1 database):**
63
+
33
64
  ```bash
34
- npm run dev
65
+ npm run dev:remote
35
66
  ```
36
67
 
68
+ ## Available Scripts
69
+
70
+ - `npm run dev:local` - Start development server with local D1 database
71
+ - `npm run dev:remote` - Start development server with remote D1 database
72
+ - `npm run deploy` - Deploy to Cloudflare Workers
73
+ - `npm run migrate` - Run migrations on local database
74
+ - `npm run db:migrate:local` - Run migrations on local database (alias)
75
+ - `npm run db:migrate:remote` - Run migrations on remote database
76
+ - `npm run seed:local` - Seed local database with admin user
77
+ - `npm run seed:remote` - Seed remote database with admin user
78
+ - `npm run db:generate` - Generate new migration from schema changes
79
+ - `npm run db:push` - Push schema changes directly to database (development only)
80
+
37
81
  ## Deployment
38
82
 
39
83
  1. Deploy to Cloudflare:
@@ -43,21 +87,76 @@ A single-tenant AuthHero authentication server using Cloudflare Workers and D1.
43
87
  ```
44
88
 
45
89
  2. Run production migrations:
90
+
46
91
  ```bash
47
- npm run db:migrate:prod
92
+ npm run db:migrate:remote
93
+ ```
94
+
95
+ 3. Seed the production database:
96
+ ```bash
97
+ ADMIN_EMAIL=admin@example.com ADMIN_PASSWORD=yourpassword npm run seed:remote
48
98
  ```
49
99
 
50
100
  ## Project Structure
51
101
 
52
102
  ```
103
+ ├── migrations/
104
+ │ └── 0000_init.sql # Initial database schema migration
53
105
  ├── src/
54
- │ ├── index.ts # Worker entry point
55
- ├── app.ts # AuthHero app configuration
56
- └── types.ts # TypeScript type definitions
57
- ├── wrangler.toml # Cloudflare Worker configuration
106
+ │ ├── db/
107
+ │ └── schema.ts # Drizzle schema (for migration generation)
108
+ ├── index.ts # Worker entry point
109
+ ├── app.ts # AuthHero app configuration
110
+ │ ├── seed.ts # Database seeding worker
111
+ │ └── types.ts # TypeScript type definitions
112
+ ├── drizzle.config.ts # Drizzle Kit configuration
113
+ ├── seed-helper.js # Helper script for automated seeding
114
+ ├── wrangler.toml # Cloudflare Worker configuration
58
115
  └── package.json
59
116
  ```
60
117
 
118
+ ## Database Migrations
119
+
120
+ This project uses [Drizzle Kit](https://orm.drizzle.team/kit-docs/overview) for generating database migrations from schema changes.
121
+
122
+ ### Schema-Driven Migrations
123
+
124
+ The database schema is defined in `src/db/schema.ts` using Drizzle ORM. When you need to make schema changes:
125
+
126
+ 1. **Modify the schema** (if customizing beyond the default AuthHero schema)
127
+
128
+ 2. **Generate a new migration:**
129
+
130
+ ```bash
131
+ npm run db:generate
132
+ ```
133
+
134
+ This creates a new SQL migration file in the `migrations/` directory.
135
+
136
+ 3. **Apply the migration locally:**
137
+
138
+ ```bash
139
+ npm run db:migrate:local
140
+ ```
141
+
142
+ 4. **Apply to production:**
143
+ ```bash
144
+ npm run db:migrate:remote
145
+ ```
146
+
147
+ ### Migration Architecture
148
+
149
+ - **Drizzle ORM**: Defines the database schema in TypeScript
150
+ - **drizzle-kit**: Generates SQL migrations by comparing schema to database state
151
+ - **Kysely**: Used at runtime for executing queries (via @authhero/kysely-adapter)
152
+
153
+ This approach gives you:
154
+
155
+ - Type-safe schema definitions
156
+ - Incremental migrations (only changes are migrated)
157
+ - Full control over migration SQL
158
+ - Compatibility with Cloudflare D1
159
+
61
160
  ## API Documentation
62
161
 
63
162
  Visit your worker URL with `/docs` to see the Swagger UI documentation.
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from "drizzle-kit";
2
+
3
+ // SQLite/D1 configuration for Cloudflare Workers
4
+ export default defineConfig({
5
+ out: "./migrations",
6
+ schema: "./src/db/schema.ts",
7
+ dialect: "sqlite",
8
+ });