@jasonsbarr/json-db

0.2.0 • Public • Published

@jasonsbarr/json-db

Simple JSON file-based database with a fluent query API.

Usage

import { Db } from "@jasonsbarr/json-db";

Creating a database

In file.json:

{
    "users": [],
    "companies": [],
    "organizations": [],
    "members": [],
    "organization_member": []
}
// entities map a model to its table
const entities = {
    user: "users",
    company: "companies"
}

const db = Db("path/to/file.json", { entities });

Fetching records

const users = from("users")
    .select()
    .where("joinDate", ">", new Date("January 1, 2015").getTime())
    .include("organization")
    .get();

Inserting records

Unique IDs are generated automatically on insertion. IDs are UUID v4.

const newCompany1 = {
    name: "Acme Inc.",
    founded: 1955
};

const newCompany2 = {
    name: "Umbrella Corporation",
    founded: 1968
}

const rows = into("companies")
    .insert(newCompany1)
    .insert(newCompany2)
    .write();

Updating records

const rows = from("companies")
    .updateWhere("founded", "<", 1970)
    .set({ old: true })
    .write();

Deleting records

const rows = from("users")
    .delete("0803d10b-6f50-4e40-b9e7-e107466c65ac")
    .write();

/@jasonsbarr/json-db/

    Package Sidebar

    Install

    npm i @jasonsbarr/json-db

    Weekly Downloads

    0

    Version

    0.2.0

    License

    MIT

    Unpacked Size

    12.1 kB

    Total Files

    4

    Last publish

    Collaborators

    • jasonsbarr