reddb-sdk 1.0.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/lib/RedDBClient.js +94 -0
- package/package.json +13 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
const fetch = require('node-fetch');
|
|
2
|
+
|
|
3
|
+
class RedDBClient {
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} baseURL - URL do cluster (ex: http://localhost:3000)
|
|
6
|
+
* @param {string} reddbPassword - senha do RedDB
|
|
7
|
+
* @param {string|null} adminPassword - senha admin (opcional, necessária para escrita)
|
|
8
|
+
*/
|
|
9
|
+
constructor(baseURL, reddbPassword, adminPassword = null) {
|
|
10
|
+
this.baseURL = baseURL.replace(/\/$/, '');
|
|
11
|
+
this.reddbPassword = reddbPassword;
|
|
12
|
+
this.adminPassword = adminPassword;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// =========================
|
|
16
|
+
// Operações de escrita (set/delete) - exigem adminPassword
|
|
17
|
+
// =========================
|
|
18
|
+
async set(key, value, document = 'users.json') {
|
|
19
|
+
if (!this.adminPassword) throw new Error('Admin password required for set');
|
|
20
|
+
|
|
21
|
+
const res = await fetch(`${this.baseURL}/set`, {
|
|
22
|
+
method: 'POST',
|
|
23
|
+
headers: { 'Content-Type': 'application/json' },
|
|
24
|
+
body: JSON.stringify({
|
|
25
|
+
key,
|
|
26
|
+
value,
|
|
27
|
+
document,
|
|
28
|
+
reddbPassword: this.reddbPassword,
|
|
29
|
+
adminPassword: this.adminPassword
|
|
30
|
+
})
|
|
31
|
+
});
|
|
32
|
+
return res.json();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async delete(key, document = 'users.json') {
|
|
36
|
+
if (!this.adminPassword) throw new Error('Admin password required for delete');
|
|
37
|
+
|
|
38
|
+
const res = await fetch(`${this.baseURL}/delete`, {
|
|
39
|
+
method: 'POST',
|
|
40
|
+
headers: { 'Content-Type': 'application/json' },
|
|
41
|
+
body: JSON.stringify({
|
|
42
|
+
key,
|
|
43
|
+
document,
|
|
44
|
+
reddbPassword: this.reddbPassword,
|
|
45
|
+
adminPassword: this.adminPassword
|
|
46
|
+
})
|
|
47
|
+
});
|
|
48
|
+
return res.json();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// =========================
|
|
52
|
+
// Operações de leitura (apenas REDDB_PASSWORD)
|
|
53
|
+
// =========================
|
|
54
|
+
async get(key, document = 'users.json') {
|
|
55
|
+
const url = `${this.baseURL}/get/${encodeURIComponent(key)}?document=${encodeURIComponent(document)}&reddbPassword=${encodeURIComponent(this.reddbPassword)}`;
|
|
56
|
+
const res = await fetch(url);
|
|
57
|
+
return res.json();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async listKeys(document = 'users.json') {
|
|
61
|
+
const url = `${this.baseURL}/listKeys?document=${encodeURIComponent(document)}&reddbPassword=${encodeURIComponent(this.reddbPassword)}`;
|
|
62
|
+
const res = await fetch(url);
|
|
63
|
+
return res.json();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// =========================
|
|
67
|
+
// Endpoints admin
|
|
68
|
+
// =========================
|
|
69
|
+
async reset() {
|
|
70
|
+
if (!this.adminPassword) throw new Error('Admin password required for reset');
|
|
71
|
+
|
|
72
|
+
const res = await fetch(`${this.baseURL}/admin/reset`, {
|
|
73
|
+
method: 'POST',
|
|
74
|
+
headers: { 'Content-Type': 'application/json' },
|
|
75
|
+
body: JSON.stringify({
|
|
76
|
+
reddbPassword: this.reddbPassword,
|
|
77
|
+
adminPassword: this.adminPassword
|
|
78
|
+
})
|
|
79
|
+
});
|
|
80
|
+
return res.json();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async listAdminKeys() {
|
|
84
|
+
if (!this.adminPassword) throw new Error('Admin password required for listAdminKeys');
|
|
85
|
+
|
|
86
|
+
const res = await fetch(`${this.baseURL}/admin/keys`, {
|
|
87
|
+
method: 'GET',
|
|
88
|
+
headers: { 'Content-Type': 'application/json' }
|
|
89
|
+
});
|
|
90
|
+
return res.json();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = RedDBClient;
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "reddb-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "SDK Node.js para RedDB Cluster",
|
|
5
|
+
"main": "lib/RedDBClient.js",
|
|
6
|
+
"keywords": ["reddb", "cluster", "database", "sdk"],
|
|
7
|
+
"author": "MagicDB",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"node-fetch": "^3.3.2"
|
|
11
|
+
},
|
|
12
|
+
"type": "commonjs"
|
|
13
|
+
}
|