Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kurtbuilds/sqlmo
SQL data primitives. Use it to generate SQL queries, auto-generate SQL migrations, and more.
https://github.com/kurtbuilds/sqlmo
orm postgresql rust sql
Last synced: 2 months ago
JSON representation
SQL data primitives. Use it to generate SQL queries, auto-generate SQL migrations, and more.
- Host: GitHub
- URL: https://github.com/kurtbuilds/sqlmo
- Owner: kurtbuilds
- Created: 2022-12-26T22:40:39.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-10-19T00:05:41.000Z (3 months ago)
- Last Synced: 2024-10-19T03:19:17.383Z (3 months ago)
- Topics: orm, postgresql, rust, sql
- Language: Rust
- Homepage: https://crates.io/crates/sqlmo
- Size: 200 KB
- Stars: 4
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `sqlmo`
`sqlmo` is a set of primitives to represent SQL tables and queries. Use these primitives to:
- **Auto-generate migrations**: Load SQL representations in a standardized form (`sqlmo::Schema`), calculate differences between
schemas (`sqlmo::Migration`), and generate SQL to apply the migration (`sqlmo::Migration::to_sql`).
- **Build SQL queries**: Represent SQL queries in a data model, to create APIs for query generation. Then, generate the
SQL query. *Note: this library does not support parsing SQL queries (yet).*For auto-generating migrations, there are a few built-in schema sources:
- **Postgres**: [`sqlmo_sqlx`](./sqlmo_sqlx)
- **OpenAPI v3**: [`sqlmo_openapi`](./sqlmo_openapi)If you need another source, you should define a way to build a `sqlmo::Schema` from your data source, then use `sqlmo`
to auto-generate migrations.Current tools that support this:
- [`ormlite`](https://github.com/kurtbuilds/ormlite)
If you use this library, submit a PR to be added to this list.
## Usage
This example reads the schema from a postgres database, defines an empty schema (which should be filled in),
and then computes the migration to apply to the database.```rust
use sqlmo_sqlx::FromPostgres;#[tokio::main]
async fn main() {
let url = std::env::var("DATABASE_URL").unwrap();
let mut conn = sqlx::postgres::PgConnection::connect(&url).await?;
let current = Schema::try_from_postgres(&mut conn, schema_name).await?;
let end_state = Schema::default(); // Load your end-state by manually defining it, or building it from another source
let migration = current.migrate_to(end_state, &sqlmo::Options::default());
for statement in migration.statements {
let statement = statement.to_sql(Dialect::Postgres);
println!("{}", statement);
}
}
```# Roadmap
- [ ] When calculating migrations, create commented out lines for column deletion
- [ ] ? When calculating migrations, do alter column by calculating word distance between column names