Skip to main content

FileDB

GitHub Workflow Status (branch) GitHub release (latest by date) GitHub Release Date GitHub top language LGTM Grade deno-code-coverage GitHub Repo stars GitHub nest badge

⚡ A lightweight local JSON database for Deno.

Why Use FileDB?

  • Simplicity: the module is semantic and easy to use.
  • Familiar: the module is inspired by MongoDB, you can use this module just like you know that.
  • Suit with RESTful API: the module API is suited with RESTful API, you may refer to this example.

Quick Start

$ git clone https://github.com/jswildcards/filedb.git
$ cd ./filedb/example
$ deno run --allow-read --allow-write hello_world.ts

Usage

More examples can be found here.

// main.ts
import { FileDB, Document } from "https://deno.land/x/filedb@0.0.4/mod.ts";

interface User extends Document {
  firstName?: string;
  lastName?: string;
  favourites?: string[];
}

const db = new FileDB({ rootDir: "./data", isAutosave: true }); // create database with autosave
const users = db.getCollection<User>("users"); // get User collection

// insert users
users.insertOne({
  firstName: "fancy",
  lastName: "foo",
  favourites: ["🍎 Apple", "🍐 Pear"],
});

users.insertMany([
  {
    firstName: "betty",
    lastName: "bar",
    favourites: ["🍌 Banana"],
  },
  {
    firstName: "benson",
    lastName: "baz",
    favourites: ["🍌 Banana"],
  },
]);

// get users
console.log(users.find((el) => el.lastName?.includes("ba")).value());
console.log(users.findOne({ firstName: "fancy" }));

// update users
await users.updateOne(
  (el) => el.favourites?.[0] === "🍌 Banana",
  { favourites: ["🍎 Apple", "🍐 Pear"] },
);
await users.updateMany(
  (el) => el.lastName?.includes("ba"),
  { favourites: ["🍉 Watermelon"] },
);

// delete users
users.deleteOne({ firstName: "fancy" });
await users.deleteMany((el) => (el.favourites?.length ?? []) >= 1);

// drop database
await db.drop();

Run file

$ deno run --allow-read --allow-write main.ts

API

Please see the documentation

Contribution

Contributing to this module is very welcome. Read this guideline.

Caution!

Although this module is inspired by MongoDB, it cannot provide features as rich as MongoDB at this moment. We will implement and release the inequality filters and aggregate functions as soon as possible. So stay tuned.

This module is still unstable. So the module API may be varied largely between build versions.

This module is only suitable for small-scaled projects. As when the database is large enough, it will be slow down with this file-based database structure.