1
votes

I would like to add version for my modules, but i don't know how can I do this. I tried to create a common module.ts but the same name of services killed each us. I tried different module.ts for the versions, it was better but the services with same names didn't work.

This is my last structure: test-module

1.0
   controllers
      test.controller.ts
   services
      test.service.ts
   test.module.ts
1.1
   controllers
      test.controller.ts
   services
      test.service.ts
   test.module.ts

This is my test service(s) for the versions:

import * as _ from 'lodash';
import { Injectable } from '@nestjs/common';

@Injectable()
export class TestService {
  public test() {
    return '1.0'; // and 1.1 in 1.1 directory
  }
}

This is my module.ts:

import { Module, Logger } from '@nestjs/common';

import { TestModule as DorotTwo } from 'test-module/1.1/test.module';
import { TestModule as DorotOne } from 'test-module/1.0/test.module'

@Module({
  controllers: [ProtobufController],
  providers: [],

  imports: [
    DorotTwo,
    DorotOne,
  ],
})
export class ProjectModule {
  constructor() {
    Logger.log('App initialized');
  }
}

This is a simple test Controller in the project who want use the modules. A tried import TestService from 1.0 or 1.1 but the test function's response is always 1.0 because that is the last element in the import.

@Controller()
export class ProtobufController {
  constructor(private readonly testService: TestService) {
    console.log(this.testService.test()); // Always 1.0
  }
.....

It is working if I use full different names for services for example (eg: UserAuthenticationService10, RegisterAuthenticationService10), but this is horrible and if i forget rename it in new version, it will overwrite.

Is exists an example where I can read how can I create this versioned module?

1

1 Answers

0
votes

Would using custom providers be a satisfying solution for you?

Example:

// 1.0
@Module({
  providers: [
    { provide: 'TestService_1.0', useClass: TestService }
  ]
})
export class TestModule {}

// 1.1
@Module({
  providers: [
    { provide: 'TestService_1.1', useClass: TestService }
  ]
})
export class TestModule {}

// Then

@Controller()
export class ProtobufController {
  constructor(
    @Inject('TestService_1.0') private readonly testService_10,
    @Inject('TestService_1.1') private readonly testService_11
  ) {
    console.log(this.testService_10.test());
    console.log(this.testService_11.test());
  }
}

I obviously haven't tested this and you should adapt it to your usecase. I suggest you to have a look at https://docs.nestjs.com/fundamentals/custom-providers.