Make writing a habit together! This is the third day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.
Step 3 towards full stack
Model Data Model
The use of Mongoose from the official website is based on modular introduction, and we use nestJS-TypeGOOSE dependency to achieve module introduction
npm i nestjs-typegoose --save
Copy the code
Modify the main.ts file to remove the database connection from the entry file and replace it with the one introduced in the app.module.ts module
async function bootstrap() { // mongoose.connect('mongodb://localhost/nestjs-test-api', {}, (err: Any) => {// if (err) {// console.log(err, 'Database connection error! '); //} // console.log(' Database connection successful! '); / /}); . } bootstrap();Copy the code
// app.module.ts
import { TypegooseModule } from 'nestjs-typegoose';
@Module({
imports: [
TypegooseModule.forRoot('mongodb://localhost/nestjs-test-api', {}),
PostsModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
Copy the code
After using the TypegooseModule to link to the database and importing the corresponding Post data model under the Post module, the model can be injected into the controller for use.
From the simple relationship shown above, we can modify the corresponding code roughly to implement the modular injection model.
First modify the model file post.model.ts
import { prop } from '@typegoose/typegoose';
export class Post {
@prop()
title: string
@prop()
content: string
}
// export const PostModel = getModelForClass(Post);
Copy the code
You can export the data model directly instead of using the original export package. We then introduce the correct data model in the posts.module.ts module.
import { TypegooseModule } from 'nestjs-typegoose';
import { Post } from './post.model';
@Module({
imports: [
TypegooseModule.forFeature([Post]),
],
controllers: [PostsController],
})
Copy the code
Inject the model posts.controller.ts into your controller
import { ModelType } from '@typegoose/typegoose/lib/types';
import { InjectModel } from 'nestjs-typegoose';
import { Post as PostSchema } from './post.model';
export class PostsController {
constructor(
@InjectModel(PostSchema)
private readonly postModel: ModelType<PostSchema>
) { }
...
}
Copy the code
With the @InjectModel decorator, the model can be accessed through this.postModel in the interface.
@Controller('posts') @ApiTags('Posts') export class PostsController { constructor( @InjectModel(PostSchema) private readonly postModel: ModelType<PostSchema> ) { } @Get() @ApiOperation({ summary: 'Get article list data ',}) async index() {return await this.postmodel.find (); }... }Copy the code
This decouples the model and can be injected into any module.
CRUD generator
Nest CLI automatically generates the code files required by the module. Make our development simple and efficient
nest g resource
Copy the code
cmd
Running effect
-
Run the generated template file
After executing commands in the project root directory, you can generate all Nest build files, including modules, services, and controllers, as well as entity classes, DTO classes, and test files. Just focus on the model definition to implement the business.
conclusion
At this point, we are familiar with the basic rules of use for 'Nest', followed by how to manage multiple sub-projects within the project.Copy the code