supabase-types-watch
v0.1.0
Published
Auto-regenerate Supabase TypeScript types when migrations change
Maintainers
Readme
supabase-types-watch
Auto-regenerate Supabase TypeScript types whenever a migration changes — no more manual supabase gen types runs.
The Problem
Every time you write a new Supabase migration during local development, the TypeScript types in your project go stale. The official Supabase documentation recommends running supabase gen types typescript --local manually or setting up a nightly GitHub Action — but neither solution gives you instant feedback while actively developing migrations.
supabase-types-watch fills that gap: it watches your supabase/migrations/ directory and re-runs type generation automatically whenever a .sql file is added or changed.
Installation
npm install -D supabase-types-watchQuick Start
Add it as a dev script alongside your framework's dev server:
{
"scripts": {
"dev": "concurrently \"next dev\" \"supabase-types-watch\""
}
}Install concurrently if you don't have it:
npm install -D concurrentlyUsage with concurrently
# Next.js
"dev": "concurrently \"next dev\" \"supabase-types-watch\""
# Vite
"dev": "concurrently \"vite\" \"supabase-types-watch\""
# Remix
"dev": "concurrently \"remix dev\" \"supabase-types-watch\""On startup, supabase-types-watch runs type generation once immediately, then enters watch mode and regenerates types on every migration change.
Configuration
All options can be set via CLI flags, a "supabase-types-watch" key in package.json, or a [typescript] section in supabase/config.toml. CLI flags take highest priority.
| Option | Default | Description |
|---|---|---|
| migrationsDir | supabase/migrations | Directory to watch for .sql files |
| output | src/types/database.types.ts | Output path for generated TypeScript types |
| debounceMs | 500 | Milliseconds to wait after a change before regenerating (prevents duplicate runs on bulk file operations) |
| verbose | false | Print extra diagnostic output |
| command | npx supabase gen types typescript --local | Shell command used to generate types |
package.json config block
{
"supabase-types-watch": {
"migrationsDir": "supabase/migrations",
"output": "src/types/database.types.ts",
"debounceMs": 500,
"verbose": false
}
}CLI options
Options:
--migrations-dir <path> Override migrations directory
--output <path> Override output file path
--debounce <ms> Override debounce delay
--verbose Enable verbose logging
--command <cmd> Override the gen types command
-V, --version Output the version number
-h, --help Display helpOne-Time Run Mode
Run type generation once and exit (useful in CI or pre-commit hooks):
npx supabase-types-watch runCI Usage
Add a one-time type generation step before your type-check step:
- name: Generate Supabase types
run: npx supabase-types-watch run
- name: Type check
run: tsc --noEmitThis ensures CI always uses fresh types without committing generated files.
How It Works
- Watch — chokidar watches
supabase/migrations/**/*.sqlforaddandchangeevents. - Debounce — Multiple file events within
debounceMsare collapsed into a single trigger to avoid redundant runs during migrations that write several files at once. - Generate — Node's
child_process.spawn(via cross-spawn for Windows compatibility) runs the configured command and streams output directly to your terminal. - Resilient — A failed generation (non-zero exit code or missing CLI) is logged but does not kill the watcher. You can fix the migration and the next save will retry automatically.
Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feat/my-feature - Make your changes with tests
- Run
npm test && npm run lintto verify - Open a pull request
All contributions welcome!
