Simple JSON "database" for Deno. You can use it for small projects like a url shortener.

INFO: If you find a bug or have a feature request, feel free to create an issue or contribute. 🙂

Run

deno run --allow-read --allow-write xxx.ts

Quick Usage

import { SimpleDB } from "https://raw.githubusercontent.com/EntenKoeniq/SimpleDB/master/mod.ts";

const db = new SimpleDB({filePath: "./test.json"}); // or 'new SimpleDB();' to create a database with the default name "db.json"

/* ===== INSERT ===== */
let insert = await db.insert({ username: "EntenKoeniq" });
await db.save();
console.log(insert);

/* ===== FIND ONE ===== */
let findOne = await db.findOne({ username: "EntenKoeniq" });

if (findOne === undefined) console.log("Not found!");
else console.log(findOne);

/* ===== EXISTS ===== */
console.log(await db.exists({ username: "EntenKoeniq" }));

/* ===== EXISTS ===== */
await db.findOneAndUpdate({ username: "EntenKoeniq" }, { username: "KoeniqEnten" });
await db.save();

/* ===== DELETE ===== */
if (await db.delete({ username: "EntenKoeniq" })) {
  await db.save();
  console.log("Succesfully deleted!");
}

JSON example (without delete)

[
  {
    "_id": "n2toa7c4yr2ils1gmdlitu55",
    "username": "KoeniqEnten"
  }
]

How to use

insert

await db.insert({ url: "google.de", short: "cj5f39", clicks: 0 }); // return a boolean value

exists

await db.exists({ url: "google.de" }); // return a boolean value

findOne

/*
  * Look for a key "URL" with the value "google.de".
  * Return: index with key "url" and value "google.de" not found (undefined).
  * Return: index with key "url" and value "google.de" found (object of index).
*/
await db.findOne({ url: "google.de" });

/* ===== RESULT ===== */
{
  "_id": "n2toa7c4yr2ils1gmdlitu55",
  "url": "google.de",
  "short": "cj5f39",
  "clicks": 0
}

findOneAndUpdate

/*
  * Replace value of key "url" from the first index with key "short" and value "cj5f39".
  * Return: index with key "short" and value "cj5f39" not found (undefined).
  * Return: index with key "short" and value "cj5f39" found (object of index).
*/
await db.findOneAndUpdate({ short: "cj5f39" }, { url: "google.com" });

delete

await db.delete({ url: "google.de" }); // Delete the first index with key "url" and value "google.de" (return a boolean value)

save

await db.save(); // Save all to x.json