Skip to content

Drylozu/MeowDB.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MeowDB.js

MeowDB

Downloads Minified Size Vulnerabilities License Last Commit GitHub Repo stars

NPM

"Database" in JSON (Node.JS Library).

Released v2.2.3. See CHANGELOG.

Installation

  • npm install meowdb --save.

I recommend not using versions lower than 2.1.9 or being aware of updates to the library.

Usage

JavaScript - CommonJS require

const MeowDB = require("meowdb");

const myDatabase = new MeowDB({
    dir: __dirname,
    name: "database",
    raw: false // Defines if MeowDBObjects will be returned (optional, default: false)
});

TypeScript - ES6 import

With TypeScript you should've the esModuleInterop flag.

import MeowDB from "meowdb";
// The generic type is optional, by default it's "full" but when using the raw option, use "raw" instead of "full"
const myDatabase = new MeowDB<'full'>({
    dir: __dirname,
    name: "database",
    raw: false // Defines if MeowDBObjects will be returned (optional, default: false)
});

Example of all functions

// Creating object (it'll search property by property and if it doesn't exist, it'll create it otherwise it'll not modify the current information~)
// * where the first parameter is the ID, they're like properties of an object (same thing in most functions)
const newObject = myDatabase.create("0001", {
    name: "David",
    country: "CO",
    info: "Nothing to show"
});
console.log(newObject);

// Obtaining an object
const object = myDatabase.get("0001");
console.log(object);

// Modifying an object and saving it
object.name = "Deivid";
object.save();
console.log(object.name);

// Setting directly the value of an element
const newName = myDatabase.set("0001.info", "Just a person");
console.log(newName);

// Listing all objects
let temp = "";
Object.entries(myDatabase.all()).forEach((user) => {
    temp += `   - ${user[1].name} (ID: ${user[0]})\n`;
});
console.log(temp.trimRight());

// Finding an object
const anObject = myDatabase.find((user) => user.name === "Deivid");
console.log(anObject);

// Filtering objects
const someObjects = myDatabase.filter((user) => user.country === "CO");
console.log(someObjects);

// Deleting an object
const deletedObject = myDatabase.delete("0001");
console.log(deletedObject);

Important note while using TypeScript

You can use TypeScript Generics to create/get/update/set/find/filter the data, it doesn't matter what type you use.

const nonObjectValue = myDatabase.get<string>('0002.name');
console.log(nonObjectValue); // TS will interpret it as string

const numberValue = myDatabase.get<number>('some id here');
console.log(numberValue); // TS will interpret it as a number

const booleanValue = myDatabase.get<boolean>('some id here');
console.log(booleanValue); // TS will interpret it as a boolean

// With Objects/also works with interfaces
type Person = {
    name: string;
    country: string;
    info: string;
};

const objectValue = myDatabase.get<Person>('0002'); // This will return a MeowDBObject with the properties that you specified in the generic type
console.log(objectValue.name); // While typing '.name', you'll get *autocomplete*
// It also works when you save an MeowDBObject
objectValue.info = 'Hi!';

/// Important: Read the note in the Usage/TypeScript section.
objectValue.save(); // This will return a plain 'Person' object.

"Documentation"

  • new MeowDB(options)
    • create(id, initialValue)
    • exists(id)
    • get(id)
    • set(id, value)
    • all()
    • delete(id)
    • find(callback, id?)
    • filter(callback, id?)
  • MeowDBError

new MeowDB(options)

Creates or gets a database

  • Parameters:
    • options - An object with the options
      • options.dir - A string indicating the directory that will have the database (must be an absolute path - the folder should be created)
      • options.name - A string with the name of the database
      • options.raw? - A boolean that represents if MeowDBObjects won't returned (default: false, MeowDBObjects will be returned).
  • Throws: MeowDBError - If any option is invalid

Methods

all()

Returns all data stored in the database

  • Returns: MeowDBObject | object - All data

create(id, initialValue)

Creates an element in the database with the specified ID and sets it's value

  • Parameters:
    • id - A string representing the ID of the element to create
    • initialValue - The initial value of the element
  • Returns: object - The created element
  • Throws: MeowDBError - If the ID or initialValue is invalid

delete(id)

Deletes an element from the database

  • Parameters:
    • id - A string representing the ID of the element to delete
  • Returns: object - The deleted element
  • Throws: MeowDBError - If the ID is invalid

exists(id)

Checks if an element exists in the database

  • Parameters:
    • id - A string representing the ID of the element to check
  • Returns: boolean - If it exists
  • Throws: MeowDBError - If the ID is invalid

get(id)

Gets an element of the database

  • Parameters:
    • id - A string representing the ID of the element to get
  • Returns: MeowDBObject | object | any - The element
  • Throws: MeowDBError - If the ID is invalid

set(id, value)

Sets the value of an element in the database

  • Parameters:
    • id - A string representing the ID of the element to update
    • value - The new value of the element
  • Returns: any - The value setted
  • Throws: MeowDBError - If the ID or value is invalid

find(callback, id?)

Finds an element in the database. You should only use this function if you're finding for objects

  • Parameters:
  • callback - A function that handles all the elements and decides which one will be returned
    • id? - A string representing the ID of the root element to find another elements (optional)
  • Returns: MeowDBObject | object | any - The element
  • Throws: MeowDBError - If the ID or callback is invalid

filter(callback, id?)

Filters elements in the database. You should only use this function if you're filtering for objects

  • Parameters:
    • callback - A function that handles all the elements and decides which ones will be returned
    • id? - A string representing the ID of the root element to find another elements (optional)
  • Returns: (MeowDBObject | object | [string, any])[] - The elements (MeowDBObject[] if they're objects, array with ID and value if not)
  • Throws: MeowDBError - If the ID or callback is invalid

MeowDBError

Extends Error, only used for error reference.