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.

[Question] Possible to initialize with configuration (or any other injected service) and how are environment variables parsed?

See original GitHub issue

As far as I can tell, there are two options to initialize Drivine:

  1. From environment variables
  2. From values set using a fluent API

From this, I get two questions:

  1. How are environment variables parsed? In the docs, it shows the method DatabaseRegistry.buildOrResolveFromEnv('NEO'). What is 'NEO' in this case? EDIT:
    After digging through the Drivine code, I found that the environment variables are hardcoded and prefixed with the name given (NEO, in this case), resulting in environment variables being forced to take the name NAME_DATABASE_TYPE. Using a configuration package which uses environment variables could conflict with this naming (as it does in my case).
    Specifying how buildOrResolveFromEnv works in the docs might clear things up a bit. I can submit a PR later if you would like me to.

  2. Is there a way to inject configuration (or anything in Nest’s DI tree) into the module initialization?
    Nest provides dynamic modules, being able to use DI. Is there a way to use that with Drivine as well?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jasperbluescommented, Aug 11, 2022

@Bastianowicz What you can do in the meantime is inject the factory instead, like this:

    private persistenceManager: PersistenceManager

    constructor(factory: PersistenceManagerFactory) {
        this.persistenceManager = factory.get(myLateRegisteredDatabase)
    }

I reproduced the issue you folks are having on a current project. It occurs when:

  • Using the builder pattern (not resolve from env) to register a DB on startup

And above is how I worked around that for now.

1reaction
NilsMollercommented, May 27, 2022

Thank you for the quick response. I was updating my question as you were typing 😃

It uses DotEnv (popular config approach) behind the scenes, so whatever works in DotEnv will work. (From memory I think env vars do, right?)

Indeed it does use env vars. As I mentioned in the updated question, ConnectionProperties#ConnectionPropertiesFromEnv uses process.env already, so both approaches will work just fine.

You mean to bootstrap Drivine dynamically, or declare connection properties at Runtime? Yeah its possible using the fluent API. Drivine is not that large, so its fairly easy to declare the bits and pieces manually and plug them together. Its also easy to register connections/databases (new ones) on the fly.

Yes, to bootstrap it dynamically. For example, Nest’s GraphQL library uses a factory to allow for DI to work, simply by injecting anything into the factory method. This can inject, for example, a configuration service. A similar approach could be taken here:

DatabaseRegistry.getInstance()
  .builder()
  .withType(DatabaseType.NEO4J)
  .protocol(config.db.protocol)
  .host(config.db.host)
  .port(config.db.port)
  .userName(config.db.username)
  .password(config.db.password)
  .register(config.db.dbName)

Where config would be injected.
I mention doing it this way, as my configuration package has separators to parse environment variables to config classes, making it incompatible with Drivine’s way of using environment variables.
I’m not sure how to approach this. Perhaps you could point me in the right direction?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Environment Variables: What They Are and How To Use Them
One of the most common uses of a system environment variable is setting up a PATH variable to a global package/library to be...
Read more >
How to set environment variables in Jenkins? - Stack Overflow
Go to your job Configure screen · Find Add build step in Build section and select Inject environment variables · Set the desired...
Read more >
Using Environment Variables in Node.js for App Configuration ...
Environment variables are considered the best way to configure applications, with the main benefits being: Secrets such as database credentials ...
Read more >
Node Environment Variables: Process env Node
The process.env is a global variable injected at runtime by your Node.js application to depict the state of the environment your app is...
Read more >
24. Externalized Configuration - Spring
You can use properties files, YAML files, environment variables, ... Property values can be injected directly into your beans by using the @Value...
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