UNPKG

flexi-db

Version:

A lightweight, flexible JSON-based database with caching, async operations, and customizable data storage.

105 lines (76 loc) โ€ข 4.04 kB
# FlexiDB [![GitHub](https://img.shields.io/github/license/TariQ-2/FlexiDB)](https://github.com/TariQ-2/FlexiDB/blob/master/LICENSE) [![GitHub last commit](https://img.shields.io/github/last-commit/TariQ-2/FlexiDB)](https://github.com/TariQ-2/FlexiDB/commits/master) [![GitHub issues](https://img.shields.io/github/issues-raw/TariQ-2/FlexiDB)](https://github.com/TariQ-2/FlexiDB/issues) [![GitHub closed issues](https://img.shields.io/github/issues-closed-raw/TariQ-2/FlexiDB)](https://github.com/TariQ-2/FlexiDB/issues) [![GitHub repo size](https://img.shields.io/github/repo-size/TariQ-2/FlexiDB)](https://github.com/TariQ-2/FlexiDB) [![npm downloads](https://img.shields.io/npm/dt/flexi-db.svg?maxAge=3600)](https://github.com/TariQ-2/FlexiDB) [![npm version](https://img.shields.io/npm/v/flexi-db.svg?maxAge=3600)](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!