dsync

License: MIT OR Apache-2.0

A utility to generate database structs and querying code from diesel schema files. Primarily built for create-rust-app.

Currently, it's more advantageous to generate code over deriving code with macros because intellisense and autocompletion isn't quite there when it comes to macro expansion.

Demo

Given the following schema:

rust // schema.rs diesel::table! { todos (id) { id -> Int4, text -> Text, completed -> Bool, } }

We run:

sh cargo dsync -i schema.rs -o models

Now we have everything we need!

```rust use models::todos;

async fn demo(db: Connection) { let created_todo = todos::create(&mut db, todos::CreateTodo { text: "Create a demo", completed: false, }).await?;

let todos_list = todos::paginate(&mut db, 1, 10).await?;

let updatedtodo = todos::update(&mut db, createdtodo.id, UpdateTodo { text: created_todo.text, completed: true, }).await?; } ```

For a complete example, see test/simple_table/schema.rs which generates all the code in test/simple_schema/models.

Usage

  1. Add this crate:

    sh cargo add dsync

  2. Create a new binary in your project which uses the crate (for example, bin/dsync.rs)

    ```rust use std::{collections::HashMap, path::PathBuf}; use dsync::{GenerationConfig, TableOptions};

    pub fn main() { let dir = env!("CARGOMANIFESTDIR");

    dsync::generatefiles( PathBuf::fromiter([dir, "src/schema.rs"]), PathBuf::from_iter([dir, "src/models"]), GenerationConfig { /* ... your generation options ... */ } ); } ```

  3. Create a Cargo.toml binary entry:

    toml [[bin]] name = "dsync" path = "bin/dsync.rs"

  4. Execute!

    sh cargo run --bin dsync

    Protip: to use cargo dsync, create an alias in .cargo/config:

    toml [alias] dsync="run --bin dsync"

Pre-built binary

Setting up a custom binary allows you to completely customize the generation; however, if complete customization isn't necessary, you can install the CLI directly (you'll have to make sure you keep it up-to-date by running this periodically):

cargo install dsync

CLI Usage

sh dsync -i src/schema.rs -o src/models

Docs

See dsync --help for more information.

Feel free to open tickets for support or feature requests.

Development/Testing

Use ./test/test_all.sh to run tests. After running the test, there should be no unexpected changes to files in ./test (use git status and git diff to see if there were any changes).

License

This tool is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details.