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

simple-node-jsondb

v1.0.15

Published

A very easy to use jsonDB for small projects/ Testing.

Downloads

7

Readme

This library is no longer maintained.

Use version 2 instead

simple-node-jsondbv2

simple-node-jsondb

Installation

$ npm install simple-node-jsondb@latest const db = require('simple-node-jsondb')

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.

Things I would probably add (maybe / depends)

  • Capability to Push into array.
  • Capability to Pull from array.
  • Capability to Find Deep properties.
  • Indexing

Not so simple anymore huh?

Table Of Contents

  • Installation
  • Features
  • Things I would probably add
  • Table Of Contents
  • API
    • Example Dataset
  • db.init()
  • db.insert()
    • Insert one
    • Insert many
  • db.find()
    • Find All
    • Find one
    • Multiple filters
    • Advance Search
  • db.update()
    • Update All records
    • Update One
  • db.delete()
    • Delete one specific record
    • Delete records with conditions
    • Delete All
  • Special Operators

API

You can either use api as:

db.find().then((result)=>{
	/*Your logic*/
}).catch(()=>{
	/*Error handle*/
})

or ***Recommended

try{
	let result = await db.find()
	/*Your logic*/
}catch(err){
	/*Error handle*/
}

Example Dataset

Data will change accordingly throughout the documentation. Automatically start as an empty object

{}

db.init([dbPath])

To use the db, you need to initialize the db onload before you can use it, by default the db path will be in the node_module folder (<path>/node_modules/simpleJsonDB). Simply pass in the desired absolute path to change the directory.

/*Recommend to use "path"*/
const path = require("path")
/* This will initialize the db in your root project folder */
const dbPath = path.join(__dirname,'./db.json') 
db.init( dbPath )

db.insert([collection/table], [data])

  • Insert data into database.
  • Refer as "collection" for noSQL users, or "table" for users that is more fimilar with SQL.

| Params | Required | Type | Description | | ---- | ---- | ---- | ---- | | collection/table | True | String | Target collection/table | | data | True | Object/Array | Data to insert into collection/table. Can be either Object or Array. |

Return

  • None

Insert one

const newUser = {
	"name":"Alice",
	"age":15,
	"food": [
		"Orange","Fish"
	],
}
/*Omitting async handles*/
/* New collection/table "users" will be added automatically */
await db.insert( 'users', newUser )

let result = await db.find( 'users' , {} )
console.log(result)
{
	"users":[
		{
			"name":"John",
			"age":20,
			"food": [
				"Burger","Sushi"
			]
		}
	]
}

Insert many

By using Array

const newUsers = [
	{
		"name":"Susan",
		"age":21,
		"food": [
			"Salad","Apple"
		],
	},
	{
		"name":"Alice",
		"age":15,
		"food": [
			"Orange","Fish"
		]
	}
]
/*Omitting async handles*/
await db.insert( 'users', newUsers )

let result = await db.find( 'users' , {} )
console.log(result)
{
	"name":"John",
	"age":20,
	"food": [
		"Burger","Sushi"
	],
},
{
	"name":"Susan",
	"age":21,
	"food": [
		"Salad","Apple"
	],
},
{
	"name":"Alice",
	"age":15,
	"food": [
		"Orange","Fish"
	]
}

db.find([collection/table], [filter])

  • Find and retrieve data from database

| Params | Required | Type | Description | | ---- | ---- | ---- | ---- | | collection/table | True | String | Target collection/table | | filter | True | Object/Array | Filter. Can be either Object or Array. Pass an empty Object to find all |

Return

  • Array

Basic usage

Find All

/*Omitting async handles*/
let result = await db.find( 'users' , {} )

Find one

Finding a single record.

/* Omitting async handles*/
let result = await db.find( 'users' , { name : "John" } )
console.log(result)
/* Result:
	[{
		"name":"John",
		"age":20,
		"food": [
			"Burger","Sushi"
		]
	}]
*/

Multiple filters

Finding with multiple AND conditions by using Array.

/* Omitting async handles*/
let result = await db.find( 'users' , [ { name : "John" }, { age : 20 } ] )
console.log(result)
/* Result:
	[{
		"name":"John",
		"age":20,
		"food": [
			"Burger","Sushi"
		]
	}]
*/

Advance Search

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.

/* Omitting async handles*/
let result = await db.find( 'users' , {
	age : { $ge : 15, $l : 21 }
})
console.log(result)
/* Result:
	[
		{
			"name":"John",
			"age":20,
			"food": [
				"Burger","Sushi"
			]
		},
		{
			"name":"Alice",
			"age":15,
			"food": [
				"Orange","Fish"
			]
		}
	]
*/

db.update([collection/table], [filter], [newData])

  • Find and retrieve data from database

| Params | Required | Type | Description | | ---- | ---- | ---- | ---- | | collection/table | True | String | Target collection/table | | filter | True | Object/Array | Filter. Can be either Object or Array. | | newData | True | Object/Array | New data to overwrite. Can be either Object or Array. |

Return

  • Array

Update All records

/* Omitting async handles*/
await db.update( 'users', {}, { food:[] } )
let result = await db.find( 'users', {})
console.log(result)
/* Result:
	[
		{
			"name":"John",
			"age":20,
			"food": []
		},
		{
			"name":"Susan",
			"age":21,
			"food": []
		},
		{
			"name":"Alice",
			"age":15,
			"food": []
		}
	]
*/

Update One

/* Omitting async handles*/
/* Will automatically add any pre-undefined property */
await db.update( 'users', { name:"John" }, [ { age: 25 }, { sports : "Basketball" } ] )
let result = await db.find( 'users', { name:"John" } )
console.log(result)
/* Result:
	[
		{
			"name":"John",
			"age":25,
			"food": [],
			"sports":"Basketball"
		}
	]
*/
  • filter param works with Special Operators and AND too.

db.delete([collection/table], [filter], [confirm])

  • Find and retrieve data from database

| Params | Required | Type | Description | | ---- | ---- | ---- | ---- | | collection/table | True | String | Target collection/table | | filter | True | Object/Array | Filter. Can be either Object or Array. | | confirm | False | Object/Array | Only needed when no filter is specified to protect accidentialy deletion of all data. Set it True to confirm the action. |

Return

  • None

Delete one specific record

/* Omitting async handles*/
await db.delete( 'users', { name : "John" } )
let result = await db.find( 'users', {})
console.log(result)
/* Result:
	[
		{
			"name":"Susan",
			"age":21,
			"food": []
		},
		{
			"name":"Alice",
			"age":15,
			"food": []
		}
	]
*/

Delete records with conditions

/* Omitting async handles*/
await db.delete( 'users', { age : { $ge : 15 } } )
let result = await db.find( 'users', {})
console.log(result)
/* Result:
	[]
*/

Delete All

/* Omitting async handles*/
await db.delete( 'users', {}, true )

Special Operators

Operators | Descriptions ------------- | ------------- $ge | Greater and equals to $g | Greater than $le | Lesser and equals to $l | Lesser than