simple-node-jsondbv2
v1.0.4
Published
A very easy to use jsonDB for small projects/ Testing.
Downloads
1
Readme
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.
| Params | Required | Type | Description | | ---- | ---- | ---- | ---- | | collection | True | String | Target collection/table | | data | True | Object/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
| Params | Required | Type | Description | | ---- | ---- | ---- | ---- | | collection | True | String | Target collection/table | | filter | True | Object | Filter. 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
| Params | Required | Type | Description | | ---- | ---- | ---- | ---- | | collection | True | String | Target collection/table | | filter | True | Object | Filter. Pass an empty Object to find all |
Return
- Object
dbUpdate(collection: string, filter: object, data: object, newDoc: boolean[false])
- Update all the document that matches the filter.
| Params | Required | Type | Description | | ---- | ---- | ---- | ---- | | collection/ | 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. | | newDoc | False | BOolean | If 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.
| Params | Required | Type | Description | | ---- | ---- | ---- | ---- | | collection | True | String | Target collection | | filter | True | Object/Array | Filter. 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
Operators | Descriptions ------------- | ------------- $gte | Greater and equals to $gt | Greater than $lte | Lesser and equals to $lt | Lesser than