Prisma Client Rust's installation operates in a different way to most Rust projects.
prisma-client-rust
is used by the generated client and possibly own code, as it provides access to Prisma internals and helper functions.
prisma-client-rust-cli
contains the code generation and access to the Prisma CLI, but does not provide an executable binary - this must be created yourself. why?
First, the main library and CLI package must be added to your project's Cargo.toml:
[dependencies]
prisma-client-rust = { git = "https://github.com/Brendonovich/prisma-client-rust", tag = "0.4.1" }
prisma-client-rust-cli = { git = "https://github.com/Brendonovich/prisma-client-rust", tag = "0.4.1" }
The easiest way to create a binary to access the CLI through is by creating a src/bin
folder if you don't already have one, and inside it creating a file called something like prisma.rs
(This will determine the name of your binary). Inside this file insert the following:
fn main() {
prisma_client_rust_cli::run();
}
Technically, this is all that is required! Just run the following to access the CLI:
$ cargo run --bin <your binary name> -- <command>
This isn't a very friendly command to run, though. Luckily Cargo allows us to define project-wide aliases for running commands! Create a folder at the root of your project called .cargo
and inside it create a file config.toml
with the following contents:
[alias]
prisma = "run --bin <your binary name> --"
Now you can run cargo prisma <command>
anywhere in your project to access the CLI!
This approach has a problem, though: prisma-client-rust-cli
is included as a dependency in your library/application, which is likely not desirable. The solution to this is to move the CLI binary to a separate package and configure your project to use Cargo workspaces. Below is a sample project structure that has one binary target in src/main.rs
, and a separate package for the CLI named prisma-cli
, which is included in the workspace members of Cargo.toml
.
Cargo.toml
.cargo/
config.toml
src/
main.rs
prisma-cli/
Cargo.toml
src/
main.rs
For the above example, Cargo.toml
would include prisma-client-rust
as a dependency as it is required by the generated file, whereas prisma-cli/Cargo.toml
would include prisma-client-rust-cli
as a dependency, and so the binary in src/main.rs
would not be bundled with all the CLI code, only the required library code.
In older versions of Prisma Client Rust, it was possible to cargo install prisma-client-rust-cli
and have a global install of the CLI available to use at any time. This had a major problem though: Versioning. Managing multiple projects that used different versions of Prisma Client Rust got very annoying very quickly, plus it went against the recommmended installation instructions of Prisma Client JS, Go, and Python.
Unlike these three languages, Rust (or more specifically Cargo) does not provide a method for executing binaries available inside dependencies. Since installing a globally available binary was ruled out, providing the CLI as a library was seen as the only other option, plus personally I think that being able to run cargo prisma <command>
is quite a nice experience and matches with clients in other languages.
Next, learn how to setup your schema to generate the client.