Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/j03-dev/rusql-alchemy
Django ORM Like for Rust
https://github.com/j03-dev/rusql-alchemy
django-orm easy-to-use orm rust turso
Last synced: about 2 months ago
JSON representation
Django ORM Like for Rust
- Host: GitHub
- URL: https://github.com/j03-dev/rusql-alchemy
- Owner: j03-dev
- Created: 2024-05-31T11:34:13.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-08-22T10:33:41.000Z (5 months ago)
- Last Synced: 2024-08-22T22:19:33.610Z (5 months ago)
- Topics: django-orm, easy-to-use, orm, rust, turso
- Language: Rust
- Homepage:
- Size: 238 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Rusql Alchemy
## Rusql Alchemy is Django ORM like library for Rust
### Why ?
Just for fun! XD
## Sqlite
### Setup `.env` file
```bash
DATABASE_URL=sqlite://
```
### Setup `Cargo.toml`
```toml
[dependencies.rusql-alchemy]
git = "https://github.com/j03-dev/rusql-alchemy"
branch= "main"
features = ["sqlite"] # the default features is sqlite
```
### Model
```rust
use rusql_alchemy::prelude::*;#[derive(Debug, Model, FromRow)]
struct User {
#[model(primary_key=true, auto=true, null=false)]
id: Integer,
#[model(unique=true, null=false)]
name: String,
#[model(null=false)]
age: Integer,
#[model(default="user")]
role: String
}
```
## Postgres### Setup `.env` file
``` bash
DATABASE_URL=postgres://:@/
```### Setup `Cargo.toml`
```toml
[dependencies.rusql-alchemy]
git = "https://github.com/j03-dev/rusql-alchemy"
branch="main"
default-features = false
features = ["postgres"]
```
### Model: In postgres primary key should be `Serial` type
```rust
use rusql_alchemy::prelude::*;#[derive(Debug, Model, FromRow)]
struct User_ {
#[model(primary_key=true)]
id: Serial,
#[model(unique=true, null=false)]
name: String,
#[model(null=false)]
age: Integer,
#[model(default="user")]
role: String
}
```## Migrate
```rust
use rusql_alchemy::prelude::*;#[tokio::main]
async fn main() {
let conn = Database::new().await.conn;
migrate([Use], &conn);
}
```
## Query### Insert
```rust
use rusql_alchemy::prelude::*;#[tokio::main]
async fn main() {
let conn = Database::new().await.conn;User_ {
name: "[email protected]".to_string(),
email: "[email protected]".to_string(),
password: "p455w0rd".to_string(),
age: 18,
weight: 60.0,
..Default::default()
}
.save(&conn)
.await;let users = User_::all(&conn).await;
println!("{:#?}", users);User_::create(
kwargs!(
name = "joe",
email = "[email protected]",
password = "strongpassword",
age = 19,
weight = 80.1
),
&conn,
)
.await;
}
```
### Select
```rust
use rusql_alchemy::prelude::*;#[tokio::main]
async fn main() {
let conn = config::db::Database::new().await.conn;let users = User_::all(&conn).await;
println!("{:#?}", users);let user = User_::get(
kwargs!(email == "[email protected]").and(kwargs!(password == "strongpassword")),
&conn,
).await;
println!("{:#?}", user);let users = User_::filter(kwargs!(age <= 18), &conn).await;
println!("{:#?}", users);
}
```
### Update
```rust
use rusql_alchemy::prelude::*;#[tokio::main]
async fn main() {
let conn = Database::new().await.conn;if let Some(mut user) = User_::get(
kwargs!(email == "[email protected]").and(kwargs!(password == "strongpassword")),
&conn,
)
.await
{
user.role = "admin".into();
user.update(&conn).await;
}
}
```
### Delete
```rust
use rusql_alchemy::prelude::*;#[tokio::main]
async fn main() {
let conn = Database::new().await.conn;if let Some(user) = User_::get(kwargs!(role == "admin"), &conn).await {
user.delete(&conn).await; // delete one
}
let users = User_::all(&conn).await;
users.delete(&conn).await; // delete all
}
```