Code Monkey home page Code Monkey logo

Comments (7)

shekohex avatar shekohex commented on May 14, 2024

it the obvious result as any Controller or Module should have only a single route. with that have said,

expressjs supports routes alias as an array

so we can use

app.get(['/user/:id', '/profile/:id'], (req, res) => { ... });

as nest-router uses the build-in MODULE_PATH which is provided in nestjs/core

we cannot provide it as an array.
so we have a workaround here:

you can create another module with the same name and post-fix it with v2 or whatever,
and also do that for your controller, actually you would not have to re implement the same methods of that controller you can extend it and override the changed API/method of the old method or create new one, nest should get the old methods too.

to be clear i'll make it as an example:

we have module Users and in that module we have one controller UserPayment

...
@Controller('/payment')
export class UserPayment {
    @Get(':id')
    async getUserPayments(@Parm('id') userId: number) {
        ...
        // some complex sql calls
    }
   ...
   // another methods
}
...
@Module({
    controllers: [UserPayment]
})
export class Users {}

then we have updated our api and now we have api v2

we will create another module UsersV2 and another UserPaymentV2 controller

...
@Controller('/payment')
export class UserPaymentV2 extends UserPayment {
    @Get(':id')
    async getUserPayments(@Parm('id') userId: number) {
        ...
        // some complex sql calls
       // with update for v2
    }
   ...
   // another methods from extended class
}
...
@Module({
    controllers: [UserPaymentV2]
})
export class UsersV2 {}

then in our routes

const routes: Routes = [
  {
    path: 'v1',
    children: [{ path: 'users', module: Users }],
  },
  {
    path: 'v2',
    children: [{ path: 'users', module: UsersV2 }],
  },
];

PS: children array can be just an array of modules if they have the same path.

from nest-router.

vahidvdn avatar vahidvdn commented on May 14, 2024

@shekohex Do you think this is a good idea? Let say we will have 3 versions. In the user folder, there will be 3 files for each user.module.ts and user.controller.ts
Which seems very hard to maintain. Any idea?

from nest-router.

shekohex avatar shekohex commented on May 14, 2024

@vahidvdn so you have 3 versions of the same Controller?
The easiest way is that you every time you create a new version you extend the original Controller class and override/add any new methods you want.
And you version them in the routes file.

from nest-router.

vahidvdn avatar vahidvdn commented on May 14, 2024

@shekohex Thanks for the point.
What I mean was about managing files. Let say we will have module, moduleV2, and moduleV3. Also for controllers. It seems very messy.

from nest-router.

shekohex avatar shekohex commented on May 14, 2024

Did you tried to use the same Module, but with 3 Different Controllers?
ModuleA: ControllerV1, ControllerV2, ControllerV3

Every one of these extends the previous one with new changes and with a new route in the @Controller()

Is that ok?

from nest-router.

vahidvdn avatar vahidvdn commented on May 14, 2024

@shekohex No, that's not. In your example:

const routes: Routes = [
  {
    path: 'v1',
    children: [{ path: 'users', module: Users }],
  },
  {
    path: 'v2',
    children: [{ path: 'users', module: UsersV2 }],
  },
];

In v2 path, you mentioned UsersV2 module. So, as I understand we can't have only one module. Am I right?

from nest-router.

shekohex avatar shekohex commented on May 14, 2024

Yes, any module will only have one version, but as many Controllers you wish, so I guess you could instead of using this package you could add the versions to the Controllers itself.

If you don't need to versioning based on the modules.

from nest-router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.