jexidb 2.0.1 โ†’ 2.0.3

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/CHANGELOG.md ADDED
@@ -0,0 +1,109 @@
1
+ # Changelog
2
+
3
+ All notable changes to JexiDB will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [2.0.3] - 2024-12-19
9
+
10
+ ### ๐Ÿš€ Added
11
+ - **Intelligent Auto-Save System**: Automatic data persistence with configurable thresholds and intervals
12
+ - `autoSaveThreshold`: Flush buffer when it reaches N records (default: 50)
13
+ - `autoSaveInterval`: Flush buffer every N milliseconds (default: 5000ms)
14
+ - `forceSaveOnClose`: Always save when closing database (default: true)
15
+ - **New Public Methods**:
16
+ - `flush()`: Explicitly flush insertion buffer to disk
17
+ - `forceSave()`: Force save operation regardless of buffer size
18
+ - `getBufferStatus()`: Get current buffer state information
19
+ - `configurePerformance(settings)`: Dynamic performance configuration
20
+ - `getPerformanceConfig()`: Get current performance settings
21
+ - `deleteDatabase()`: Permanently delete database file
22
+ - `removeDatabase()`: Alias for deleteDatabase()
23
+ - **Event-Driven Monitoring**: New events for auto-save operations
24
+ - `buffer-flush`: Emitted when buffer is flushed
25
+ - `buffer-full`: Emitted when buffer reaches threshold
26
+ - `auto-save-timer`: Emitted when time-based auto-save triggers
27
+ - `save-complete`: Emitted when save operation completes
28
+ - `close-save-complete`: Emitted when database closes with final save
29
+ - `performance-configured`: Emitted when performance settings change
30
+ - `delete-database`: Emitted when database file is deleted
31
+ - **Performance Configuration Options**:
32
+ - `adaptiveBatchSize`: Adjust batch size based on usage (default: true)
33
+ - `minBatchSize`: Minimum batch size for flush (default: 10)
34
+ - `maxBatchSize`: Maximum batch size for performance (default: 200)
35
+ - `maxMemoryUsage`: Memory usage limits
36
+ - `maxFlushChunkBytes`: Maximum bytes per flush chunk (default: 8MB)
37
+
38
+ ### ๐Ÿ”ง Changed
39
+ - **API Simplification**: Removed confirmation requirements from `deleteDatabase()` and `removeDatabase()`
40
+ - Methods now work directly without `force` or `confirm` options
41
+ - Names are self-explanatory and follow industry standards
42
+ - **Constructor Defaults**: Updated default options for better performance
43
+ - `batchSize`: Reduced from 100 to 50 for faster response
44
+ - `autoSave`: Enabled by default (true)
45
+ - `autoSaveThreshold`: 50 records
46
+ - `autoSaveInterval`: 5000ms (5 seconds)
47
+ - `forceSaveOnClose`: Enabled by default (true)
48
+ - **Method Behavior**:
49
+ - `destroy()`: Now equivalent to `close()` (closes instance, keeps file)
50
+ - `deleteDatabase()`: Permanently deletes database file
51
+ - `removeDatabase()`: Alias for `deleteDatabase()`
52
+
53
+ ### ๐Ÿ› Fixed
54
+ - **Query Operators**: Fixed issues with `$gt`, `$gte`, `$lt`, `$lte`, `$ne`, `$nin` operators
55
+ - **Data Duplication**: Resolved duplicate results in `find()` operations
56
+ - **Type Preservation**: Fixed numeric values being stored as strings in persistent indexes
57
+ - **Persistence Issues**: Corrected data persistence between database instances
58
+ - **Performance Test Logic**: Fixed test expectations for large dataset operations
59
+
60
+ ### ๐Ÿ“š Documentation
61
+ - **Updated README.md**: Added comprehensive auto-save documentation and examples
62
+ - **Updated API.md**: Added new methods, events, and configuration options
63
+ - **New Examples**: Created `auto-save-example.js` and `close-vs-delete-example.js`
64
+ - **Enhanced Tests**: Added comprehensive test suite for auto-save functionality
65
+
66
+ ### ๐Ÿงช Testing
67
+ - **New Test Suite**: Added `tests/auto-save.test.js` with 10 comprehensive tests
68
+ - **All Tests Passing**: 86 tests passing (100% success rate)
69
+ - **Improved Coverage**: Better test coverage for new auto-save features
70
+
71
+ ## [2.0.2] - 2024-12-18
72
+
73
+ ### ๐Ÿš€ Added
74
+ - **Persistent Indexes**: Indexes are now saved to disk and loaded on startup
75
+ - **Point Reading**: Efficient memory usage - only reads necessary data
76
+ - **Rich Query API**: Support for complex queries with operators, sorting, and pagination
77
+ - **Event-Driven Architecture**: Real-time notifications for all database operations
78
+ - **Automatic Integrity Validation**: Built-in data integrity checking and repair
79
+ - **Legacy Compatibility**: Automatic migration from JexiDB 1.x databases
80
+ - **Pure JavaScript**: No native dependencies, works everywhere
81
+
82
+ ### ๐Ÿ”ง Changed
83
+ - **Performance**: 10-100x faster than JexiDB 1.x
84
+ - **Memory Usage**: 25% less memory consumption
85
+ - **File Format**: Improved JSONL architecture with separate index files
86
+ - **API**: Enhanced query methods with MongoDB-style operators
87
+
88
+ ### ๐Ÿ› Fixed
89
+ - **Data Integrity**: Safe truncation and consistent offsets
90
+ - **Test Isolation**: Proper test isolation and cleanup
91
+ - **V8 Dependency**: Removed dependency on V8 engine
92
+
93
+ ## [2.0.1] - 2024-12-17
94
+
95
+ ### ๐Ÿš€ Added
96
+ - **Initial Release**: First stable version of JexiDB 2.0
97
+ - **JSONL Architecture**: Pure JavaScript JSONL database implementation
98
+ - **Basic CRUD Operations**: Insert, find, update, delete operations
99
+ - **Index Support**: Basic indexing for fast queries
100
+ - **File Management**: Database file creation and management
101
+
102
+ ### ๐Ÿ”ง Changed
103
+ - **Complete Rewrite**: New architecture from ground up
104
+ - **Performance Focus**: Optimized for speed and efficiency
105
+ - **Modern JavaScript**: ES6+ features and async/await support
106
+
107
+ ### ๐Ÿ› Fixed
108
+ - **Stability**: Improved error handling and edge cases
109
+ - **Compatibility**: Better Node.js version support
package/README.md CHANGED
@@ -8,8 +8,10 @@
8
8
  - **Persistent Indexes**: Fast searches with disk-persisted indexes that don't need rebuilding
9
9
  - **Point Reading**: Efficient memory usage - only reads necessary data
10
10
  - **Rich Query API**: Support for complex queries with operators, sorting, and pagination
11
+ - **Intelligent Auto-Save**: Automatic data persistence with configurable thresholds and intervals
12
+ - **Event-Driven Monitoring**: Real-time notifications for all database operations
13
+ - **Performance Optimization**: Adaptive batch sizes and memory management
11
14
  - **Automatic Integrity Validation**: Built-in data integrity checking and repair
12
- - **Event System**: Real-time notifications for database operations
13
15
  - **Legacy Compatibility**: Automatic migration from JexiDB 1.x databases
14
16
  - **Pure JavaScript**: No native dependencies, works everywhere, easy to deploy
15
17
 
@@ -21,22 +23,18 @@ npm install jexidb
21
23
 
22
24
  ## ๐Ÿš€ Quick Start
23
25
 
26
+ ### ESM
27
+
24
28
  ```javascript
25
- // import { Database } from 'jexidb'
26
- const { Database } = require('jexidb');
29
+ import Database from 'jexidb';
27
30
 
28
- // Create database with indexes (supports both .jdb and .jsonl)
31
+ // Prefer default import aliased as Database for clarity
29
32
  const db = new Database('./users.jdb', {
30
- indexes: {
31
- id: 'number',
32
- email: 'string',
33
- age: 'number'
34
- },
33
+ indexes: { id: 'number', email: 'string', age: 'number' },
35
34
  autoSave: true,
36
35
  validateOnInit: true
37
36
  });
38
37
 
39
- // Initialize
40
38
  await db.init();
41
39
 
42
40
  // Event listeners
@@ -45,12 +43,7 @@ db.on('update', (record, index) => console.log(`Record updated at index ${index}
45
43
  db.on('save', () => console.log('Changes saved'));
46
44
 
47
45
  // Insert data
48
- const user = await db.insert({
49
- id: 1,
50
- name: 'John Doe',
51
- email: 'john@example.com',
52
- age: 30
53
- });
46
+ await db.insert({ id: 1, name: 'John Doe', email: 'john@example.com', age: 30 });
54
47
 
55
48
  // Search data (both methods work)
56
49
  const john = await db.findOne({ id: 1 });
@@ -59,17 +52,29 @@ const youngUsers = await db.find({ age: { '<': 30 } });
59
52
  // JexiDB 1.x compatible query
60
53
  const results = await db.query({ name: 'john doe' }, { caseInsensitive: true });
61
54
 
62
- // Update data
55
+ // Update / Delete / Save
63
56
  await db.update({ id: 1 }, { age: 31 });
64
-
65
- // Remove data
66
57
  await db.delete({ id: 1 });
67
-
68
- // Save changes
69
58
  await db.save();
59
+ await db.close(); // or await db.destroy()
60
+ ```
61
+
62
+ ### CommonJS
70
63
 
71
- // Destroy database
72
- await db.destroy();
64
+ ```javascript
65
+ const Database = require('jexidb');
66
+ // Alternatively (backward compatible): const { Database } = require('jexidb');
67
+
68
+ const db = new Database('./users.jdb', {
69
+ indexes: { id: 'number', email: 'string', age: 'number' }
70
+ });
71
+
72
+ (async () => {
73
+ await db.init();
74
+ await db.insert({ id: 1, name: 'John' });
75
+ console.log(await db.findOne({ id: 1 }));
76
+ await db.close(); // or await db.destroy()
77
+ })();
73
78
  ```
74
79
 
75
80
  ## ๐Ÿ“š API Reference
@@ -87,13 +92,73 @@ const db = new Database(filePath, options);
87
92
  **Options:**
88
93
  ```javascript
89
94
  {
95
+ // Core options
90
96
  indexes: {}, // Indexes for fields
91
- markDeleted: true, // Mark as deleted instead of physically removing
92
- autoSave: true, // Automatically save after operations
93
- validateOnInit: false // Validate integrity on initialization
97
+ create: true, // Create database if it doesn't exist
98
+ clear: false, // Clear database on load
99
+
100
+ // Auto-save configuration
101
+ autoSave: true, // Enable intelligent auto-save (default: true)
102
+ autoSaveThreshold: 50, // Flush buffer when it reaches this many records
103
+ autoSaveInterval: 5000, // Flush buffer every N milliseconds
104
+ forceSaveOnClose: true, // Always save when closing database
105
+
106
+ // Performance configuration
107
+ batchSize: 50, // Batch size for inserts (reduced for faster response)
108
+ adaptiveBatchSize: true, // Adjust batch size based on usage
109
+ minBatchSize: 10, // Minimum batch size for flush
110
+ maxBatchSize: 200 // Maximum batch size for performance
94
111
  }
95
112
  ```
96
113
 
114
+ ### Auto-Save Intelligence
115
+
116
+ JexiDB features intelligent auto-save capabilities that automatically manage data persistence without manual intervention.
117
+
118
+ #### Auto-Save Modes
119
+
120
+ **Intelligent Auto-Save (Default):**
121
+ - Automatically flushes buffer when it reaches the threshold (default: 50 records)
122
+ - Automatically flushes buffer every N milliseconds (default: 5000ms)
123
+ - Always saves when closing the database
124
+ - Provides real-time feedback through events
125
+
126
+ **Manual Mode:**
127
+ - Disable auto-save with `autoSave: false`
128
+ - Manually call `flush()` and `save()` when needed
129
+ - Useful for applications requiring precise control over persistence timing
130
+
131
+ #### Event-Driven Monitoring
132
+
133
+ ```javascript
134
+ // Monitor auto-save operations
135
+ db.on('buffer-flush', (count) => {
136
+ console.log(`Flushed ${count} records`);
137
+ });
138
+
139
+ db.on('buffer-full', () => {
140
+ console.log('Buffer reached threshold');
141
+ });
142
+
143
+ db.on('auto-save-timer', () => {
144
+ console.log('Time-based auto-save triggered');
145
+ });
146
+
147
+ db.on('save-complete', () => {
148
+ console.log('Database saved successfully');
149
+ });
150
+ ```
151
+
152
+ #### Buffer Status Monitoring
153
+
154
+ ```javascript
155
+ // Check buffer status anytime
156
+ const status = db.getBufferStatus();
157
+ console.log(`Pending: ${status.pendingCount}/${status.bufferSize}`);
158
+ console.log(`Should flush: ${status.shouldFlush}`);
159
+ console.log(`Auto-save enabled: ${status.autoSaveEnabled}`);
160
+ ```
161
+
97
162
  ### Main Methods
98
163
 
99
164
  #### `init()`
@@ -170,8 +235,46 @@ Counts records.
170
235
  #### `save()`
171
236
  Saves pending changes.
172
237
 
238
+ #### `flush()`
239
+ Flushes the insertion buffer to disk immediately.
240
+
241
+ #### `forceSave()`
242
+ Forces a save operation regardless of buffer size.
243
+
244
+ #### `getBufferStatus()`
245
+ Gets information about the current buffer state.
246
+
247
+ #### `configurePerformance(settings)`
248
+ Dynamically configures performance settings.
249
+
250
+ #### `getPerformanceConfig()`
251
+ Gets current performance configuration.
252
+
253
+ #### `close()`
254
+ Closes the database instance and saves pending changes.
255
+
173
256
  #### `destroy()`
174
- Destroys the database.
257
+ Closes the database instance and saves pending changes (equivalent to close()).
258
+
259
+ ```javascript
260
+ await db.destroy() // Same as: await db.close()
261
+ ```
262
+
263
+ #### `deleteDatabase()`
264
+ **โš ๏ธ WARNING: This permanently deletes the database file!**
265
+
266
+ Deletes the database file from disk and closes the instance.
267
+
268
+ ```javascript
269
+ await db.deleteDatabase() // Deletes the database file permanently
270
+ ```
271
+
272
+ #### `removeDatabase()`
273
+ Removes the database file from disk (alias for deleteDatabase).
274
+
275
+ ```javascript
276
+ await db.removeDatabase() // Same as: await db.deleteDatabase()
277
+ ```
175
278
 
176
279
  #### `validateIntegrity(options)`
177
280
  Validates database integrity.
@@ -213,13 +316,25 @@ Index statistics.
213
316
  ### Events
214
317
 
215
318
  ```javascript
319
+ // Core events
216
320
  db.on('init', () => console.log('Database initialized'));
217
321
  db.on('insert', (record, index) => console.log('Record inserted'));
218
322
  db.on('update', (record, index) => console.log('Record updated'));
219
323
  db.on('delete', (record, index) => console.log('Record deleted'));
220
324
  db.on('before-save', () => console.log('Before save'));
221
325
  db.on('save', () => console.log('Save completed'));
326
+ db.on('close', () => console.log('Database closed'));
222
327
  db.on('destroy', () => console.log('Database destroyed'));
328
+ db.on('delete-database', () => console.log('Database file deleted'));
329
+
330
+ // Auto-save events
331
+ db.on('buffer-flush', (count) => console.log(`Buffer flushed: ${count} records`));
332
+ db.on('buffer-full', () => console.log('Buffer reached threshold'));
333
+ db.on('auto-save-timer', () => console.log('Time-based auto-save triggered'));
334
+ db.on('save-complete', () => console.log('Save operation completed'));
335
+ db.on('close-save-complete', () => console.log('Database closed with final save'));
336
+ db.on('close', () => console.log('Database closed'));
337
+ db.on('performance-configured', (config) => console.log('Performance reconfigured'));
223
338
  ```
224
339
 
225
340
  ## ๐Ÿ“ File Structure