flexi-db
Version:
A lightweight, flexible JSON-based database with caching, async operations, and customizable data storage.
105 lines (76 loc) โข 4.04 kB
Markdown
# FlexiDB
[](https://github.com/TariQ-2/FlexiDB/blob/master/LICENSE) [](https://github.com/TariQ-2/FlexiDB/commits/master) [](https://github.com/TariQ-2/FlexiDB/issues) [](https://github.com/TariQ-2/FlexiDB/issues) [](https://github.com/TariQ-2/FlexiDB) [](https://github.com/TariQ-2/FlexiDB) [](https://github.com/TariQ-2/FlexiDB)
**FlexiDB** is a lightweight, flexible JSON-based database for Node.js projects. It offers in-memory caching, async operations, atomic transactions, and zero dependencies.
## ๐ Features
* **โก High Performance** โ In-memory caching and debounced disk writes
* **๐ Async API** โ All methods are promise-based
* **๐พ Custom Storage** โ Use any directory for data and backups
* **๐ Auto Backups** โ Optional periodic file backups
* **๐ Atomic Transactions** โ Grouped operations with rollback
* **๐จ Type Safety** โ Auto conversion for numeric operations
* **๐ชฆ Zero Dependencies** โ Uses Node.js core only
## ๐พ Installation
```bash
npm install flexi-db
```
## ๐งช Quick Example
```js
const FlexiDB = require('flexi-db');
const db = new FlexiDB('mydb.json', { dataDir: 'data', autoBackup: true });
(async () => {
await db.set('user1', { name: 'Ali', age: 25 });
await db.add('score', 10);
await db.push('items', 'apple');
await db.transaction([
{ type: 'set', key: 'user2', value: { name: 'Sara' } },
{ type: 'add', key: 'score', value: 5 }
]);
console.log(db.all());
})();
```
## ๐งฉ API Overview
### Core Methods
| Method | Description |
| ----------------- | ---------------------------------------- |
| `set(key, value)` | Store a value |
| `get(key)` | Retrieve a value |
| `has(key)` | Check key existence |
| `delete(key)` | Remove a key |
| `all(limit?)` | Get all key-value pairs (optional limit) |
### Math Operations
| Method | Notes |
| ---------------------------- | -------------------------------- |
| `add(key, number)` | Auto-converts non-numbers to 0 |
| `subtract(key, number)` | Subtract from number |
| `math(key, operator, value)` | Supports `+`, `-`, `*`, `/`, `%` |
### Array Utilities
| Method | Description |
| ---------------- | -------------------------------- |
| `push(key, val)` | Push value to array at given key |
### Advanced
| Method | Description |
| -------------------- | ---------------------------------- |
| `transaction([...])` | Run multiple operations atomically |
| `backup(fileName)` | Create a manual backup |
| `reset()` | Clear all data |
| `destroy()` | Save data and stop auto-backups |
## โ๏ธ Options
You can pass options in the constructor:
```js
new FlexiDB('file.json', {
dataDir: 'data', // default: 'FlexiDB'
autoBackup: true // default: false
});
```
## ๐ File Structure
All data and backups are stored in the specified `dataDir`, helping you keep your project organized.
```
my-project/
โโโ data/ # or 'FlexiDB' by default
โโโ mydb.json
โโโ backup-2025.json
```
## ๐ License
MIT โ Free to use, modify, and distribute.
## ๐ค Contributing
Feel free to open issues or submit PRs. All contributions are welcome!