Brendonovich / prisma-client-rust

Type-safe database access for Rust
https://prisma.brendonovich.dev
Apache License 2.0
1.83k stars 108 forks source link

Use nodejs for running the CLI #276

Open Brendonovich opened 1 year ago

Brendonovich commented 1 year ago

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

bangbaew commented 1 year ago

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.

Brendonovich commented 1 year ago

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 πŸ˜…

bangbaew commented 1 year ago

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?

Brendonovich commented 1 year ago

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.

satodaiki commented 1 year ago

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

Brendonovich commented 1 year ago

+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.

satodaiki commented 1 year ago

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?

maxmousse commented 8 months ago

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

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)

Brendonovich commented 8 months ago

@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.

maxmousse commented 8 months ago

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: