SkyORM
Warning
SkyORM is under very early development. Expect frequent breaking changes!
SkyORM is a light-weight ORM intended to be used in conjunction with sqlx.
Inspired by sqlx' compile-time checked queries, SkyORM lets you generate your Rust models directly from your database schema at compile time. To do so, first generate a schema in JSON format using SkyORM's CLI, like so:
sky-orm generate-schema -d postgresql://postgres:mypassword@localhost:5432/database_name
This will create a schema.json file containing relevant database information, such as tables, columns, types, and
relations (for databases that support it). You can then automatically insert these models in your Rust code using the
model! macro:
model! {
// Table name. Any attributes added right here will be added to the
// model struct.
#[derive(Serialize, Deserialize)]
"user",
fields: {
// By default, column names will be converted to snake case, i.e.
// firstName will become first_name.
// You can rename columns with the `->` token, this uses the column
// name as it appears in the database:
id -> user_id,
firstName -> user_first_name,
// Attributes can be added to struct fields, this uses the struct field name:
#[serde(skip)]
password,
// Types can be overridden. E.g. imagine that account_type is stored as a
// string in our DB, but we have an enum that implements `Decode`:
account_type: UserAccountType,
// All of these can be combined as well:
#[serde(skip)]
userRoleInProduct -> user_role: UserRoleType
}
}
The model! macro will also automatically implement relations based on foreign-key constraints, if your database
supports it. To find target entities, it will look in the super module, with the table name converted to snake_case,
so it makes sense to ensure that all your models share the same parent module.