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

fs-databases

v1.0.7

Published

A package you can use to create a JSON Database with fs

Downloads

5

Readme

Database Package

Documentation

Setup

const db = require('db.json');
const Database = new db.Database({
  name: "My Database", // the name of your database
  type: "object", // the type of your database: object or array
  folder: "./databases" // the folder where your database files are stored
});

Use environment file to setup

const db = require('db.json');
let Database;
db.convertToEnvironmentObject({
  path: "./environment.txt" // the environment file where your options are stored
}).then((result) => {
  Database = new db.Database(result.object);
});

Files

Get file by index
let file;
Database.files().then((files) =>{
  file = files[0 /*index of your file*/]
});
Get file by name
let file;
Database.files().then((files) => {
  file = files.find((file) => file.name === "database.json" /*Name of your file*/);
});
Get whole database
file.read().then((content) => {
  console.log(content.data /*newest data in file*/ || file.content /*value when the files were defined*/);
});
Object databases
Set key to value
file.set("foo" /*key*/, "bar" /*value*/).then((result) => {
  console.log(result);
});
Remove key
file.remove("foo" /*key*/).then((result) => {
  console.log(result);
});
Get key
file.get("foo" /*key*/).then((result) => {
  console.log(result.value);
});

Array databases

Add value
file.add("Hello World" /*value*/).then((result) => {
  console.log(result);
});
Set index to value
file.set(2 /*index*/, "db.json" /*value*/).then((result) => {
  console.log(result);
});
Remove index
file.remove(2, /*index*/).then((result) => {
  console.log(result);
});
Get index
file.get(1 /*index*/).then((result) => {
  console.log(result.value);
})

Synchronous Documentation

Setup

const db = require('db.json');
const Database = new db.Database({
  name: "My Database", // the name of your database
  type: "object", // the type of your database: object or array
  folder: "./databases" // the folder where your database files are stored
  synchronous: true // make your database synchronous
});

Use environment file to setup

const db = require('db.json');
const Database = new db.Database(db.convertToEnvironmentObjectSync({
  path: "./environment.txt" // the environment file where your options are stored
}));

Files

Get file by index
const file = Database.files()[0 /*index of your file*/]
Get file by name
const file = Database.files().find((file) => file.name === "database.json" /*Name of your file*/);
Get whole database
console.log(file.read().data || file.content);
Object databases
Set key to value
console.log(file.set("foo" /*key*/, "bar" /*value*/));
Remove key
console.log(file.remove("foo" /*key*/));
Get key
console.log(file.get("foo" /*key*/));

Array databases

Add value
console.log(file.add("Hello World" /*value*/));
Set index to value
console.log(file.set(2 /*index*/, "db.json" /*value*/));
Remove index
console.log(file.remove(2, /*index*/));
Get index
console.log(file.get(1 /*index*/).value);

Todo

  • There are currently no todos

Disclaimer

This package is mainly promise based but also contains a synchronous function for each function. If you're using a class to register a database it will call the asynchronous version and return the result synchronously.