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.

Optionally add imports by each command

See original GitHub issue

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

Currently lib doesn’t support command wise module loading.

suppose I have two commands

  1. seed
  2. cache:clear

Now the both commands have different requirement like seed will connect to DB and cache:clear will connect to redis

So If we run cache:clear when app will boot up it will connect to the database which is kind of not needed. Currently this is the blocker for me.

Describe the solution you’d like

To achieve this i am currently doing, However it is not perfect but can be a good starting point:-


@Module({})
export class XYZConsoleModule {
  static register(options: CLIOption): DynamicModule {
    return {
      module: XYZConsoleModule,
      imports: [...options.imports],
      providers: [...options.commands],
      exports: [],
    };
  }
}

async function bootstrap(options: CLIOption) {
  return NestFactory.createApplicationContext(
    XYZConsoleModule.register(options),
    {
      logger: false,
    },
  );
}
async function main() {
  const program = new Command();
  program
    .description('An ClI tool')
    .command('seed')
    .description('Seeds the database')
    .action(async () => {
      await bootstrap({
        imports: [
          SystemModule,
          TypeOrmModule.forFeature([Step, Badge, Trophy]),
        ],
        commands: [SeedCommand],
      });
    });
  program
    .command('cache:clear')
    .description('Clears the redis cache')
    .action(async () => {
      await bootstrap({
        imports: [SystemConfigModule, CacheManagerModule],
        commands: [CacheCommand],
      });
    });
  await program.parseAsync(process.argv);
  process.exit(0);
}
main().catch((e) => {
  console.error(e);
  process.exit(1);
});

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

This will optimise the boot time of base application and we are not loading any imports which are not related to each other.

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
jmcdo29commented, Jan 7, 2022

I don’t think it’s a huge “issue” per se, but it should be possible to look into Nest’s lazy loading feature and possibly speed the commands up. But I do agree that the database should already be up and running, or the need for the database should be abstracted away from the commands themselves. Multiple command setups could alleviate the issue too. I’m just looking at a different angle here

0reactions
jmcdo29commented, Jan 8, 2022

Okay, after reading through the docs on lazy loading again, I think I can figure out a good way to write up a lazy loading example for the nest-commander docs.

@rakesh-avegenXpear I don’t think this is something that will be able to be baked into the package, but if you can provide a minimum repo with the multi-database-module setup you’ve got I can work on the lazy loading idea I have to proof of concept it with a real use case.

I think it would be too complex to figure out how to set up the configuration for nest-commander to take care of the lazy references, just figuring out how to hook up the lazy command to the service via config only, but I could be wrong and playing around with an example may give me a clear thought of how to do this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I import a module dynamically given its name as ...
The name argument specifies what module to import in absolute or relative terms (e.g. either pkg.mod or ..mod ). If the name is...
Read more >
ES2020: `import()` – dynamically importing ES modules - 2ality
The proposal enables dynamic module imports​​ The proposed operator for loading modules dynamically works as follows: const moduleSpecifier = './ ...
Read more >
Dynamic imports - The Modern JavaScript Tutorial
The import(module) expression loads the module and returns a promise that resolves into a module object that contains all its exports. It can...
Read more >
Dynamically Importing Components with React.lazy
In React, dynamically importing a component is easy—you invoke React. lazy with the standard dynamic import syntax and specify a fallback UI.
Read more >
How to Dynamically Import ECMAScript Modules
You can import modules dynamically if you use import as a function — import(pathToModule) — a feature available starting ES2020.
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