#Newbie on NestJS Framework

108 messages · Page 1 of 1 (latest)

halcyon nymph
hazy pulsar
austere totem
#

Why is it hard to separate the front end and back end

hazy pulsar
#

auth etc.

#

idk how the modules work

austere totem
#

What about auth?

hazy pulsar
#

when i will build the restaurant

#

what is a module can u make a example?

Like:

  • 1
  • 2
austere totem
#

NestJS is a Node framework. It exists entirely on the server. You can use whatever front end you want, they're unrelated. Hell, you could write a front end using web assemply or an iOS/Android app.

austere totem
hazy pulsar
#

like what i must do for moduless. iodk

austere totem
#

A module is a collection of compenents that create a feature, in my opinion. You have things like providers (services, configurations to be injected, possibly global enhancer bindings), controllers, how you can call to the module from outside the server, imports, other features that this feature requires, and exports, how you expose providers to other modules

austere totem
#

Have you actually started coding anything, or is it all conceptual knowledge right now?

hazy pulsar
austere totem
#

Okay, what about Nest's use of the term "modules" confuses you?

#

How can I help clarify it

hazy pulsar
#

I am having a hard time determining what is a module and what is not at this time?

As described above, I plan to build a restaurant application. Which should allow customers to request restaurants, which ones I accept or reject in the admin area, etc. For this we need a user, auth, admin area, customer area etc. How does this work? What would you say? Restaurants (is e.g. restaurant a module? a restaurant also has products that can be ordered later, as well as categories e.g. (salads - chicken salad))

Can u make a list, what for modules are correct here? like

austere totem
#

Can u make a list, what for modules are correct here?
I canmake a list of what modules I would create. Technically, a Nest application only needs one module to function. However, separation of concerns makes life easier in the long run

#

So, it seems like you'd really have two modules here: a User/AuthModule and a RestaurantMoudle.

  • The admin area is inside of the RestaurantModule and makes use of the User/AuthModule to ensure that only the admin can access and modify that data.
  • The User/AuthModule is in charge of allowing users to signup and sign-in, which should probably return the user's type as well on sign in/authenticate, so that you can properly protect the admin endpoints inside the RestaurantMoudle.

Does that make sense to you?

hazy pulsar
#

Is this the right way? Idk.. Like in laravel i have a Module in there is a controller for admin and user area..

#

and then policies etc.

#

and in there i can use all models

#

like user, etc.

austere totem
#

You could make an admin module if you want to, but it really is going to depend on how you want to set up your admin endpoint(s)

#

It all comes out to how you architect your server

hazy pulsar
#

what my question answers

#

This is like i have in laravel

#

and i cant build that so or?

#

the structure

#

only

#

very clean and organized

austere totem
#

Why can't you build that?

#

Nest doesn'trestrict your file structure or anything like that

hazy pulsar
#

the structure?

#

okk..

austere totem
#

You can put whatever you want wherever you want

hazy pulsar
#

but the dtos i have seen in typescript with nest not like mine how i use it

austere totem
#

Sure, it's opinionated, but that doesn't mean it's restrictive

austere totem
hazy pulsar
#

ok i test arround with it!

#

thx to yu

austere totem
#

If you have specific questions about how to connect the modules or things of the like, I'll be happy to answer that. Most of the time when it comes to creating/determining modules people will have differing opinions depending on how they want to approach things so it's hard to give more definitive answers

hazy pulsar
#

did u have a nice and clean typescript code style file?

#

ye orr

austere totem
#

Oh god, gts. I've got mixed feeliings on that as, to my knowledge, it doesn't have an integrated IDE plugin and has to be ran separately and can change your files from there. I'm used to linters changing things, but it was superannoying how it parses things out.

#

I usually just stick with the stock eslint config that Nest provides on nest new. Maybe add in a few rules here or there as necessary

hazy pulsar
#

eslint nice 🙂

#

yee

#

and first of all all what i code make a test for or? like i have only made ín php for all controllers a test. what u prefere? also all services etc.?=

#

or no testing

austere totem
hazy pulsar
#

on which app?

austere totem
#

Any of them. It's a bunch of testing examples

hazy pulsar
#

So @austere totem like, now is this plan correct?

  • in User create a AuthModule for the Auth of Users.
  • Restaurant Module and in there a Admin Module and Customer Module?
#

or how u mean?

austere totem
#

You could make a customer and admin module within the restaurant module, or you could just have the endpoints mixed and have proper authentication on the admin ones. It's really up to you

hazy pulsar
#

ok

hazy pulsar
#

idk how to start xd

#

@austere totem

#

its hard idk

austere totem
#

What's hard? What's hard about it? What habe you tried? Where are you stuck?

hazy pulsar
#

i stuck on start i created a new nestjs application

#

idk how to start

#

generated a module

austere totem
#

What have you tried?

hazy pulsar
#

nothing rn but its hard to make the strucutre

#

structure

austere totem
#

Stop overthinking

hazy pulsar
#

? 😦

austere totem
#

Start with simple, build the functionality, re-evaluate and refactor as complexity grows

#

So generally, generate the module, generate the controller and service, add in the business logic

hazy pulsar
#

But is auth in User? or under the restaurant?

austere totem
#

That's your decision. Where does it make sense to add auth? My opinion is it's its own feature

hazy pulsar
#

like Auth (module(

#

?

austere totem
#

Yep

#

That's one way to go about it 🙂

hazy pulsar
#

@austere totem but thats not the best or?

#

better is un User folder or?

#

the auth

austere totem
#

How about you do this, because my answer will almost always be "what works best for you?": Try implementing it how your gut initially feels is right, and then evaluate it.. See if it will scale and if not refactor.

hazy pulsar
#

can u say me a project which i can make to learn? like say me something and i build and show here to get better

#

@austere totem

hazy basinBOT
#

The linked repository is an excellent list of curated resources for NestJS.
It includes various resources such as modules, examples, articles, and tools that can be useful for developers using NestJS.
The repository aims to provide a one-stop solution for developers exploring the NestJS ecosystem and leveraging its features to build robust and scalable applications.

cerulean mantle
#

these are all examples of repos that could help

hazy pulsar
#

can u say me a small project? @austere totem

#

😄

#

something

halcyon nymph
#

@hazy pulsar - You want to try that again? What you've written makes no sense at all.

hazy pulsar
#

why not in admin or so

#

makes for me no sense

austere totem
#

You're focusing too much on structure and not enough on the logic. If you want to put it under admin that's fine. Don't do like everyone else just because that's your they still do it; experiment, learn why that's the way people do it

hazy pulsar
#

bro wait a problem now

#

i created a role decorator

#

and a guard

#

so i used

@Get()
  @Roles(['admin'])
  getAllTasks() {
    return {
      message: 'This action returns all tasks',
    };
  }
#

why dont work?

#
@Injectable()
export class RolesGuard implements CanActivate {
  constructor(private reflector: Reflector) {}

  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    console.log('RolesGuard');
    const roles = this.reflector.get('roles', context.getHandler());

    if (!roles) {
      return true;
    }

    const request = context.switchToHttp().getRequest();
    const user = request.user;

    const hasRole = () => user.roles.some((role) => roles.includes(role));

    return user && user.roles && hasRole();
  }
}
hazy pulsar
#

@halcyon nymph @austere totem

#

i have both

#
import { SetMetadata } from '@nestjs/common';

export const Roles = (...roles: string[]) => SetMetadata('roles', roles);