json-database-st 1.0.13 → 2.0.0

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/README.md CHANGED
@@ -4,141 +4,73 @@
4
4
  [![NPM Downloads](https://img.shields.io/npm/dm/json-database-st.svg)](https://www.npmjs.com/package/json-database-st)
5
5
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
6
 
7
- A secure, performant, and feature-rich JSON file database for Node.js. Designed for projects that need simple, persistent data storage without the overhead of a traditional database server, but with modern features like **encryption, indexing, and schema validation**.
7
+ The high-performance, secure, JSON-based database engine powering **Project Ares** and the **ST Conglomerate**.
8
8
 
9
- Ideal for small to medium-sized projects, configuration management, user session data, or any application where data safety and integrity are critical.
9
+ Designed for Node.js applications that need data persistence without the complexity of SQL servers.
10
10
 
11
- ## Features
11
+ ## 🚀 Key Features
12
12
 
13
- * **🔒 Security First:**
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.
13
+ - **🔒 AES-256-GCM Encryption:** Military-grade security for your data at rest.
14
+ - **⚡ O(1) Indexing:** Instant lookups, no matter how big the database gets.
15
+ - **🛡️ Atomic Writes:** Zero corruption risk. Uses lockfiles and temp-write-swap strategy.
16
+ - **🔢 Math Helpers:** Atomic `add` and `subtract` for financial/gaming logic.
17
+ - **📄 Pagination:** Built-in support for handling large lists efficiently.
18
+ - **📸 Snapshots:** One-line command to backup your entire database.
17
19
 
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
20
+ ## 📦 Installation
37
21
 
38
22
  ```bash
39
- # This package requires lodash as a peer dependency
40
- npm install json-database-st lodash
23
+ npm install json-database-st lodash proper-lockfile
41
24
  ```
42
25
 
43
- ## Quick Start: Secure & Indexed Database
44
-
45
- This example demonstrates setting up a secure, encrypted database with a high-speed index on user emails.
26
+ ## Quick Start
46
27
 
47
28
  ```javascript
48
29
  const JSONDatabase = require('json-database-st');
49
- const path = require('path');
50
- const crypto = require('crypto');
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
- });
30
+
31
+ const db = new JSONDatabase('data.json');
64
32
 
65
33
  async function run() {
66
- try {
67
- // 3. Set data. The index will be updated automatically.
68
- await db.set('users.alice', { email: 'alice@example.com', name: 'Alice' });
69
- await db.set('users.bob', { email: 'bob@example.com', name: 'Bob' });
34
+ // 1. Set Data
35
+ await db.set('user.name', 'Sethun');
36
+
37
+ // 2. Atomic Math (New!)
38
+ await db.set('user.balance', 1000);
39
+ await db.add('user.balance', 500); // Balance is now 1500
70
40
 
71
- // This would throw an IndexViolationError because the email is not unique
72
- // await db.set('users.impostor', { email: 'alice@example.com', name: 'Impostor' });
73
-
74
- // 4. Use the high-speed index for an instant lookup
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
- }
41
+ // 3. Arrays with Uniqueness
42
+ await db.push('inventory', { id: 1, item: 'Laptop' });
43
+
44
+ console.log(await db.get('user'));
94
45
  }
95
46
 
96
47
  run();
97
48
  ```
98
49
 
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.
50
+ ## 📖 Documentation
110
51
 
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)**
52
+ Full documentation is available in the `website/docs.html` file included in this repository.
113
53
 
114
- ## API Summary
54
+ ## 🏗️ Project Ares Integration
115
55
 
116
- * `new JSONDatabase(filename, [options])`
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)`
56
+ This database is optimized for the **ST Financial Engine**.
131
57
 
132
- ## Limitations
58
+ - Use `encryptionKey` for all financial records.
59
+ - Use `paginate()` for transaction history lists.
60
+ - Use `createSnapshot('daily')` for automated backups.
133
61
 
134
- * **In-Memory Operation:** The entire database file is loaded into memory on initialization. This makes it extremely fast for reads but limits the practical file size to what can comfortably fit in your available RAM.
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.
62
+ ## 🤝 Contributing
137
63
 
138
- ## Contributing
64
+ Built by **SethunThunder** for the ST Empire.
65
+ Laybon Gold 1.5 Assisted.
139
66
 
140
- Contributions, issues, and feature requests are welcome! Please feel free to open an issue to discuss any significant changes.
67
+ ## 🚀 Performance (v2.0)
141
68
 
142
- ## License
69
+ | Operation | 10k Records | 1M Records |
70
+ | :--- | :--- | :--- |
71
+ | **Indexed Read** | 0.15 ms | 0.07 ms |
72
+ | **Write (Ingest)** | 42,450 ops/sec | 57,845 ops/sec |
73
+ | **Single Update** | 100 ms | 6.3 s |
143
74
 
144
- [MIT](LICENSE)
75
+ > **Fastest in Class:** Indexed reads are O(1). Updates are 1.5x faster than v1.0.
76
+ > [View Full Benchmarks](./BENCHMARKS.md)
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "json-database-st",
3
- "version": "1.0.13",
3
+ "version": "2.0.0",
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",