autostruct is a command-line utility that generates Rust structs from PostgreSQL database schemas. It connects to a PostgreSQL database, reads the schema, and generates Rust structs for each table found in the database schema. The generated structs are written to the specified output directory.
AutoStruct simplifies the process of creating Rust structs from an existing PostgreSQL schema. It supports various PostgreSQL data types including:
- Basic types (integer, numeric, boolean, etc.)
- Character types (varchar, text, citext)
- Date/Time types (timestamp, date, time, interval)
- Network types (inet, cidr, macaddr)
- JSON types (json, jsonb)
- Geometric types (point, line, polygon)
- Range types (int4range, numrange, daterange)
- Arrays
- Composite types
- Enumerated types
- And more
The generated structs include proper type mappings and can be used with popular Rust ORMs like SQLx.
To use AutoStruct, you need to have Rust and Cargo installed. You can install Rust using rustup.
- Clone the repository:
git clone https://github.com/yourusername/autostruct.git
cd autostruct- Build the project:
cargo build --release- You can now use the
autostructbinary from thetarget/releasedirectory.
AutoStruct provides a generate subcommand to generate Rust structs from a PostgreSQL schema.
autostruct generate [OPTIONS]-o, --output <OUTPUT>: Sets the directory in which the generated files should be written to. Default is./output.-d, --database_url <DATABASE_URL>: Sets the connection string to the PostgreSQL database. Can also be set via theDATABASE_URLenvironment variable.--singular: Creates struct names in the singular variant of the table name. Default isfalse.--framework <FRAMEWORK>: Specifies the framework to use for generated code. Options are:none: Basic struct generation with Debug and Clone derivessqlx: Adds SQLx-specific derives and types
--exclude <EXCLUDE>: Tables to exclude from generation (can be specified multiple times)-t, --timeout: Sets the timeout to be used when establishing a database connection
Generate Rust structs from a PostgreSQL database:
autostruct generate -o ./models -d "postgres://user:password@localhost/db"Generate Rust structs with SQLx support and singular table names:
autostruct generate -o ./models -d "postgres://user:password@localhost/db" --framework sqlx --singular-
Ensure you have Rust and Cargo installed. You can install Rust using rustup.
-
Clone the repository:
git clone https://github.com/yourusername/autostruct.git
cd autostruct- Build the project:
cargo buildTo build the project, run:
cargo buildTo run the project with the generate command, use:
cargo run -- generate -o ./output -d "your_database_connection_string"The project uses integration tests with test containers to verify functionality against a real PostgreSQL database. Integration tests require Docker Desktop to be installed and running. For more information on how to install Docker Desktop, see the official documentation.
To run all tests (unit + integration) it is recommended to use the commands provided via the Makefile:
make test.allFor more information on the Makefile commands, run:
make helpWhile the project currently focuses on PostgreSQL support, we welcome contributions to add support for other databases. If you're interested in adding support for another database, please open an issue to discuss the implementation approach.