No description
  • Rust 98.4%
  • Nix 1.6%
Find a file
DestinyofYeet 7276fddbb5
All checks were successful
Build / build (pull_request) Successful in 1m36s
Check / check_commit_format (pull_request) Successful in 13s
fix(readme): null -> none
2026-06-16 23:26:49 +02:00
.forgejo/workflows feat(workflows): adds build and check workflow 2026-06-02 19:36:28 +02:00
django-rs-macro chore: fmt 2026-06-15 12:32:54 +02:00
examples feat(rewrite): Rewrites Tasks to get results 2026-06-16 15:49:45 +02:00
nix feat(readme): adds basic readme 2026-06-16 16:21:24 +02:00
src feat(readme): adds basic readme 2026-06-16 16:21:24 +02:00
.envrc chore: init 2026-05-01 20:49:01 +02:00
.gitignore chore(gitignore): removes result 2026-06-14 23:19:15 +02:00
Cargo.lock feat(tests): basic sqlite tests 2026-06-15 14:17:16 +02:00
Cargo.toml chore(tempfile): update tempfile 2026-06-16 22:52:13 +02:00
flake.lock chore: init 2026-05-01 20:49:01 +02:00
flake.nix feat(workflows): adds build and check workflow 2026-06-02 19:36:28 +02:00
LICENSE feat(workflows): adds build and check workflow 2026-06-02 19:36:28 +02:00
README.md fix(readme): null -> none 2026-06-16 23:26:49 +02:00

Django-rs

I wanted to build something like the python framework Django

How to use?

Take this struct for example


pub struct MyStruct {
  name: String,
  value: i32
}

To use this in django-rs, you need to derive and implement a few things.


#[derive(FromIter, SaveData)]
pub struct MyStruct {
  id: Option<i64>,

  name: String,
  value: i32
}

impl Model for MyStruct {
  // ...
}
  

A new id field has appeared. This id field controls wether the struct should be inserted or updated in the database. When creating a new instance of MyStruct set it to None. All MyStructs from the Database have the id field set.

impl Model for MyStruct {
    // This is the name of the table that gets created
    const TABLE_NAME: &'static str = "MyStructs";

    // This is the migration path of the Model
    fn get_migration() -> &'static Vec<ModelMigration> {
        static MIGRATION: LazyLock<Vec<ModelMigration>> = LazyLock::new(|| {
            vec![ModelMigration::new(
                // This is the ordering. The framework will step through the Migrations in the sorted order.
                0,
                MigrationKind::Create(vec![
                    // A 'id' Column is currently required. Once I abstract get_migration in some way, this shouldn't be required
                    CreateColumn::new(
                        "id",
                        ColumnType::Integer,
                        CreateOptions::default().set_primary_key(),
                    ),

                    CreateColumn::new(
                        "name",
                        ColumnType::String,
                        CreateOptions::default().set_non_nullable().set_unique(),
                    ),

                    CreateColumn::new(
                      "value",
                      ColumnType::Integer,
                      CreateOptions::default().set_non_nullable()
                    )
                ]),
            )]
        });

        &MIGRATION
    }

    // This is the real check if it gets inserted or updated
    fn get_id(&self) -> Option<i64> {
        self.id
    }

    fn set_id(&mut self, id: i64) {
        self.id = Some(id);
    }
}

In your main function you can then initialise the server. In the future I want to implement a PostgresStrategy and some other LoggingStrategy.

pub fn main() {
  let server = DjangoServer::new(8, TracingStrategy {}, SqliteStrategy::new("somePath.db"))?;
  let db = server.get_database()

  db.migrate_model::<MyStruct>().unwrap();

  let mut my_struct = MyStruct {
    id: None,
    name: "some_name".to_string(),
    value: 1337
  };

  // This will set the 'id' field
  db.save_model(&db.get_connection(), &mut my_struct).unwrap();

  let my_retrieved_struct: MyStruct = db.search_single_model::<MyStruct>(
      &db.get_connection(),
      SearchQuery::empty()
        // This searches for id = {my_struct.id}
        .add_constraint(("id", my_struct.id.unwrap()))
  )
  // this returns a Result<Option<MyStruct>, DatabaseStrategyError>
  .unwrap()
  .unwrap();

  assert_eq!(my_retrieved_struct, my_struct);
}