JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 17
  • Score
    100M100P100Q59999F
  • License MIT

Light local storage database for NodeJS

Package Exports

  • aloedb-node

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (aloedb-node) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

AloeDB Logo

AloeDB-Node

Light local storage database for NodeJS

Work in progress of a Work in progress!
Forked from Kirlovon their Deno package!


Port

This is a port of the Deno package: https://github.com/Kirlovon/AloeDB

Features

  • ✨ Simple to use API, similar to MongoDB!
  • 🚀 Optimized for a large number of operations.
  • ⚖ No dependencies!
  • 📁 Stores data in readable JSON file.

Importing

import { Database } from "aloedb-node";

Example 1 (Using a interface)

import { Database } from "aloedb-node";

// Structure of stored documents
interface Film {
    title: string;
    year: number;
    film: boolean;
    genres: string[];
    authors: { director: string };
}

(async () => {
    // Initialization
    const db = new Database<Film>("./path/to/the/file.json");

    // Insert operations
    await db.insertOne({
        title: "Drive",
        year: 2012,
        film: true,
        genres: ["crime", "drama", "noir"],
        authors: { director: "Nicolas Winding Refn" },
    });

    // Search operations
    const found = await db.findOne({ title: "Drive", film: true });
    console.log(found);

    // Update operations
    await db.updateOne({ title: "Drive" }, { year: 2011 });

    // Delete operations
    await db.deleteOne({ title: "Drive" });
})();

Example 2 (Class without a separate interface (Also using uuids and timestamps))

import { Database } from "aloedb-node";
import { v1 as uuidv1 } from "uuid";

const db = new Database<Omit<Weather, "save" | "delete" | "update">>("./db/weather.json");

export class Weather {
    id: string;
    timestamp: number;
    temperature: number;
    humidity: number;

    constructor(data: Omit<Weather, "save" | "delete" | "update" | "id" | "timestamp"> & Partial<Pick<Weather, "id" | "timestamp">>) {
        this.id = data.id ?? uuidv1();
        this.timestamp = data.timestamp ?? new Date().getTime();
        this.temperature = data.temperature;
        this.humidity = data.humidity;
    }

    save() {
        return db.insertOne(this);
    }

    delete() {
        return db.deleteOne({ id: this.id });
    }

    update() {
        return db.updateOne({ id: this.id }, this);
    }

    static async findOne(query: Partial<Omit<Weather, "save" | "delete" | "update">>): Promise<Weather | null> {
        const object = await db.findOne(query);
        if (object) return new Weather(object);
        return null;
    }

    static async findMany(query: Partial<Omit<Weather, "save" | "delete" | "update">>): Promise<Weather[]> {
        const objects = await db.findMany(query);

        return objects.map((obj) => {
            return new Weather(obj);
        });
    }
}

Using this class

// Create a new entity
const newWeather = new Weather({temperature: 15, humidity: 32});
await newWeather.save();

// Update a existing entity
newWeather.temperature = 16;
newWeather.update();

// Find and existing entity
const oldWeather = await Weather.findOne({ id: "13d2e1c2-feda-498f-8540-dd92f1087161" });

// Delete a existing entity
await oldWeather.delete();

// Get an array of many entities
const moreWeathers = await Weather.findMany({});

for (const weather of moreWeathers) {
    console.log(weather.id);
}

Support

If you want to buy someone a coffee, try to get in contact with the contributors from this package, they did 99.9% of the work, I just made it run in node because I was frustrated with existing packages.