1.0.0 • Published 4 years ago

bluedb v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Introduction

bluedb is a lightweight json db with TypeScript support.

Usage

import { Connection } from "bluedb";

type DatabaseSchema = {
    array: string[],
    object: {
        nestedObject: {
            string: string,
        },
    },
    value: string | number,
};

const connection = new Connection<DatabaseSchema>("db.json");

connection.db.value = "Hello.";
connection.db.array.push("A value.");

connection.persistChanges();

Changes will be persisted synchronously.

By design, there aren't any predefined functions (e.g. for creating a collection). This is to allow for more flexibility when implementing this library in your own code. For example, you could extend it by wrapping it within a singleton-like, globally available container class.

import { Connection } from "bluedb";

type DatabaseSchema = {
    array: string[],
    value: string | number,
};

export class DatabaseService {

    private static connection?: Connection<DatabaseSchema>;

    static createConnection(path: string) {
        DatabaseService.connection  = new Connection<DatabaseSchema>(path);
    }

    static getConnection() {
        if (!DatabaseService.connection) throw new Error ("No existing connection.");

        return DatabaseService.connection;
    }
}
import { DatabaseService } from "../path/DatabaseService"

const connection = DatabaseService.getConnection();

connection.db.value = "Hello.";
connection.persistChanges();