npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

file-system-db

v2.1.0

Published

A simple, lightweight, synchronous, database for Node.js powered by JSON.

Downloads

2,166

Readme

license version gzipped size downloads last commit

discord buy me a coffee

Please Note: This package saves data persistently. This means that this will not work on places like Heroku and similar services.

v2 Migration: If you are migrating from v1 to v2, consider reading the detailed changes on this pull request.

Features

  • 📦 Works Out of the Box | Only two lines of code are required to set up and start using your own database.

  • 🤹‍♂️ Unlimited Databases | You can create as many databases as you want, whenever you want.

  • 🗃️ Saved as JSON Files | This package uses JSON files to act as databases, so you can easily understand, edit and backup/export the data.

  • 📝 Key-Value Based | Designed with beginners in mind, so you can store any type of data in your database with ease.

  • 📔 Dot Notation Support | You can use dot notation to store and retrieve JSON fields and data.

  • Fast and Synchronous | All operations are synchronous, so saving and retrieving data takes less than few milliseconds.

  • 🚫 No Dependencies | This package is built on top of the File System module built into Node.js, making the package size very small.

Install Package

File System DB's size footprint is tiny, making the installation process really quick and easy.

npm install file-system-db --save

Setup and Usage

To set up your database, you only need to write two lines of code.

First of all, let's import the package.

const { FSDB } = require("file-system-db");

Now for the fun part, let's create a database. Creating one only takes a single line of code, and the best part is you can make as many as you want! You don't need to worry about making sure the JSON file and directory path exists, as FSDB can handle that for you.

It's as simple as creating a variable and assigning it to a new FSDB instance.

const db = new FSDB();

There are also two optional parameters that can be passed to the constructor:

  • path - The file path to the JSON file that will act as the database. (defaults to "database.json")
  • compact - This determines whether the database should be compacted after every save. This won't look easily readable to humans, but it will save you unnecessary storage space. (defaults to true)

Example:

// Creates a database at `./db.json` and don't compact it
const db = new FSDB("./db.json", false);

If at any point you want to backup your database in the case of having to undo something later, you can use db.backup(). Simply pass the path to the file you want to save the backup to. Please note that all backups are saved with compact set to true to save space.

// Saves the current contents of the database to `"./db-backup.json"`
db.backup("./db-backup.json");

Here are some examples of how to use the database.

// Saves the JSON: `{ "player": "WillTDA" }`
db.set("player", "WillTDA");

db.get("player");
// => "WillTDA"

You can also use dot notation to store and retrieve JSON data.

// Saves the JSON: `{ "player": { "name": "WillTDA", "level": 15 } }`
db.set("player.name", "WillTDA");
db.set("player.level", 15);

// Alternatively, using an object as the value will also work
db.set("player", { name: "WillTDA", level: 15 });

db.get("player");
// => { "name": "WillTDA", "level": 15 }

You can get all data in the database with db.getAll().

db.getAll();
// => [{ "key": "player", "value": { "name": "WillTDA", "level": 15 } }]

To get all data starting with a certain key, you can use db.startsWith().

db.startsWith("play");
// => [{ "key": "player.name", "value": "WillTDA" }, { "key": "player.level", "value": 15 }]

To see if a key exists, use db.has().

db.has("player.name");
// => true

To delete data, you can use db.delete().

// Saves the JSON: `{ "player": { "name": "WillTDA" } }`
db.delete("player.level");

You can delete all data in the database with db.deleteAll().

// Saves the JSON: `{}`
db.deleteAll();

Pushing and pulling data on arrays is also supported.

// Saves the JSON: `{ "inventory": [ "Diamond Sword", "Diamond Pickaxe" ] }`
db.push("inventory", "Diamond Sword");
db.push("inventory", "Diamond Pickaxe");

// Saves the JSON: `{ "inventory": [ "Diamond Sword" ] }`
db.pull("inventory", "Diamond Pickaxe");

// Alternatively, you can also pass multiple values to these methods
// Saves the JSON: `{ "inventory": [ "Diamond Sword", "Diamond Pickaxe" ] }`
db.push("inventory", "Diamond Sword", "Diamond Pickaxe");

// Saves the JSON: `{ "inventory": [] }`
db.pull("inventory", "Diamond Sword", "Diamond Pickaxe");

Mathematical operations on numbers can also be done.

// Saves the JSON: `{ "coins": 500 }`
db.add("coins", 500);

// Saves the JSON: `{ "coins": 400 }`
db.subtract("coins", 100);

// Saves the JSON: `{ "coins": 800 }`
db.multiply("coins", 2);

// Saves the JSON: `{ "coins": 200 }`
db.divide("coins", 4);

Contact Us