This Swift script generates Swift structs from a Supabase SQL schema file. It's designed to automate the process of creating Swift data models that correspond to your Supabase database tables.
- Parses CREATE TABLE statements from a Supabase SQL schema file
- Generates Swift structs with properties matching table columns
- Maps Supabase SQL data types to appropriate Swift types
- Handles nullable fields
- Outputs generated structs to a Swift file
- Swift 5.0 or later
- macOS environment
- Supabase project with a SQL schema file
- Supabase CLI (for generating the schema file)
- Ensure you have Swift installed on your system.
- Save the
generate.swift
script to your project directory. - Use the Supabase CLI to generate your SQL schema file:
supabase db dump -f schema.sql
- Run the script from the command line, providing the path to your SQL schema file:
swift generate.swift schema.sql
- The script will generate a file named
generated.swift
in the same directory, containing the Swift structs.
Assuming you've generated your Supabase schema file using the CLI command above, you would run the script like this:
swift generate.swift schema.sql
This will generate a generated.swift
file in your current directory. The generated file might look something like this:
import Foundation
struct users: Codable {
let id: String
let created_at: Date?
let email: String?
let name: String?
}
struct posts: Codable {
let id: Int
let created_at: Date?
let title: String
let content: String?
let author_id: String
}
// ... more structs for other tables ...
You can then import this generated.swift
file into your Swift project to use these structs.