@jasonsbarr/json-db
v0.2.0
Published
Simple JSON DB class for educational purposes
Downloads
2
Readme
@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();