Skip to content

An easy, open-source, Node.js database based on YAML files. The module is fully written in TypeScript to enhance types for JavaScript.

License

Notifications You must be signed in to change notification settings

TFAGaming/quick-yaml.db

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

An easy, open-source, Node.js database based on YAML files. The module is fully written in TypeScript to enhance types for JavaScript.

Resources

License

Stars

Watchers

Forks