Angular-style schematics for the NestJS framework, enabling generation of application components through a command-line interface.
npx @tessl/cli install tessl/npm-nestjs--schematics@11.0.00
# NestJS Schematics
1
2
NestJS Schematics provides Angular-style code generators for the NestJS framework, enabling developers to quickly scaffold application components, modules, and entire project structures through a command-line interface. Built on Angular DevKit schematics, it offers 20+ generators with TypeScript support, dependency injection setup, and standardized project patterns.
3
4
## Package Information
5
6
- **Package Name**: @nestjs/schematics
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install -g @nestjs/cli` (includes schematics) or `npm install @nestjs/schematics`
10
11
## Core Imports
12
13
For programmatic use:
14
15
```typescript
16
import {
17
MetadataManager,
18
ModuleDeclarator,
19
NameParser,
20
DEFAULT_LANGUAGE,
21
PROJECT_TYPE
22
} from '@nestjs/schematics';
23
```
24
25
Most users interact via the Nest CLI:
26
27
```bash
28
nest generate <schematic> <name> [options]
29
# or
30
ng generate @nestjs/schematics:<schematic> <name> [options]
31
```
32
33
## Basic Usage
34
35
### CLI Usage (Most Common)
36
37
```bash
38
# Generate a new Nest application
39
nest new my-app
40
41
# Generate components
42
nest generate service users
43
nest generate controller auth --no-spec
44
nest generate module products
45
nest generate guard roles
46
47
# Generate resources with CRUD operations
48
nest generate resource posts --type=rest --crud
49
50
# Generate library in monorepo
51
nest generate library shared
52
```
53
54
### Programmatic Usage
55
56
```typescript
57
import { NameParser, ModuleDeclarator } from '@nestjs/schematics';
58
59
// Parse component names and paths
60
const parser = new NameParser();
61
const location = parser.parse({ name: 'user-service', path: 'src/users' });
62
63
// Declare module imports
64
const declarator = new ModuleDeclarator();
65
const updatedContent = declarator.declare(moduleContent, {
66
metadata: 'providers',
67
symbol: 'UserService',
68
type: 'class'
69
});
70
```
71
72
## Architecture
73
74
NestJS Schematics is built around several key components:
75
76
- **Schematic Collection**: 20+ code generators for different NestJS components and patterns
77
- **Utility Library**: Comprehensive set of classes and functions for AST manipulation, module management, and file operations
78
- **Template System**: File templates with variable substitution for consistent code generation
79
- **Angular DevKit Integration**: Built on Angular's schematic framework for robust file operations and tree transformations
80
- **TypeScript AST Support**: Advanced code analysis and modification capabilities
81
82
## Capabilities
83
84
### Schematic Collection
85
86
Complete set of code generators for NestJS applications including controllers, services, modules, guards, pipes, filters, and more. Supports both standalone components and full applications with monorepo structures.
87
88
```typescript { .api }
89
// Core application schematics
90
function application(options: ApplicationOptions): Rule;
91
function module(options: ModuleOptions): Rule;
92
function controller(options: ControllerOptions): Rule;
93
function service(options: ServiceOptions): Rule;
94
95
// Common schematic options pattern
96
interface BaseSchematicOptions {
97
name: string;
98
path?: string | Path;
99
module?: Path;
100
skipImport?: boolean;
101
metadata?: string;
102
language?: string;
103
sourceRoot?: string;
104
spec?: boolean;
105
specFileSuffix?: string;
106
flat?: boolean;
107
}
108
```
109
110
[Schematics Collection](./schematics.md)
111
112
### Utility Library
113
114
Comprehensive toolkit for TypeScript/JavaScript code manipulation, module management, and project structure operations. Includes AST parsers, file system helpers, and NestJS-specific utilities.
115
116
```typescript { .api }
117
class MetadataManager {
118
insert(metadata: string, symbol: string, staticOptions?: DeclarationOptions['staticOptions']): string | undefined;
119
}
120
121
class ModuleDeclarator {
122
declare(content: string, options: DeclarationOptions): string;
123
}
124
125
class NameParser {
126
parse(options: ParseOptions): Location;
127
}
128
129
interface DeclarationOptions {
130
metadata: string;
131
symbol: string;
132
type?: string;
133
staticOptions?: {
134
name: string;
135
value: string;
136
};
137
}
138
```
139
140
[Utility Library](./utilities.md)
141
142
### Default Constants
143
144
Pre-configured default values and project type constants for consistent application generation.
145
146
```typescript { .api }
147
const DEFAULT_LANGUAGE: string;
148
const DEFAULT_VERSION: string;
149
const DEFAULT_PATH_NAME: string;
150
const PROJECT_TYPE: {
151
LIBRARY: 'library';
152
APPLICATION: 'application';
153
};
154
```
155
156
## Types
157
158
### Core Interfaces
159
160
```typescript { .api }
161
interface Location {
162
name: string;
163
path: Path;
164
}
165
166
interface ParseOptions {
167
name: string;
168
path?: string;
169
}
170
171
interface FindOptions {
172
name: string;
173
path: Path;
174
}
175
176
enum NodeDependencyType {
177
Default = 'dependencies',
178
Dev = 'devDependencies',
179
Peer = 'peerDependencies',
180
Optional = 'optionalDependencies'
181
}
182
183
interface NodeDependency {
184
type: NodeDependencyType;
185
name: string;
186
version: string;
187
overwrite?: boolean;
188
}
189
```