@siddiqus/app-settings
v1.0.4
Published
A NodeJS utility for storing application settings with in-memory cache access
Downloads
9
Maintainers
Readme
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:
- It creates (if not existing) a table to store the settings
- 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
theget
orgetAll
functions
Contributing Guidelines
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.