@suyotech-dev/suyotechdb
Version:
JSON File Based Database Inspired by Mongoose ODM
188 lines (148 loc) • 4.9 kB
Markdown
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.
- [suyotechdb - File-based JSON Database Inspired by Mongoose ODM](
- [Developed by - suyotech.com](
- [Table Of Contents](
- [Features](
- [Installation](
- [Usage](
- [CJS](
- [ES6](
- [CRUD Operations](
- [createOne](
- [insertMany](
- [findOne](
- [findAndDeleteOne](
- [findOneAndUpdate](
- [udpateMany](
- [deleteMany](
- [find](
- 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)
To install the required dependencies, run:
```bash
npm install suyotechdb
```
```bash
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);
```
```bash
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);
```
Use the **createOne** function to add a new item
```bash
userModel.createOne({
name : "suyog",
email : "test@domain.com",
age : 20,
})
```
Use the **insertMany** function to add a new item
```bash
const users = [
{
name : "suyog",
email : "test@domain.com",
age : 20,
},
{
name : "user2",
email : "email@domain.com",
age : 26,
}
]
userModel.insertMany(users)
```
Use the **findOne** function to find one document
```bash
const user = userModel.findOne({name : "suyog"})
console.log(user); // { id: ksldf09-ksdjflsd-sdkfsld, name: 'suyog',email : "suyog@gmail.com",age : 20 }
```
Use the **findAndDeleteOne** to find first matching document and delelte.
```bash
const query = {name : "suyog"}
const result = userModel.findAndDeleteOne(query)
console.log(result) // deleted successfully
```
Use the **findOneAndUpdate** to find first matching docuemnt and update.
```bash
const query = {name : "suyog"}
cosnt updateData = {age : 30}
const result = userModel.findAndDeleteOne(query,updateData)
console.log(result) // updated successully
```
Use the **udpateMany** to find first matching docuemnt and update.
```bash
const query = {name : "suyog"}
cosnt updateData = {age : 30}
const result = userModel.updateMany(query,updateData)
console.log(result) // updated successully
```
Use the **deleteMany** to find first matching docuemnt and update.
```bash
const query = {name : "suyog"};
const result = userModel.deleteMany(query);
console.log(result) // updated successully
```
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.
```bash
const query = {age :{$gte: 10}}
const result = userModel
.find(query)
.sort({name : 1})
.limit(5)
.skip(2)
.select({name : 1})
.exec()
```