Switch from @astrojs/db to drizzle#45
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughMigrates from Astro DB to Drizzle ORM/Turso: removes Astro DB integration, adds Drizzle schema and config, introduces a DB factory, updates seed and page code to use Drizzle, updates data and package scripts, and adjusts CI and docs to use new environment variables. Changes
Sequence Diagram(s)sequenceDiagram
participant Page as Page ([episode].astro)
participant Seed as Seed (db/seed.ts)
participant DBFactory as DB Factory (db/index.ts)
participant Remote as Remote DB (Turso/libSQL)
Page->>DBFactory: createDb(ASTRO_DB_REMOTE_URL, ASTRO_DB_APP_TOKEN)
DBFactory->>Remote: open connection (url, authToken)
Page->>Remote: run queries via Drizzle ORM
Seed->>DBFactory: createDb(ASTRO_DB_REMOTE_URL, ASTRO_DB_APP_TOKEN)
DBFactory->>Remote: open connection (url, authToken)
Seed->>Remote: insert seed data / relations
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
db/seed.ts (2)
20-23: Consider validating environment variables before use.The non-null assertions (
!) will cause a runtime error with an unclear message if the environment variables are missing. Adding explicit validation would provide better error messages.🛡️ Proposed validation
+const url = process.env.ASTRO_DB_REMOTE_URL; +const token = process.env.ASTRO_DB_APP_TOKEN; + +if (!url || !token) { + console.error('Missing required environment variables: ASTRO_DB_REMOTE_URL and/or ASTRO_DB_APP_TOKEN'); + process.exit(1); +} + -const db = createDb( - process.env.ASTRO_DB_REMOTE_URL!, - process.env.ASTRO_DB_APP_TOKEN! -); +const db = createDb(url, token);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@db/seed.ts` around lines 20 - 23, The code calls createDb(...) using non-null assertions on process.env.ASTRO_DB_REMOTE_URL and process.env.ASTRO_DB_APP_TOKEN which will throw unclear runtime errors if those env vars are missing; add explicit validation in db/seed.ts before calling createDb: check process.env.ASTRO_DB_REMOTE_URL and process.env.ASTRO_DB_APP_TOKEN, and if either is falsy, throw or log a descriptive error (e.g., "Missing ASTRO_DB_REMOTE_URL" / "Missing ASTRO_DB_APP_TOKEN") and exit so createDb is only invoked with validated values. Ensure you reference the same symbols (createDb, ASTRO_DB_REMOTE_URL, ASTRO_DB_APP_TOKEN) so the guard is colocated with the current call.
26-32: Replace theas anytype cast with a more specific assertion.The
peopledata structure matches thePersonschema fields, but usingas anybypasses TypeScript's type checking unnecessarily. Consider using a more specific type assertion or ensuring the inferred type is compatible with Drizzle ORM's.values()method to maintain type safety.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@db/seed.ts` around lines 26 - 32, The current call to db.insert(Person).values(people as any) uses an unsafe any cast; replace it with a specific insert type so TypeScript validates the rows — e.g., import and use the Drizzle insert type (or define a PersonInsert type) and cast people to PersonInsert[] (or the appropriate Array<InsertTypeForPerson>) before passing to .values(), ensuring the types align with the Person table and preserving type safety for db.insert(Person).values(...).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@drizzle.config.ts`:
- Around line 1-11: The config file exports defineConfig and reads
process.env.ASTRO_DB_REMOTE_URL and process.env.ASTRO_DB_APP_TOKEN but never
loads .env, so drizzle-kit runs may see undefined values; fix by importing
dotenv (e.g., require('dotenv/config') or import 'dotenv/config') at the top of
drizzle.config.ts before the default export so dbCredentials (the
ASTRO_DB_REMOTE_URL and ASTRO_DB_APP_TOKEN references) are populated when
defineConfig executes.
---
Nitpick comments:
In `@db/seed.ts`:
- Around line 20-23: The code calls createDb(...) using non-null assertions on
process.env.ASTRO_DB_REMOTE_URL and process.env.ASTRO_DB_APP_TOKEN which will
throw unclear runtime errors if those env vars are missing; add explicit
validation in db/seed.ts before calling createDb: check
process.env.ASTRO_DB_REMOTE_URL and process.env.ASTRO_DB_APP_TOKEN, and if
either is falsy, throw or log a descriptive error (e.g., "Missing
ASTRO_DB_REMOTE_URL" / "Missing ASTRO_DB_APP_TOKEN") and exit so createDb is
only invoked with validated values. Ensure you reference the same symbols
(createDb, ASTRO_DB_REMOTE_URL, ASTRO_DB_APP_TOKEN) so the guard is colocated
with the current call.
- Around line 26-32: The current call to db.insert(Person).values(people as any)
uses an unsafe any cast; replace it with a specific insert type so TypeScript
validates the rows — e.g., import and use the Drizzle insert type (or define a
PersonInsert type) and cast people to PersonInsert[] (or the appropriate
Array<InsertTypeForPerson>) before passing to .values(), ensuring the types
align with the Person table and preserving type safety for
db.insert(Person).values(...).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e958a22e-4e5c-4ace-927d-681ee83ad54a
⛔ Files ignored due to path filters (4)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yamlsrc/img/people/dthompsondev.jpgis excluded by!**/*.jpgsrc/img/people/leonnoel.jpgis excluded by!**/*.jpgsrc/img/people/wattenberger.jpgis excluded by!**/*.jpg
📒 Files selected for processing (12)
CLAUDE.mdastro.config.mjsdb/config.tsdb/data/people-per-episode.tsdb/data/people.tsdb/data/sponsors-per-episode.tsdb/index.tsdb/schema.tsdb/seed.tsdrizzle.config.tspackage.jsonsrc/pages/[episode].astro
💤 Files with no reviewable changes (2)
- astro.config.mjs
- db/config.ts
| import { defineConfig } from 'drizzle-kit'; | ||
|
|
||
| export default defineConfig({ | ||
| schema: './db/schema.ts', | ||
| out: './drizzle', | ||
| dialect: 'turso', | ||
| dbCredentials: { | ||
| url: process.env.ASTRO_DB_REMOTE_URL!, | ||
| authToken: process.env.ASTRO_DB_APP_TOKEN! | ||
| } | ||
| }); |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Check if drizzle-kit documentation mentions automatic .env loading
# and verify the current setup works
# Check if there's a .env or .env.local file in the repo
fd -t f '\.env' --max-depth 1
# Check if any other config files load dotenv
rg -n "dotenv" --type ts --type js -g '!node_modules'Repository: shipshapecode/starpod
Length of output: 216
🏁 Script executed:
# Check package.json for drizzle scripts and configuration
cat -n package.json | grep -A 20 '"scripts"'Repository: shipshapecode/starpod
Length of output: 877
🏁 Script executed:
# Look for any references to how drizzle-kit is invoked
rg -n "drizzle" package.json -A 2 -B 2Repository: shipshapecode/starpod
Length of output: 641
🏁 Script executed:
# Check if there's a .env file referenced elsewhere or if env vars are set differently
find . -name ".env*" -o -name "*.env*" 2>/dev/null | head -20Repository: shipshapecode/starpod
Length of output: 47
🏁 Script executed:
# Check the actual drizzle.config.ts more carefully for any dotenv loading
cat -n drizzle.config.tsRepository: shipshapecode/starpod
Length of output: 401
🏁 Script executed:
# Search for CI/CD configurations that might set environment variables
fd -t f "\.github|\.gitlab|\.circleci|github|gitlab|circleci" --max-depth 2 2>/dev/null | head -20Repository: shipshapecode/starpod
Length of output: 47
🏁 Script executed:
# Check .gitignore to see if .env files are ignored
cat -n .gitignore 2>/dev/null | grep -i "env"Repository: shipshapecode/starpod
Length of output: 130
🏁 Script executed:
# Look for any Vercel or deployment configuration
fd -t f "vercel.json|netlify.toml|astro.config|environment" --max-depth 2Repository: shipshapecode/starpod
Length of output: 81
🌐 Web query:
drizzle-kit .env automatic loading environment variables
💡 Result:
Drizzle-kit does not automatically load environment variables from .env files by default. Users must manually load them in the drizzle.config.ts (or .js) file using a library like dotenv. To enable .env loading: 1. Install dotenv: npm i -D dotenv 2. In drizzle.config.ts: import * as dotenv from "dotenv"; dotenv.config({ path: ".env.local" }); // or appropriate .env file import type { Config } from "drizzle-kit"; export default { schema: "./src/db/schema.ts", out: "./drizzle", dialect: "postgresql", dbCredentials: { url: process.env.DATABASE_URL!, }, } satisfies Config; For Next.js projects, use @next/env instead: import { loadEnvConfig } from "@next/env"; loadEnvConfig("./"); Alternative approaches: - Use Node.js --env-file flag (Node v20.6+): node --env-file=.env.local ./node_modules/drizzle-kit/bin.cjs generate - Prefix commands: dotenv -e .env.local -- drizzle-kit generate A GitHub discussion (#3405) from Nov 2024 announced planned built-in support for automatic .env loading (including .env*, .env.local based on NODE_ENV, --env-file, --no-env flags), using dotenvx. Some users report it "working out of the box" in recent versions (e.g., 0.30.5+), and logs show "Env file: .env.development.local", but official docs do not confirm full implementation as of 2026-03-27. Check latest changelog or test npx drizzle-kit push to verify if automatic loading is active in your version. Official docs recommend manual dotenv import for dbCredentials.
Citations:
- 1: Drizzle Kit dotenv support drizzle-team/drizzle-orm#3405
- 2: https://stackoverflow.com/questions/77593688/unable-to-load-env-database-url-in-drizzle-kits-drizzle-config-ts-with-next-js
- 3: Unable to Load .env Database URL in Drizzle-Kit's drizzle.config.ts with Next.js 14 drizzle-team/drizzle-orm#1600
- 4: https://orm.drizzle.team/docs/drizzle-config-file
- 5: https://fix-kit-docs.drizzle-orm-fe.pages.dev/kit-docs/conf
- 6: https://www.erowa.com/en
Add dotenv loading to drizzle.config.ts to ensure environment variables are available when running drizzle-kit commands.
Drizzle Kit doesn't automatically load .env files. The configuration references process.env.ASTRO_DB_REMOTE_URL and process.env.ASTRO_DB_APP_TOKEN without loading them, which means these values will be undefined unless set externally (e.g., in CI/CD or shell environment). This is inconsistent with db/seed.ts, which already imports dotenv/config.
Solution: Add dotenv import
+import 'dotenv/config';
import { defineConfig } from 'drizzle-kit';
export default defineConfig({🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@drizzle.config.ts` around lines 1 - 11, The config file exports defineConfig
and reads process.env.ASTRO_DB_REMOTE_URL and process.env.ASTRO_DB_APP_TOKEN but
never loads .env, so drizzle-kit runs may see undefined values; fix by importing
dotenv (e.g., require('dotenv/config') or import 'dotenv/config') at the top of
drizzle.config.ts before the default export so dbCredentials (the
ASTRO_DB_REMOTE_URL and ASTRO_DB_APP_TOKEN references) are populated when
defineConfig executes.
Summary by CodeRabbit
Refactor
Chores
New Content
Documentation