Giter VIP home page Giter VIP logo

megalithofficial / meg.db-light Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 38 KB

MegDB-light, A lightweight TypeScript database module providing a type-safe API for efficient data storage and retrieval. Easily extendable with custom drivers, including a built-in JSONDriver.

License: GNU General Public License v3.0

TypeScript 100.00%
begginer-friendly custom-driver customisable database easy-to-use fast javascript json lightweight node-js

meg.db-light's Introduction

MegDB-light: A Robust TypeScript Database Module πŸš€

MegDB-light is a robust, lightweight database module crafted in TypeScript. It offers a user-friendly and extensible API for seamless data manipulation, enabling you to execute common operations such as set, get, delete, and more with ease. The module's design is flexible, accommodating different data storage drivers.

Key Features

  • Super Lightweight: MegDB-light is incredibly lightweight, ensuring your applications remain efficient and high-performing. πŸš€

  • Type-Safe Operations: MegDB-light exploits TypeScript’s type system to ensure type safety across your database interactions, enhancing reliability and robustness. πŸ›‘οΈ

  • Extensibility: MegDB-light is easily extendable. You can implement custom data storage drivers, making the module compatible with a variety of storage solutions. πŸ“š

Installation

You can incorporate MegDB-light into your TypeScript project by installing it via npm:

npm install meg.db-light

How to Use

MegDB Class

The Megdb class offers a high-level API for data interaction.

import { Megdb, JSONDriver } from 'meg.db-light';

// Define a type for the data
interface User {
    name: string;
    age: number;
    hobbies: string[];
};

interface format {
    "john": User;
};

// Create a new JSONDriver instance
const jsonDriver = new JSONDriver<format>('users.json');

// Create a new Megdb instance with the JSONDriver instance
const megDB = new Megdb<format>(jsonDriver);

// Use the MegDB instance
async function main() {
    // Set a value
    await megDB.set('john', { name: 'John Doe', age: 30, hobbies: ['reading', 'coding'] });

    // Get a value
    const john = await megDB.get('john.name');
    console.log(john.name);  // Outputs: John Doe

    // Update a value
    await megDB.add('john.age', 1);

    // Get the updated value
    const updatedJohn = await megDB.get('john.age');
    console.log(updatedJohn);  // Outputs: 31

    // Push a value into an array
    await megDB.push('john.hobbies', "gaming");

    // Get the updated array
    const updatedHobbies = await megDB.get('john.hobbies');
    console.log(updatedHobbies);  // Outputs: ['reading', 'coding', 'gaming']

    // Pull a value from an array
    await megDB.filter('john', hobby => hobby !== 'coding');

    // Get the updated array
    const finalHobbies = await megDB.get('john.hobbies');
    console.log(finalHobbies);  // Outputs: ['reading', 'gaming']

    // Delete a value
    await megDB.delete('john.age');

    // Try to get the deleted value
    const deletedValue = await megDB.get('john.age');
    console.log(deletedValue);  // Outputs: undefined

    // Get all data
    const allData = await megDB.all();
    console.log(allData);  // Outputs: { john: { name: 'John Doe', hobbies: ['reading', 'gaming'] } }
}

main();

Custom Driver

You can create your custom driver by extending the BaseDriver class and implementing the loadData and saveData methods.

import { BaseDriver } from 'meg.db-light';

export class CustomDriver<T> extends BaseDriver<T> {
    constructor(filePath: string) {
        super(filePath);
    }

    public async loadData(): Promise<T> {
        // Implement loading data from your custom storage
    }

    public async saveData(data: T): Promise<void> {
        // Implement saving data to your custom storage
    }
}

Examples

JSONDriver

A built-in driver for storing data in a JSON file.

import { Megdb } from 'meg.db-light';
import { JSONDriver } from 'megdb/drivers/JSONDriver';

// Define a type for the data
interface User {
    name: string;
    age: number;
    hobbies: string[];
};

interface format {
    "john": User;
};

// Create a new JSONDriver instance
const jsonDriver = new JSONDriver<format>('users.json');

// Create a new Megdb instance with the JSONDriver instance
const megDB = new Megdb<format>(jsonDriver);

// Utilize the MegDB instance
async function main() {
    // Operations with MegDB...
}

main();

License

This project is licensed under the GNU General Public License v3.0 License - see the LICENSE file for details.

meg.db-light's People

Contributors

megalithofficial avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.