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 🙏

© 2025 – Pkg Stats / Ryan Hefner

chfiledb

v0.4.1

Published

A simple database based on locally stored JSON-files

Readme

Overview

This module manages a tiny and simple database, which is storing datasets as JSON files.

Naming and structure

The data is organized as one JSON file per data set in folders. The folders are referred to below as a database. The JSON files are referred to as documents.

All methods return promises.

All parameters are transferred in an object, that is being destructured in the method (a.k.a. Named Arguments).

Usage

Install the module to your project.

npm i chfiledb

The module can be imported to your nodeJS project as ES6 module.

import chFileDB from 'chfiledb';

Caching

Documents that are loaded, created or saved are stored in a cache. Subsequent read accesses then access the object in RAM instead of the file, which speeds up reading considerably.

Database Methods

The so-called database methods allow the manipulation of entire databases. This allows databases to be created, deleted and connected.

init()

By default the database is being stored in a subfolder of the module.

Parameter

  • DBPath Absolute operating system path, where your databases will be stored.

Returning object contains:

  • status The status of the operation
import chfiledb from 'chfiledb';
chfiledb.init({ DBPath: 'C:/Users/alfa/Desktop/data' }).then(
    console.log
).catch(
    console.warn
)

create()

Creates a new database with the given dbName as ID.

Parameters

  • dbName: Defines the name of the new database. The name is case sensitive. This parameter is required, so no default provided.
  • debug: Gives additional outputs in case of an error. Defaults to false.

Returning Object contains

  • status: The status of the operation
  • connection: Object to work with the created database immediately.
chfiledb.create({ name: 'mydb'}).then(
    console.log
).catch(
    console.warn
)

delete()

Deletes a database with the give dbName as ID.

Parameters

  • dbName: Defines the name of the database to be deleted. The name is case sensitive. This parameter is required, so no default provided.
  • debug: Gives additional outputs in case of an error. Defaults to false

Returning object contains:

  • status: The status of the operation
chFileDB.delete({ dbName: 'myDB' }).then(
    console.log
).catch(
    console.warn
)

connect()

The connect() method creates a connection to the database. Actually, it checks, if the database is there and returns an object to interact with the database. See below.

Parameters

  • name: Defines the name of the database to be connected to. This parameter is required, so no default provided.
  • autoCreate: Controls whether the database should be created automatically if it does not exist. Defaults to true.
  • debug: Gives additional outputs in case of an error. Defaults to false

Returning Object contains

  • connection: Object to work with the created database immediately.
chFileDB.connect({
    dbName: 'myDB',
    autoCreate: true,
    debug: true,
}).then(
    console.log
).catch(
    console.warn
)

Document Methods

The document methods are contained in the return of the connect() method.

listIDs()

This method creates an array of all existing documents in the connected database. This is handy to get a full overview of the documents or to implement pagination.

This method does not use any parameter.

Returning object contains:

  • status: The status of the operation
  • payload: Array with the IDs
chFileDB.connect({ dbName: 'myDB' }).then(
    conn => conn.listIDs()
).then(
    console.log
).catch(
    console.warn
)

createDoc()

This method creates a new document in the connected database. The method can get the following parameters.

Parameters:

  • payload: Contains an object to be stored in the document. If the object doesn't contain an attribut id, it will be created automatically.
  • overwrite: If set to true, a document with the same id will be overwritten. There will be no confirmation and no backup. Defaults to false.
  • debug: Gives additional outputs in case of an error. Defaults to false.

Returning object contains:

  • status: The status of the operation
  • connection object: To chain more methods
  • payload: The Object, that has been written to the database
const rezept = {
    name: "Pasta Carbonara",
    zubereitungszeit: 30,
    portionen: 4
};
const film = {
    titel: "Inception",
    regisseur: "Christopher Nolan",
    jahr: 2010,
};

chFileDB.connect({
    dbName: 'myDB'
}).then(
    conn => conn.createDoc({
        payload: rezept
    })
).then(
    conn => conn.createDoc({
        payload: film
    })
).catch(
    console.warn
)

loadDoc()

This method is loading a single document with a given ID.

Parameters

  • id: ID of the document to be loaded. This parameter is required, so no default provided.
  • ignoreCache: The cache is ignored and the data record is read from the file.
  • debug: Gives additional outputs in case of an error. Defaults to false.

Returning object contains

  • status: Status of the operation
  • doc: Document as stored currently
  • connection: Connection to the database to chain more operations
  • cached: Info, if the loaded data has been read from cache
chFileDB.connect({ dbName: 'myDB' }).then(
    conn => conn.loadDoc({ id: '2486ikkhe1q' })
).then(
    console.log
).catch(
    console.warn
)

loadDocs()

This method is loading a bunch of documents based on a given array of IDs.

Parameters

  • ids: Expects an array of IDs of the documents to be loaded.
  • ignoreCache: The cache is ignored and the data record is read from the file.
  • debug: Gives additional outputs in case of an error. Defaults to false.

Returning object contains

  • status: Status of the operation
  • docs: Array with the documents referred to by the IDs. An ID, that doesn't lead to a document, returns undefined.
  • connection: Connection to the database to chain more operations
chFileDB.connect({ dbName: 'myDB' }).then(
    conn => conn.loadDocs({
        ids: [
            '14hrd1th3px',
            '2486ikkhe1q',
            'pinuc3ldigw'
        ]
    })
).then(
    console.log
).catch(
    console.warn
)

updateDoc()

This method updates and adds attributes into a document. The attributes of the document remain unaffected unless they are given a new value.

Parameters

  • payload: The attributes to be updated or added. The payload has to contain the ID of the document to be updated.
  • debug: Gives additional outputs in case of an error. Defaults to false.

Returning object contains

  • status: Status of the operation
  • doc: Document as stored currently
  • connection: Connection to the database to chain more operations
// Document before updating
// {
//  id: "2486ikkhe1q",
//  customNum: 13
// }

chFileDB.connect({ dbName: 'myDB' }).then(
    conn => conn.updateDoc({
        payload:{
            id:'2486ikkhe1q',
            anAttr: 1000
        }
    })
).then(
    console.log
).catch(
    console.warn
)

// {
//  id: "2486ikkhe1q",
//  customNum: 13,
//  anAttr: 1000
// }

deleteDoc()

This method deletes a single document. There is no security check or backup so be sure, that you indeed want to delete the document.

Parameters

  • id: The id of the document to delete. This parameter is required, so no default provided.
  • payload: Instead of an ID, you can transfer the object to be deleted. The method then takes the id from that object.
  • debug: Gives additional outputs in case of an error. Defaults to false.

Returning object contains

  • status: Status of the operation
chFileDB.connect({
    dbName: 'myDB'
}).then(
    conn => conn.deleteDoc({
        id: 'q6igqag1sxs'
    })
)

hideDoc()

This method hides a document as an alternative to delete it. Hidden documents are intended to be treated just like deleted documents. But with the possibility to regain the data.

Parameters

  • id: ID of the document to be hidden
  • debug: Gives additional outputs in case of an error. Defaults to false.

Returning object contains

  • status: Status of the operation
  • doc: Document as stored currently
  • connection: Connection to the database to chain more operations
chFileDB.connect({ dbName: 'myDB' }).then(
    conn => conn.hideDoc({
        id: 'l5gg71jl90g',
    })
).then(
    console.log
).catch(
    console.warn
)

unhideDoc()

This method unhides a document, that has been hidden beforehand.

Parameters

  • id: ID of the document to be unhidden
  • debug: Gives additional outputs in case of an error. Defaults to false.

Returning object contains

  • status: Status of the operation
  • doc: Document as stored currently
  • connection: Connection to the database to chain more operations
chFileDB.connect({ dbName: 'myDB' }).then(
    conn => conn.unhideDoc({
        id: 'l5gg71jl90g',
    })
).then(
    console.log
).catch(
    console.warn
)

findDoc()

This method will search for documents that fit a given pattern.

To be done


deleteAttribute()

This method deletes given attributes from a document.

Parameters

  • id: Document ID, in which the attribute shall be removed.
  • attributes: An array with te attribute key to be remove.
  • debug: Gives additional outputs in case of an error. Defaults to false.

Returning object contains

  • status: Status of the operation
  • doc: Document as stored currently
  • connection: Connection to the database to chain more operations
// Document before updating
// {
//  id: "2486ikkhe1q",
//  customNum: 13,
//  a1: 100
// }

chFileDB.connect({ dbName: 'myDB' }).then(
    conn => conn.deleteAttributes({
        id: '2486ikkhe1q',
        attributes: [
            'a1'
        ],
        debug: true
    })
).then(
    console.log
).catch(
    console.warn
)


// Document after updating
// {
//  id: "2486ikkhe1q",
//  customNum: 13
// }