prisma-client-rust icon indicating copy to clipboard operation
prisma-client-rust copied to clipboard

Use nodejs for running the CLI

Open Brendonovich opened this issue 3 years ago β€’ 10 comments

Using the pre-packaged CLI is troublesome and annoying, it'd be better to implement this for PCR.

Brendonovich avatar Mar 14 '23 04:03 Brendonovich

Would the total download size significantly increase compared to the native CLI? I'm running rust on docker for development and production, and I'll have to download the cli every time I deploy using Dockerfile with cargo prisma generate in the pipeline, and my cloud provider is not going to cache the layers, will it download the 800mb node binary each time I deploy it?

If it really needs to be changed to original nodejs version of cli, will I be able to run it using Deno? since Deno is very small in size (~100mb) and is very easy to put in a pipeline.

bangbaew avatar Mar 14 '23 06:03 bangbaew

Would the total download size significantly increase compared to the native CLI?

No, it'd likely be about the same, except it'd be up to you how you'd like to install Node - either do it yourself or PCR will do it for you.

will it download the 800mb node binary each time I deploy it?

It'll only download Node if you don't have it installed already. The CLI and engine binaries will be downloaded no matter what.

will I be able to run it using Deno?

No, the Prisma CLI only runs on Node.

my cloud provider is not going to cache the layers

I'd suggest finding a way to do so πŸ˜…

Brendonovich avatar Mar 14 '23 07:03 Brendonovich

my cloud provider is not going to cache the layers

I'd suggest finding a way to do so πŸ˜…

image

I'm using Okteto Cloud, a free managed Kubernetes cluster, sometimes it caches everything and takes only 1 minute for an incremental build, which I consider lucky, but most of the times it starts over at cargo fetch and takes 18 minutes to complete building, that's quite frustrating πŸ˜‚, do you have any advice in decreasing Rust's build time?

bangbaew avatar Mar 14 '23 14:03 bangbaew

most of the times it starts over at cargo fetch and takes 18 minutes to complete building

That's probably because because you're running cargo fetch after adding Cargo.toml and Cargo.lock, which I don't think is something that cargo-chef recommends doing - it kinda bypasses the entire point of cargo-chef in the first place.

Brendonovich avatar Mar 14 '23 14:03 Brendonovich

+1 I crave this feature too. I want to run prisma-dbml-generator, but I can't do it with this client.

satodaiki avatar Jul 31 '23 14:07 satodaiki

+1

Please use reactions on the initial post instead of +1 comments.

I want to run prisma-dbml-generator, but I can't do it with this client.

Have you tried using the npm CLI to run both generators? I'm not 100% it will work but I think I've done it before.

Brendonovich avatar Jul 31 '23 16:07 Brendonovich

Sorry, I didn't give you enough information.

I currently have the following schema.prisma file

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["postgresqlExtensions"]
}

generator dbml {
  provider   = "prisma-dbml-generator"
}

// ...Schema definition is omitted...

If you want to use prisma-rust-client, this prisma file must be rewritten as follows

generator client {
  provider        = "cargo prisma"
  // previewFeatures = ["postgresqlExtensions"]
}

generator dbml {
  provider   = "prisma-dbml-generator"
}

This is only intended to generate Rust clients, so running the command with npx prisma generate would fail.

% yarn prisma generate
yarn run v1.22.15
$ /home/username/Project/study/prisma-client-rust/examples/basic/node_modules/.bin/prisma generate
Prisma schema loaded from prisma/schema.prisma
Error:
Generator "cargo prisma" failed:
warning: skipping duplicate package test-macros found at /home/username/.cargo/git/checkouts/prisma-engines-8e1416dd0e571d11/45b026c/libs/test-macros
warning: skipping duplicate package test-setup found at /home/username/.cargo/git/checkouts/prisma-engines-8e1416dd0e571d11/45b026c/libs/test-setup
warning: ambiguous glob re-exports
  --> crates/sdk/src/lib.rs:34:13
   |
34 |     pub use super::{args::, prisma::, *};
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^-^
   |             |                           |
   |             |                           but the name dmmf in the type namespace is also re-exported here
   |             the name dmmf in the type namespace is first re-exported here
   |
   = note: #[warn(ambiguous_glob_reexports)] on by default
warning: variable does not need to be mutable
   --> crates/sdk/src/args.rs:142:25
    |
142 |                     let mut s = "Json".to_string();
    |                         ----^
    |                         |
    |                         help: remove this mut
    |
    = note: #[warn(unused_mut)] on by default
warning: prisma-client-rust-sdk (lib) generated 2 warnings (run cargo fix --lib -p prisma-client-rust-sdk to apply 1 suggestion)
warning: unused variable: field_type
  --> crates/generator/src/models/include_select.rs:72:13
   |
72 |         let field_type = field.type_tokens(module_path);
   |             ^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: _field_type
   |
   = note: #[warn(unused_variables)] on by default
warning: unused variable: model_name_snake
  --> crates/generator/src/models/mod.rs:87:17
   |
87 |             let model_name_snake = snake_ident(model_name);
   |                 ^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: _model_name_snake
warning: prisma-client-rust-generator (lib) generated 2 warnings (run cargo fix --lib -p prisma-client-rust-generator to apply 2 suggestions)
    Finished dev [unoptimized + debuginfo] target(s) in 2.13s
warning: the following packages contain code that will be rejected by a future version of Rust: bigint v4.4.3
note: to see what the problems were, use the option --future-incompat-report, or run cargo report future-incompatibilities --id 1
     Running /home/username/Project/study/prisma-client-rust/target/debug/prisma
thread 'main' panicked at 'Failed to deserialize DMMF from Prisma engines: Error { path: Path { segments: [Map { key: "generator" }, Map { key: "binaryTargets" }, Seq { index: 0 }] }, original: Error("invalid type: map, expected a string", line: 1, column: 833) }', crates/sdk/src/runtime.rs:62:26
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace
:γƒγ‚§γƒƒγ‚―γƒžγƒΌγ‚―ε€§: Generated DBML Schema to ./prisma/dbml in 8ms
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

So I thought it was necessary to implement this issue in order to use prisma-dbml-generator with prisma-rust-client.

Since the schema.prisma is different for each client, it would be possible to operate both in parallel by using a library such as prisma-import to manage prisma files for each client.

I'm not 100% it will work but I think I've done it before.

Do you have a sample of that code?

satodaiki avatar Aug 01 '23 06:08 satodaiki

Out of curiosity, do you think it would be possible to do something like this:

  • Given an env with node/npm and rust/cargo, install the js prisma cli
  • Create a binary create that run prisma-client-rust-generator::run()
  • In the prisma schema, configure the provider to run this binary
  • Then run the js prisma cli to generate the code (npx prisma generate). It should delegate code generation to the prisma-client-rust package.

It would be an easy solution for projects that already rely on node and rust (example:Β my case is an angular frontend and rust backend that both live in a Nx monorepo)

maxmousse avatar Jan 23 '24 10:01 maxmousse

@maxmousse It's totally possible, and wouldn't actually require any changes for end users. What makes this tricky is that a) I don't want to require users to have node installed, so i'd have to install it locally, and b) I don't want to install the prisma cli as a global package (not hard just annoying). Prisma Client Python has an easier time with this as nodeenv exists, but there's no equivalent for rust.

Brendonovich avatar Jan 23 '24 10:01 Brendonovich

Thanks for your quick response @Brendonovich !

Yeah I understand that users should not need to install node =)

Also, I gave a try to the 'Use the js prisma cli solution'. I encountered the next problems:

  • I was not able to directly use the prisma-client-rust-generator crate as I did not found it in the 0.6.11 taged version. So I directly used prisma-client-rust-cli. Do you think it would make sens to have prisma-client-rust-generator exposed with a binary, in order to be able to call it directly from the schema ? Tell me if you are interested, I'd be glad to help and provide a PR =)
  • When trying to generate with the js prisma cli and the prisma-client-rust-cli generator, I encounterd the next error:
➜  test-prisma-rust git:(master) βœ— npx prisma generate
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Error: 
Generator "RUST_BACKTRACE=1 cargo run" failed:

   Finished dev [unoptimized + debuginfo] target(s) in 0.16s
warning: the following packages contain code that will be rejected by a future version of Rust: connection-string v0.1.13
note: to see what the problems were, use the option `--future-incompat-report`, or run `cargo report future-incompatibilities --id 1`
    Running `target/debug/test-prisma-rust`
thread 'main' panicked at /home/maxim/.cargo/git/checkouts/prisma-client-rust-fa967aa5ad0ec391/3ac68d0/sdk/src/runtime.rs:57:26:
Failed to deserialize DMMF from Prisma engines: Error { path: Path { segments: [Map { key: "generator" }, Map { key: "binaryTargets" }, Seq { index: 0 }] }, original: Error("invalid type: map, expected a string", line: 1, column: 726) }
stack backtrace:
  0: rust_begin_unwind
            at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/panicking.rs:595:5
  1: core::panicking::panic_fmt
            at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/panicking.rs:67:14
  2: core::result::unwrap_failed
            at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/result.rs:1652:5
  3: core::result::Result<T,E>::expect
            at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/result.rs:1034:23
  4: prisma_client_rust_sdk::runtime::GeneratorMetadata::run
            at /home/maxim/.cargo/git/checkouts/prisma-client-rust-fa967aa5ad0ec391/3ac68d0/sdk/src/runtime.rs:56:32
  5: prisma_client_rust_sdk::PrismaGenerator::run
            at /home/maxim/.cargo/git/checkouts/prisma-client-rust-fa967aa5ad0ec391/3ac68d0/sdk/src/lib.rs:94:9
  6: prisma_client_rust_cli::run
            at /home/maxim/.cargo/git/checkouts/prisma-client-rust-fa967aa5ad0ec391/3ac68d0/cli/src/lib.rs:20:5
  7: test_prisma_rust::main
            at ./src/main.rs:2:5
  8: core::ops::function::FnOnce::call_once
            at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

For what I understand, the js cli does not call the generator with the same input as the rust one. If you want, I can provide a repo to reproduce, and again, I'd be glad to help.

maxmousse avatar Jan 23 '24 13:01 maxmousse