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

quick-yaml.db

v1.2.2

Published

An easy, open-source, Node.js database based on YAML files.

Downloads

252

Readme

Quick-YAML.db

Quick-YAML.db is an open-source Node.js library that allows you to store data in a YAML file easily. The module is fully written in TypeScript to enhance types for JavaScript.

GitHub repository: https://github.com/TFAGaming/quick-yaml.db

Changes

  • New constructor parameter: options (QuickYAMLOptions)
  • Set default values from each model if a model's variable doesn't exist in the database.
  • Handle methods parameters.

Installation

Use the command below to install the package (js-yaml is also required to install):

npm install quick-yaml.db js-yaml

Example Usage

In this example, create a YAML file and set the file's path into the constructor's parameter. The file extensions allowed to use: .yaml, .yml. The code below is based on TypeScript, using CommonJS module.

Define a new database:

Create a new database using the class QuickYAML with a model. Using the class without a model will always return never to the variable and the type when using any of the methods that are related to the class.

import { QuickYAML } from 'quick-yaml.db';

type Model = [
    { variable: 'name', type: string },
    { variable: 'age', type: number },
    { variable: 'alive', type: boolean },
    { variable: 'languages', type: string[] }
];

const db = new QuickYAML<Model>('./example.yaml');

If you want to set the variables with a specific value in the database when the manager is ready, use the following method:

new QuickYAML<Model>('./example.yaml', {
    model: {
        setValuesOnReady: true,
        defaultModelValues: [
            { variable: 'name', value: 'John' },
            { variable: 'age', value: 24 },
            { variable: 'alive', value: true },
            { variable: 'languages', value: ['English', 'French'] }
        ]
    }
});

The method: set

Adds a new variable to the database with a value. If the variable already exist, it will update the variable's value.

const obj = {
    name: 'John',
    age: 24,
    alive: true,
    languages: ['English', 'French']
};

type Keys = keyof typeof obj;

for (const key in obj) {
    db.set(key as Keys, obj[key as Keys]);
};

The YAML file content when the method is used:

name: John
age: 24
alive: true
languages:
  - English
  - French

The method: has

Checks if a variable exist in the database or not.

db.has('name');
db.has('age');
db.has('hobbies');

The return for each of the tests above:

true
true
false

The method: get

Gets a variable's value from the database, returns undefined if it doesn't exist.

db.get('name');
db.get('age');
db.get('hobbies');

The return for each of the tests above:

'John'
24
undefined

The method: find

Gets a variable's object data from the database.

db.find('name');
db.find('languages');
db.find('hobbies');

The return for each of the tests above:

{ variable: 'name', value: 'John' }
{ variable: 'languages', value: ['English', 'French'] }
{ variable: 'hobbies', value: undefined }

The method: delete

Deletes a variable from the database.

db.delete('name');

The method: clear

Deletes every variable in the YAML data.

db.clear();

Other methods:

Object related functions:

db.entries();

db.keys();
db.values();

Array related functions:

db.first();
db.last();

db.indexOf(variable);

db.push(variable, ...values);
db.pull(variable, ...values);

db.map((value, key, index) => ...);
db.forEach((value, key, index) => ...);

Other useful functions:

db.ensure(variable, default_value);

db.pick(...variables);
db.purge(...variables);

License

GNU General Public License v3