पाठ 21 / 25

Generated Types, Seeding and Environments

Type-safe queries and repeatable data.

Types from the schema

supabase gen types typescript reads your database schema and writes a Database type; passing it to createClient<Database>() gives typed rows, inserts and RPC arguments. Regenerate after every migration, ideally in CI. supabase/seed.sql fills the local database with test data on db reset. Keep separate projects (or Supabase branching, which creates preview environments from Git branches on supported plans) for development, staging and production; check the docs for current branching options.

Generating and using types

CLI plus TypeScript.

// shell:
//   supabase gen types typescript --local > src/lib/database.types.ts
//   supabase gen types typescript --project-id your-project-ref > src/lib/database.types.ts

import { createClient } from '@supabase/supabase-js'
import type { Database } from './database.types'

const supabase = createClient<Database>(url, anonKey)

// title is typed as string, done as boolean
const { data } = await supabase.from('tasks').select('id, title, done')

type Task = Database['public']['Tables']['tasks']['Row']

Fail CI when types are stale

Generate types in CI and compare with the committed file; a diff means someone changed the schema without updating types.

त्वरित जाँच: What does passing the generated Database type to createClient give you?

  • Automatic RLS policies
  • Typed rows, inserts and RPC arguments
  • Faster queries at runtime
  • Encrypted columns
Answer

Typed rows, inserts and RPC arguments — Types catch column and table typos at compile time.