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

@mvdlei/pg-toggle

v0.0.3

Published

Postgres feature toggle, easy to use, just connect and toggle.

Downloads

2

Readme

pg-toggle

pg-toggle is a lightweight, flexible package for managing feature toggles in a PostgreSQL database. It allows you to create, retrieve, and update feature toggles easily, providing robust logging and retry mechanisms to ensure smooth operation.

Installation

npm install pg-toggle

Usage

Importing the Package

import { Toggle } from "pg-toggle";

Initializing the Toggle Manager

To initialize the Toggle manager, make sure your PostgreSQL database is running and properly configured. Use the init method to run the required migrations and obtain a Toggle instance.

const t = await Toggle.init("postgres://postgres:postgres@localhost:5433/database");

Creating Toggles

Use the create method to create a set of toggles. This method takes an object where keys are toggle names and values are booleans indicating whether the toggle is enabled.

const toggles = await t.create({
  FEATURE_ONE: true,
  FEATURE_TWO: false,
});
console.log(toggles);

Getting a Toggle

To retrieve the status of a specific toggle, use the get method. It returns a boolean indicating whether the toggle is enabled. If the toggle does not exist, it logs an error and returns false.

const isEnabled = await t.get("FEATURE_ONE");
console.log(isEnabled);

Setting a Toggle

Use the set method to update the status of a toggle. If the toggle does not exist, it creates a new one with the specified status.

const updatedStatus = await t.set("FEATURE_ONE", false);
console.log(updatedStatus);

API

Toggle

static init(options: string): Promise<Toggle>

Initializes the Toggle manager, runs necessary migrations, and returns a Toggle instance.

  • options: IClientOptions - The configuration options for the PostgreSQL client, uses basic string format for connection.

create<T extends Record<string, boolean>>(toggles: T): Promise<T & Record<string, boolean>>

Creates a set of toggles in the database.

  • toggles: T - An object where keys are toggle names and values are booleans.

get(name: string): Promise<boolean>

Retrieves the status of a specific toggle.

  • name: string - The name of the toggle.

set(name: string, enabled: boolean): Promise<boolean>

Sets the status of a specific toggle. If the toggle does not exist, it creates a new one.

  • name: string - The name of the toggle.
  • enabled: boolean - The status to set for the toggle.

Example

Here's a complete example of how to use pg-toggle:

import { Toggle } from "pg-toggle";

const main = async () => {
  const t = await Toggle.init("postgres://postgres:postgres@localhost:5433/database");

  const toggles = await t.create({
    TEST_TOGGLE: false,
  });
  console.log(toggles);

  const maybe = await t.get("NON_EXISTENT_TOGGLE");
  console.log(maybe);

  const newly = await t.set("NEW_TOGGLE", true);
  console.log(newly);
};

main();

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.