chain-db-ts 1.0.0-rc.1 → 1.0.0-rc.2

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/CHANGELOG.md ADDED
@@ -0,0 +1,110 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ ## [1.0.0-rc.2] - 2025-03-05
6
+
7
+ ### Added
8
+
9
+ - New `TableDoc` interface for working with specific documents
10
+ - Added `refetch()` method to `TableDoc` to get the latest document data
11
+ - Added `getTableName()` method to `TableDoc` to get the table name
12
+ - Added `isEmpty()` method to `TableDoc` to check if the document is empty
13
+ - Added `DocId<Model>` type that adds a readonly `doc_id` property to models
14
+ - Added `getCurrentDocId()` method to `Table` to get the current document ID
15
+ - The `persist()` method now returns the created document with its `doc_id`
16
+ - Document IDs are now accessible directly via `doc_id` property in both `Table` and `TableDoc` instances
17
+
18
+ ### Changed
19
+
20
+ - **BREAKING CHANGE**: Renamed `table` property to `currentDoc` in `Table` class for better semantics
21
+ - Improved error messages with more details about the operation and error
22
+ - Enhanced type safety with the `DocId<Model>` type
23
+
24
+ ### Removed
25
+
26
+ - **BREAKING CHANGE**: Removed `update()` method from `Table` class
27
+ - To update documents, you must now use `getDoc()` to get a document reference and then call `update()` on that reference
28
+ - This change prevents accidental creation of duplicate records and makes the API more intuitive
29
+
30
+ ### Migration Guide
31
+
32
+ #### Property Renaming
33
+
34
+ Before:
35
+
36
+ ```typescript
37
+ // Accessing table data
38
+ console.log(greetingTable.table)
39
+
40
+ // Accessing document data from TableDoc
41
+ console.log(specificDoc.table)
42
+ ```
43
+
44
+ After:
45
+
46
+ ```typescript
47
+ // Accessing current document data from Table
48
+ console.log(greetingTable.currentDoc)
49
+
50
+ // Accessing document data from TableDoc
51
+ console.log(specificDoc.doc)
52
+ ```
53
+
54
+ #### Updating Documents
55
+
56
+ Before:
57
+
58
+ ```typescript
59
+ // Update the last item
60
+ greetingTable.table.greeting = 'Updated greeting'
61
+ await greetingTable.update()
62
+
63
+ // Update a specific document by ID
64
+ greetingTable.table.greeting = 'Updated specific document'
65
+ await greetingTable.update('550e8400-e29b-41d4-a716-446655440000')
66
+ ```
67
+
68
+ After:
69
+
70
+ ```typescript
71
+ // Get a specific document by ID
72
+ const specificDoc = await greetingTable.getDoc('550e8400-e29b-41d4-a716-446655440000')
73
+
74
+ // Update the document
75
+ specificDoc.doc.greeting = 'Updated greeting'
76
+ await specificDoc.update()
77
+
78
+ // Optionally, refetch the document to get the latest data
79
+ await specificDoc.refetch()
80
+ ```
81
+
82
+ #### Accessing Document IDs
83
+
84
+ New in this version:
85
+
86
+ ```typescript
87
+ // When persisting data, you get the document ID in the result
88
+ const result = await greetingTable.persist()
89
+ console.log(result.doc_id) // The ID of the newly created document
90
+
91
+ // You can also get the current document ID directly from the table
92
+ const currentDocId = greetingTable.getCurrentDocId()
93
+
94
+ // When working with a specific document, the ID is available in multiple ways
95
+ const specificDoc = await greetingTable.getDoc(docId)
96
+ console.log(specificDoc.doc_id) // From the TableDoc instance
97
+ console.log(specificDoc.doc.doc_id) // From the document object
98
+ ```
99
+
100
+ ## [1.0.0-rc.1] - 2025-03-01
101
+
102
+ Initial release candidate.
103
+
104
+ ### Added
105
+
106
+ - Type-safe API with TypeScript generics
107
+ - Promise-based API with async/await
108
+ - WebSocket support for real-time updates
109
+ - Advanced query capabilities
110
+ - Complete history access
@@ -2,9 +2,10 @@ export declare const DEFAULT_API_SERVER = "http://localhost:2818";
2
2
  export declare const API_BASE = "/api/v1";
3
3
  export declare const CONNECT: string;
4
4
  export declare const GET_TABLE: (table: string) => string;
5
- export declare const UPDATE_LAST_ITEM: (table: string) => string;
5
+ export declare const UPDATE_ITEM: (table: string) => string;
6
6
  export declare const PERSIST_NEW_DATA: (table: string) => string;
7
7
  export declare const GET_HISTORY: (table: string, limit?: number) => string;
8
8
  export declare const FIND_WHERE_BASIC: (table: string) => string;
9
9
  export declare const FIND_WHERE_ADVANCED: (table: string) => string;
10
+ export declare const GET_DOC: (table: string, doc_id: string) => string;
10
11
  export declare const WEB_SOCKET_EVENTS: string;
@@ -1,14 +1,14 @@
1
1
  "use strict";
2
2
  exports.__esModule = true;
3
- exports.WEB_SOCKET_EVENTS = exports.FIND_WHERE_ADVANCED = exports.FIND_WHERE_BASIC = exports.GET_HISTORY = exports.PERSIST_NEW_DATA = exports.UPDATE_LAST_ITEM = exports.GET_TABLE = exports.CONNECT = exports.API_BASE = exports.DEFAULT_API_SERVER = void 0;
3
+ exports.WEB_SOCKET_EVENTS = exports.GET_DOC = exports.FIND_WHERE_ADVANCED = exports.FIND_WHERE_BASIC = exports.GET_HISTORY = exports.PERSIST_NEW_DATA = exports.UPDATE_ITEM = exports.GET_TABLE = exports.CONNECT = exports.API_BASE = exports.DEFAULT_API_SERVER = void 0;
4
4
  // Contants
5
5
  exports.DEFAULT_API_SERVER = 'http://localhost:2818';
6
6
  exports.API_BASE = '/api/v1';
7
7
  exports.CONNECT = "".concat(exports.API_BASE, "/database/connect");
8
8
  var GET_TABLE = function (table) { return "".concat(exports.API_BASE, "/table/").concat(table); };
9
9
  exports.GET_TABLE = GET_TABLE;
10
- var UPDATE_LAST_ITEM = function (table) { return "".concat(exports.API_BASE, "/table/").concat(table, "/update"); };
11
- exports.UPDATE_LAST_ITEM = UPDATE_LAST_ITEM;
10
+ var UPDATE_ITEM = function (table) { return "".concat(exports.API_BASE, "/table/").concat(table, "/update"); };
11
+ exports.UPDATE_ITEM = UPDATE_ITEM;
12
12
  var PERSIST_NEW_DATA = function (table) { return "".concat(exports.API_BASE, "/table/").concat(table, "/persist"); };
13
13
  exports.PERSIST_NEW_DATA = PERSIST_NEW_DATA;
14
14
  var GET_HISTORY = function (table, limit) {
@@ -20,4 +20,6 @@ var FIND_WHERE_BASIC = function (table) { return "".concat(exports.API_BASE, "/t
20
20
  exports.FIND_WHERE_BASIC = FIND_WHERE_BASIC;
21
21
  var FIND_WHERE_ADVANCED = function (table) { return "".concat(exports.API_BASE, "/table/").concat(table, "/find-advanced"); };
22
22
  exports.FIND_WHERE_ADVANCED = FIND_WHERE_ADVANCED;
23
+ var GET_DOC = function (table, doc_id) { return "".concat(exports.API_BASE, "/table/").concat(table, "/doc/").concat(doc_id); };
24
+ exports.GET_DOC = GET_DOC;
23
25
  exports.WEB_SOCKET_EVENTS = "".concat(exports.API_BASE, "/events");
@@ -0,0 +1,37 @@
1
+ import { ChainDB } from './chain-db';
2
+ import { DocId, TableDoc } from './types';
3
+ /**
4
+ * Implementation of TableDoc interface
5
+ * Represents a specific document from a table
6
+ */
7
+ export declare class TableDocImpl<Model> implements TableDoc<Model> {
8
+ /**
9
+ * The document data
10
+ */
11
+ doc: DocId<Model>;
12
+ /**
13
+ * The document ID
14
+ */
15
+ private doc_id;
16
+ private tableName;
17
+ private db;
18
+ constructor(tableName: string, doc_id: string, data: DocId<Model>, db: ChainDB);
19
+ /**
20
+ * Update the document data
21
+ * This will update the specific document without creating a new one
22
+ */
23
+ update(): Promise<void>;
24
+ /**
25
+ * Refetch the document data from the database
26
+ * Useful when the document might have been updated by another application
27
+ */
28
+ refetch(): Promise<void>;
29
+ /**
30
+ * Get the table name this document belongs to
31
+ */
32
+ getTableName(): string;
33
+ /**
34
+ * Check if the table is empty
35
+ */
36
+ isEmpty(): boolean;
37
+ }
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ exports.__esModule = true;
42
+ exports.TableDocImpl = void 0;
43
+ var axios_1 = __importDefault(require("axios"));
44
+ var constants_1 = require("./constants");
45
+ var utils_1 = require("./utils");
46
+ /**
47
+ * Implementation of TableDoc interface
48
+ * Represents a specific document from a table
49
+ */
50
+ var TableDocImpl = /** @class */ (function () {
51
+ function TableDocImpl(tableName, doc_id, data, db) {
52
+ this.tableName = tableName;
53
+ this.doc_id = doc_id;
54
+ this.doc = data;
55
+ this.db = db;
56
+ }
57
+ /**
58
+ * Update the document data
59
+ * This will update the specific document without creating a new one
60
+ */
61
+ TableDocImpl.prototype.update = function () {
62
+ return __awaiter(this, void 0, void 0, function () {
63
+ var url, body, response, e_1;
64
+ return __generator(this, function (_a) {
65
+ switch (_a.label) {
66
+ case 0:
67
+ url = "".concat(this.db.server).concat((0, constants_1.UPDATE_ITEM)(this.tableName));
68
+ body = {
69
+ data: this.doc,
70
+ doc_id: this.doc_id
71
+ };
72
+ _a.label = 1;
73
+ case 1:
74
+ _a.trys.push([1, 3, , 4]);
75
+ return [4 /*yield*/, (0, utils_1.post)(url, body, this.db.auth)];
76
+ case 2:
77
+ response = _a.sent();
78
+ if (!response.data.success) {
79
+ throw new Error(response.data.message);
80
+ }
81
+ return [3 /*break*/, 4];
82
+ case 3:
83
+ e_1 = _a.sent();
84
+ throw new Error("Something went wrong updating document ".concat(this.doc_id, ": ").concat(e_1.message || String(e_1)));
85
+ case 4: return [2 /*return*/];
86
+ }
87
+ });
88
+ });
89
+ };
90
+ /**
91
+ * Refetch the document data from the database
92
+ * Useful when the document might have been updated by another application
93
+ */
94
+ TableDocImpl.prototype.refetch = function () {
95
+ return __awaiter(this, void 0, void 0, function () {
96
+ var url, response, e_2;
97
+ return __generator(this, function (_a) {
98
+ switch (_a.label) {
99
+ case 0:
100
+ url = "".concat(this.db.server).concat((0, constants_1.GET_DOC)(this.tableName, this.doc_id));
101
+ _a.label = 1;
102
+ case 1:
103
+ _a.trys.push([1, 3, , 4]);
104
+ return [4 /*yield*/, axios_1["default"].get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
105
+ case 2:
106
+ response = _a.sent();
107
+ if (!response.data.success) {
108
+ throw new Error(response.data.message);
109
+ }
110
+ // Update the table data with the latest data from the database
111
+ this.doc = response.data.data;
112
+ return [3 /*break*/, 4];
113
+ case 3:
114
+ e_2 = _a.sent();
115
+ throw new Error("Something went wrong refetching document ".concat(this.doc_id, ": ").concat(e_2.message || String(e_2)));
116
+ case 4: return [2 /*return*/];
117
+ }
118
+ });
119
+ });
120
+ };
121
+ /**
122
+ * Get the table name this document belongs to
123
+ */
124
+ TableDocImpl.prototype.getTableName = function () {
125
+ return this.tableName;
126
+ };
127
+ /**
128
+ * Check if the table is empty
129
+ */
130
+ TableDocImpl.prototype.isEmpty = function () {
131
+ return Object.keys(this.doc).length === 0;
132
+ };
133
+ return TableDocImpl;
134
+ }());
135
+ exports.TableDocImpl = TableDocImpl;
@@ -1,25 +1,20 @@
1
1
  import { ChainDB } from './chain-db';
2
- import { Criteria, CriteriaAdvanced } from './types';
2
+ import { Criteria, CriteriaAdvanced, DocId, TableDoc } from './types';
3
3
  declare class Table<Model> {
4
- table: Model;
4
+ currentDoc: Model;
5
5
  private name;
6
6
  private db;
7
7
  constructor(name: string, db: ChainDB);
8
8
  /**
9
- * Persist table data changes
9
+ * Persist table's document data changes
10
10
  */
11
- persist(): Promise<void>;
12
- /**
13
- * Update data of the last table's item (or create a new item if there is none).
14
- * This ensures that no new item is created.
15
- */
16
- update(): Promise<void>;
11
+ persist(): Promise<DocId<Model>>;
17
12
  /**
18
13
  * Get the history of changes. A list of transactions from the most recent to the most old
19
14
  * in a range of depth
20
15
  * @param limit
21
16
  */
22
- getHistory(limit: number): Promise<Model[]>;
17
+ getHistory(limit: number): Promise<DocId<Model>[]>;
23
18
  /**
24
19
  * Refetch the table data
25
20
  */
@@ -52,7 +47,7 @@ declare class Table<Model> {
52
47
  * score: 100
53
48
  * })
54
49
  */
55
- findWhere(criteria: Criteria<Model>, limit?: number, reverse?: boolean): Promise<Model[]>;
50
+ findWhere(criteria: Criteria<Model>, limit?: number, reverse?: boolean): Promise<DocId<Model>[]>;
56
51
  /**
57
52
  * Find items in the table using advanced criteria with operators
58
53
  * @param criteria Array of criteria to filter items. Each criteria contains:
@@ -81,6 +76,16 @@ declare class Table<Model> {
81
76
  * }
82
77
  * ])
83
78
  */
84
- findWhereAdvanced(criteria: CriteriaAdvanced<Model>[], limit?: number, reverse?: boolean): Promise<Model[]>;
79
+ findWhereAdvanced(criteria: CriteriaAdvanced<Model>[], limit?: number, reverse?: boolean): Promise<DocId<Model>[]>;
80
+ /**
81
+ * Get the current document ID
82
+ */
83
+ getCurrentDocId(): string;
84
+ /**
85
+ * Get a specific document by its ID
86
+ * @param doc_id The document ID to retrieve
87
+ * @returns A TableDoc instance with the specific document data
88
+ */
89
+ getDoc(doc_id: string): Promise<TableDoc<Model>>;
85
90
  }
86
91
  export default Table;
@@ -42,15 +42,16 @@ exports.__esModule = true;
42
42
  var axios_1 = __importDefault(require("axios"));
43
43
  var constants_1 = require("./constants");
44
44
  var utils_1 = require("./utils");
45
+ var table_doc_1 = require("./table-doc");
45
46
  var Table = /** @class */ (function () {
46
47
  function Table(name, db) {
47
48
  this.name = '';
48
- this.table = {};
49
+ this.currentDoc = {};
49
50
  this.name = name;
50
51
  this.db = db;
51
52
  }
52
53
  /**
53
- * Persist table data changes
54
+ * Persist table's document data changes
54
55
  */
55
56
  Table.prototype.persist = function () {
56
57
  return __awaiter(this, void 0, void 0, function () {
@@ -60,7 +61,7 @@ var Table = /** @class */ (function () {
60
61
  case 0:
61
62
  url = "".concat(this.db.server).concat((0, constants_1.PERSIST_NEW_DATA)(this.name));
62
63
  body = {
63
- data: this.table
64
+ data: this.currentDoc
64
65
  };
65
66
  _a.label = 1;
66
67
  case 1:
@@ -71,7 +72,7 @@ var Table = /** @class */ (function () {
71
72
  if (!response.data.success) {
72
73
  throw new Error(response.data.message);
73
74
  }
74
- return [3 /*break*/, 4];
75
+ return [2 /*return*/, response.data.data];
75
76
  case 3:
76
77
  e_1 = _a.sent();
77
78
  throw new Error("Something went wrong with persist operation: ".concat(e_1.message || String(e_1)));
@@ -80,38 +81,6 @@ var Table = /** @class */ (function () {
80
81
  });
81
82
  });
82
83
  };
83
- /**
84
- * Update data of the last table's item (or create a new item if there is none).
85
- * This ensures that no new item is created.
86
- */
87
- Table.prototype.update = function () {
88
- return __awaiter(this, void 0, void 0, function () {
89
- var url, body, response, e_2;
90
- return __generator(this, function (_a) {
91
- switch (_a.label) {
92
- case 0:
93
- url = "".concat(this.db.server).concat((0, constants_1.UPDATE_LAST_ITEM)(this.name));
94
- body = {
95
- data: this.table
96
- };
97
- _a.label = 1;
98
- case 1:
99
- _a.trys.push([1, 3, , 4]);
100
- return [4 /*yield*/, (0, utils_1.post)(url, body, this.db.auth)];
101
- case 2:
102
- response = _a.sent();
103
- if (!response.data.success) {
104
- throw new Error(response.data.message);
105
- }
106
- return [3 /*break*/, 4];
107
- case 3:
108
- e_2 = _a.sent();
109
- throw new Error("Something went wrong with update operation: ".concat(e_2.message || String(e_2)));
110
- case 4: return [2 /*return*/];
111
- }
112
- });
113
- });
114
- };
115
84
  /**
116
85
  * Get the history of changes. A list of transactions from the most recent to the most old
117
86
  * in a range of depth
@@ -119,7 +88,7 @@ var Table = /** @class */ (function () {
119
88
  */
120
89
  Table.prototype.getHistory = function (limit) {
121
90
  return __awaiter(this, void 0, void 0, function () {
122
- var url, response, e_3;
91
+ var url, response, e_2;
123
92
  return __generator(this, function (_a) {
124
93
  switch (_a.label) {
125
94
  case 0:
@@ -136,8 +105,8 @@ var Table = /** @class */ (function () {
136
105
  // Return data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
137
106
  return [2 /*return*/, response.data.data];
138
107
  case 3:
139
- e_3 = _a.sent();
140
- throw new Error("Something went wrong with getHistory operation: ".concat(e_3.message || String(e_3)));
108
+ e_2 = _a.sent();
109
+ throw new Error("Something went wrong with getHistory operation: ".concat(e_2.message || String(e_2)));
141
110
  case 4: return [2 /*return*/];
142
111
  }
143
112
  });
@@ -148,7 +117,7 @@ var Table = /** @class */ (function () {
148
117
  */
149
118
  Table.prototype.refetch = function () {
150
119
  return __awaiter(this, void 0, void 0, function () {
151
- var url, response, e_4;
120
+ var url, response, e_3;
152
121
  return __generator(this, function (_a) {
153
122
  switch (_a.label) {
154
123
  case 0:
@@ -159,11 +128,11 @@ var Table = /** @class */ (function () {
159
128
  return [4 /*yield*/, axios_1["default"].get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
160
129
  case 2:
161
130
  response = _a.sent();
162
- this.table = response.data.data ? response.data.data : {};
131
+ this.currentDoc = response.data.data ? response.data.data : {};
163
132
  return [3 /*break*/, 4];
164
133
  case 3:
165
- e_4 = _a.sent();
166
- throw new Error("Something went wrong with refetch operation: ".concat(e_4.message || String(e_4)));
134
+ e_3 = _a.sent();
135
+ throw new Error("Something went wrong with refetch operation: ".concat(e_3.message || String(e_3)));
167
136
  case 4: return [2 /*return*/];
168
137
  }
169
138
  });
@@ -173,7 +142,7 @@ var Table = /** @class */ (function () {
173
142
  * Check if the table is empty
174
143
  */
175
144
  Table.prototype.isEmpty = function () {
176
- return Object.keys(this.table).length === 0;
145
+ return Object.keys(this.currentDoc).length === 0;
177
146
  };
178
147
  /**
179
148
  * Get the table's name
@@ -205,7 +174,7 @@ var Table = /** @class */ (function () {
205
174
  if (limit === void 0) { limit = 1000; }
206
175
  if (reverse === void 0) { reverse = true; }
207
176
  return __awaiter(this, void 0, void 0, function () {
208
- var url, body, response, e_5;
177
+ var url, body, response, e_4;
209
178
  return __generator(this, function (_a) {
210
179
  switch (_a.label) {
211
180
  case 0:
@@ -227,14 +196,13 @@ var Table = /** @class */ (function () {
227
196
  // Return found data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
228
197
  return [2 /*return*/, response.data.data];
229
198
  case 3:
230
- e_5 = _a.sent();
231
- throw new Error("Something went wrong with findWhere operation: ".concat(e_5.message || String(e_5)));
199
+ e_4 = _a.sent();
200
+ throw new Error("Something went wrong with findWhere operation: ".concat(e_4.message || String(e_4)));
232
201
  case 4: return [2 /*return*/];
233
202
  }
234
203
  });
235
204
  });
236
205
  };
237
- // TODO: Implement documentation
238
206
  /**
239
207
  * Find items in the table using advanced criteria with operators
240
208
  * @param criteria Array of criteria to filter items. Each criteria contains:
@@ -267,7 +235,7 @@ var Table = /** @class */ (function () {
267
235
  if (limit === void 0) { limit = 1000; }
268
236
  if (reverse === void 0) { reverse = true; }
269
237
  return __awaiter(this, void 0, void 0, function () {
270
- var url, body, response, e_6;
238
+ var url, body, response, e_5;
271
239
  return __generator(this, function (_a) {
272
240
  switch (_a.label) {
273
241
  case 0:
@@ -288,9 +256,46 @@ var Table = /** @class */ (function () {
288
256
  }
289
257
  // Return found data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
290
258
  return [2 /*return*/, response.data.data];
259
+ case 3:
260
+ e_5 = _a.sent();
261
+ throw new Error("Something went wrong with findWhereAdvanced operation: ".concat(e_5.message || String(e_5)));
262
+ case 4: return [2 /*return*/];
263
+ }
264
+ });
265
+ });
266
+ };
267
+ /**
268
+ * Get the current document ID
269
+ */
270
+ Table.prototype.getCurrentDocId = function () {
271
+ return this.currentDoc.doc_id;
272
+ };
273
+ /**
274
+ * Get a specific document by its ID
275
+ * @param doc_id The document ID to retrieve
276
+ * @returns A TableDoc instance with the specific document data
277
+ */
278
+ Table.prototype.getDoc = function (doc_id) {
279
+ return __awaiter(this, void 0, void 0, function () {
280
+ var url, response, e_6;
281
+ return __generator(this, function (_a) {
282
+ switch (_a.label) {
283
+ case 0:
284
+ url = "".concat(this.db.server).concat((0, constants_1.GET_DOC)(this.name, doc_id));
285
+ _a.label = 1;
286
+ case 1:
287
+ _a.trys.push([1, 3, , 4]);
288
+ return [4 /*yield*/, axios_1["default"].get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
289
+ case 2:
290
+ response = _a.sent();
291
+ if (!response.data.success) {
292
+ throw new Error(response.data.message);
293
+ }
294
+ // Create a TableDoc instance with the document data
295
+ return [2 /*return*/, new table_doc_1.TableDocImpl(this.name, doc_id, response.data.data, this.db)];
291
296
  case 3:
292
297
  e_6 = _a.sent();
293
- throw new Error("Something went wrong with findWhereAdvanced operation: ".concat(e_6.message || String(e_6)));
298
+ throw new Error("Something went wrong with getDoc operation: ".concat(e_6.message || String(e_6)));
294
299
  case 4: return [2 /*return*/];
295
300
  }
296
301
  });
@@ -51,3 +51,37 @@ export type EventData = {
51
51
  timestamp: number;
52
52
  };
53
53
  export type EventCallback = (data: EventData) => void;
54
+ /**
55
+ * Represents a specific document from a table
56
+ * Contains only the necessary methods to work with a specific document
57
+ */
58
+ export interface TableDoc<Model> {
59
+ /**
60
+ * The document data
61
+ */
62
+ doc: DocId<Model>;
63
+ /**
64
+ * Update the document data
65
+ * This will update the specific document without creating a new one
66
+ */
67
+ update(): Promise<void>;
68
+ /**
69
+ * Get the table name this document belongs to
70
+ */
71
+ getTableName(): string;
72
+ /**
73
+ * Refetch the document data from the database
74
+ * Useful when the document might have been updated by another application
75
+ */
76
+ refetch(): Promise<void>;
77
+ /**
78
+ * Check if the document is empty
79
+ */
80
+ isEmpty(): boolean;
81
+ }
82
+ export type DocId<Model> = Model & {
83
+ /**
84
+ * The document ID (unique identifier). Immutable.
85
+ */
86
+ readonly doc_id: string;
87
+ };
@@ -1,2 +1,4 @@
1
1
  export { ChainDB, connect } from './features/chain-db';
2
- export { BasicResponse, Operators, CriteriaAdvanced, Criteria, EventTypes, EventData, EventCallback, } from './features/types';
2
+ export { BasicResponse, Operators, CriteriaAdvanced, Criteria, TableDoc, EventTypes, EventData, EventCallback, } from './features/types';
3
+ export { default as Events } from './features/events';
4
+ export { TableDocImpl } from './features/table-doc';
package/dist/cjs/index.js CHANGED
@@ -11,10 +11,14 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
11
11
  o[k2] = m[k];
12
12
  }));
13
13
  exports.__esModule = true;
14
- exports.EventTypes = exports.Operators = exports.connect = exports.ChainDB = void 0;
14
+ exports.TableDocImpl = exports.Events = exports.EventTypes = exports.Operators = exports.connect = exports.ChainDB = void 0;
15
15
  var chain_db_1 = require("./features/chain-db");
16
16
  __createBinding(exports, chain_db_1, "ChainDB");
17
17
  __createBinding(exports, chain_db_1, "connect");
18
18
  var types_1 = require("./features/types");
19
19
  __createBinding(exports, types_1, "Operators");
20
20
  __createBinding(exports, types_1, "EventTypes");
21
+ var events_1 = require("./features/events");
22
+ __createBinding(exports, events_1, "default", "Events");
23
+ var table_doc_1 = require("./features/table-doc");
24
+ __createBinding(exports, table_doc_1, "TableDocImpl");
@@ -2,9 +2,10 @@ export declare const DEFAULT_API_SERVER = "http://localhost:2818";
2
2
  export declare const API_BASE = "/api/v1";
3
3
  export declare const CONNECT: string;
4
4
  export declare const GET_TABLE: (table: string) => string;
5
- export declare const UPDATE_LAST_ITEM: (table: string) => string;
5
+ export declare const UPDATE_ITEM: (table: string) => string;
6
6
  export declare const PERSIST_NEW_DATA: (table: string) => string;
7
7
  export declare const GET_HISTORY: (table: string, limit?: number) => string;
8
8
  export declare const FIND_WHERE_BASIC: (table: string) => string;
9
9
  export declare const FIND_WHERE_ADVANCED: (table: string) => string;
10
+ export declare const GET_DOC: (table: string, doc_id: string) => string;
10
11
  export declare const WEB_SOCKET_EVENTS: string;