reddb-sdk 2.0.0 → 3.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.
Files changed (2) hide show
  1. package/lib/RedDBClient.js +52 -52
  2. package/package.json +1 -1
@@ -19,7 +19,6 @@ class RedDBClient {
19
19
 
20
20
  async _safeJson(res) {
21
21
  const text = await res.text();
22
-
23
22
  try {
24
23
  return JSON.parse(text);
25
24
  } catch {
@@ -37,11 +36,7 @@ class RedDBClient {
37
36
  return res;
38
37
  } catch (err) {
39
38
  clearTimeout(id);
40
-
41
- if (err.name === 'AbortError') {
42
- throw new Error('Timeout: cluster did not respond in time');
43
- }
44
-
39
+ if (err.name === 'AbortError') throw new Error('Timeout: cluster did not respond in time');
45
40
  throw new Error('Cluster offline or unreachable');
46
41
  }
47
42
  }
@@ -50,15 +45,11 @@ class RedDBClient {
50
45
  let url = `${this.baseURL}${path}`;
51
46
  let body = {};
52
47
 
53
- // accept body as object or string
54
48
  if (options.body) {
55
- body = typeof options.body === 'string'
56
- ? JSON.parse(options.body)
57
- : options.body;
49
+ body = typeof options.body === 'string' ? JSON.parse(options.body) : options.body;
58
50
  }
59
51
 
60
52
  body.reddbPassword = this.reddbPassword;
61
-
62
53
  if (isAdmin) {
63
54
  if (!this.adminPassword) throw new Error('Admin password not provided');
64
55
  body.adminPassword = this.adminPassword;
@@ -78,24 +69,15 @@ class RedDBClient {
78
69
  try {
79
70
  this._log('Request:', options.method, url);
80
71
 
81
- const res = await this._fetchWithTimeout(url, {
82
- ...options,
83
- headers
84
- });
85
-
72
+ const res = await this._fetchWithTimeout(url, { ...options, headers });
86
73
  const data = await this._safeJson(res);
87
74
 
88
- if (!res.ok || data.error) {
89
- throw new Error(data.error || `HTTP error ${res.status}`);
90
- }
75
+ if (!res.ok || data.error) throw new Error(data.error || `HTTP error ${res.status}`);
91
76
 
92
77
  return data;
93
78
  } catch (err) {
94
79
  this._log(`Error (attempt ${attempt + 1}):`, err.message);
95
-
96
- if (attempt === this.retries) {
97
- throw new Error(`Failed after ${this.retries + 1} attempts: ${err.message}`);
98
- }
80
+ if (attempt === this.retries) throw new Error(`Failed after ${this.retries + 1} attempts: ${err.message}`);
99
81
  }
100
82
  }
101
83
  }
@@ -104,63 +86,81 @@ class RedDBClient {
104
86
  // CRUD
105
87
  // =========================
106
88
  async set(key, value, document = 'users.json') {
107
- return this._request('/set', {
108
- method: 'POST',
109
- body: { key, value, document }
110
- }, true);
89
+ return this._request('/set', { method: 'POST', body: { key, value, document } }, true);
111
90
  }
112
91
 
113
92
  async delete(key, document = 'users.json') {
114
- return this._request('/delete', {
115
- method: 'POST',
116
- body: { key, document }
117
- }, true);
93
+ return this._request('/delete', { method: 'POST', body: { key, document } }, true);
118
94
  }
119
95
 
120
96
  async get(key, document = 'users.json') {
121
- return this._request(`/get/${encodeURIComponent(key)}`, {
122
- method: 'GET',
123
- body: { document }
124
- });
97
+ return this._request(`/get/${encodeURIComponent(key)}`, { method: 'GET', body: { document } });
125
98
  }
126
99
 
127
100
  async listKeys(document = 'users.json') {
128
- return this._request('/listKeys', {
129
- method: 'GET',
130
- body: { document }
131
- });
101
+ return this._request('/listKeys', { method: 'GET', body: { document } });
132
102
  }
133
103
 
134
104
  async exists(key, document = 'users.json') {
135
- return this._request(`/exists/${encodeURIComponent(key)}`, {
136
- method: 'GET',
137
- body: { document }
138
- });
105
+ return this._request(`/exists/${encodeURIComponent(key)}`, { method: 'GET', body: { document } });
139
106
  }
140
107
 
141
108
  async update(key, value, document = 'users.json') {
142
- return this._request('/update', {
109
+ return this._request('/update', { method: 'POST', body: { key, value, document } }, true);
110
+ }
111
+
112
+ // =========================
113
+ // COLLECTIONS
114
+ // =========================
115
+ async addCollection(collectionName, document = 'users.json') {
116
+ return this._request('/addCollection', { method: 'POST', body: { collectionName, document } }, true);
117
+ }
118
+
119
+ async removeCollection(collectionName, document = 'users.json') {
120
+ return this._request('/removeCollection', { method: 'POST', body: { collectionName, document } }, true);
121
+ }
122
+
123
+ async listCollectionKeys(collection, document = 'users.json') {
124
+ return this._request('/listCollectionKeys', { method: 'GET', body: { collection, document } });
125
+ }
126
+
127
+ async updateCollectionItem(document, collection, key, value) {
128
+ return this._request('/updateCollectionItem', {
143
129
  method: 'POST',
144
- body: { key, value, document }
130
+ body: { document, collection, key, value }
145
131
  }, true);
146
132
  }
147
133
 
148
- async addCollection(collectionName, document = 'users.json') {
149
- return this._request('/addCollection', {
134
+ async deleteCollectionItem(document, collection, key) {
135
+ return this._request('/deleteCollectionItem', {
150
136
  method: 'POST',
151
- body: { collectionName, document }
137
+ body: { document, collection, key }
152
138
  }, true);
153
139
  }
154
140
 
155
- async listCollectionKeys(collection, document = 'users.json') {
156
- return this._request('/listCollectionKeys', {
141
+ async getCollectionItem(document, collection, key) {
142
+ return this._request('/getCollectionItem', {
143
+ method: 'GET',
144
+ body: { document, collection, key }
145
+ });
146
+ }
147
+
148
+ async listCollectionItems(document, collection) {
149
+ return this._request('/listCollectionItems', {
157
150
  method: 'GET',
158
- body: { collection, document }
151
+ body: { document, collection }
159
152
  });
160
153
  }
161
154
 
162
155
  // =========================
163
- // Admin
156
+ // DOCUMENT
157
+ // =========================
158
+ async getDocument(document) {
159
+ return this._request('/getDocument', { method: 'GET', body: { document } });
160
+ }
161
+
162
+ // =========================
163
+ // ADMIN
164
164
  // =========================
165
165
  async reset() {
166
166
  return this._request('/admin/reset', { method: 'POST' }, true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reddb-sdk",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "SDK Node.js para RedDB Cluster",
5
5
  "main": "lib/RedDBClient.js",
6
6
  "keywords": [