jexidb 1.0.2
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/.gitattributes +2 -0
- package/LICENSE +21 -0
- package/README.md +125 -0
- package/babel.config.json +5 -0
- package/dist/Database.cjs +983 -0
- package/package.json +58 -0
- package/src/Database.mjs +365 -0
- package/src/FileHandler.mjs +133 -0
- package/src/IndexManager.mjs +186 -0
- package/src/serializers/Advanced.mjs +120 -0
- package/src/serializers/Simple.mjs +21 -0
- package/test/test-json-compressed.jdb +0 -0
- package/test/test-json.jdb +12 -0
- package/test/test-v8-compressed.jdb +0 -0
- package/test/test-v8.jdb +0 -0
- package/test/test.mjs +152 -0
package/.gitattributes
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Edenware.app
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img width="270" src="https://edenware.app/jexidb/images/jexidb-logo-icon.jpg" alt="JexiDB logo" title="JexiDB logo" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
JexiDB is a lightweight, standalone JavaScript database manager that stores data on disk using either JSON format or V8 serialization. It supports indexing and querying capabilities for efficient data operations. Ideal for local Node.js projects as well as apps built with Electron or NW.js. Written in pure JavaScript, it requires no compilation and is compatible with both CommonJS and ESM modules.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
To install JexiDB, you can use npm:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install EdenwareApps/jexidb
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### Creating a Database Instance
|
|
20
|
+
|
|
21
|
+
To create a new instance of the database, you need to provide a file path where the database will be stored and an optional configuration object for indexes.
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
// const { Database } = require('jexidb'); // commonjs
|
|
25
|
+
|
|
26
|
+
import { Database } from 'jexidb'; // ESM
|
|
27
|
+
|
|
28
|
+
const db = new Database('path/to/database.jdb', { // file will be created if it does not already exist
|
|
29
|
+
v8: false, // false by default, set to true to use V8 serialization instead of JSON.
|
|
30
|
+
compress: false, // set to true to compress each entry
|
|
31
|
+
compressIndex: false, // set to true to compress the index only
|
|
32
|
+
indexes: { // keys to use in queries, only those key values are kept in memory, so fewer specified keys lead to improved performance
|
|
33
|
+
id: 'number',
|
|
34
|
+
name: 'string'
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Initializing the Database
|
|
40
|
+
|
|
41
|
+
Before using the database, you need to initialize it. This will load the existing data and indexes from the file.
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
await db.init();
|
|
45
|
+
```
|
|
46
|
+
Only the values specified as indexes are kept in memory for faster queries. JexiDB will never load the entire file into memory.
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
### Inserting Data
|
|
50
|
+
|
|
51
|
+
You can insert data into the database by using the `insert` method. The data should be an object that contains the defined indexes. All object values will be saved into database.
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
await db.insert({ id: 1, name: 'John Doe' });
|
|
55
|
+
await db.insert({ id: 2, name: 'Jane Doe', anyArbitraryField: '1' });
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Querying Data
|
|
59
|
+
|
|
60
|
+
The `query` method allows you to retrieve data based on specific criteria. You can specify criteria for multiple fields.
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
const results = await db.query({ name: 'John Doe' });
|
|
64
|
+
console.log(results); // [{ id: 1, name: 'John Doe' }]
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Note: For now the query should be limited to using the fields specified as 'indexes' when instantiating the class.
|
|
68
|
+
|
|
69
|
+
#### Querying with Conditions
|
|
70
|
+
|
|
71
|
+
You can use conditions to perform more complex queries:
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
const results = await db.query({ id: { '>': 1 } });
|
|
75
|
+
console.log(results); // [{ id: 2, name: 'Jane Doe' }]
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Updating Data
|
|
79
|
+
|
|
80
|
+
To update existing records, use the `update` method with the criteria to find the records and the new data.
|
|
81
|
+
|
|
82
|
+
```javascript
|
|
83
|
+
await db.update({ id: 1 }, { name: 'John Smith' });
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Deleting Data
|
|
87
|
+
|
|
88
|
+
You can delete records that match certain criteria using the `delete` method.
|
|
89
|
+
|
|
90
|
+
```javascript
|
|
91
|
+
const deletedCount = await db.delete({ name: 'Jane Doe' });
|
|
92
|
+
console.log(`Deleted ${deletedCount} record(s).`);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Iterating Through Records
|
|
96
|
+
|
|
97
|
+
You can iterate through records in the database using the `walk` method, which returns an async generator.
|
|
98
|
+
|
|
99
|
+
```javascript
|
|
100
|
+
for await (const record of db.walk()) {
|
|
101
|
+
console.log(record);
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Saving Changes
|
|
106
|
+
|
|
107
|
+
After making any changes to the database, you need to save them using the `save` method. This will persist the changes to disk.
|
|
108
|
+
|
|
109
|
+
```javascript
|
|
110
|
+
await db.save();
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Conclusion
|
|
114
|
+
|
|
115
|
+
JexiDB provides a simple yet powerful way to store and manage data in JavaScript applications. With its indexing and querying features, you can build efficient data-driven applications.
|
|
116
|
+
|
|
117
|
+
<p align="center">
|
|
118
|
+
<img width="380" src="https://edenware.app/jexidb/images/jexidb-mascot3.jpg" alt="JexiDB mascot" title="JexiDB mascot" />
|
|
119
|
+
</p>
|
|
120
|
+
|
|
121
|
+
# Contributing
|
|
122
|
+
|
|
123
|
+
Please, feel free to contribute to the project by opening a discussion under Issues section or sending your PR.
|
|
124
|
+
|
|
125
|
+
If you find this library useful, please consider making a donation of any amount via [PayPal by clicking here](https://www.paypal.com/donate/?item_name=megacubo.tv&cmd=_donations&business=efox.web%40gmail.com) to help the developer continue to dedicate himself to the project. ❤
|