Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nestjsplus/dyn-schematics

NestJS Dynamic Module Schematics for @nestjs/cli
https://github.com/nestjsplus/dyn-schematics

Last synced: about 2 months ago
JSON representation

NestJS Dynamic Module Schematics for @nestjs/cli

Awesome Lists containing this project

README

        



Nest Logo

NestJS CLI Schematics for Dynamic Module Generation



License
npm version
Built with NestJS

### Overview

This package contains a set of [NestJS CLI](https://docs.nestjs.com/cli/overview) schematics for generating modules that can be dynamically configured. The concepts behind the generated code is covered in detail [in this article](https://dev.to/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370).

See [below](#dynamic-package-generation) for a description of the use cases.

### Installation

Install the package **globally** as shown below. Due to the implementation of the NestJS CLI, the package **must** be global.

```bash
npm install -g @nestjsplus/dyn-schematics
```

### Dynamic Package Generation

Currently, there are two use cases supported, each with a corresponding schematic:

1. Generating a complete, new standalone NestJS *package* containing a dynamic module. Such a package can easily be installed locally with npm, or packaged and distributed via the [npm registry](https://npmjs.com), or a private registry. In other words, use this to build a re-usable library.
2. Adding a generated dynamic module *to an existing NestJS project*. In this case, the module is created in its own folder, and is wired in to the existing project using appropriate module metadata, includes, etc. This schematic works much like Nest's built-in `module` schematic, but creates a fully implemented *dynamic module*.

### Nest CLI

These schematics are built on top of the [Nest CLI](https://docs.nestjs.com/cli/usages) infrastructure, so their usage is exactly as documented on that page.

> Note: since these schematics are built on top of the Nest CLI, all of the optional arguments (such as specifying an optional path in which to generate the code) and options (such as `--dry-run`, `--flat`) are available. Currently, however, the schematics do not generate spec files.

> Note: I'm working on schematics to add new components to an existing dynamic module, such as additional *options providers*. This should be coming soon.

### Use-case #1: Generating a standalone package

The following step will create a new folder using ``, which will contain the standalone package files and folders for your new dynamic module package.

#### Use the `dynpkg` schematic

```bash
nest g -c @nestjsplus/dyn-schematics dynpkg
```

- `dynpkg` is the name of the schematic used to generate a new standalone package containing a dynamic module.
- `` is the name of the new package you're building.

The schematic will prompt you asking whether to create a *test client*. If you answer yes, it will add a small module, which you can later easily remove, to test out the newly generated schematic. I recommend you choose `yes` when first testing out the schematic.

Move to the sub-folder just created:

```bash
cd
```
- where `pkg-name` is the name you supplied in the original `nest g` command above.

Install the dependencies for the generated package:

```bash
npm install
```

#### Verify generated package

If you answered `yes` to the prompt `Generate a testing client?`, a small testing module was automatically generated called `ClientModule`. You can test that the template was properly generated by running:

```bash
npm run start:dev
```

Then browse to [http://localhost:3000](http://localhost:3000). Your browser should display `Hello from Module!`.

#### Optionally publish package

The `package.json` and `tsconfig.json` files are generated according to the process described [in this article](https://dev.to/nestjs/publishing-nestjs-packages-with-npm-21fm). This means that publishing the package to npm is as simple as:
1. updating the `package.json` with your author information, etc.
2. running `npm publish`

See [the npm packaging](https://dev.to/nestjs/publishing-nestjs-packages-with-npm-21fm) article for more information.

### Use-case #2: Adding a dynamic module to an existing project

Make sure you're in the project root folder, just as you would be if running something like `nest g controller myController`.

#### Use the `dnymod` schematic

```bash
nest g -c @nestjsplus/dyn-schematics dynmod
```

- `dynmod` is the name of the schematic used to generate a new dynamic module (which will be added to your existing project).
- `` is the name of the new module you're building.

The schematic adds the new module in a folder named ``. Just like using the built-in module schematic (e.g., `nest g module myNewModule`), this will add the generated module to the imports list of your root module with the appropriate metadata and includes. At this point, you can customize the generated module as needed.

### Customizing

The files in the project have comments that should help guide you.

#### More help

You can also refer to these articles:

[How to build completely dynamic NestJS mdoules](https://dev.to/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370) - for details on the concepts behind the *dynamic module pattern*.

[Built a NestJS module for Knex.js in 5 minutes](https://dev.to/nestjs/build-a-nestjs-module-for-knex-js-or-other-resource-based-libraries-in-5-minutes-12an) - an end-to-end tutorial on using these schematics.

### Change Log

See [Changelog](CHANGELOG.md) for more information.

### Contributing

Contributions welcome! See [Contributing](CONTRIBUTING.md).

### Author

**John Biundo (Y Prospect on [Discord](https://discord.gg/G7Qnnhy))**

### License

Licensed under the MIT License - see the [LICENSE](LICENSE) file for details.