Socket
Book a DemoInstallSign in
Socket

tbs-site-config

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tbs-site-config

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.

0.0.8
latest
Source
npmnpm
Version published
Weekly downloads
56
-15.15%
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 07 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.