Giter Club home page Giter Club logo

app-settings's Introduction

Node App Settings

Synchronous data access layer for your app configurations or feature flags as records in your existing database

Overview

This package provides an interface for storing data in your existing database with an in-memory cache layer for synchronous data access. The data could be feature flags, application configurations, etc. or anything else that you would need to change on the fly.

Supported databases at the moment are Postgres, MongoDB, and MySQL. The codebase follows a strategy pattern and devs can easily add new database implementations. Feel free to raise a pull request!

Quick Start

Install as a dependency

yarn add @siddiqus/app-settings

Example code:

import { AppSettings, MongoConfig } from '@siddiqus/app-settings';

// your database config, you can use your own configuration mechanism here
const config: MongoConfig = {
  dialect: 'mongodb',
  host: 'localhost',
  user: 'admin',
  password: 'password',
  database: 'testdb',
}; 

// this can be a singleton in your system
const appSettings = new AppSettings({
  dbConfig: config,
  tableName: 'app_settings_pg',
  refreshIntervalInSeconds: 300 // default interval is 300 seconds (5 minutes)
});

async function run() {
  await appSettings.init(); // this is needed to setup the db tables

  await appSettings.set({
    key: 'hey',
    value: { hey: 'there' },
    type: 'json',
  });

  await appSettings.set({
    key: 'someKey',
    value: 'boop',
    type: 'string', // string | boolean | json | number
  });

  const heyJson = appSettings.get('hey', {}); // notice this does not need await

  console.log(heyJson) // value is the { hey: there } object, or {} by default if the key does not exist 

Internals

Initialization

On initializing the instance with your database configurations, few things happen:

  1. It creates (if not existing) a table to store the settings
  2. It will fetch the records from the table and store the formatted data in an in-memory cache

Fetching Data

  • Any time the set method is called, it will refresh the cache after the record is added/updated
  • A process refreshes the cache every 5 minutes (this is configurable)
  • Since the data is coming from in-memory, no need to await the get or getAll functions

Contributing Guidelines

1. **Fork the Repository**: Start by forking the project repository to your GitHub account. 2. **Clone Locally**: Clone the forked repository to your local machine using a Git client. ```sh git clone https://github.com/siddiqus/app-settings ``` 3. **Create a New Branch**: Always work on a new branch, giving it a descriptive name. ```sh git checkout -b new-feature-x ``` 4. **Make Your Changes**: Develop and test your changes locally. 5. **Commit Your Changes**: Commit with a clear message describing your updates. ```sh git commit -m 'Implemented new feature x.' ``` 6. **Push to GitHub**: Push the changes to your forked repository. ```sh git push origin new-feature-x ``` 7. **Submit a Pull Request**: Create a PR against the original project repository. Clearly describe the changes and their motivations.

Once your PR is reviewed and approved, it will be merged into the main branch.


License

This project is protected under the MIT License. For more details, refer to the reference website.

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.