@nestjsplus/dyn-schematics
v1.0.12
Published
NestJS Dynamic Module Schematics for @nestjs/cli
Downloads
5,476
Readme
Overview
This package contains a set of NestJS CLI schematics for generating modules that can be dynamically configured. The concepts behind the generated code is covered in detail in this article.
See below 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.
npm install -g @nestjsplus/dyn-schematics
Dynamic Package Generation
Currently, there are two use cases supported, each with a corresponding schematic:
- 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, or a private registry. In other words, use this to build a re-usable library.
- 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 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 <pkg-name>
, which will contain the standalone package files and folders for your new dynamic module package.
Use the dynpkg
schematic
nest g -c @nestjsplus/dyn-schematics dynpkg <pkg-name>
dynpkg
is the name of the schematic used to generate a new standalone package containing a dynamic module.<pkg-name>
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:
cd <pkg-name>
- where
pkg-name
is the name you supplied in the originalnest g
command above.
Install the dependencies for the generated package:
npm install
Verify generated package
If you answered yes
to the prompt Generate a testing client?
, a small testing module was automatically generated called <pkg-name>ClientModule
. You can test that the template was properly generated by running:
npm run start:dev
Then browse to http://localhost:3000. Your browser should display Hello from <pkg-name>Module!
.
Optionally publish package
The package.json
and tsconfig.json
files are generated according to the process described in this article. This means that publishing the package to npm is as simple as:
- updating the
package.json
with your author information, etc. - running
npm publish
See the npm packaging 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
nest g -c @nestjsplus/dyn-schematics dynmod <module-name>
dynmod
is the name of the schematic used to generate a new dynamic module (which will be added to your existing project).<module-name>
is the name of the new module you're building.
The schematic adds the new module in a folder named <module-name>
. 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 - for details on the concepts behind the dynamic module pattern.
Built a NestJS module for Knex.js in 5 minutes - an end-to-end tutorial on using these schematics.
Change Log
See Changelog for more information.
Contributing
Contributions welcome! See Contributing.
Author
John Biundo (Y Prospect on Discord)
License
Licensed under the MIT License - see the LICENSE file for details.