chain-db-ts 1.0.0-rc.3 → 1.0.0-rc.4

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/features/table.js CHANGED
@@ -34,43 +34,49 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
34
34
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
35
  }
36
36
  };
37
- import axios from 'axios';
38
37
  import { FIND_WHERE_ADVANCED, FIND_WHERE_BASIC, GET_HISTORY, GET_TABLE, PERSIST_NEW_DATA, GET_DOC } from './constants';
39
- import { post } from './utils';
38
+ import { get, post } from './utils';
40
39
  import { TableDocImpl } from './table-doc';
41
40
  var Table = /** @class */ (function () {
42
41
  function Table(name, db) {
42
+ /**
43
+ * The current document data
44
+ */
45
+ // private currentDoc: Model
43
46
  this.name = '';
44
- this.currentDoc = {};
47
+ // this.currentDoc = {} as Model
45
48
  this.name = name;
46
49
  this.db = db;
47
50
  }
48
51
  /**
49
- * Persist table's document data changes
52
+ * Create a new document in the table
50
53
  */
51
- Table.prototype.persist = function () {
54
+ Table.prototype["new"] = function (data) {
55
+ var _a;
52
56
  return __awaiter(this, void 0, void 0, function () {
53
57
  var url, body, response, e_1;
54
- return __generator(this, function (_a) {
55
- switch (_a.label) {
58
+ return __generator(this, function (_b) {
59
+ switch (_b.label) {
56
60
  case 0:
57
61
  url = "".concat(this.db.server).concat(PERSIST_NEW_DATA(this.name));
58
62
  body = {
59
- data: this.currentDoc
63
+ data: data
60
64
  };
61
- _a.label = 1;
65
+ _b.label = 1;
62
66
  case 1:
63
- _a.trys.push([1, 3, , 4]);
67
+ _b.trys.push([1, 3, , 4]);
64
68
  return [4 /*yield*/, post(url, body, this.db.auth)];
65
69
  case 2:
66
- response = _a.sent();
70
+ response = _b.sent();
67
71
  if (!response.data.success) {
68
72
  throw new Error(response.data.message);
69
73
  }
70
- return [2 /*return*/, response.data.data];
74
+ // Create a TableDoc instance with the document data
75
+ return [2 /*return*/, new TableDocImpl(this.name, (_a = response.data.data) === null || _a === void 0 ? void 0 : _a.doc_id, response.data.data, this.db)];
71
76
  case 3:
72
- e_1 = _a.sent();
73
- throw new Error("Something went wrong with persist operation: ".concat(e_1.message || String(e_1)));
77
+ e_1 = _b.sent();
78
+ // throw new Error(`Something went wrong with persist operation: ${e.message || String(e)}`)
79
+ throw new Error(e_1);
74
80
  case 4: return [2 /*return*/];
75
81
  }
76
82
  });
@@ -79,11 +85,13 @@ var Table = /** @class */ (function () {
79
85
  /**
80
86
  * Get the history of changes. A list of transactions from the most recent to the most old
81
87
  * in a range of depth
82
- * @param limit
88
+ * @param limit Maximum number of items to return (default: 100)
83
89
  */
84
90
  Table.prototype.getHistory = function (limit) {
91
+ if (limit === void 0) { limit = 100; }
85
92
  return __awaiter(this, void 0, void 0, function () {
86
- var url, response, e_2;
93
+ var url, response, history, e_2;
94
+ var _this = this;
87
95
  return __generator(this, function (_a) {
88
96
  switch (_a.label) {
89
97
  case 0:
@@ -91,54 +99,47 @@ var Table = /** @class */ (function () {
91
99
  _a.label = 1;
92
100
  case 1:
93
101
  _a.trys.push([1, 3, , 4]);
94
- return [4 /*yield*/, axios.get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
102
+ return [4 /*yield*/, get(url, this.db.auth)];
95
103
  case 2:
96
104
  response = _a.sent();
97
105
  if (!response.data.success) {
98
106
  throw new Error(response.data.message);
99
107
  }
100
- // Return data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
101
- return [2 /*return*/, response.data.data];
108
+ history = response.data.data;
109
+ return [2 /*return*/, history.map(function (item) { return new TableDocImpl(_this.name, item.doc_id, item, _this.db); })];
102
110
  case 3:
103
111
  e_2 = _a.sent();
104
- throw new Error("Something went wrong with getHistory operation: ".concat(e_2.message || String(e_2)));
112
+ // throw new Error(`Something went wrong with getHistory operation: ${e.message || String(e)}`)
113
+ throw new Error(e_2);
105
114
  case 4: return [2 /*return*/];
106
115
  }
107
116
  });
108
117
  });
109
118
  };
110
119
  /**
111
- * Refetch the table data
120
+ * Get the last record stored in the table
112
121
  */
113
- Table.prototype.refetch = function () {
122
+ Table.prototype.last = function () {
123
+ var _a;
114
124
  return __awaiter(this, void 0, void 0, function () {
115
- var url, response, e_3;
116
- return __generator(this, function (_a) {
117
- switch (_a.label) {
125
+ var url, response, data;
126
+ return __generator(this, function (_b) {
127
+ switch (_b.label) {
118
128
  case 0:
119
129
  url = "".concat(this.db.server).concat(GET_TABLE(this.name));
120
- _a.label = 1;
130
+ return [4 /*yield*/, get(url, this.db.auth)
131
+ // this.currentDoc = response.data.data ? (response.data.data as DocId<Model>) : ({} as DocId<Model>)
132
+ // return this.currentDoc
133
+ // Create a TableDoc instance with the document data
134
+ ];
121
135
  case 1:
122
- _a.trys.push([1, 3, , 4]);
123
- return [4 /*yield*/, axios.get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
124
- case 2:
125
- response = _a.sent();
126
- this.currentDoc = response.data.data ? response.data.data : {};
127
- return [3 /*break*/, 4];
128
- case 3:
129
- e_3 = _a.sent();
130
- throw new Error("Something went wrong with refetch operation: ".concat(e_3.message || String(e_3)));
131
- case 4: return [2 /*return*/];
136
+ response = _b.sent();
137
+ data = response.data.data ? response.data.data : {};
138
+ return [2 /*return*/, new TableDocImpl(this.name, (_a = response.data.data) === null || _a === void 0 ? void 0 : _a.doc_id, data, this.db)];
132
139
  }
133
140
  });
134
141
  });
135
142
  };
136
- /**
137
- * Check if the table is empty
138
- */
139
- Table.prototype.isEmpty = function () {
140
- return Object.keys(this.currentDoc).length === 0;
141
- };
142
143
  /**
143
144
  * Get the table's name
144
145
  */
@@ -149,7 +150,7 @@ var Table = /** @class */ (function () {
149
150
  * Find items in the table using basic criteria with exact matches
150
151
  * @param criteria Object with fields and values to match exactly, e.g.: {age: 44, name: "john"}
151
152
  * @param limit Maximum number of items to return (default: 1000)
152
- * @param reverse If true, returns items in reverse order (default: true)
153
+ * @param reverse If true, returns items in reverse order, newer items first (default: true)
153
154
  * @returns Array of found items matching the criteria
154
155
  * @example
155
156
  * // Find items where age is 44
@@ -169,7 +170,8 @@ var Table = /** @class */ (function () {
169
170
  if (limit === void 0) { limit = 1000; }
170
171
  if (reverse === void 0) { reverse = true; }
171
172
  return __awaiter(this, void 0, void 0, function () {
172
- var url, body, response, e_4;
173
+ var url, body, response, foundData, e_3;
174
+ var _this = this;
173
175
  return __generator(this, function (_a) {
174
176
  switch (_a.label) {
175
177
  case 0:
@@ -188,18 +190,43 @@ var Table = /** @class */ (function () {
188
190
  if (!response.data.success) {
189
191
  throw new Error(response.data.message);
190
192
  }
191
- // Return found data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
192
- return [2 /*return*/, response.data.data];
193
+ foundData = response.data.data;
194
+ return [2 /*return*/, foundData.map(function (item) { return new TableDocImpl(_this.name, item.doc_id, item, _this.db); })];
193
195
  case 3:
194
- e_4 = _a.sent();
195
- throw new Error("Something went wrong with findWhere operation: ".concat(e_4.message || String(e_4)));
196
+ e_3 = _a.sent();
197
+ // throw new Error(`Something went wrong with findWhere operation: ${e.message || String(e)}`)
198
+ throw new Error(e_3);
196
199
  case 4: return [2 /*return*/];
197
200
  }
198
201
  });
199
202
  });
200
203
  };
201
204
  /**
202
- * Find items in the table using advanced criteria with operators
205
+ * Find the first item in the table using basic criteria with exact matches
206
+ * @param criteria Object with fields and values to match exactly, e.g.: {age: 44, name: "john"}
207
+ * @param reverse If true, returns items in reverse order, newer items first (default: true)
208
+ * @returns The first found item matching the criteria or null if no item is found
209
+ */
210
+ Table.prototype.find = function (criteria, reverse) {
211
+ if (reverse === void 0) { reverse = true; }
212
+ return __awaiter(this, void 0, void 0, function () {
213
+ var found, data;
214
+ return __generator(this, function (_a) {
215
+ switch (_a.label) {
216
+ case 0: return [4 /*yield*/, this.findWhere(criteria, 1, reverse)];
217
+ case 1:
218
+ found = _a.sent();
219
+ if (found.length === 0) {
220
+ data = {};
221
+ return [2 /*return*/, new TableDocImpl(this.name, '', data, this.db)];
222
+ }
223
+ return [2 /*return*/, found[0]];
224
+ }
225
+ });
226
+ });
227
+ };
228
+ /**
229
+ * Find the first item in the table using advanced criteria with operators
203
230
  * @param criteria Array of criteria to filter items. Each criteria contains:
204
231
  * - field: The field name to filter
205
232
  * - operator: The operator to use in comparison. Available operators:
@@ -214,7 +241,7 @@ var Table = /** @class */ (function () {
214
241
  * - EndsWith: Check if field ends with value (for strings)
215
242
  * - value: The value to compare against
216
243
  * @param limit Maximum number of items to return (default: 1000)
217
- * @param reverse If true, returns items in reverse order (default: true)
244
+ * @param reverse If true, returns items in reverse order, newer items first (default: true)
218
245
  * @returns Array of found items matching the criteria
219
246
  * @example
220
247
  * // Find items where greeting contains "arg"
@@ -230,7 +257,8 @@ var Table = /** @class */ (function () {
230
257
  if (limit === void 0) { limit = 1000; }
231
258
  if (reverse === void 0) { reverse = true; }
232
259
  return __awaiter(this, void 0, void 0, function () {
233
- var url, body, response, e_5;
260
+ var url, body, response, foundData, e_4;
261
+ var _this = this;
234
262
  return __generator(this, function (_a) {
235
263
  switch (_a.label) {
236
264
  case 0:
@@ -249,32 +277,70 @@ var Table = /** @class */ (function () {
249
277
  if (!response.data.success) {
250
278
  throw new Error(response.data.message);
251
279
  }
252
- // Return found data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
253
- return [2 /*return*/, response.data.data];
280
+ foundData = response.data.data;
281
+ return [2 /*return*/, foundData.map(function (item) { return new TableDocImpl(_this.name, item.doc_id, item, _this.db); })];
254
282
  case 3:
255
- e_5 = _a.sent();
256
- throw new Error("Something went wrong with findWhereAdvanced operation: ".concat(e_5.message || String(e_5)));
283
+ e_4 = _a.sent();
284
+ // throw new Error(`Something went wrong with findWhereAdvanced operation: ${e.message || String(e)}`)
285
+ throw new Error(e_4);
257
286
  case 4: return [2 /*return*/];
258
287
  }
259
288
  });
260
289
  });
261
290
  };
262
291
  /**
263
- * Get the current document ID
292
+ * Find the first single item in the table using advanced criteria with operators
293
+ * @param criteria Array of criteria to filter items. Each criteria contains:
294
+ * - field: The field name to filter
295
+ * - operator: The operator to use in comparison. Available operators:
296
+ * - Eq (==) Equal
297
+ * - Ne (!=) Not Equal
298
+ * - Gt (>) Greater Than
299
+ * - Ge (>=) Greater Than or Equal
300
+ * - Lt (<) Less Than
301
+ * - Le (<=) Less Than or Equal
302
+ * - Contains: Check if field contains value (for strings and arrays)
303
+ * - StartsWith: Check if field starts with value (for strings)
304
+ * - EndsWith: Check if field ends with value (for strings)
305
+ * - value: The value to compare against
306
+ * @param reverse If true, returns items in reverse order, newer items first (default: true)
307
+ * @returns The first found item matching the criteria or null if no item is found
308
+ * @example
309
+ * // Find items where greeting contains "arg"
310
+ * table.findAdvanced([
311
+ * {
312
+ * field: "greeting",
313
+ * operator: Operators.Contains,
314
+ * value: "hello"
315
+ * }
316
+ * ])
264
317
  */
265
- Table.prototype.getCurrentDocId = function () {
266
- var _doc = this.currentDoc;
267
- // Support to applications using older versions
268
- return _doc.doc_id ? _doc.doc_id : '';
318
+ Table.prototype.findAdvanced = function (criteria, reverse) {
319
+ if (reverse === void 0) { reverse = true; }
320
+ return __awaiter(this, void 0, void 0, function () {
321
+ var found, data;
322
+ return __generator(this, function (_a) {
323
+ switch (_a.label) {
324
+ case 0: return [4 /*yield*/, this.findWhereAdvanced(criteria, 1, reverse)];
325
+ case 1:
326
+ found = _a.sent();
327
+ if (found.length === 0) {
328
+ data = {};
329
+ return [2 /*return*/, new TableDocImpl(this.name, '', data, this.db)];
330
+ }
331
+ return [2 /*return*/, found[0]];
332
+ }
333
+ });
334
+ });
269
335
  };
270
336
  /**
271
337
  * Get a specific document by its ID
272
338
  * @param doc_id The document ID to retrieve
273
339
  * @returns A TableDoc instance with the specific document data
274
340
  */
275
- Table.prototype.getDoc = function (doc_id) {
341
+ Table.prototype.getByDocId = function (doc_id) {
276
342
  return __awaiter(this, void 0, void 0, function () {
277
- var url, response, e_6;
343
+ var url, response, e_5;
278
344
  return __generator(this, function (_a) {
279
345
  switch (_a.label) {
280
346
  case 0:
@@ -282,7 +348,7 @@ var Table = /** @class */ (function () {
282
348
  _a.label = 1;
283
349
  case 1:
284
350
  _a.trys.push([1, 3, , 4]);
285
- return [4 /*yield*/, axios.get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
351
+ return [4 /*yield*/, get(url, this.db.auth)];
286
352
  case 2:
287
353
  response = _a.sent();
288
354
  if (!response.data.success) {
@@ -291,8 +357,9 @@ var Table = /** @class */ (function () {
291
357
  // Create a TableDoc instance with the document data
292
358
  return [2 /*return*/, new TableDocImpl(this.name, doc_id, response.data.data, this.db)];
293
359
  case 3:
294
- e_6 = _a.sent();
295
- throw new Error("Something went wrong with getDoc operation: ".concat(e_6.message || String(e_6)));
360
+ e_5 = _a.sent();
361
+ // throw new Error(`Something went wrong with getDoc operation: ${e.message || String(e)}`)
362
+ throw new Error(e_5);
296
363
  case 4: return [2 /*return*/];
297
364
  }
298
365
  });
@@ -9,6 +9,11 @@ export type Connection = {
9
9
  user: string;
10
10
  password: string;
11
11
  };
12
+ export type ConnectionWithToken = {
13
+ server: string | null;
14
+ database: string;
15
+ token: string;
16
+ };
12
17
  export type Criteria<Model> = Partial<Record<keyof Model, string | number | boolean>>;
13
18
  export type CriteriaAdvanced<Model> = {
14
19
  field: Partial<keyof Model>;
@@ -1,2 +1,3 @@
1
1
  import { BasicResponse } from './types';
2
- export declare const post: (url: string, body: any, auth?: string) => Promise<import("axios").AxiosResponse<BasicResponse<any>, any>>;
2
+ export declare const post: (url: string, body: any, auth?: string) => Promise<import("axios").AxiosResponse<BasicResponse<any>, any, {}>>;
3
+ export declare const get: (url: string, auth?: string) => Promise<import("axios").AxiosResponse<BasicResponse<any>, any, {}>>;
package/features/utils.js CHANGED
@@ -1,7 +1,14 @@
1
1
  import axios from 'axios';
2
+ // Using Axios
2
3
  export var post = function (url, body, auth) {
3
4
  if (auth === void 0) { auth = ''; }
4
5
  return axios.post(url, body, {
5
6
  headers: { 'content-type': 'application/json', Authorization: "Basic ".concat(auth) }
6
7
  });
7
8
  };
9
+ export var get = function (url, auth) {
10
+ if (auth === void 0) { auth = ''; }
11
+ return axios.get(url, {
12
+ headers: { Authorization: "Basic ".concat(auth) }
13
+ });
14
+ };
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { ChainDB, connect } from './features/chain-db';
1
+ export { ChainDB, connect, connectWithToken } from './features/chain-db';
2
2
  export { BasicResponse, Operators, CriteriaAdvanced, Criteria, TableDoc, EventTypes, EventData, EventCallback, } from './features/types';
3
3
  export { default as Events } from './features/events';
4
4
  export { TableDocImpl } from './features/table-doc';
5
+ export { default as Table } from './features/table';
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
- export { ChainDB, connect } from './features/chain-db';
1
+ export { ChainDB, connect, connectWithToken } from './features/chain-db';
2
2
  export { Operators, EventTypes, } from './features/types';
3
3
  export { default as Events } from './features/events';
4
4
  export { TableDocImpl } from './features/table-doc';
5
+ export { default as Table } from './features/table';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chain-db-ts",
3
- "version": "1.0.0-rc.3",
3
+ "version": "1.0.0-rc.4",
4
4
  "description": "Chain DB Client for Javascript/Typescript Node apps.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./index.js",