reddb-sdk 1.0.5 → 2.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.
@@ -2,7 +2,7 @@ const fetch = require('node-fetch');
2
2
 
3
3
  class RedDBClient {
4
4
  constructor(baseURL, reddbPassword, adminPassword = null, options = {}) {
5
- if (!baseURL) throw new Error('baseURL é obrigatório');
5
+ if (!baseURL) throw new Error('Base URL is required');
6
6
 
7
7
  this.baseURL = baseURL.replace(/\/$/, '');
8
8
  this.reddbPassword = reddbPassword;
@@ -23,7 +23,7 @@ class RedDBClient {
23
23
  try {
24
24
  return JSON.parse(text);
25
25
  } catch {
26
- throw new Error(`Resposta inválida do servidor:\n${text}`);
26
+ throw new Error(`Invalid server response:\n${text}`);
27
27
  }
28
28
  }
29
29
 
@@ -39,10 +39,10 @@ class RedDBClient {
39
39
  clearTimeout(id);
40
40
 
41
41
  if (err.name === 'AbortError') {
42
- throw new Error('Timeout: cluster não respondeu a tempo');
42
+ throw new Error('Timeout: cluster did not respond in time');
43
43
  }
44
44
 
45
- throw new Error('Cluster offline ou inacessível');
45
+ throw new Error('Cluster offline or unreachable');
46
46
  }
47
47
  }
48
48
 
@@ -50,7 +50,7 @@ class RedDBClient {
50
50
  let url = `${this.baseURL}${path}`;
51
51
  let body = {};
52
52
 
53
- // aceita body como objeto OU string
53
+ // accept body as object or string
54
54
  if (options.body) {
55
55
  body = typeof options.body === 'string'
56
56
  ? JSON.parse(options.body)
@@ -60,7 +60,7 @@ class RedDBClient {
60
60
  body.reddbPassword = this.reddbPassword;
61
61
 
62
62
  if (isAdmin) {
63
- if (!this.adminPassword) throw new Error('Admin password não fornecida');
63
+ if (!this.adminPassword) throw new Error('Admin password not provided');
64
64
  body.adminPassword = this.adminPassword;
65
65
  }
66
66
 
@@ -85,22 +85,24 @@ class RedDBClient {
85
85
 
86
86
  const data = await this._safeJson(res);
87
87
 
88
- // 🔥 tratamento de erro da API
89
88
  if (!res.ok || data.error) {
90
- throw new Error(data.error || `Erro HTTP ${res.status}`);
89
+ throw new Error(data.error || `HTTP error ${res.status}`);
91
90
  }
92
91
 
93
92
  return data;
94
93
  } catch (err) {
95
- this._log(`Erro (tentativa ${attempt + 1}):`, err.message);
94
+ this._log(`Error (attempt ${attempt + 1}):`, err.message);
96
95
 
97
96
  if (attempt === this.retries) {
98
- throw new Error(`Falha após ${this.retries + 1} tentativas: ${err.message}`);
97
+ throw new Error(`Failed after ${this.retries + 1} attempts: ${err.message}`);
99
98
  }
100
99
  }
101
100
  }
102
101
  }
103
102
 
103
+ // =========================
104
+ // CRUD
105
+ // =========================
104
106
  async set(key, value, document = 'users.json') {
105
107
  return this._request('/set', {
106
108
  method: 'POST',
@@ -129,17 +131,48 @@ class RedDBClient {
129
131
  });
130
132
  }
131
133
 
132
- async reset() {
133
- return this._request('/admin/reset', {
134
- method: 'POST'
134
+ async exists(key, document = 'users.json') {
135
+ return this._request(`/exists/${encodeURIComponent(key)}`, {
136
+ method: 'GET',
137
+ body: { document }
138
+ });
139
+ }
140
+
141
+ async update(key, value, document = 'users.json') {
142
+ return this._request('/update', {
143
+ method: 'POST',
144
+ body: { key, value, document }
135
145
  }, true);
136
146
  }
137
147
 
138
- async listAdminKeys() {
139
- return this._request('/admin/keys', {
140
- method: 'POST'
148
+ async addCollection(collectionName, document = 'users.json') {
149
+ return this._request('/addCollection', {
150
+ method: 'POST',
151
+ body: { collectionName, document }
141
152
  }, true);
142
153
  }
154
+
155
+ async listCollectionKeys(collection, document = 'users.json') {
156
+ return this._request('/listCollectionKeys', {
157
+ method: 'GET',
158
+ body: { collection, document }
159
+ });
160
+ }
161
+
162
+ // =========================
163
+ // Admin
164
+ // =========================
165
+ async reset() {
166
+ return this._request('/admin/reset', { method: 'POST' }, true);
167
+ }
168
+
169
+ async listAdminKeys() {
170
+ return this._request('/admin/keys', { method: 'POST' }, true);
171
+ }
172
+
173
+ async backup() {
174
+ return this._request('/admin/backup', { method: 'POST' }, true);
175
+ }
143
176
  }
144
177
 
145
178
  module.exports = RedDBClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reddb-sdk",
3
- "version": "1.0.5",
3
+ "version": "2.0.0",
4
4
  "description": "SDK Node.js para RedDB Cluster",
5
5
  "main": "lib/RedDBClient.js",
6
6
  "keywords": [
@@ -12,7 +12,8 @@
12
12
  "author": "MagicDB",
13
13
  "license": "MIT",
14
14
  "dependencies": {
15
- "node-fetch": "^2.6.7"
15
+ "node-fetch": "^2.6.7",
16
+ "reddb-sdk": "^1.0.5"
16
17
  },
17
18
  "type": "commonjs"
18
19
  }
package/teste.js ADDED
@@ -0,0 +1,40 @@
1
+ // teste.js
2
+ const RedDBClient = require('reddb-sdk'); // npm link ou npm install reddb-sdk
3
+
4
+ const client = new RedDBClient('http://localhost:3000', 'senha_reddb', 'senha_admin', {
5
+ debug: true // mostra logs detalhados
6
+ });
7
+
8
+ (async () => {
9
+ try {
10
+ console.log('--- Teste RedDB SDK ---');
11
+
12
+ // Criar/atualizar uma chave em batata.json
13
+ const setResult = await client.set('batata', 'valor da batata', 'batata.json');
14
+ console.log('SET:', setResult);
15
+
16
+ // Ler a chave
17
+ const getResult = await client.get('batata', 'batata.json');
18
+ console.log('GET:', getResult);
19
+
20
+ // Listar chaves do documento
21
+ const listKeysResult = await client.listKeys('batata.json');
22
+ console.log('LIST KEYS:', listKeysResult);
23
+
24
+ // Deletar a chave
25
+ const deleteResult = await client.delete('batata', 'batata.json');
26
+ console.log('DELETE:', deleteResult);
27
+
28
+ // Criar outro documento sem resetar
29
+ await client.set('minhaChave', 'meuValor', 'meu_doc.json');
30
+ console.log('SET novo documento:', await client.listKeys('meu_doc.json'));
31
+
32
+ // Listar chaves do admin
33
+ const adminKeys = await client.listAdminKeys();
34
+ console.log('ADMIN KEYS:', adminKeys);
35
+
36
+ console.log('--- Teste finalizado ---');
37
+ } catch (err) {
38
+ console.error('Erro no teste RedDB SDK:', err);
39
+ }
40
+ })();