npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xpresser-db-config

v1.2.1

Published

Database config loader for xpresserjs

Downloads

9

Readme

Xpresser Db Config

STILL IN DEVELOPMENT

Every app that connects to a database at some point saves some configuration data that should be modified by the application.

This plugin makes that process easy for you.

  • Provide driver to use any Database.
  • Autoload configs in memory
  • AutoUpdate configs in realtime with changes.
  • Provide access functions to get configs.

Looks Like

File: db-config.js (Where default db configuration is defined)

module.exports = [
  // standalone configs
  {
    config: {
      allowLogin: true,
      backup: true,
      cronjobs: false,
    },
    autoload: true,
  },
  // Grouped configs
  {
    group: 'app',
    config: {
      name: 'App Name',
      slogan: 'App slogan',
    },
    autoload: true,
  },
];

/// Will be saved to memory as.
({
  allowLogin: true,
  backup: true,
  cronjobs: false,
  app: {
    name: 'App Name',
    slogan: 'App slogan',
  },
});

/// Will be stored to your configured db as
[
  {key: 'allowLogin', value: true, type: 'boolean', autoload: true, ...},
  {key: 'backup', value: true, type: 'boolean', autoload: true, ...},
  {key: 'cronjobs', value: false, type: 'boolean', autoload: true, ...},
  {group: 'app', key: 'key', value: 'App Name', type: 'string', autoload: true, ...},
  {group: 'app', key: 'slogan', value: 'App Slogan', type: 'string', autoload: true, ...},
];

Getting/Setting configuration

const {getConfig, setConfig} = require("xpresser-db-config");

await getConfig('app.name'); // App Name
await getConfig('app.slogan'); // App Slogan

await setConfig('app.name', "New Name");

Setup

Install xpresser-db-config

npm i xpresser-db-config
# or
yarn add xpresser-db-config

Add to your plugins.json before other plugins. Note: If you have a plugin that initializes database connection used by the db-config, add this plugin after it.

{
  "npm://xpresser-db-config": true
}

Add to your xpresser config.

({
    paths: {
        // db-config declaration file
        dbConfig: "backend://db-config",
        // db-config driver
        dbConfigClass: "npm://xpresser-db-config/drivers/xpress-mongo/XpressMongoDbConfig",
        // db-config backup folder
        dbConfigBackupFolder: "base://storage/db-config-backup",
    }
})

Create db-config.(js|ts) file in your backend folder.

import { defineDbConfig } from "xpresser-db-config/src/functions";

type Meta = {
    title: string;
    desc: string;
    example?: any;
    options?: Record<any, any>;
};

export = defineDbConfig<Meta>(({ v }) => {
    return [
        {
            // Active Providers Config
            group: "app",
            autoload: true,
            config: {
                name: "My App Name",

                // Value with meta
                slogan: v("We are the best", {
                    title: "Slogan",
                    desc: "This is the slogan of our app"
                })
            }
        }
    ];
});

Add extensions to your use-xjs-cli.json file.

{
  "extensions": [
    "npm://xpresser-db-config"
  ]
}

The setup is complete!. Next, we need to migrate our configs to the database. Run the command below to migrate configs.

npx xjs dbc:migrate

Other commands are below.

Commands

# Migrate configs to database
npx xjs dbc:migrate

# Find config
npx dbc:find <query> [format] 

# Set config
npx dbc:set <key> <value>

# Reset config
npx dbc:reset

# Backup configs
npx dbc:backup

# Restore configs
npx dbc:restore <file>