0.0.8 • Published 1 year ago

tbs-site-config v0.0.8

Weekly downloads
-
License
UNLICENSED
Repository
github
Last release
1 year ago

TBS Site Configs

Package for handling site config distribution between repository. Which site config data saved on server memory as variable, so data can be consumed much faster than HTTP request.

Installation

Yarn

yarn add tbs-site-config

NPM

npm install tbs-site-config

Getting Started

For initializing package you need to write in 2 file

app.module.ts

  • Master repository initialization
import { TbsSiteConfigModule } from 'tbs-site-config';

@Module({
  imports: [
    TbsSiteConfigModule.registerAsync({
      options: {
        name: "redis", // this line will be overrided on library, so you can write as you want caused it wont be used
        transport: Transport.REDIS,
        options: { port: +process.env.REDIS_PORT, host: process.env.REDIS_HOST },
      },
      isMaster: true,
      mongoDbUrl: process.env.MONGO_URL,
      mongoDbName: "tbs_db_utils",
      mongoDbCollection: SiteConfig.name.toLowerCase() + "s",
    })
  ],
  controllers: [],
  providers: [],
})
export class ExampleModule {}

The initialization code above is intended for the master repository, which means that the repository is the one that owns and manipulates the site config data. isMaster being filled with true indicates that this is the master repository

  • client repository initialization
import { TbsSiteConfigModule } from 'tbs-site-config';

@Module({
  imports: [
    TbsSiteConfigModule.registerAsync({
      configUrl: process.env.UTIL_SERVICE_URL + "/api/v1/configs",
      options: {
        name: "redis", // this line will be overrided on library, so you can write as you want caused it wont be used
        transport: Transport.REDIS,
        options: { port: +process.env.REDIS_PORT, host: process.env.REDIS_HOST },
      },
    })
  ],
  controllers: [],
  providers: [],
})
export class ExampleModule {}

On client repository only need the config url for fetching initial data from that endpoint.

main.ts

import {TbsSiteConfigServer} from 'tbs-site-config'

TbsSiteConfigServer(app, {
  transport: Transport.REDIS,
  options: { port: +process.env.REDIS_PORT, host: process.env.REDIS_HOST },
});

await app.startAllMicroservices();

Master and client repository must initialize above code for start the microservice server. Which microservice server will be used for listening any changes on master repository

Get The Site Config

import { SITE_CONFIGS } from 'tbs-site-config';

@Injectable()
export class SiteConfigService {
  constructor(
    @Inject(SITE_CONFIGS) private readonly siteConfig: Record<string, any>,
  ) {
  }

  async findAll() {
    return this.siteConfig;
  }
  
  async findOne(key: string) {
    return this.siteConfig[key]
  }
}

note: While injecting site config the data type is must be Record<string,any> also for accessing single data you must pass the key

Update The Site Config

import { TbsSiteConfigService, SITE_CONFIGS } from 'tbs-site-config';

@Controller()
export class SiteConfigController {
  constructor(
    private readonly siteConfigService: TbsSiteConfigService,
    @Inject(SITE_CONFIGS) private readonly siteConfig: Record<string, any>
  ) {
  }

  async updateOne(key: string, data: Record<string, any>) {
    return this.siteConfigService.update(data);
  }
  
  async updateSpecificField(key: string, value: string, status: number) {
    return this.siteConfigService.update({ ...this.siteConfig[key], value, status });
  }
}

note: To modify the current data, it only supports modifying all the data, and cannot only modify some or certain fields

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago