简体   繁体   中英

NestJs: Is there a way to generate a resource, already wired to a TypeOrm entity?

I'm playing around with NestJs, for the purpose of automating REST API creation as much as possible. So far, I do not see any quick way to create a fully functional resource.

For example, if I use the Nest CLI like this:

nest g resource

And call my resource "posts", I get a module, a controller, a service, dto's and an empty entity

In order to make it functional, I would need to manually go through every method of the PostsService, remove the placeholder, and wire it to the corresponding entity method. For example, this needs to be altered:

findAll() {
    return `This action returns all posts`;
  }

to:

findAll(){
 return this.postsRepository.find()
}

Not to mention, that I need to add various decorators and imports. In my opinion, this undermines the whole idea of CRUD generator . A lot of files are created for me, but they need to be altered.

Is there any way in Nest to create a fully functioning resource(of course i would still need to manually set the fields of the entity), with all parts wired to each other correctly?

I also tried to find something similar. And this is the best what I found https://github.com/ashinzekene/generator-nestjs-app I think if such a thing existed, then it should have been mentioned in this repository. https://github.com/nestjs/awesome-nestjs But it's not. Maybe I'm wrong

The purpose of nest isnt to get up and running asap -> You would probably want to use something a little bit more user friendly / or one with a more guided workflow.

in terms of what the generator is giving you...

  • Controller Only deals with the request / response being recieved and sent... it doesnt have any other type of functionality...
  • DTO think of it like an interface but with more functionality, as you can add data-validation / etc to make sure the request object is formated properly...
  • Service Exposes the methods that contain all of our business logic. for a crud app, this would be where your actual get post put patch delete would be... and how the data us modified in your db. Then once this finishes it would return the response if any to the controller which sends the info out.

this is helpful for many reasons... one if our project has 200 endpoints and 40 people working in different teams you dont want each team to have a different way of doing things, also you want it to be clear where each thing is... if you endpoints need to be changed, you simply go to your controller... if the logic needs to change, you go to that service etc...

The Purpose of nest is to give you a framework for extremely modular apps / essentially everything decoupled..

think of it in terms of an express... you could have a single page express app that just has everything in one place... or you could have every single thing on it's own area...

esentially nest doesnt do anything crazy that express cant, it's just the underlying inversion of control (the.main file which calls the app.module which builds all the different pieces that are needed for that endpoint / app / service / or etc.)

If you just want to use nest because it seems shiny, id consider looking up something like fastify or / even appsmith / or something else like that...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM