suyotechdb
v0.0.7
Published
JSON File Based Database Inspired by Mongoose ODM
Downloads
17
Maintainers
Readme
suyotechdb - File-based JSON Database Inspired by Mongoose ODM with Zero Dependency
This repository provides a lightweight, file-based JSON database system inspired by Mongoose ODM. It allows you to model, store, and retrieve data using a schema-based approach similar to Mongoose, but all data is stored in local JSON files.
Developed by - suyotech.com
Table Of Contents
Features
- Zero Dependency
- Schema-based data modeling
- Simple CRUD operations
- Validation and default values
- File-based storage for easy setup and portability
- All functions are syncronous. (Async not supported for easy usage)
Installation
To install the required dependencies, run:
npm install suyotechdb
Usage
CJS
const { Schema, Model } = require('suyotechdb');
const userSchema = new Schema({
name: { type: String, required: true,default : "" },
email: { type: String, required: true, default: "" },
age: { type: Number, default: 18 },
});
// define database path where to save files
const database_path = "./db";
const User = new Model('users',database_path, userSchema);
ESM
import { Schema, Model } from 'suyotechdb' ;
const userSchema = new Schema({
name: { type: String, required: true,default : "" },
email: { type: String, required: true, default: "" },
age: { type: Number, default: 18 },
});
//define database path where to save files
const database_path = "./db"
const userModel = new Model('users',databse_path, userSchema);
CRUD Operations
createOne
Use the createOne function to add a new item
userModel.createOne({
name : "suyog",
email : "[email protected]",
age : 20,
})
insertMany
Use the insertMany function to add a new item
const users = [
{
name : "suyog",
email : "[email protected]",
age : 20,
},
{
name : "user2",
email : "[email protected]",
age : 26,
}
]
userModel.insertMany(users)
findOne
Use the findOne function to find one document
const user = userModel.findOne({name : "suyog"})
console.log(user); // { id: ksldf09-ksdjflsd-sdkfsld, name: 'suyog',email : "[email protected]",age : 20 }
findAndDeleteOne
Use the findAndDeleteOne to find first matching document and delelte.
const query = {name : "suyog"}
const result = userModel.findAndDeleteOne(query)
console.log(result) // deleted successfully
findOneAndUpdate
Use the findOneAndUpdate to find first matching docuemnt and update.
const query = {name : "suyog"}
cosnt updateData = {age : 30}
const result = userModel.findAndDeleteOne(query,updateData)
console.log(result) // updated successully
udpateMany
Use the udpateMany to find first matching docuemnt and update.
const query = {name : "suyog"}
cosnt updateData = {age : 30}
const result = userModel.updateMany(query,updateData)
console.log(result) // updated successully
deleteMany
Use the deleteMany to find first matching docuemnt and update.
const query = {name : "suyog"};
const result = userModel.deleteMany(query);
console.log(result) // updated successully
find
This is special query which can use many different chained operations
- It supports following operation for query operators
- $in - Finds value in field
- $nin - Check not in values
- $eq - Equals to
- $ne - Not equals to
- $gte - Greater Than
- $lte - Less Than
- Chained Functions
- sort - Sort by given query
- limit - Limits Documents
- skip - Skips Documents
- select - Select or deselect based value 0 or 1 respectively
- distinct - Distinct Field Values
- count - Count of find query result
- exec - Function need after find or chained to return data.
const query = {age :{$gte: 10}}
const result = userModel
.find(query)
.sort({name : 1})
.limit(5)
.skip(2)
.select({name : 1})
.exec()