node-chunkdb

json database for storing big data

Usage no npm install needed!

<script type="module">
  import nodeChunkdb from 'https://cdn.skypack.dev/node-chunkdb';
</script>

README

ChunkDB

Is a powerful json database for storing a large amount of data on a file system.

Require Node JS.

Installation


Using npm:

$ npm i node-chunkdb

In Node:

// Load build

import ChunkDB from 'node-chunkdb'

or

const ChunkDB = require('node-chunkdb');

Api:

// Initialization

let INDEX_FILE_PATH = '.../db/index.json';

let STORE_ENTITIES_PATH = '.../db/entities';

let db = new ChunkDB(INDEX_FILE_PATH, STORE_ENTITIES_PATH);

Options:

  • Enabling indentation when the entity is serialized in json format

    db.enableIndent = false (default)

    Note : it will cause an increase in the size of the entity file

  • Indentation value that will be used when the entity is serialized in json format

    db.indentSpaces = 2 (default) - Used only if indentation is enabled

EntityInfo Class:

This class is used to create the response object when creating a entity and contains the following properties:

  • id : unique id for entity create, example XEhsdaGxW

  • date: entity creation date

  • fileName: entities path

  • reference: optional information for searching the entity in the db, example { email: tony@mail.com }

  • Example

    let entityInfo = db.add(....);
    

    Output:

    entityInfo = {
      
      id: 'XEhsdaGxW',
      date: dayjs-object,
      filename: "server/db/entities/entity-XEhsdaGxW.json",
      reference: { email: tony@mail.com }
    
      }
    

Members:

- db.index.entities

Get the index of entities stored in the db

The collection contains the data necessary to find the requested entity,

entities is an array and therefore various methods can be used .find, .map, .sort, .some, .every etc .. to query

entities does not contain entity data but only EntityInfo

let entities = db.index.entities;

entities = [
  {
    id: 'XEhsdaGxW',
    date: '2020-05-19T06:32:35.173Z',
    fileName: 'server/db/entities/entity-XEhsdaGxW.json',
    reference: { options: { a: 1 } , creationTimeRequired: 1024 }
  },
  {
    id: 'Ywit-ZM94',
    date: '2020-05-22T08:10:33.842Z',
    fileName: 'server/db/entities/entity-Ywit-ZM94.json',
    reference: { options: { a: 2 }, creationTimeRequired: 500}
  }
]
Example Query:
let entities = db.index.entities.find((entity) => entity.reference.creationTimeRequired < 1000);

// OUTPUT
entities = [
  {
    id: 'Ywit-ZM94',
    date: '2020-05-22T08:10:33.842Z',
    fileName: 'server/db/entities/entity-Ywit-ZM94.json',
    reference: { options: { a: 2 }, creationTimeRequired: 500}
  }
]

let entityContent = db.get(entities[0].id);

// OUTPUT
entityContent = { name: Tony, role: admin }

Methods:

- db.add

Add to db (entity creation)

db.add(entity: object, reference: any) -> EntityInfo

// If an error occurs, an exception EntityError or otner will be raised

Entity: is a js object that will be serialized and adding to db.

Reference (optional): is anything that will serve in finding the entity

RETURN: the method returns an EntityInfo object with information about the created entity

  • Example

    let entityInfo = db.add({ name: Tony, role: admin }, { email: tony@mail.com });
    

- db.remove

Remove entity from the db

db.remove(id) -> boolean

Id: is the identifier of the entity

RETURN : the method returns a Boolean value to confirm the successful operation

  • Example

    let { id } = db.add(....);
    // id = 'XEhsdaGxW'
    db.remove(id); 
    // Output TRUE or FALSE
    

- db.get

Get entity content from the db

db.get(id) -> EntityContent || null

Id: is the identifier of the entity

RETURN : the method returns the contents of the entity

  • Example

    // id = 'XEhsdaGxW'
    let entityContent = db.get(id);
    
    // OUTPUT
    entityContent = { name: Tony, role: admin }