question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enabling Node-API libraries: Remove `nApi` preview feature flag and make Node-API libraries the new default, provide escape hatch to old behavior

See original GitHub issue

We added the preview feature flag nApi in release 2.20.0 to let users try out the new Node-API library that could potentially replace the existing Rust binary. Results were promising, and the few bugs or regressions we found we could fortunately fix, so we are moving forward with figuring out how to enable this library for all users by default.

But we also need to give everyone an option to opt-out of the new behavior and keep using the existing binary as it is in production right now if they need it. Reasons for that can be special use cases, unwanted new or changed behavior, undetected bugs on our side and so on.

Enabling Node-API library by default

During the Preview phase of the functionality, users could opt in to the feature by either adding the nApi preview feature flag to the previewFeatures list of the generator block of Prisma Client, or by setting an environment variable PRISMA_FORCE_NAPI=true in their system environment to also enable the Node-API library for Prisma Client.

Now when a user just installs and uses Prisma, they directly get the Node-API library instead of the binary everywhere. The preview feature flag nApi and existing environment variable PRSMA_FORCE_NAPI are removed and not supported anymore.

The escape hatch back to the binary

So with the Node-API library enabled, everyone will get the Node-API library for both Prisma Client and Prisma CLI by default. But as laid out above, we need an escape hatch that allows to switch back to the binary as well. This is not trivial:

  • To understand if a preview feature flag in the Prisma schema file is enabled for the CLI to download the correct engine file, we actually already need an engine that can parse the schema and return a representation of it that includes “nApi is one of the enabled preview features”.
  • This can be necessary as early as when the user runs npm install, as Prisma triggers a postinstall hook that runs prisma generate to generate a Prisma Client ready to be used as early as possible.
  • This is a chicken or egg problem, that we got around in the preview phase by only using the Node-API library in the CLI when the environment variable was set (and not via the preview feature flag in the Prisma Schema). This is confusing and hard to document. For the escape hatch we need to do better here.
  • The escape hatch also has to be present in the generated Client, which might not have access to many of the places that could be used for this configuration (A generated Prisma Client could be distributed via Npm, where only the @prisma/client and .prisma/client are included, or just copied between projects that do not have any of the other Prisma files present).

This means the configuration for the escape hatch has to be considered carefully.

Options

There are multiple options that we can consider (ordered by how “fixed” they are): (Note that we make a suggestion on which of these options should be implemented below)

Client and CLI

  1. Environment variable - An environment variable is a simple global configuration option that can be read from both CLI and Client. It can be different per system though, and does not have the best developer experience as setting them is different per deployment platform, operating system and so on.

  2. PrismaClient constructor - The constructor PrismaClient in the code when using Prisma Client is used by us as a place to override the datasource’s url property on runtime. It by definition only covers the client, after everything (both CLI and Client) were already installed and generated.

  3. schema.prisma file - The Prisma Schema file is another place where we can put configuration, like the previewFeatures list in the generator block of Prisma Clients.

    It has a few disadvantages:

    • There is no top level configuration in Prisma Schema yet - only blocks like generator, datasource or model etc. can take properties. This configuration for the escape hatch does not make much sense scoped to any of these, but would need to be global.
    • We do not actually have a parser for the schema file CLI or Client side - only via talking to an existing engine binary or library they can actually find out about the configuration included in that file.
  4. package.json file - The project’s package.json file is already used to be able to customize the location of the schema.prisma file via a schema entry in the prisma object, both for the CLI and Client. The same location and functionality can be used for the escape hatch:

    "prisma": {
      "foo": "bar"
    }
    
  5. Configuration file for Prisma - If there was an explicit configuration file for Prisma outside of schema.prisma or package.json, that could also be used to adopt this configuration. But of course, it adds a lot of additional complexity that is hopefully not necessary in the context of this project.

Generated Prisma Client

  • For the generated Client (which includes the TypeScript types) there is only 1 option: Similar to how it includes a copy of the schema.prisma file (.prisma/client/schema.prisma), it also needs to include the buildtime (when prisma generate is run) value of the escape hatch. The obvious choice is a property in the Prisma Client generator: queryEngineType = 'napi' | 'binary'. To be able to override this on runtime, the generated Client will also listen to the environment variable described above.
    • Note: This is not a generator property to be documented publicly

Suggestions

We suggest as the escape hatch we implement options 1) Environment variable and option 4) package.jsonprisma object:

  • In package.json:

    "prisma": {
      "queryEngineType": "binary" // (vs. default `node-api`)
    }
    
  • Environment variable: PRISMA_QUERY_ENGINE_TYPE=binary (vs. default node-api)

    • As usual the environment variable can be used to override any other choice made via other configuration options.

We chose these as they are available, proven to work, and fulfill the demands we have for the escape hatch configuration. (Options 2 and 3 are to late in the process, and option 5 is far to big to introduce for this configuration.)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
pantharshit00commented, Aug 24, 2021
1reaction
pantharshit00commented, Aug 24, 2021

@kripod Nope, nApi hasn’t left preview yet. That might happen next release but we still have some work remaining to do.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's new in Prisma? (Q3/2021)
If you've been using this preview feature, you can remove the nApi flag from previewFeatures in your Prisma Schema. Learn more about the...
Read more >
Using feature flags with Unleash and Node.js - LogRocket Blog
A feature flag is a variable that you can switch at any moment and the change will reflect in the program without the...
Read more >
Get Started with Feature Flags in Node - Split Blog
Develop a small application programming interface with Node.js and Express, and deploy feature flags to toggle functionality.
Read more >
@prisma/cli: Versions | Openbase
Node-API is a new technique for binding Prisma's Rust-based query engine directly to Prisma ... You can enable this feature with the napi...
Read more >
crates.io/crates - SJTUG Mirror Index
ac-library-rs-expander/ · ac190eedea3d478386ee2335906a0ecbeed9459b5e2a22fd7356e66f2/ ... actix-default-responder/ ... allow-me/.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found