1.0.4 • Published 2 years ago

simple-node-jsondbv2 v1.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

simple-node-jsondbv2

Extremely lightweight and simple local json database for personal / small / test projects.

Version 2 of simple-node-jsondb

Installation

npm i simple-node-jsondbv2

Usage

const db = require('simple-node-jsondbv2');

or

import { dbInsert, dbUpdate } from 'simple-node-jsondbv2';

Features

  • + Extremely lightweight
  • + Very simple to use
  • + Syntax similar to mongoose
  • + Capable of find, insert, update, delete
  • - Targeting deep Object does not work. You need to manipulate the data manually and save it as whole.
  • - Indexing not supported yet, thus not recommended for large scale production use. Use a proper DB for that.

Table Of Contents

  • dbInit()
  • dbInsert()
    • Insert one
    • Insert many
  • dbFind()
    • Find All
    • Multiple filters
    • Advance Search
  • dbFindOne()
  • dbUpdate()
    • Update All records
    • Update One
    • New record
  • dbDelete()
    • Delete one specific record
    • Delete records with conditions
  • Special Operators

dbInit(dbPath: string)

To use the db, you need to initialize the db folder onload before you can use it, Simply pass in the desired absolute path to change the directory. Path should be absolute

const path = require("path")
const dbPath = path.join(__dirname, '<your-path-folder>') 
dbInit(dbPath)

This will create a "db" folder on that path, all the data will be stored in the "db" folder.

dbInsert(collection: string, data: object[] | object)

  • Insert data into database.
  • Refer as "collection" for noSQL users, or "table" for users that is more fimilar with SQL.
  • New object id will be injected as "_id" for each document.
ParamsRequiredTypeDescription
collectionTrueStringTarget collection/table
dataTrueObject/Object[]Data to insert into collection/table. Can be either Object or Array.

Return

  • None

Example

Insert one

const fruit = {
    name: "Apple",
    price: 15
}
// New collection "fruits" will be created automatically.
await dbInsert( 'fruits', fruit );

Insert many

By using Array

const fruitList = [
    { name: "Apple", price: 15 },
    { name: "Orange", price: 30 },
]
await dbInsert( 'fruits', fruitList );

dbFind(collection: string, filter: object)

  • Find and retrieve data from database
ParamsRequiredTypeDescription
collectionTrueStringTarget collection/table
filterTrueObjectFilter. Pass an empty Object to find all

Return

  • Array

Example

Find All

const results = await dbFind( 'fruits' , {});

Multiple filters

const results = await dbFind( 'fruits' , {name: "Apple", price: 15});

Advance filters

Advanced search with filtering can be done by using Special Operators. Check out Special Operators section in the end of the page for more info.

const results = await dbFind('fruits',{
    name: "Apple",
    price: { $lte: 10 }
});

dbFindOne(collection: string, filter: object)

  • Find and retrieve data from database
ParamsRequiredTypeDescription
collectionTrueStringTarget collection/table
filterTrueObjectFilter. Pass an empty Object to find all

Return

  • Object

dbUpdate(collection: string, filter: object, data: object, newDoc: booleanfalse)

  • Update all the document that matches the filter.
ParamsRequiredTypeDescription
collection/TrueStringTarget collection/table
filterTrueObject/ArrayFilter. Can be either Object or Array.
newDataTrueObject/ArrayNew data to overwrite. Can be either Object or Array.
newDocFalseBOoleanIf enabled, it will create a new record is non are found

Return

{ 
    modified: number,
    new: number 
}

Example

Update All records

const stats = dbUpdate("fruits", {}, { price: 10 })

Update One

const stats = dbUpdate("fruits", {name: "Apple"}, { price: 10 })

dbDelete(collection: string, filter: object)

  • Delete document that matches the filter.
ParamsRequiredTypeDescription
collectionTrueStringTarget collection
filterTrueObject/ArrayFilter. Can be either Object or Array.

Return

{ 
    deleted: number
}

Example

Delete one specific record

const deleted = await dbDelete("fruits", { name: "Apple" })

Delete records with conditions

const deleted = await dbDelete("fruits", { 
    price: { $gte: 10, $lt:20 }
})

Special Operators

OperatorsDescriptions
$gteGreater and equals to
$gtGreater than
$lteLesser and equals to
$ltLesser than
1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago