0.3.0 is the final version of this fork, the original has development again and has feature parity
This is a fork of Wulf's dsync with some Pull Requests merged, see Difference with original
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.
Given the following schema:
// schema.rs
diesel::table! {
todos (id) {
id -> Int4,
text -> Text,
completed -> Bool,
}
}We run:
cargo dsync -i schema.rs -o modelsNow we have everything we need!
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 updated_todo = todos::update(&mut db, created_todo.id, UpdateTodo {
text: created_todo.text,
completed: true,
}).await?;
}For more examples, look into the test/ folder, where in test.sh the options used are listed, schema.rs is the diesel schema and all other files are output from dsync
-
Add this crate:
cargo add libdsync-hasezoey
-
Create a new binary in your project which uses the crate (for example,
bin/dsync.rs)use std::{collections::HashMap, path::PathBuf}; use dsync_hasezoey::{GenerationConfig, TableOptions}; pub fn main() { let dir = env!("CARGO_MANIFEST_DIR"); dsync_hasezoey::generate_files( PathBuf::from_iter([dir, "src/schema.rs"]), PathBuf::from_iter([dir, "src/models"]), GenerationConfig { /* ... your generation options ... */ } ); }
-
Create a
Cargo.tomlbinary entry:[[bin]] name = "dsync" path = "bin/dsync.rs"
-
Execute!
cargo run --bin dsyncProtip: to use cargo dsync, create an alias in .cargo/config:
[alias]
dsync="run --bin dsync"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-hasezoey-i: input argument: path to schema file-o: output argument: path to directory where generated code should be written-c: connection type (for example:diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>)-g: (optional, repeatable) list of columns that are automatically generated by create/update triggers (for example,created_at,updated_at)--tsync: (optional) adds#[tsync]attribute to generated structs (see https://github.com/Wulf/tsync)--no-serde: (optional) if set, does not output any serde related code--model-path: (optional) set a custom model import path, defaultcrate::models::--schema-path: (optional) set a custom schema import path, defaultcrate::schema::--only-necessary-derives: (optional) set to only generate the base necessary diesel derives for a struct--once-common-structs: (optional) if set, common structs likePaginationResultare only generated once--single-model-file: (optional) if set, only generate a singletable.rsfile instead of atable/directory withmod.rsandgenerated.rs--file-mode: (optional, defaultoverwrite) set which file mode to useoverwrite: Overwrite original if exists and has dsync file signaturenewfile: If changes to the original file would be done, create a.dsyncnew.rsfile insteadnone: Do not change the file and do not create a new file (still lists the file if changes would be done)
--read-only-prefix: (optional, repeatable) table prefixes to treat as read-only tables (likeview_)--no-impls: (optional) set to disable generatingimpl Struct(only generate the structs)--lessen-conflicts: (optional) set to lessen conflicts with diesel types--create-str: (optional) set to haveCreate*structs be generated with&'a strinstead ofString
Notes:
- any other
file-modethannewfilewill check that the file is a dsync-managed file - if
--once-common-structsis used, then when a table namedcommonis found, a error it thrown - if
--no-implsis used without--once-common-structs, noPaginationResultstruct is generated - if
--no-implsand--once-common-structsare used,PaginationResultis generated intocommon.rs
./test/readme_cli_base_example:
$ dsync -i src/schema.rs -o src/models -c "diesel::SqliteConnection"
Modified models/todos/generated.rs
Modified models/todos/mod.rs
Modified models/mod.rs
Modified 3 files
$ find . -xdev -type f
./src/models/mod.rs
./src/models/todos/mod.rs
./src/models/todos/generated.rs
./src/schema.rs./test/readme_cli_advanced_example:
$ dsync -i src/schema.rs -o src/models -c "diesel::SqliteConnection" --no-serde --only-necessary-derives --once-common-structs --single-model-file -g updated_at -g created_at
Modified models/todos.rs
Modified models/common.rs
Modified models/mod.rs
Modified 3 files
$ find . -xdev -type f
./src/models/mod.rs
./src/models/todos.rs
./src/models/common.rs
./src/schema.rsSee dsync --help for more information.
Feel free to open tickets for support or feature requests.
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).
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.
This fork has been deprecated as the original project is maintained again and at least on git has feature-parity.
This Fork was made because the original project was not updated anymore since March 2023 and had become incompatible with diesel 2.1.0.
See CHANGELOG.md for all changes since the start of the fork.
If you get a error like the following, then the fix is to add feature returning_clauses_for_sqlite_3_35 to the diesel dependency
75 | .get_result(db)
| ---------- ^^ the trait `QueryFragment<Sqlite, DoesNotSupportReturningClause>` is not implemented for `ReturningClause<(sql_schema::media_types::columns::_id, sql_schema::media_types::columns::name)>`
| |
| required by a bound introduced by this call