snowbase 6.0.2 → 6.0.5
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 +34 -34
- package/LICENSE +7 -7
- package/README.md +229 -229
- package/bin/snowbase.js +286 -315
- package/package.json +2 -2
- package/src/Snowbase.js +403 -403
- package/src/helpers/crypto.js +69 -69
- package/src/helpers/utilsAsync.js +228 -228
- package/src/storages/FileStorage.js +170 -170
- package/src/storages/FileStorageAsync.js +167 -167
package/CHANGELOG.md
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## [6.0.0] - 2026-07-XX
|
|
4
|
-
|
|
5
|
-
### Added
|
|
6
|
-
|
|
7
|
-
- Added a cache layer to avoid repeated reads from disk and improve response times.
|
|
8
|
-
- Added cache validation based on file modification time and file size.
|
|
9
|
-
- Added support for a custom backup directory.
|
|
10
|
-
- Added an async version of the database for non-blocking operations.
|
|
11
|
-
- Added optional encryption support for stored data.
|
|
12
|
-
- Added backup versioning with filenames following the pattern backup_YYYY-MM-DD_HH-MM-SS-XXXXX.json.
|
|
13
|
-
- Added a CLI with commands for init, inspect, backup, restore, clear, get, set, remove, and has.
|
|
14
|
-
- Added richer database operations such as set, save, get, remove, clear, all, has, count, and find.
|
|
15
|
-
- Added event-based lifecycle support with ready and error events.
|
|
16
|
-
- Added operation events for get, set, remove, clear, writes, and backup creation.
|
|
17
|
-
|
|
18
|
-
### Changed
|
|
19
|
-
|
|
20
|
-
- Reworked the storage layer to support persistence, backup, logging, and encryption in a more structured way.
|
|
21
|
-
- Improved initialization so the base directory, backup directory, log directory, and database file are created automatically when missing.
|
|
22
|
-
- Standardized backup and logging behavior to be configurable through constructor options.
|
|
23
|
-
- The keys are now saved in alphabetic order.
|
|
24
|
-
|
|
25
|
-
### Fixed
|
|
26
|
-
|
|
27
|
-
- Fixed issues that caused repeated file reads when the database directory changed or the file state was updated.
|
|
28
|
-
- Fixed backup handling to keep the backup folder organized and avoid excessive backup accumulation.
|
|
29
|
-
- Improved validation to reject invalid database root structures.
|
|
30
|
-
|
|
31
|
-
### Performance Improvements
|
|
32
|
-
|
|
33
|
-
- Reduced unnecessary disk I/O by reusing cached data until the underlying file changes.
|
|
34
|
-
- Improved write and backup flow for faster and more predictable persistence operations.
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [6.0.0] - 2026-07-XX
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Added a cache layer to avoid repeated reads from disk and improve response times.
|
|
8
|
+
- Added cache validation based on file modification time and file size.
|
|
9
|
+
- Added support for a custom backup directory.
|
|
10
|
+
- Added an async version of the database for non-blocking operations.
|
|
11
|
+
- Added optional encryption support for stored data.
|
|
12
|
+
- Added backup versioning with filenames following the pattern backup_YYYY-MM-DD_HH-MM-SS-XXXXX.json.
|
|
13
|
+
- Added a CLI with commands for init, inspect, backup, restore, clear, get, set, remove, and has.
|
|
14
|
+
- Added richer database operations such as set, save, get, remove, clear, all, has, count, and find.
|
|
15
|
+
- Added event-based lifecycle support with ready and error events.
|
|
16
|
+
- Added operation events for get, set, remove, clear, writes, and backup creation.
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- Reworked the storage layer to support persistence, backup, logging, and encryption in a more structured way.
|
|
21
|
+
- Improved initialization so the base directory, backup directory, log directory, and database file are created automatically when missing.
|
|
22
|
+
- Standardized backup and logging behavior to be configurable through constructor options.
|
|
23
|
+
- The keys are now saved in alphabetic order.
|
|
24
|
+
|
|
25
|
+
### Fixed
|
|
26
|
+
|
|
27
|
+
- Fixed issues that caused repeated file reads when the database directory changed or the file state was updated.
|
|
28
|
+
- Fixed backup handling to keep the backup folder organized and avoid excessive backup accumulation.
|
|
29
|
+
- Improved validation to reject invalid database root structures.
|
|
30
|
+
|
|
31
|
+
### Performance Improvements
|
|
32
|
+
|
|
33
|
+
- Reduced unnecessary disk I/O by reusing cached data until the underlying file changes.
|
|
34
|
+
- Improved write and backup flow for faster and more predictable persistence operations.
|
package/LICENSE
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
Copyright © 2021 Pedro Henrique Brandão (p3droob)
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
-
|
|
5
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
-
|
|
7
|
-
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
1
|
+
Copyright © 2021 Pedro Henrique Brandão (p3droob)
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,229 +1,229 @@
|
|
|
1
|
-
<div align="center">
|
|
2
|
-
<h1>Snowbase ❄️</h1>
|
|
3
|
-
<br>
|
|
4
|
-
<img src="./img/snowbase-logo.png" alt="Snowbase logo" width="200" height="200" style="border-radius:35%;border: 4px solid #e7e9ebff;"/>
|
|
5
|
-
<p>A lightweight file-based database for Node.js</p>
|
|
6
|
-
<a href="https://www.npmjs.com/package/snowbase">
|
|
7
|
-
<img src="https://img.shields.io/npm/v/snowbase?color=blue" alt="Current version"/>
|
|
8
|
-
</a>
|
|
9
|
-
<a href="https://www.npmjs.com/package/snowbase">
|
|
10
|
-
<img
|
|
11
|
-
src="https://packagephobia.com/badge?p=snowbase@6.0.0"
|
|
12
|
-
alt="install size">
|
|
13
|
-
</a>
|
|
14
|
-
<a href="https://www.npmjs.com/package/snowbase">
|
|
15
|
-
<img src="https://img.shields.io/npm/dt/snowbase?color=darkcyan" alt="npm downloads"/>
|
|
16
|
-
</a>
|
|
17
|
-
<img src="https://img.shields.io/npm/l/snowbase" alt="license"/>
|
|
18
|
-
<br>
|
|
19
|
-
<b>Simple.</b> <b>Fast.</b> <b>Persistent.</b>
|
|
20
|
-
</div>
|
|
21
|
-
|
|
22
|
-
---
|
|
23
|
-
|
|
24
|
-
- [Overview](#overview)
|
|
25
|
-
- [Features](#features)
|
|
26
|
-
- [Installation](#installation)
|
|
27
|
-
- [Usage](#usage)
|
|
28
|
-
- [Configuration](#configuration)
|
|
29
|
-
- [Events](#events)
|
|
30
|
-
- [Async API](#async-api)
|
|
31
|
-
- [CLI](#cli)
|
|
32
|
-
- [License](#license)
|
|
33
|
-
|
|
34
|
-
## Overview
|
|
35
|
-
|
|
36
|
-
Snowbase is a simple, fast, and persistent file-based database for Node.js.
|
|
37
|
-
It creates and manages a local JSON file to store your data safely, making it perfect for lightweight applications, prototypes, scripts, and small tools.
|
|
38
|
-
|
|
39
|
-
No servers. No complex setup. Just storage.
|
|
40
|
-
|
|
41
|
-
## ✨ Features
|
|
42
|
-
|
|
43
|
-
- ⚡ Lightweight and easy to use
|
|
44
|
-
- 📦 File-based persistence with no external database required
|
|
45
|
-
- 🔐 Optional encryption for stored data
|
|
46
|
-
- 🧾 Optional logging for operations
|
|
47
|
-
- 💾 Automatic backups with versioned files
|
|
48
|
-
- ⚙️ Supports both sync and async APIs
|
|
49
|
-
- 🧩 Works well for prototypes, local apps, and small projects
|
|
50
|
-
|
|
51
|
-
## 📥 Installation
|
|
52
|
-
|
|
53
|
-
```bash
|
|
54
|
-
npm install snowbase
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
## Usage
|
|
58
|
-
|
|
59
|
-
### CommonJS
|
|
60
|
-
|
|
61
|
-
```js
|
|
62
|
-
const Snowbase = require("snowbase");
|
|
63
|
-
|
|
64
|
-
const db = new Snowbase();
|
|
65
|
-
|
|
66
|
-
db.on("ready", () => {
|
|
67
|
-
console.log("Database is ready");
|
|
68
|
-
});
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
### ES Modules
|
|
72
|
-
|
|
73
|
-
```js
|
|
74
|
-
import Snowbase from "snowbase";
|
|
75
|
-
|
|
76
|
-
const db = new Snowbase();
|
|
77
|
-
|
|
78
|
-
db.on("ready", () => {
|
|
79
|
-
console.log("Database is ready");
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
//Multiples database
|
|
83
|
-
const db1 = new Snowbase({ baseDir: "storage" });
|
|
84
|
-
const db2 = new Snowbase({ baseDir: "database" }); // creates an isolated new Snowbase storage
|
|
85
|
-
// You need to specify the baseDir if use the CLI
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
### Basic operations
|
|
89
|
-
|
|
90
|
-
```js
|
|
91
|
-
// Set a value
|
|
92
|
-
db.set("user/name", "Pedro");
|
|
93
|
-
|
|
94
|
-
// Get a value
|
|
95
|
-
console.log(db.get("user/name")); // Pedro
|
|
96
|
-
|
|
97
|
-
// Remove a value
|
|
98
|
-
db.remove("user/name");
|
|
99
|
-
|
|
100
|
-
// Clear the whole database
|
|
101
|
-
db.clear();
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
## Configuration
|
|
105
|
-
|
|
106
|
-
You can configure Snowbase during initialization:
|
|
107
|
-
|
|
108
|
-
```js
|
|
109
|
-
const db = new Snowbase({
|
|
110
|
-
baseDir: "./my-database",
|
|
111
|
-
encryption: true,
|
|
112
|
-
secret: "my-secret",
|
|
113
|
-
logging: true,
|
|
114
|
-
backup: true,
|
|
115
|
-
});
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
### Options
|
|
119
|
-
|
|
120
|
-
- baseDir: directory where the database file will be stored
|
|
121
|
-
- encryption: enables encrypted persistence
|
|
122
|
-
- secret: secret key used when encryption is enabled
|
|
123
|
-
- logging: enables operation logging
|
|
124
|
-
- backup: enables automatic backups
|
|
125
|
-
|
|
126
|
-
## Advanced examples
|
|
127
|
-
|
|
128
|
-
### Set and save nested data
|
|
129
|
-
|
|
130
|
-
```js
|
|
131
|
-
db.set("patients", {
|
|
132
|
-
john: {
|
|
133
|
-
age: 20,
|
|
134
|
-
name: "John",
|
|
135
|
-
city: "New York",
|
|
136
|
-
},
|
|
137
|
-
albert: {
|
|
138
|
-
age: 31,
|
|
139
|
-
name: "Albert",
|
|
140
|
-
city: "Chicago",
|
|
141
|
-
},
|
|
142
|
-
});
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
### Read and query data
|
|
146
|
-
|
|
147
|
-
```js
|
|
148
|
-
console.log(db.get("patients/john/city")); // New York
|
|
149
|
-
console.log(db.has("patients/albert")); // true
|
|
150
|
-
console.log(db.count("patients")); // 2
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
### Find and remove
|
|
154
|
-
|
|
155
|
-
```js
|
|
156
|
-
const result = db.find("patients", ({ key }) => key.includes("a"));
|
|
157
|
-
console.log(result);
|
|
158
|
-
|
|
159
|
-
db.remove("patients/albert"); // true
|
|
160
|
-
db.remove("patients/susy"); // false
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
## Events
|
|
164
|
-
|
|
165
|
-
Snowbase can emit events for lifecycle and database operations. You can listen to them with the standard EventEmitter API.
|
|
166
|
-
|
|
167
|
-
```js
|
|
168
|
-
const db = new Snowbase();
|
|
169
|
-
|
|
170
|
-
db.on("ready", () => {
|
|
171
|
-
console.log("Database is ready");
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
db.on("error", (err) => {
|
|
175
|
-
console.error(err.message);
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
db.on("get", (details, timestamp) => {
|
|
179
|
-
console.log("Get event", details, timestamp);
|
|
180
|
-
});
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
### Available events
|
|
184
|
-
|
|
185
|
-
- ready: emitted when the database instance is initialized
|
|
186
|
-
- error: emitted when an error occurs; receives an Error object
|
|
187
|
-
- get: emitted after a read operation; payload includes the path and the resolved value
|
|
188
|
-
- set: emitted after a write operation; payload includes the path, key, and value
|
|
189
|
-
- remove: emitted after a remove operation; payload includes the path, removed value, and whether it was removed
|
|
190
|
-
- clear: emitted after clearing the database; payload includes the previous data
|
|
191
|
-
- \_write: emitted after data is written to disk
|
|
192
|
-
- create backup: emitted when a backup file is created
|
|
193
|
-
|
|
194
|
-
## Async API
|
|
195
|
-
|
|
196
|
-
For non-blocking operations, use the async version:
|
|
197
|
-
|
|
198
|
-
```js
|
|
199
|
-
import Snowbase from "snowbase/async";
|
|
200
|
-
|
|
201
|
-
const db = new Snowbase({
|
|
202
|
-
encryption: true,
|
|
203
|
-
secret: "my-secret",
|
|
204
|
-
backup: true,
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
await db.set("user/name", "Pedro");
|
|
208
|
-
console.log(await db.get("user/name"));
|
|
209
|
-
```
|
|
210
|
-
|
|
211
|
-
## CLI
|
|
212
|
-
|
|
213
|
-
Snowbase also includes a CLI for common tasks:
|
|
214
|
-
|
|
215
|
-
```bash
|
|
216
|
-
snowbase init
|
|
217
|
-
snowbase inspect
|
|
218
|
-
snowbase backup
|
|
219
|
-
snowbase restore
|
|
220
|
-
snowbase get user/name
|
|
221
|
-
snowbase set user/name Pedro
|
|
222
|
-
snowbase remove user/name
|
|
223
|
-
snowbase clear
|
|
224
|
-
snowbase has
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
## License
|
|
228
|
-
|
|
229
|
-
MIT @ Pedro Henrique Brandão (p3droob)
|
|
1
|
+
<div align="center">
|
|
2
|
+
<h1>Snowbase ❄️</h1>
|
|
3
|
+
<br>
|
|
4
|
+
<img src="./img/snowbase-logo.png" alt="Snowbase logo" width="200" height="200" style="border-radius:35%;border: 4px solid #e7e9ebff;"/>
|
|
5
|
+
<p>A lightweight file-based database for Node.js</p>
|
|
6
|
+
<a href="https://www.npmjs.com/package/snowbase">
|
|
7
|
+
<img src="https://img.shields.io/npm/v/snowbase?color=blue" alt="Current version"/>
|
|
8
|
+
</a>
|
|
9
|
+
<a href="https://www.npmjs.com/package/snowbase">
|
|
10
|
+
<img
|
|
11
|
+
src="https://packagephobia.com/badge?p=snowbase@6.0.0"
|
|
12
|
+
alt="install size">
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://www.npmjs.com/package/snowbase">
|
|
15
|
+
<img src="https://img.shields.io/npm/dt/snowbase?color=darkcyan" alt="npm downloads"/>
|
|
16
|
+
</a>
|
|
17
|
+
<img src="https://img.shields.io/npm/l/snowbase" alt="license"/>
|
|
18
|
+
<br>
|
|
19
|
+
<b>Simple.</b> <b>Fast.</b> <b>Persistent.</b>
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
- [Overview](#overview)
|
|
25
|
+
- [Features](#features)
|
|
26
|
+
- [Installation](#installation)
|
|
27
|
+
- [Usage](#usage)
|
|
28
|
+
- [Configuration](#configuration)
|
|
29
|
+
- [Events](#events)
|
|
30
|
+
- [Async API](#async-api)
|
|
31
|
+
- [CLI](#cli)
|
|
32
|
+
- [License](#license)
|
|
33
|
+
|
|
34
|
+
## Overview
|
|
35
|
+
|
|
36
|
+
Snowbase is a simple, fast, and persistent file-based database for Node.js.
|
|
37
|
+
It creates and manages a local JSON file to store your data safely, making it perfect for lightweight applications, prototypes, scripts, and small tools.
|
|
38
|
+
|
|
39
|
+
No servers. No complex setup. Just storage.
|
|
40
|
+
|
|
41
|
+
## ✨ Features
|
|
42
|
+
|
|
43
|
+
- ⚡ Lightweight and easy to use
|
|
44
|
+
- 📦 File-based persistence with no external database required
|
|
45
|
+
- 🔐 Optional encryption for stored data
|
|
46
|
+
- 🧾 Optional logging for operations
|
|
47
|
+
- 💾 Automatic backups with versioned files
|
|
48
|
+
- ⚙️ Supports both sync and async APIs
|
|
49
|
+
- 🧩 Works well for prototypes, local apps, and small projects
|
|
50
|
+
|
|
51
|
+
## 📥 Installation
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npm install snowbase
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
### CommonJS
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
const Snowbase = require("snowbase");
|
|
63
|
+
|
|
64
|
+
const db = new Snowbase();
|
|
65
|
+
|
|
66
|
+
db.on("ready", () => {
|
|
67
|
+
console.log("Database is ready");
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### ES Modules
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
import Snowbase from "snowbase";
|
|
75
|
+
|
|
76
|
+
const db = new Snowbase();
|
|
77
|
+
|
|
78
|
+
db.on("ready", () => {
|
|
79
|
+
console.log("Database is ready");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
//Multiples database
|
|
83
|
+
const db1 = new Snowbase({ baseDir: "storage" });
|
|
84
|
+
const db2 = new Snowbase({ baseDir: "database" }); // creates an isolated new Snowbase storage
|
|
85
|
+
// You need to specify the baseDir if use the CLI
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Basic operations
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
// Set a value
|
|
92
|
+
db.set("user/name", "Pedro");
|
|
93
|
+
|
|
94
|
+
// Get a value
|
|
95
|
+
console.log(db.get("user/name")); // Pedro
|
|
96
|
+
|
|
97
|
+
// Remove a value
|
|
98
|
+
db.remove("user/name");
|
|
99
|
+
|
|
100
|
+
// Clear the whole database
|
|
101
|
+
db.clear();
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Configuration
|
|
105
|
+
|
|
106
|
+
You can configure Snowbase during initialization:
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
const db = new Snowbase({
|
|
110
|
+
baseDir: "./my-database",
|
|
111
|
+
encryption: true,
|
|
112
|
+
secret: "my-secret",
|
|
113
|
+
logging: true,
|
|
114
|
+
backup: true,
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Options
|
|
119
|
+
|
|
120
|
+
- baseDir: directory where the database file will be stored
|
|
121
|
+
- encryption: enables encrypted persistence
|
|
122
|
+
- secret: secret key used when encryption is enabled
|
|
123
|
+
- logging: enables operation logging
|
|
124
|
+
- backup: enables automatic backups
|
|
125
|
+
|
|
126
|
+
## Advanced examples
|
|
127
|
+
|
|
128
|
+
### Set and save nested data
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
db.set("patients", {
|
|
132
|
+
john: {
|
|
133
|
+
age: 20,
|
|
134
|
+
name: "John",
|
|
135
|
+
city: "New York",
|
|
136
|
+
},
|
|
137
|
+
albert: {
|
|
138
|
+
age: 31,
|
|
139
|
+
name: "Albert",
|
|
140
|
+
city: "Chicago",
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Read and query data
|
|
146
|
+
|
|
147
|
+
```js
|
|
148
|
+
console.log(db.get("patients/john/city")); // New York
|
|
149
|
+
console.log(db.has("patients/albert")); // true
|
|
150
|
+
console.log(db.count("patients")); // 2
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Find and remove
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
const result = db.find("patients", ({ key }) => key.includes("a"));
|
|
157
|
+
console.log(result);
|
|
158
|
+
|
|
159
|
+
db.remove("patients/albert"); // true
|
|
160
|
+
db.remove("patients/susy"); // false
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Events
|
|
164
|
+
|
|
165
|
+
Snowbase can emit events for lifecycle and database operations. You can listen to them with the standard EventEmitter API.
|
|
166
|
+
|
|
167
|
+
```js
|
|
168
|
+
const db = new Snowbase();
|
|
169
|
+
|
|
170
|
+
db.on("ready", () => {
|
|
171
|
+
console.log("Database is ready");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
db.on("error", (err) => {
|
|
175
|
+
console.error(err.message);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
db.on("get", (details, timestamp) => {
|
|
179
|
+
console.log("Get event", details, timestamp);
|
|
180
|
+
});
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Available events
|
|
184
|
+
|
|
185
|
+
- ready: emitted when the database instance is initialized
|
|
186
|
+
- error: emitted when an error occurs; receives an Error object
|
|
187
|
+
- get: emitted after a read operation; payload includes the path and the resolved value
|
|
188
|
+
- set: emitted after a write operation; payload includes the path, key, and value
|
|
189
|
+
- remove: emitted after a remove operation; payload includes the path, removed value, and whether it was removed
|
|
190
|
+
- clear: emitted after clearing the database; payload includes the previous data
|
|
191
|
+
- \_write: emitted after data is written to disk
|
|
192
|
+
- create backup: emitted when a backup file is created
|
|
193
|
+
|
|
194
|
+
## Async API
|
|
195
|
+
|
|
196
|
+
For non-blocking operations, use the async version:
|
|
197
|
+
|
|
198
|
+
```js
|
|
199
|
+
import Snowbase from "snowbase/async";
|
|
200
|
+
|
|
201
|
+
const db = new Snowbase({
|
|
202
|
+
encryption: true,
|
|
203
|
+
secret: "my-secret",
|
|
204
|
+
backup: true,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
await db.set("user/name", "Pedro");
|
|
208
|
+
console.log(await db.get("user/name"));
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## CLI
|
|
212
|
+
|
|
213
|
+
Snowbase also includes a CLI for common tasks:
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
snowbase init
|
|
217
|
+
snowbase inspect
|
|
218
|
+
snowbase backup
|
|
219
|
+
snowbase restore
|
|
220
|
+
snowbase get user/name
|
|
221
|
+
snowbase set user/name Pedro
|
|
222
|
+
snowbase remove user/name
|
|
223
|
+
snowbase clear
|
|
224
|
+
snowbase has
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## License
|
|
228
|
+
|
|
229
|
+
MIT @ Pedro Henrique Brandão (p3droob)
|