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.
@@ -0,0 +1,71 @@
1
+ import Database from '../src/index.js';
2
+
3
+ async function demonstrateCloseVsDelete() {
4
+ console.log('๐Ÿ” Demonstrating close() vs destroy() vs deleteDatabase()\n');
5
+
6
+ // Example 1: close() - Fecha instรขncia, mantรฉm arquivo
7
+ console.log('๐Ÿ“ Example 1: close() - Closes instance, keeps file');
8
+ const db1 = new Database('./example-close.jdb', { indexes: { id: 'number' } });
9
+ await db1.init();
10
+ await db1.insert({ id: 1, name: 'John' });
11
+ await db1.insert({ id: 2, name: 'Jane' });
12
+
13
+ console.log(' Records before close:', await db1.count());
14
+ await db1.close(); // Fecha instรขncia, mantรฉm arquivo
15
+ console.log(' โœ… Instance closed, file preserved\n');
16
+
17
+ // Reopen the same file
18
+ const db1Reopened = new Database('./example-close.jdb', { indexes: { id: 'number' } });
19
+ await db1Reopened.init();
20
+ console.log(' Records after reopening:', await db1Reopened.count());
21
+ await db1Reopened.close();
22
+ console.log(' โœ… Same data preserved\n');
23
+
24
+ // Example 2: destroy() - Equivalente a close()
25
+ console.log('๐Ÿ“ Example 2: destroy() - Equivalent to close()');
26
+ const db2 = new Database('./example-destroy.jdb', { indexes: { id: 'number' } });
27
+ await db2.init();
28
+ await db2.insert({ id: 1, name: 'Alice' });
29
+ await db2.insert({ id: 2, name: 'Bob' });
30
+
31
+ console.log(' Records before destroy:', await db2.count());
32
+ await db2.destroy(); // Equivalente a close()
33
+ console.log(' โœ… Instance destroyed, file preserved\n');
34
+
35
+ // Reopen the same file
36
+ const db2Reopened = new Database('./example-destroy.jdb', { indexes: { id: 'number' } });
37
+ await db2Reopened.init();
38
+ console.log(' Records after reopening:', await db2Reopened.count());
39
+ await db2Reopened.close();
40
+ console.log(' โœ… Same data preserved\n');
41
+
42
+ // Example 3: deleteDatabase() - Remove arquivo fรญsico
43
+ console.log('๐Ÿ—‘๏ธ Example 3: deleteDatabase() - Removes physical file');
44
+ const db3 = new Database('./example-delete.jdb', { indexes: { id: 'number' } });
45
+ await db3.init();
46
+ await db3.insert({ id: 1, name: 'Charlie' });
47
+ await db3.insert({ id: 2, name: 'Diana' });
48
+
49
+ console.log(' Records before delete:', await db3.count());
50
+
51
+ // Delete database file permanently
52
+ await db3.deleteDatabase();
53
+ console.log(' โœ… Database file deleted\n');
54
+
55
+ // Try to reopen - should fail
56
+ try {
57
+ const db3Reopened = new Database('./example-delete.jdb', { indexes: { id: 'number' } });
58
+ await db3Reopened.init();
59
+ } catch (error) {
60
+ console.log(' โŒ Cannot reopen (expected):', error.message);
61
+ }
62
+
63
+ console.log('\n๐Ÿ“‹ Summary:');
64
+ console.log(' โ€ข close() - Closes instance, keeps file');
65
+ console.log(' โ€ข destroy() - Equivalent to close()');
66
+ console.log(' โ€ข deleteDatabase() - Permanently deletes database file');
67
+ console.log(' โ€ข removeDatabase() - Alias for deleteDatabase()');
68
+ }
69
+
70
+ // Run the demonstration
71
+ demonstrateCloseVsDelete().catch(console.error);
@@ -0,0 +1,113 @@
1
+ // Example: Using JexiDB with ESM (ES Modules)
2
+ // This file demonstrates how to use the library in modern ESM environments
3
+
4
+ import Database from 'jexidb';
5
+
6
+ async function main() {
7
+ console.log('๐Ÿš€ JexiDB Examples\n');
8
+
9
+ // Example 1: Basic usage
10
+ console.log('=== Example 1: Basic Usage ===');
11
+
12
+ const db = new Database('./example-users.jdb', {
13
+ indexes: { id: 'number', email: 'string', age: 'number' }
14
+ });
15
+
16
+ await db.init();
17
+
18
+ // Insert records
19
+ await db.insert({ id: 1, name: 'John Doe', email: 'john@example.com', age: 30 });
20
+ await db.insert({ id: 2, name: 'Jane Smith', email: 'jane@example.com', age: 25 });
21
+
22
+ console.log('โœ… Records inserted: John Doe, Jane Smith');
23
+
24
+ // Find all records
25
+ const allUsers = await db.find({});
26
+ console.log('๐Ÿ“‹ All users:', allUsers.length);
27
+
28
+ // Find with criteria
29
+ const youngUsers = await db.find({ age: { '<': 30 } });
30
+ console.log('๐Ÿ‘ถ Young users:', youngUsers.length);
31
+
32
+ // Find one record
33
+ const john = await db.findOne({ name: 'John Doe' });
34
+ console.log('๐Ÿ‘ค John user:', john?.name);
35
+
36
+ // Update records
37
+ const updated = await db.update({ id: 1 }, { age: 31 });
38
+ console.log('๐Ÿ”„ Updated records:', updated?.length);
39
+
40
+ // Get statistics
41
+ const stats = await db.getStats();
42
+ console.log('๐Ÿ“Š Database stats:', stats.summary);
43
+
44
+ await db.save();
45
+ console.log('๐Ÿ’พ Database saved');
46
+
47
+ await db.destroy();
48
+ console.log('๐Ÿ”’ Database closed\n');
49
+
50
+ // Example 2: New options (create, clear)
51
+ console.log('=== Example 2: New Options (create, clear) ===');
52
+
53
+ const db2 = new Database('./example-options.jdb', {
54
+ indexes: { category: 'string' },
55
+ create: true,
56
+ clear: false
57
+ });
58
+
59
+ await db2.init();
60
+ console.log('โœ… Database created with create: true');
61
+
62
+ await db2.insert({ id: 1, category: 'Electronics' });
63
+ await db2.insert({ id: 2, category: 'Books' });
64
+ await db2.save();
65
+ await db2.destroy();
66
+
67
+ // Test clear option
68
+ const db3 = new Database('./example-options.jdb', {
69
+ indexes: { category: 'string' },
70
+ clear: true
71
+ });
72
+
73
+ await db3.init();
74
+ console.log('โœ… Database cleared with clear: true, length:', db3.length);
75
+ await db3.destroy();
76
+
77
+ // Example 3: readColumnIndex
78
+ console.log('\n=== Example 3: readColumnIndex ===');
79
+
80
+ const db4 = new Database('./example-columns.jdb', {
81
+ indexes: { category: 'string', status: 'string' },
82
+ create: true
83
+ });
84
+
85
+ await db4.init();
86
+
87
+ await db4.insertMany([
88
+ { id: 1, category: 'Electronics', status: 'active' },
89
+ { id: 2, category: 'Books', status: 'inactive' },
90
+ { id: 3, category: 'Electronics', status: 'active' },
91
+ { id: 4, category: 'Clothing', status: 'active' }
92
+ ]);
93
+
94
+ // Get unique values from indexed columns
95
+ const categories = db4.readColumnIndex('category');
96
+ console.log('๐Ÿ“Š Unique categories:', Array.from(categories));
97
+
98
+ const statuses = db4.readColumnIndex('status');
99
+ console.log('๐Ÿ“Š Unique statuses:', Array.from(statuses));
100
+
101
+ // Test error for non-indexed column
102
+ try {
103
+ db4.readColumnIndex('name');
104
+ } catch (error) {
105
+ console.log('โœ… Correctly threw error for non-indexed column:', error.message);
106
+ }
107
+
108
+ await db4.destroy();
109
+
110
+ console.log('\n๐ŸŽ‰ All examples completed successfully!');
111
+ }
112
+
113
+ main().catch(console.error);
Binary file
@@ -0,0 +1,9 @@
1
+ {"id":1,"category":"Electronics","status":"active","_id":0,"_created":1754747670129,"_updated":1754747670129}
2
+ {"id":2,"category":"Books","status":"inactive","_id":1,"_created":1754747670129,"_updated":1754747670129}
3
+ {"id":3,"category":"Electronics","status":"active","_id":2,"_created":1754747670129,"_updated":1754747670129}
4
+ {"id":4,"category":"Clothing","status":"active","_id":3,"_created":1754747670129,"_updated":1754747670129}
5
+ {"id":1,"category":"Electronics","status":"active","_id":4,"_created":1754747697710,"_updated":1754747697710}
6
+ {"id":2,"category":"Books","status":"inactive","_id":5,"_created":1754747697710,"_updated":1754747697710}
7
+ {"id":3,"category":"Electronics","status":"active","_id":6,"_created":1754747697710,"_updated":1754747697710}
8
+ {"id":4,"category":"Clothing","status":"active","_id":7,"_created":1754747697710,"_updated":1754747697710}
9
+ [0,110,216,326,433,543,649,759]
Binary file
File without changes
Binary file
@@ -0,0 +1,5 @@
1
+ {"id":1,"name":"John Doe","email":"john@example.com","age":31,"_id":0,"_created":1754747670096,"_updated":1754747670100}
2
+ {"id":2,"name":"Jane Smith","email":"jane@example.com","age":25,"_id":1,"_created":1754747670096,"_updated":1754747670096}
3
+ {"id":1,"name":"John Doe","email":"john@example.com","age":31,"_id":2,"_created":1754747697684,"_updated":1754747697691}
4
+ {"id":2,"name":"Jane Smith","email":"jane@example.com","age":25,"_id":3,"_created":1754747697684,"_updated":1754747697684}
5
+ [0,121,244,365]
@@ -0,0 +1,55 @@
1
+ // Simple test to demonstrate readColumnIndex method
2
+ const Database = require('../dist/index.js').default;
3
+
4
+ async function testReadColumnIndex() {
5
+ console.log('๐Ÿงช Testing readColumnIndex Method\n');
6
+
7
+ const db = new Database('./test-columns.jdb', {
8
+ indexes: { category: 'string', status: 'string' },
9
+ create: true
10
+ });
11
+
12
+ await db.init();
13
+
14
+ // Insert test data with duplicates
15
+ await db.insertMany([
16
+ { id: 1, category: 'Electronics', status: 'active', price: 100 },
17
+ { id: 2, category: 'Books', status: 'inactive', price: 20 },
18
+ { id: 3, category: 'Electronics', status: 'active', price: 150 }, // Duplicate category and status
19
+ { id: 4, category: 'Clothing', status: 'active', price: 50 },
20
+ { id: 5, category: 'Electronics', status: 'active', price: 200 } // Another duplicate
21
+ ]);
22
+ await db.save();
23
+
24
+ console.log('๐Ÿ“Š Test Data Inserted:');
25
+ console.log(' - 5 records total');
26
+ console.log(' - 3 Electronics, 1 Books, 1 Clothing');
27
+ console.log(' - 4 active, 1 inactive status');
28
+ console.log(' - 5 different prices\n');
29
+
30
+ // Test indexed columns
31
+ console.log('=== Indexed Columns ===');
32
+
33
+ const categories = db.readColumnIndex('category');
34
+ console.log('๐Ÿ“‚ readColumnIndex("category"):', Array.from(categories));
35
+ // Expected: ['Electronics', 'Books', 'Clothing']
36
+
37
+ const statuses = db.readColumnIndex('status');
38
+ console.log('๐Ÿ“Š readColumnIndex("status"):', Array.from(statuses));
39
+ // Expected: ['active', 'inactive']
40
+
41
+ // Test non-indexed columns (should throw error)
42
+ console.log('\n=== Non-Indexed Columns ===');
43
+
44
+ try {
45
+ const prices = db.readColumnIndex('price');
46
+ console.log('โŒ This should not appear');
47
+ } catch (error) {
48
+ console.log('โŒ readColumnIndex("price") error:', error.message);
49
+ }
50
+
51
+ await db.destroy();
52
+ console.log('\nโœ… Test completed successfully!');
53
+ }
54
+
55
+ testReadColumnIndex().catch(console.error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jexidb",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "JexiDB - A fast and reliable local CRUD database for Electron apps with pure JavaScript JSONL architecture",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./src/index.js",
@@ -58,7 +58,10 @@
58
58
  "files": [
59
59
  "dist",
60
60
  "src",
61
- "README.md"
61
+ "README.md",
62
+ "CHANGELOG.md",
63
+ "docs",
64
+ "examples"
62
65
  ],
63
66
  "dependencies": {
64
67
  }