sehawq.db 2.3.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/.github/workflows/npm-publish.yml +30 -0
- package/LICENSE +21 -0
- package/package.json +15 -0
- package/readme.md +46 -0
- package/src/index.js +173 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
name: Publish to NPM
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
tags:
|
|
8
|
+
- 'v*' # Sadece versiyon tag'lerinde çalışır (örn: v2.1.0)
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
publish:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- name: Checkout repository
|
|
16
|
+
uses: actions/checkout@v3
|
|
17
|
+
|
|
18
|
+
- name: Setup Node.js
|
|
19
|
+
uses: actions/setup-node@v3
|
|
20
|
+
with:
|
|
21
|
+
node-version: '18'
|
|
22
|
+
registry-url: 'https://registry.npmjs.org'
|
|
23
|
+
|
|
24
|
+
- name: Install dependencies
|
|
25
|
+
run: npm install
|
|
26
|
+
|
|
27
|
+
- name: Publish package
|
|
28
|
+
run: npm publish --access public
|
|
29
|
+
env:
|
|
30
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Ömer
|
|
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/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sehawq.db",
|
|
3
|
+
"version": "2.3.0",
|
|
4
|
+
"description": "Lightweight JSON-based key-value database with namespaces, array & math helpers.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"database",
|
|
8
|
+
"json",
|
|
9
|
+
"key-value",
|
|
10
|
+
"quick.db",
|
|
11
|
+
"lightweight"
|
|
12
|
+
],
|
|
13
|
+
"author": "Omer (sehawq)",
|
|
14
|
+
"license": "MIT"
|
|
15
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# sehawq.db
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/sehawq.db)
|
|
4
|
+
[](https://www.npmjs.com/package/sehawq.db)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
**Lightweight JSON-based key-value database for Node.js**
|
|
8
|
+
Minimal, dependency-free, and easy-to-use. Perfect for small projects, bots, CLIs, and prototyping.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 🚀 Features
|
|
13
|
+
|
|
14
|
+
- **JSON-based lightweight storage** — No extra dependencies, works with pure Node.js.
|
|
15
|
+
- **Key-Value structure** — Simple `set`, `get`, `delete` logic.
|
|
16
|
+
- **Dot-notation namespace** — Access nested data with `user.123.balance`.
|
|
17
|
+
- **Sync & Async API** — Choose blocking or non-blocking file operations.
|
|
18
|
+
- **Auto-save** — Writes changes to disk at regular intervals.
|
|
19
|
+
|
|
20
|
+
### 🔧 Array Helpers
|
|
21
|
+
- `push(key, value)` — Add an element to an array.
|
|
22
|
+
- `pull(key, value)` — Remove an element from an array.
|
|
23
|
+
|
|
24
|
+
### ➗ Math Helpers
|
|
25
|
+
- `add(key, number)` — Increment a numeric value.
|
|
26
|
+
- `subtract(key, number)` — Decrement a numeric value.
|
|
27
|
+
|
|
28
|
+
### 💾 Backup & Restore
|
|
29
|
+
- `backup(filePath)` — Save a backup of the database.
|
|
30
|
+
- `restore(filePath)` — Restore database from a backup.
|
|
31
|
+
|
|
32
|
+
### 📡 Event Emitter
|
|
33
|
+
Hooks into database operations:
|
|
34
|
+
- `set` — Triggered when data is added or updated.
|
|
35
|
+
- `delete` — Triggered when a key is removed.
|
|
36
|
+
- `clear` — Triggered when all data is cleared.
|
|
37
|
+
- `push` / `pull` — Triggered on array modification.
|
|
38
|
+
- `add` — Triggered on numeric increment.
|
|
39
|
+
- `backup` / `restore` — Triggered on backup or restore.
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## 📦 Installation
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install sehawq.db
|
package/src/index.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
const fs = require("fs").promises;
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const EventEmitter = require("events");
|
|
4
|
+
|
|
5
|
+
class SehawqDB extends EventEmitter {
|
|
6
|
+
/**
|
|
7
|
+
* Create a new SehawqDB instance.
|
|
8
|
+
* @param {Object} options
|
|
9
|
+
* @param {string} [options.path="sehawq.json"] File path for storage.
|
|
10
|
+
* @param {number} [options.autoSaveInterval=0] Autosave interval in ms (0 disables autosave).
|
|
11
|
+
*/
|
|
12
|
+
constructor(options = {}) {
|
|
13
|
+
super();
|
|
14
|
+
this.filePath = path.resolve(options.path || "sehawq.json");
|
|
15
|
+
this.autoSaveInterval = options.autoSaveInterval || 0;
|
|
16
|
+
this.data = {};
|
|
17
|
+
|
|
18
|
+
this._init();
|
|
19
|
+
|
|
20
|
+
if (this.autoSaveInterval > 0) {
|
|
21
|
+
this._interval = setInterval(() => this.save(), this.autoSaveInterval);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async _init() {
|
|
26
|
+
try {
|
|
27
|
+
await fs.access(this.filePath);
|
|
28
|
+
} catch {
|
|
29
|
+
await fs.writeFile(this.filePath, JSON.stringify({}), "utf8");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const content = await fs.readFile(this.filePath, "utf8");
|
|
34
|
+
this.data = JSON.parse(content);
|
|
35
|
+
} catch {
|
|
36
|
+
this.data = {};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ---------------- Core methods ----------------
|
|
41
|
+
set(key, value) {
|
|
42
|
+
this._setByPath(key, value);
|
|
43
|
+
this.emit("set", { key, value });
|
|
44
|
+
this.save();
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get(key) {
|
|
49
|
+
return this._getByPath(key);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
delete(key) {
|
|
53
|
+
this._deleteByPath(key);
|
|
54
|
+
this.emit("delete", { key });
|
|
55
|
+
this.save();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
has(key) {
|
|
59
|
+
return this._getByPath(key) !== undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
all() {
|
|
63
|
+
return this.data;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
clear() {
|
|
67
|
+
this.data = {};
|
|
68
|
+
this.emit("clear");
|
|
69
|
+
this.save();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
keys() {
|
|
73
|
+
return Object.keys(this.data);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
values() {
|
|
77
|
+
return Object.values(this.data);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// ---------------- Array helpers ----------------
|
|
81
|
+
push(key, value) {
|
|
82
|
+
let arr = this._getByPath(key);
|
|
83
|
+
if (!Array.isArray(arr)) arr = [];
|
|
84
|
+
arr.push(value);
|
|
85
|
+
this._setByPath(key, arr);
|
|
86
|
+
this.emit("push", { key, value });
|
|
87
|
+
this.save();
|
|
88
|
+
return arr;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
pull(key, value) {
|
|
92
|
+
let arr = this._getByPath(key);
|
|
93
|
+
if (!Array.isArray(arr)) return [];
|
|
94
|
+
arr = arr.filter(v => v !== value);
|
|
95
|
+
this._setByPath(key, arr);
|
|
96
|
+
this.emit("pull", { key, value });
|
|
97
|
+
this.save();
|
|
98
|
+
return arr;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ---------------- Math helpers ----------------
|
|
102
|
+
add(key, number) {
|
|
103
|
+
let val = this._getByPath(key);
|
|
104
|
+
if (typeof val !== "number") val = 0;
|
|
105
|
+
val += number;
|
|
106
|
+
this._setByPath(key, val);
|
|
107
|
+
this.emit("add", { key, number });
|
|
108
|
+
this.save();
|
|
109
|
+
return val;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
subtract(key, number) {
|
|
113
|
+
return this.add(key, -number);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ---------------- Backup & Restore ----------------
|
|
117
|
+
async backup(backupPath) {
|
|
118
|
+
await fs.writeFile(backupPath, JSON.stringify(this.data, null, 2), "utf8");
|
|
119
|
+
this.emit("backup", { backupPath });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async restore(backupPath) {
|
|
123
|
+
const content = await fs.readFile(backupPath, "utf8");
|
|
124
|
+
this.data = JSON.parse(content);
|
|
125
|
+
await this.save();
|
|
126
|
+
this.emit("restore", { backupPath });
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// ---------------- Save ----------------
|
|
130
|
+
async save() {
|
|
131
|
+
const tmpPath = `${this.filePath}.tmp`;
|
|
132
|
+
await fs.writeFile(tmpPath, JSON.stringify(this.data, null, 2), "utf8");
|
|
133
|
+
await fs.rename(tmpPath, this.filePath);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// ---------------- Internal utilities ----------------
|
|
137
|
+
_getByPath(pathStr) {
|
|
138
|
+
const keys = pathStr.split(".");
|
|
139
|
+
let obj = this.data;
|
|
140
|
+
for (const k of keys) {
|
|
141
|
+
if (obj && Object.prototype.hasOwnProperty.call(obj, k)) {
|
|
142
|
+
obj = obj[k];
|
|
143
|
+
} else {
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return obj;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
_setByPath(pathStr, value) {
|
|
151
|
+
const keys = pathStr.split(".");
|
|
152
|
+
let obj = this.data;
|
|
153
|
+
while (keys.length > 1) {
|
|
154
|
+
const k = keys.shift();
|
|
155
|
+
if (!obj[k] || typeof obj[k] !== "object") obj[k] = {};
|
|
156
|
+
obj = obj[k];
|
|
157
|
+
}
|
|
158
|
+
obj[keys[0]] = value;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
_deleteByPath(pathStr) {
|
|
162
|
+
const keys = pathStr.split(".");
|
|
163
|
+
let obj = this.data;
|
|
164
|
+
while (keys.length > 1) {
|
|
165
|
+
const k = keys.shift();
|
|
166
|
+
if (!obj[k]) return;
|
|
167
|
+
obj = obj[k];
|
|
168
|
+
}
|
|
169
|
+
delete obj[keys[0]];
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
module.exports = SehawqDB;
|