bigdb
v4.0.0
Published
A json file database with methods similar to mongodb methods
Downloads
77
Maintainers
Readme
Bigdb.
powered by lodash
Bigdb is a javascript backend library for reading and writing data in a json file database. It store it's data locally on the server just like sqlite, but instead it store its data in a json file. Bigdb can read and write up to 10gb data. It can be used with any library that have access to fs module of node js.
Installation.
Install with npm i bigdb.
Usage.
Bigdb METHODS are similar to that of mongoose (mongodb) and can perform both read and write operation.
conts {Bigdb} = require('bigdb')
//OR
import {Bigdb} from 'bigdb'
To create new document, for example Post, call the method Bigdb()
Your first parameter is the path where you database will leave, can be null as well
Second paramter is the name of the collection and
Optionally you can pass true or false (boolean) to the third parameter of the function.This spacifies wheather you want to keep your collection in one file or ecach file for each collection.
true - each file for each collection.
false - one file called database.json.
Call Bigdb and pass nessecary parameter.
const Posts = Bigdb(null, 'Posts')
//will create Db/database.json at the root of your app
//OR
const Post = Bigdb('path/Database', 'Posts')
//will create path/Database/database.json at the root of your app
//OR
const Post = Bigdb(null, 'Posts', true)
//will create Db/Posts.json at the root of your app
let result = await Post.create({title:'Post title 1',body:'i am Post 1'}) //single
let result = await Post.create([{title:'Post title 1',body:'i am Post 1'}, <...>]) //bulk
//this creates new documens in the database
The above code will create a corresponding path at the root of your app if it does not exist, - "Posts" is the name of the collection, if we had passed users it would create a users collection in the database
Then it will return something like this;
{
id: 1,
title:'Post title 1',
body:'i am Post 1',
createdAt: '2024-01-08T09:39:09.976Z',
updatedAt: '2024-01-08T09:39:09.976Z'
}
Its that simple
It auto asign createdAt, updatedAt and id to it as it saves to database. The id is an autoincreamenting id.
As i told you earlier that the methods are similar to mongodb methods, All queries are case insensitive, uppercase letter are the same as lowercase letter ('dog' === 'doG' === 'DOG').
We also have many more methods like;
.findById(id)
await Post.findById(1)
It returns the document with id 1 from Posts collection.
.findOne()
await Post.findOne({name:{$has:'john'}})
It returns the first document that matches the query
.find()
This takes in two optonal parameter,
- First paramerter is the query
- Second parameter is attribute you don't want to appear in the result Every query that has .find() ends with .get() .eg
await Post.find().get()
await Post.find({name:'jane doe', age:20}).get()
await Users.find({name:'jane doe', age:20}, {password:false, salary:false}).get()
It returns an array of all the documents that matches the query from the collection.
[
{
id: 1,
title:'Post title1',
body:'i am Post 1',
createdAt: '2024-01-08T09:39:09.976Z',
updatedAt: '2024-01-08T09:39:09.976Z'
},
{
id: 2,
title:'Post title2',
body:'i am Post 2',
createdAt: '2024-01-08T09:39:09.976Z',
updatedAt: '2024-01-08T09:39:09.976Z'
},
]
Also you can request for particular kind of data like
await Post.find({views:2}).get()
await Post.find({views:2, title:{$has:'Post'}}).get()
We also have the .sort(), .skip() and .limit()
await Post.find({}).sort({id:1}).get()
//1 for ascending and -1 for for descending
await Post.find().skip(2).get()
//skips the first two match
await Post.find().limit(5).get()
//it retuns 5 documents
await Post.find().sort({id:-1}).limit(5).skip(2).get()
/*it retuns 5 documents sorted in
descending order basing on id
*/
The .sort(), .limit() and .skip() have no orders in which they are called like in mongodb.
sort() - For sorting the result in either ascending or descending order. It takes in only one object parameter
limit() - For limiting the number of records you want back. It takes in only one inter parameter(Number of record you want back).
skip() - For akipping some the number of records. It takes in only one inter parameter(Number of record you want skip).
await Post.find().sort({id:-1, user_name:1}).limit(5).skip(10).get()
await Post.find().limit(5).sort({id:-1}).get()
await Post.find().limit(5).skip(3).get()
//the result for the above are the same
pagination
page() - The page you want the data for. It takes in an interger(page you want the data for). perpage() - The perpage for passing the number records you want page. It takes in an interger(the number records you want perpage).
await Post.find().page(2).perpage(12).get()
//returns page 2 and perpage of 5
await Post.find({age:10}, {title:false}).page(2).perpage(12).get()
//returns page 2 and perpage of 5
The result look like this.
{
num_records:17,
page: 1,
par_page: 12,
has_next: true,
has_prev: false,
next_page: 2,
prev_page: null,
num_pages: 2,
position:1 //position of the first element in the entire data
result: [
{
Post: "Post1",
id: 1,
createdAt: "2024-01-08T10:26:27.158Z",
updatedAt: "2024-01-08T10:26:27.158Z"
},
{
Post: "Post2",
id: 2,
createdAt: "2024-01-08T10:26:28.629Z",
updatedAt: "2024-01-08T10:26:28.629Z"
}
...
]
}
.update()
await Post.update(2, {title:'updated title'})
/*update the title of document with id 2
to 'updated title' and returns the updated document
*/
.findOneAndUpdate()
await Post.findOneAndUpdate({views:10}, {title:'updated title'})
/*update the title of the first match
to 'updated title'.
*/
Update has criteria like; $inc
await Post.update(3, {$inc:{views:1}})
//
await Post.findAndUpdate({views:2}, {$inc:{views:5}})
/* inceasing views by spacified value
*/
$mul
await Post.update(3, {$mul:{views:1}})
//
await Post.findAndUpdate({views:2}, {$mul:{views:5}})
/* multiplies views by spacified value.
*/
$push
await Product.update(3, {$push:{price:100, <...>}})
//
await Post.findAndUpdate({id:{$gte:10}}, {$push:{views:5, <...>}})
/* adding new value to an array attribute the matches
spacified criteria
*/
$pull
await Product.update(3, {$pull:{price:100}})
//
/* remove the first occurence of 100 frrom price array
*/
$pullAll
await Product.update(3, {$pullAll:{price:100}})
//
await Post.findAndUpdate({id:{$gte:10}}, {$pullAll:{price:[100, 250]}})
/* remove all the prices sprcified from the price array for
elements that spacified criteria
*/
.delete()
await Post.delete(2)
// deletes document with id 2
await Post.delete([2, 4, 5])
// deletes document with ids of 2 ,4 ,5
.findOneAndDelete()
await Post.findOneAndDelete({title:{$has:'updated'}})
/*
deletes first match of the query
*/
.findAndDelete()
await Post.findAndDelete({title:{$has:'updated'}})
/*
deletes all the matches of the query
*/
.last()
await Post.last()
//
await Post.last({title:{$search:'updated'}})
/*
both returns the last match of the query
*/
.countDocuments()
await Post.countDocuments() //1000
//
await Post.countDocuments({title:{$search:'updated'}}) //25
/*
counts the number of documents that matches the query
*/
Some of the different ways of filtering or querying for specific kind of data are;
$lt
await people.find({age:{$lt:18}}).get()
//returns people whose age is less than 18
$lte
await people.find({age:{$lte:18}}).get()
//returns people whose age is less than or equal to 18
$gt
await people.find({age:{$gt:18}}).get()
//returns people whose age is greater than to 18
$gte
await people.find({age:{$gte:18}}).get()
//returns people whose age is greater than or equal to 18
$eq
await pawait eople.find({age:{$eq:18}}).get()
//returns people whose age is equal to 18
$neq
await people.find({age:{$neq:18}}).get()
//returns people whose age is not equal to 18
$bt
await people.find({age:{$bt:[5,10]}}).get()
//returns people whose age is between 5 and 10
$nbt
await people.find({age:{$nbt:[5, 10]}}).get()
//returns people whose age is not between 5 and 10
$has
await people.find({country:{$has:'ug'}}).get()
//people whose country has ug in their name
$hasNo
await people.find({country:{$hasNo:'ug'}}).get()
//people whose country has no ug in their name
$sw
await food.find({name:{$sw:'ap'}}).get()
//foods whose name starts with ap
$ew
await food.find({name:{$ew:'ilk'}}).get()
//foods whose name end with ilk
$nsw
await food.find({name:{$nsw:'ap'}}).get()
//foods whose name is not starting with ap
$new
await food.find({name:{$ew:'ilk'}}).get()
//foods whose name is not ending with ilk
$in
await food.find({prices:{$in:5}}).get()
//for array fields
//foods whose prices list has 5
$nin
await food.find({prices:{$nin:5}}).get()
//for array fields
//foods whose prices list has no 5
$all
await food.find({prices:{$all:[5, 10]}}).get()
//for array fields
//foods whose prices matches all the value supplied as array
$or
await user.find({$or:[{id:1}, {id:{$gte:10}, lastname:'doe'}]}).get()
//users whose id is 1 or id is greater than 10 and lastname is doe
await food.find({$or:[{name:'pizze', price:{$lt:20}}]}).get()
//foods whose name is pizza or price is less than 20
$and
await user.find({$and:[{id:100}, {id:{$gte:10}, lastname:'doe'}]}).get()
//users whose id is 100 and id is greater tha 10a nd lastname is doe
await food.find({$and:[{name:'pizze', price:{$lt:20}}]}).get()
//foods whose name is pizza and price is less than 20
You can have queries like;
await user.find({
country:'canada',
first_name:{$has:'ni'},
last_name:{$has:'jo'},
age:{$gte:20},
}).get();
//returns array
NB:
-Both .paginate() and .find() methods require you to call a get method at the end to return the data.
Both .paginate() and .find() takes in second optional parameter which spacifies which attribute you want back or not, all attributes will be return by default
await user.find({},{password:true, create_at:false})
await user.find({country:'USA'},{password:false, name:false})
//returns results without password attribute