A utility to generate structs and query functions from diesel schema files. Primarily built for create-rust-app.
Given a diesel schema file like test/simple_schema/schema.rs
, we run:
cd test/simple_schema
cargo dsync -i schema.rs -o models
Then, all code in test/simple_schema/models
is generated.
Add this crate:
cargo add dsync
Create a new binary in your project which uses the crate (for example, bin/dsync.rs
)
``` 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::fromiter([dir, "src/models"]), GenerationConfig { defaulttableoptions: TableOptions { ignore: false, autogeneratedcolumns: vec!["createdat", "updatedat"] }, tableoptions: HashMap::from([ // EXAMPLE: don't generate code for the usersesions table ("usersessions", TableOptions { ignore: true, autogeneratedcolumns: vec![] }), ]) });
} ```
Create a Cargo.toml
binary entry:
[[bin]]
name = "dsync"
path = "bin/dsync.rs"
Execute!
cargo run --bin dsync
Protip: 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
CLI Usage
-i
: input argument: path to schema file-o
: output argument: path to directory where generated code should be written-g
: (optional) 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)sh
dsync -i src/schema.rs -o src/models
See 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.