json-database-st 1.0.13 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/JSONDatabase.d.ts +190 -81
- package/JSONDatabase.js +536 -608
- package/README.md +38 -113
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -4,141 +4,66 @@
|
|
|
4
4
|
[](https://www.npmjs.com/package/json-database-st)
|
|
5
5
|
[](https://opensource.org/licenses/MIT)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Designed for Node.js applications that need data persistence without the complexity of SQL servers.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
## 🚀 Key Features
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
- **🔒 AES-256-GCM Encryption:** Military-grade security for your data at rest.
|
|
12
|
+
- **⚡ O(1) Indexing:** Instant lookups, no matter how big the database gets.
|
|
13
|
+
- **🛡️ Atomic Writes:** Zero corruption risk. Uses lockfiles and temp-write-swap strategy.
|
|
14
|
+
- **🔢 Math Helpers:** Atomic `add` and `subtract` for financial/gaming logic.
|
|
15
|
+
- **📄 Pagination:** Built-in support for handling large lists efficiently.
|
|
16
|
+
- **📸 Snapshots:** One-line command to backup your entire database.
|
|
12
17
|
|
|
13
|
-
|
|
14
|
-
* **Encryption at Rest:** Built-in AES-256-GCM encryption protects your data on disk.
|
|
15
|
-
* **Path Traversal Protection:** Prevents malicious file path inputs.
|
|
16
|
-
* **Secure by Default:** Fails safely if data is tampered with or the key is wrong.
|
|
17
|
-
|
|
18
|
-
* **⚡ High-Performance Indexing:**
|
|
19
|
-
* Create indexes on your data fields (e.g., `users.email`).
|
|
20
|
-
* Enjoy near-instantaneous `O(1)` lookups with `findByIndex()`, avoiding slow full-database scans.
|
|
21
|
-
|
|
22
|
-
* **🤝 Atomic & Reliable:**
|
|
23
|
-
* **Atomic Writes:** All write operations (`set`, `transaction`, `batch`, etc.) are queued and executed atomically, preventing data corruption.
|
|
24
|
-
* **Transactions:** Execute complex multi-step operations as a single, indivisible unit.
|
|
25
|
-
* **Batching:** Perform multiple simple operations in a single, efficient disk write.
|
|
26
|
-
|
|
27
|
-
* **✅ Data Integrity:**
|
|
28
|
-
* **Schema Validation:** Integrate with libraries like Zod or Joi to enforce data structures on every write, preventing bad data from ever being saved.
|
|
29
|
-
* **Deep Uniqueness:** The `push()` method automatically prevents duplicate entries in arrays using deep object comparison.
|
|
30
|
-
|
|
31
|
-
* **📢 Modern & DX-Focused API:**
|
|
32
|
-
* **Promise-based:** Fully asynchronous `async/await` friendly API.
|
|
33
|
-
* **Event-Driven:** Emits `write`, `change`, and `error` events for reactive programming, auditing, or real-time updates.
|
|
34
|
-
* **Intuitive & Powerful:** A clean API (`get`, `set`, `find`) powered by `lodash` for flexible path notation.
|
|
35
|
-
|
|
36
|
-
## Installation
|
|
18
|
+
## 📦 Installation
|
|
37
19
|
|
|
38
20
|
```bash
|
|
39
|
-
|
|
40
|
-
npm install json-database-st lodash
|
|
21
|
+
npm install json-database-st lodash proper-lockfile
|
|
41
22
|
```
|
|
42
23
|
|
|
43
|
-
## Quick Start
|
|
44
|
-
|
|
45
|
-
This example demonstrates setting up a secure, encrypted database with a high-speed index on user emails.
|
|
24
|
+
## ⚡ Quick Start
|
|
46
25
|
|
|
47
26
|
```javascript
|
|
48
27
|
const JSONDatabase = require('json-database-st');
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
// 1. Generate a secure key (run once and store it in environment variables)
|
|
53
|
-
// const encryptionKey = crypto.randomBytes(32).toString('hex');
|
|
54
|
-
// console.log('Your secure encryption key:', encryptionKey);
|
|
55
|
-
const ENCRYPTION_KEY = 'd0a7e8c1b2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9'; // Example key
|
|
56
|
-
|
|
57
|
-
// 2. Initialize the database with encryption and an index
|
|
58
|
-
const db = new JSONDatabase(path.join(__dirname, 'secure-data.json'), {
|
|
59
|
-
encryptionKey: ENCRYPTION_KEY,
|
|
60
|
-
indices: [
|
|
61
|
-
{ name: 'user-email', path: 'users', field: 'email', unique: true }
|
|
62
|
-
]
|
|
63
|
-
});
|
|
28
|
+
|
|
29
|
+
const db = new JSONDatabase('data.json');
|
|
64
30
|
|
|
65
31
|
async function run() {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
32
|
+
// 1. Set Data
|
|
33
|
+
await db.set('user.name', 'Sethun');
|
|
34
|
+
|
|
35
|
+
// 2. Atomic Math (New!)
|
|
36
|
+
await db.set('user.balance', 1000);
|
|
37
|
+
await db.add('user.balance', 500); // Balance is now 1500
|
|
70
38
|
|
|
71
|
-
//
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
console.log('--- Finding user with index ---');
|
|
76
|
-
const alice = await db.findByIndex('user-email', 'alice@example.com');
|
|
77
|
-
console.log('Found user:', alice); // -> { email: 'alice@example.com', name: 'Alice' }
|
|
78
|
-
|
|
79
|
-
// 5. Perform a transaction
|
|
80
|
-
await db.transaction(data => {
|
|
81
|
-
data.users.bob.lastLogin = Date.now();
|
|
82
|
-
return data; // Must return the modified data
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
console.log('\n--- Bob after transaction ---');
|
|
86
|
-
console.log(await db.get('users.bob'));
|
|
87
|
-
|
|
88
|
-
} catch (err) {
|
|
89
|
-
console.error('Database operation failed:', err);
|
|
90
|
-
} finally {
|
|
91
|
-
// 6. IMPORTANT: Always close the DB for a graceful shutdown
|
|
92
|
-
await db.close();
|
|
93
|
-
}
|
|
39
|
+
// 3. Arrays with Uniqueness
|
|
40
|
+
await db.push('inventory', { id: 1, item: 'Laptop' });
|
|
41
|
+
|
|
42
|
+
console.log(await db.get('user'));
|
|
94
43
|
}
|
|
95
44
|
|
|
96
45
|
run();
|
|
97
46
|
```
|
|
98
47
|
|
|
99
|
-
## Documentation
|
|
100
|
-
|
|
101
|
-
**Full API details and advanced usage examples are available on the hosted documentation site:**
|
|
102
|
-
|
|
103
|
-
**[View Full Documentation Website](https://sethunthunder111.github.io/json-database-st/)**
|
|
104
|
-
|
|
105
|
-
## Performance
|
|
106
|
-
|
|
107
|
-
The library is designed for high performance, especially for read-heavy workloads. Indexed lookups are nearly instantaneous (`O(1)`), and the `multiProcess: false` option provides a significant boost for write speeds in single-process applications.
|
|
108
|
-
|
|
109
|
-
For a detailed analysis, see the comprehensive benchmark report.
|
|
110
|
-
|
|
111
|
-
- **[View the detailed Benchmark Analysis (BENCHMARKS.md)](BENCHMARKS.md)**
|
|
112
|
-
- **[View the interactive HTML Report](https://sethunthunder111.github.io/json-database-st/benchmarks.html)**
|
|
113
|
-
|
|
114
|
-
## API Summary
|
|
48
|
+
## 📖 Documentation
|
|
115
49
|
|
|
116
|
-
|
|
117
|
-
* `async get(path, [defaultValue])`
|
|
118
|
-
* `async set(path, value)`
|
|
119
|
-
* `async has(path)`
|
|
120
|
-
* `async delete(path)`
|
|
121
|
-
* `async push(path, ...items)`
|
|
122
|
-
* `async pull(path, ...itemsToRemove)`
|
|
123
|
-
* `async transaction(asyncFn)`
|
|
124
|
-
* `async batch(operations, [options])`
|
|
125
|
-
* `async find(collectionPath, predicate)`
|
|
126
|
-
* `async findByIndex(indexName, value)`
|
|
127
|
-
* `async clear()`
|
|
128
|
-
* `getStats()`
|
|
129
|
-
* `async close()`
|
|
130
|
-
* Events: `.on('write', handler)`, `.on('change', handler)`, `.on('error', handler)`
|
|
50
|
+
Full documentation is available in the `website/docs.html` file included in this repository.
|
|
131
51
|
|
|
132
|
-
|
|
52
|
+
- Use `encryptionKey` for all financial records.
|
|
53
|
+
- Use `paginate()` for transaction history lists.
|
|
54
|
+
- Use `createSnapshot('daily')` for automated backups.
|
|
133
55
|
|
|
134
|
-
|
|
135
|
-
* **Single-Process Focus:** While writes are atomic, this library is designed for use by a single Node.js process. Using it with multiple processes writing to the same file (e.g., in a cluster) is not recommended and can lead to race conditions.
|
|
136
|
-
* **Not a Replacement for SQL/NoSQL Servers:** For very large datasets, high write concurrency, complex queries, or multi-process/multi-server needs, a dedicated database system like PostgreSQL, MongoDB, or SQLite is the appropriate choice.
|
|
56
|
+
## 🤝 Contributing
|
|
137
57
|
|
|
138
|
-
|
|
58
|
+
Built by **SethunThunder**
|
|
139
59
|
|
|
140
|
-
|
|
60
|
+
## 🚀 Performance (v2.0)
|
|
141
61
|
|
|
142
|
-
|
|
62
|
+
| Operation | 10k Records | 1M Records |
|
|
63
|
+
| :--- | :--- | :--- |
|
|
64
|
+
| **Indexed Read** | 0.15 ms | 0.07 ms |
|
|
65
|
+
| **Write (Ingest)** | 42,450 ops/sec | 57,845 ops/sec |
|
|
66
|
+
| **Single Update** | 100 ms | 6.3 s |
|
|
143
67
|
|
|
144
|
-
|
|
68
|
+
> **Fastest in Class:** Indexed reads are O(1). Updates are 1.5x faster than v1.0.
|
|
69
|
+
> [View Full Benchmarks](./BENCHMARKS.md)
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-database-st",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.01",
|
|
4
4
|
"description": "A simple, promise-based JSON file database for Node.js with atomic operations and lodash integration.",
|
|
5
5
|
"main": "JSONDatabase.js",
|
|
6
6
|
"types": "JSONDatabase.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"test": "jest"
|
|
8
|
+
"test": "jest",
|
|
9
|
+
"benchmark": "node benchmark.js"
|
|
9
10
|
},
|
|
10
11
|
"repository": {
|
|
11
12
|
"type": "git",
|