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/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 18.0.0
@@ -1,5 +1,5 @@
1
1
  import Table from './table';
2
- import { Connection, EventCallback } from './types';
2
+ import { Connection, ConnectionWithToken, EventCallback } from './types';
3
3
  export declare class ChainDB {
4
4
  server: string;
5
5
  database: string;
@@ -13,10 +13,15 @@ export declare class ChainDB {
13
13
  * @param password Password to access the data base
14
14
  */
15
15
  connect(connection: Connection): Promise<void>;
16
+ /**
17
+ * Connect to Chain DB with a token (Synchronously). This is useful if you don't want to use a promise to connect to Chain DB.
18
+ * @param connection Connection information with token
19
+ */
20
+ connectWithToken(connection: ConnectionWithToken): void;
16
21
  /**
17
22
  * Initialize a table, fetching its more updated data
18
23
  */
19
- getTable<Model>(table_name: string): Promise<Table<Model>>;
24
+ getTable<Model>(table_name: string): Table<Model>;
20
25
  events(): {
21
26
  /**
22
27
  * Subscribe to an event
@@ -37,3 +42,4 @@ export declare class ChainDB {
37
42
  };
38
43
  }
39
44
  export declare const connect: (connection: Connection) => Promise<ChainDB>;
45
+ export declare const connectWithToken: (connection: ConnectionWithToken) => ChainDB;
@@ -39,7 +39,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
39
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
40
  };
41
41
  exports.__esModule = true;
42
- exports.connect = exports.ChainDB = void 0;
42
+ exports.connectWithToken = exports.connect = exports.ChainDB = void 0;
43
43
  var constants_1 = require("./constants");
44
44
  var utils_1 = require("./utils");
45
45
  var table_1 = __importDefault(require("./table"));
@@ -90,44 +90,43 @@ var ChainDB = /** @class */ (function () {
90
90
  });
91
91
  });
92
92
  };
93
+ /**
94
+ * Connect to Chain DB with a token (Synchronously). This is useful if you don't want to use a promise to connect to Chain DB.
95
+ * @param connection Connection information with token
96
+ */
97
+ ChainDB.prototype.connectWithToken = function (connection) {
98
+ var server = connection.server, database = connection.database, token = connection.token;
99
+ this.server = server || constants_1.DEFAULT_API_SERVER;
100
+ this.database = database;
101
+ this.auth = token;
102
+ };
93
103
  /**
94
104
  * Initialize a table, fetching its more updated data
95
105
  */
96
106
  ChainDB.prototype.getTable = function (table_name) {
97
- return __awaiter(this, void 0, void 0, function () {
98
- var tableData;
99
- return __generator(this, function (_a) {
100
- switch (_a.label) {
101
- case 0:
102
- tableData = new table_1["default"](table_name, this);
103
- return [4 /*yield*/, tableData.refetch()];
104
- case 1:
105
- _a.sent();
106
- return [2 /*return*/, tableData
107
- // const table_data = await table.get<Model>(this, table_name, model)
108
- // NOTE: Although only the "table" and "persist" properties are displayed by
109
- // the lint, all Table properties are being exposed.
110
- // This is due to a javascript limitation on classes.
111
- //
112
- // There was an attempt to return a new object with only the required
113
- // data, but this generates an error in the "this" instance of the Table.
114
- // return table_data as {
115
- // table: Model
116
- // /**
117
- // * Persist table data on chain
118
- // */
119
- // persist: () => Promise<void>
120
- // /**
121
- // * Get the history of changes. A list of transactions from the most recent to the most old
122
- // * in a range of depth
123
- // * @param depth
124
- // */
125
- // getHistory: (depth: number) => Promise<Model[]>
126
- // }
127
- ];
128
- }
129
- });
130
- });
107
+ var tableData = new table_1["default"](table_name, this);
108
+ // await tableData.refetch()
109
+ return tableData;
110
+ // const table_data = await table.get<Model>(this, table_name, model)
111
+ // NOTE: Although only the "table" and "persist" properties are displayed by
112
+ // the lint, all Table properties are being exposed.
113
+ // This is due to a javascript limitation on classes.
114
+ //
115
+ // There was an attempt to return a new object with only the required
116
+ // data, but this generates an error in the "this" instance of the Table.
117
+ // return table_data as {
118
+ // table: Model
119
+ // /**
120
+ // * Persist table data on chain
121
+ // */
122
+ // persist: () => Promise<void>
123
+ // /**
124
+ // * Get the history of changes. A list of transactions from the most recent to the most old
125
+ // * in a range of depth
126
+ // * @param depth
127
+ // */
128
+ // getHistory: (depth: number) => Promise<Model[]>
129
+ // }
131
130
  };
132
131
  ChainDB.prototype.events = function () {
133
132
  var _this = this;
@@ -182,3 +181,9 @@ var connect = function (connection) { return __awaiter(void 0, void 0, void 0, f
182
181
  });
183
182
  }); };
184
183
  exports.connect = connect;
184
+ var connectWithToken = function (connection) {
185
+ var chainDb = new ChainDB();
186
+ chainDb.connectWithToken(connection);
187
+ return chainDb;
188
+ };
189
+ exports.connectWithToken = connectWithToken;
@@ -35,12 +35,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
36
  }
37
37
  };
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
38
  exports.__esModule = true;
42
39
  exports.TableDocImpl = void 0;
43
- var axios_1 = __importDefault(require("axios"));
44
40
  var constants_1 = require("./constants");
45
41
  var utils_1 = require("./utils");
46
42
  /**
@@ -81,7 +77,8 @@ var TableDocImpl = /** @class */ (function () {
81
77
  return [3 /*break*/, 4];
82
78
  case 3:
83
79
  e_1 = _a.sent();
84
- throw new Error("Something went wrong updating document ".concat(this.doc_id, ": ").concat(e_1.message || String(e_1)));
80
+ // throw new Error(`Something went wrong updating document ${this.doc_id}: ${e.message || String(e)}`)
81
+ throw new Error(e_1);
85
82
  case 4: return [2 /*return*/];
86
83
  }
87
84
  });
@@ -101,7 +98,7 @@ var TableDocImpl = /** @class */ (function () {
101
98
  _a.label = 1;
102
99
  case 1:
103
100
  _a.trys.push([1, 3, , 4]);
104
- return [4 /*yield*/, axios_1["default"].get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
101
+ return [4 /*yield*/, (0, utils_1.get)(url, this.db.auth)];
105
102
  case 2:
106
103
  response = _a.sent();
107
104
  if (!response.data.success) {
@@ -112,7 +109,8 @@ var TableDocImpl = /** @class */ (function () {
112
109
  return [3 /*break*/, 4];
113
110
  case 3:
114
111
  e_2 = _a.sent();
115
- throw new Error("Something went wrong refetching document ".concat(this.doc_id, ": ").concat(e_2.message || String(e_2)));
112
+ // throw new Error(`Something went wrong refetching document ${this.doc_id}: ${e.message || String(e)}`)
113
+ throw new Error(e_2);
116
114
  case 4: return [2 /*return*/];
117
115
  }
118
116
  });
@@ -1,31 +1,27 @@
1
1
  import { ChainDB } from './chain-db';
2
2
  import { Criteria, CriteriaAdvanced, DocId, TableDoc } from './types';
3
+ import { TableDocImpl } from './table-doc';
3
4
  declare class Table<Model> {
4
5
  /**
5
6
  * The current document data
6
7
  */
7
- currentDoc: Model;
8
8
  private name;
9
9
  private db;
10
10
  constructor(name: string, db: ChainDB);
11
11
  /**
12
- * Persist table's document data changes
12
+ * Create a new document in the table
13
13
  */
14
- persist(): Promise<DocId<Model>>;
14
+ new(data: Model): Promise<TableDoc<Model>>;
15
15
  /**
16
16
  * Get the history of changes. A list of transactions from the most recent to the most old
17
17
  * in a range of depth
18
- * @param limit
18
+ * @param limit Maximum number of items to return (default: 100)
19
19
  */
20
- getHistory(limit: number): Promise<DocId<Model>[]>;
20
+ getHistory(limit?: number): Promise<TableDocImpl<DocId<Model>>[]>;
21
21
  /**
22
- * Refetch the table data
22
+ * Get the last record stored in the table
23
23
  */
24
- refetch(): Promise<void>;
25
- /**
26
- * Check if the table is empty
27
- */
28
- isEmpty(): boolean;
24
+ last(): Promise<TableDocImpl<DocId<Model>>>;
29
25
  /**
30
26
  * Get the table's name
31
27
  */
@@ -34,7 +30,7 @@ declare class Table<Model> {
34
30
  * Find items in the table using basic criteria with exact matches
35
31
  * @param criteria Object with fields and values to match exactly, e.g.: {age: 44, name: "john"}
36
32
  * @param limit Maximum number of items to return (default: 1000)
37
- * @param reverse If true, returns items in reverse order (default: true)
33
+ * @param reverse If true, returns items in reverse order, newer items first (default: true)
38
34
  * @returns Array of found items matching the criteria
39
35
  * @example
40
36
  * // Find items where age is 44
@@ -50,9 +46,16 @@ declare class Table<Model> {
50
46
  * score: 100
51
47
  * })
52
48
  */
53
- findWhere(criteria: Criteria<Model>, limit?: number, reverse?: boolean): Promise<DocId<Model>[]>;
49
+ findWhere(criteria: Criteria<Model>, limit?: number, reverse?: boolean): Promise<TableDocImpl<DocId<Model>>[]>;
54
50
  /**
55
- * Find items in the table using advanced criteria with operators
51
+ * Find the first item in the table using basic criteria with exact matches
52
+ * @param criteria Object with fields and values to match exactly, e.g.: {age: 44, name: "john"}
53
+ * @param reverse If true, returns items in reverse order, newer items first (default: true)
54
+ * @returns The first found item matching the criteria or null if no item is found
55
+ */
56
+ find(criteria: Criteria<Model>, reverse?: boolean): Promise<TableDocImpl<DocId<Model>>>;
57
+ /**
58
+ * Find the first item in the table using advanced criteria with operators
56
59
  * @param criteria Array of criteria to filter items. Each criteria contains:
57
60
  * - field: The field name to filter
58
61
  * - operator: The operator to use in comparison. Available operators:
@@ -67,7 +70,7 @@ declare class Table<Model> {
67
70
  * - EndsWith: Check if field ends with value (for strings)
68
71
  * - value: The value to compare against
69
72
  * @param limit Maximum number of items to return (default: 1000)
70
- * @param reverse If true, returns items in reverse order (default: true)
73
+ * @param reverse If true, returns items in reverse order, newer items first (default: true)
71
74
  * @returns Array of found items matching the criteria
72
75
  * @example
73
76
  * // Find items where greeting contains "arg"
@@ -79,16 +82,40 @@ declare class Table<Model> {
79
82
  * }
80
83
  * ])
81
84
  */
82
- findWhereAdvanced(criteria: CriteriaAdvanced<Model>[], limit?: number, reverse?: boolean): Promise<DocId<Model>[]>;
85
+ findWhereAdvanced(criteria: CriteriaAdvanced<Model>[], limit?: number, reverse?: boolean): Promise<TableDocImpl<DocId<Model>>[]>;
83
86
  /**
84
- * Get the current document ID
87
+ * Find the first single item in the table using advanced criteria with operators
88
+ * @param criteria Array of criteria to filter items. Each criteria contains:
89
+ * - field: The field name to filter
90
+ * - operator: The operator to use in comparison. Available operators:
91
+ * - Eq (==) Equal
92
+ * - Ne (!=) Not Equal
93
+ * - Gt (>) Greater Than
94
+ * - Ge (>=) Greater Than or Equal
95
+ * - Lt (<) Less Than
96
+ * - Le (<=) Less Than or Equal
97
+ * - Contains: Check if field contains value (for strings and arrays)
98
+ * - StartsWith: Check if field starts with value (for strings)
99
+ * - EndsWith: Check if field ends with value (for strings)
100
+ * - value: The value to compare against
101
+ * @param reverse If true, returns items in reverse order, newer items first (default: true)
102
+ * @returns The first found item matching the criteria or null if no item is found
103
+ * @example
104
+ * // Find items where greeting contains "arg"
105
+ * table.findAdvanced([
106
+ * {
107
+ * field: "greeting",
108
+ * operator: Operators.Contains,
109
+ * value: "hello"
110
+ * }
111
+ * ])
85
112
  */
86
- getCurrentDocId(): string;
113
+ findAdvanced(criteria: CriteriaAdvanced<Model>[], reverse?: boolean): Promise<TableDocImpl<DocId<Model>>>;
87
114
  /**
88
115
  * Get a specific document by its ID
89
116
  * @param doc_id The document ID to retrieve
90
117
  * @returns A TableDoc instance with the specific document data
91
118
  */
92
- getDoc(doc_id: string): Promise<TableDoc<Model>>;
119
+ getByDocId(doc_id: string): Promise<TableDoc<Model>>;
93
120
  }
94
121
  export default Table;
@@ -35,47 +35,50 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
36
  }
37
37
  };
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
38
  exports.__esModule = true;
42
- var axios_1 = __importDefault(require("axios"));
43
39
  var constants_1 = require("./constants");
44
40
  var utils_1 = require("./utils");
45
41
  var table_doc_1 = require("./table-doc");
46
42
  var Table = /** @class */ (function () {
47
43
  function Table(name, db) {
44
+ /**
45
+ * The current document data
46
+ */
47
+ // private currentDoc: Model
48
48
  this.name = '';
49
- this.currentDoc = {};
49
+ // this.currentDoc = {} as Model
50
50
  this.name = name;
51
51
  this.db = db;
52
52
  }
53
53
  /**
54
- * Persist table's document data changes
54
+ * Create a new document in the table
55
55
  */
56
- Table.prototype.persist = function () {
56
+ Table.prototype["new"] = function (data) {
57
+ var _a;
57
58
  return __awaiter(this, void 0, void 0, function () {
58
59
  var url, body, response, e_1;
59
- return __generator(this, function (_a) {
60
- switch (_a.label) {
60
+ return __generator(this, function (_b) {
61
+ switch (_b.label) {
61
62
  case 0:
62
63
  url = "".concat(this.db.server).concat((0, constants_1.PERSIST_NEW_DATA)(this.name));
63
64
  body = {
64
- data: this.currentDoc
65
+ data: data
65
66
  };
66
- _a.label = 1;
67
+ _b.label = 1;
67
68
  case 1:
68
- _a.trys.push([1, 3, , 4]);
69
+ _b.trys.push([1, 3, , 4]);
69
70
  return [4 /*yield*/, (0, utils_1.post)(url, body, this.db.auth)];
70
71
  case 2:
71
- response = _a.sent();
72
+ response = _b.sent();
72
73
  if (!response.data.success) {
73
74
  throw new Error(response.data.message);
74
75
  }
75
- return [2 /*return*/, response.data.data];
76
+ // Create a TableDoc instance with the document data
77
+ return [2 /*return*/, new table_doc_1.TableDocImpl(this.name, (_a = response.data.data) === null || _a === void 0 ? void 0 : _a.doc_id, response.data.data, this.db)];
76
78
  case 3:
77
- e_1 = _a.sent();
78
- throw new Error("Something went wrong with persist operation: ".concat(e_1.message || String(e_1)));
79
+ e_1 = _b.sent();
80
+ // throw new Error(`Something went wrong with persist operation: ${e.message || String(e)}`)
81
+ throw new Error(e_1);
79
82
  case 4: return [2 /*return*/];
80
83
  }
81
84
  });
@@ -84,11 +87,13 @@ var Table = /** @class */ (function () {
84
87
  /**
85
88
  * Get the history of changes. A list of transactions from the most recent to the most old
86
89
  * in a range of depth
87
- * @param limit
90
+ * @param limit Maximum number of items to return (default: 100)
88
91
  */
89
92
  Table.prototype.getHistory = function (limit) {
93
+ if (limit === void 0) { limit = 100; }
90
94
  return __awaiter(this, void 0, void 0, function () {
91
- var url, response, e_2;
95
+ var url, response, history, e_2;
96
+ var _this = this;
92
97
  return __generator(this, function (_a) {
93
98
  switch (_a.label) {
94
99
  case 0:
@@ -96,54 +101,47 @@ var Table = /** @class */ (function () {
96
101
  _a.label = 1;
97
102
  case 1:
98
103
  _a.trys.push([1, 3, , 4]);
99
- return [4 /*yield*/, axios_1["default"].get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
104
+ return [4 /*yield*/, (0, utils_1.get)(url, this.db.auth)];
100
105
  case 2:
101
106
  response = _a.sent();
102
107
  if (!response.data.success) {
103
108
  throw new Error(response.data.message);
104
109
  }
105
- // Return data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
106
- return [2 /*return*/, response.data.data];
110
+ history = response.data.data;
111
+ return [2 /*return*/, history.map(function (item) { return new table_doc_1.TableDocImpl(_this.name, item.doc_id, item, _this.db); })];
107
112
  case 3:
108
113
  e_2 = _a.sent();
109
- throw new Error("Something went wrong with getHistory operation: ".concat(e_2.message || String(e_2)));
114
+ // throw new Error(`Something went wrong with getHistory operation: ${e.message || String(e)}`)
115
+ throw new Error(e_2);
110
116
  case 4: return [2 /*return*/];
111
117
  }
112
118
  });
113
119
  });
114
120
  };
115
121
  /**
116
- * Refetch the table data
122
+ * Get the last record stored in the table
117
123
  */
118
- Table.prototype.refetch = function () {
124
+ Table.prototype.last = function () {
125
+ var _a;
119
126
  return __awaiter(this, void 0, void 0, function () {
120
- var url, response, e_3;
121
- return __generator(this, function (_a) {
122
- switch (_a.label) {
127
+ var url, response, data;
128
+ return __generator(this, function (_b) {
129
+ switch (_b.label) {
123
130
  case 0:
124
131
  url = "".concat(this.db.server).concat((0, constants_1.GET_TABLE)(this.name));
125
- _a.label = 1;
132
+ return [4 /*yield*/, (0, utils_1.get)(url, this.db.auth)
133
+ // this.currentDoc = response.data.data ? (response.data.data as DocId<Model>) : ({} as DocId<Model>)
134
+ // return this.currentDoc
135
+ // Create a TableDoc instance with the document data
136
+ ];
126
137
  case 1:
127
- _a.trys.push([1, 3, , 4]);
128
- return [4 /*yield*/, axios_1["default"].get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
129
- case 2:
130
- response = _a.sent();
131
- this.currentDoc = response.data.data ? response.data.data : {};
132
- return [3 /*break*/, 4];
133
- case 3:
134
- e_3 = _a.sent();
135
- throw new Error("Something went wrong with refetch operation: ".concat(e_3.message || String(e_3)));
136
- case 4: return [2 /*return*/];
138
+ response = _b.sent();
139
+ data = response.data.data ? response.data.data : {};
140
+ return [2 /*return*/, new table_doc_1.TableDocImpl(this.name, (_a = response.data.data) === null || _a === void 0 ? void 0 : _a.doc_id, data, this.db)];
137
141
  }
138
142
  });
139
143
  });
140
144
  };
141
- /**
142
- * Check if the table is empty
143
- */
144
- Table.prototype.isEmpty = function () {
145
- return Object.keys(this.currentDoc).length === 0;
146
- };
147
145
  /**
148
146
  * Get the table's name
149
147
  */
@@ -154,7 +152,7 @@ var Table = /** @class */ (function () {
154
152
  * Find items in the table using basic criteria with exact matches
155
153
  * @param criteria Object with fields and values to match exactly, e.g.: {age: 44, name: "john"}
156
154
  * @param limit Maximum number of items to return (default: 1000)
157
- * @param reverse If true, returns items in reverse order (default: true)
155
+ * @param reverse If true, returns items in reverse order, newer items first (default: true)
158
156
  * @returns Array of found items matching the criteria
159
157
  * @example
160
158
  * // Find items where age is 44
@@ -174,7 +172,8 @@ var Table = /** @class */ (function () {
174
172
  if (limit === void 0) { limit = 1000; }
175
173
  if (reverse === void 0) { reverse = true; }
176
174
  return __awaiter(this, void 0, void 0, function () {
177
- var url, body, response, e_4;
175
+ var url, body, response, foundData, e_3;
176
+ var _this = this;
178
177
  return __generator(this, function (_a) {
179
178
  switch (_a.label) {
180
179
  case 0:
@@ -193,18 +192,43 @@ var Table = /** @class */ (function () {
193
192
  if (!response.data.success) {
194
193
  throw new Error(response.data.message);
195
194
  }
196
- // Return found data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
197
- return [2 /*return*/, response.data.data];
195
+ foundData = response.data.data;
196
+ return [2 /*return*/, foundData.map(function (item) { return new table_doc_1.TableDocImpl(_this.name, item.doc_id, item, _this.db); })];
198
197
  case 3:
199
- e_4 = _a.sent();
200
- throw new Error("Something went wrong with findWhere operation: ".concat(e_4.message || String(e_4)));
198
+ e_3 = _a.sent();
199
+ // throw new Error(`Something went wrong with findWhere operation: ${e.message || String(e)}`)
200
+ throw new Error(e_3);
201
201
  case 4: return [2 /*return*/];
202
202
  }
203
203
  });
204
204
  });
205
205
  };
206
206
  /**
207
- * Find items in the table using advanced criteria with operators
207
+ * Find the first item in the table using basic criteria with exact matches
208
+ * @param criteria Object with fields and values to match exactly, e.g.: {age: 44, name: "john"}
209
+ * @param reverse If true, returns items in reverse order, newer items first (default: true)
210
+ * @returns The first found item matching the criteria or null if no item is found
211
+ */
212
+ Table.prototype.find = function (criteria, reverse) {
213
+ if (reverse === void 0) { reverse = true; }
214
+ return __awaiter(this, void 0, void 0, function () {
215
+ var found, data;
216
+ return __generator(this, function (_a) {
217
+ switch (_a.label) {
218
+ case 0: return [4 /*yield*/, this.findWhere(criteria, 1, reverse)];
219
+ case 1:
220
+ found = _a.sent();
221
+ if (found.length === 0) {
222
+ data = {};
223
+ return [2 /*return*/, new table_doc_1.TableDocImpl(this.name, '', data, this.db)];
224
+ }
225
+ return [2 /*return*/, found[0]];
226
+ }
227
+ });
228
+ });
229
+ };
230
+ /**
231
+ * Find the first item in the table using advanced criteria with operators
208
232
  * @param criteria Array of criteria to filter items. Each criteria contains:
209
233
  * - field: The field name to filter
210
234
  * - operator: The operator to use in comparison. Available operators:
@@ -219,7 +243,7 @@ var Table = /** @class */ (function () {
219
243
  * - EndsWith: Check if field ends with value (for strings)
220
244
  * - value: The value to compare against
221
245
  * @param limit Maximum number of items to return (default: 1000)
222
- * @param reverse If true, returns items in reverse order (default: true)
246
+ * @param reverse If true, returns items in reverse order, newer items first (default: true)
223
247
  * @returns Array of found items matching the criteria
224
248
  * @example
225
249
  * // Find items where greeting contains "arg"
@@ -235,7 +259,8 @@ var Table = /** @class */ (function () {
235
259
  if (limit === void 0) { limit = 1000; }
236
260
  if (reverse === void 0) { reverse = true; }
237
261
  return __awaiter(this, void 0, void 0, function () {
238
- var url, body, response, e_5;
262
+ var url, body, response, foundData, e_4;
263
+ var _this = this;
239
264
  return __generator(this, function (_a) {
240
265
  switch (_a.label) {
241
266
  case 0:
@@ -254,32 +279,70 @@ var Table = /** @class */ (function () {
254
279
  if (!response.data.success) {
255
280
  throw new Error(response.data.message);
256
281
  }
257
- // Return found data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
258
- return [2 /*return*/, response.data.data];
282
+ foundData = response.data.data;
283
+ return [2 /*return*/, foundData.map(function (item) { return new table_doc_1.TableDocImpl(_this.name, item.doc_id, item, _this.db); })];
259
284
  case 3:
260
- e_5 = _a.sent();
261
- throw new Error("Something went wrong with findWhereAdvanced operation: ".concat(e_5.message || String(e_5)));
285
+ e_4 = _a.sent();
286
+ // throw new Error(`Something went wrong with findWhereAdvanced operation: ${e.message || String(e)}`)
287
+ throw new Error(e_4);
262
288
  case 4: return [2 /*return*/];
263
289
  }
264
290
  });
265
291
  });
266
292
  };
267
293
  /**
268
- * Get the current document ID
294
+ * Find the first single item in the table using advanced criteria with operators
295
+ * @param criteria Array of criteria to filter items. Each criteria contains:
296
+ * - field: The field name to filter
297
+ * - operator: The operator to use in comparison. Available operators:
298
+ * - Eq (==) Equal
299
+ * - Ne (!=) Not Equal
300
+ * - Gt (>) Greater Than
301
+ * - Ge (>=) Greater Than or Equal
302
+ * - Lt (<) Less Than
303
+ * - Le (<=) Less Than or Equal
304
+ * - Contains: Check if field contains value (for strings and arrays)
305
+ * - StartsWith: Check if field starts with value (for strings)
306
+ * - EndsWith: Check if field ends with value (for strings)
307
+ * - value: The value to compare against
308
+ * @param reverse If true, returns items in reverse order, newer items first (default: true)
309
+ * @returns The first found item matching the criteria or null if no item is found
310
+ * @example
311
+ * // Find items where greeting contains "arg"
312
+ * table.findAdvanced([
313
+ * {
314
+ * field: "greeting",
315
+ * operator: Operators.Contains,
316
+ * value: "hello"
317
+ * }
318
+ * ])
269
319
  */
270
- Table.prototype.getCurrentDocId = function () {
271
- var _doc = this.currentDoc;
272
- // Support to applications using older versions
273
- return _doc.doc_id ? _doc.doc_id : '';
320
+ Table.prototype.findAdvanced = function (criteria, reverse) {
321
+ if (reverse === void 0) { reverse = true; }
322
+ return __awaiter(this, void 0, void 0, function () {
323
+ var found, data;
324
+ return __generator(this, function (_a) {
325
+ switch (_a.label) {
326
+ case 0: return [4 /*yield*/, this.findWhereAdvanced(criteria, 1, reverse)];
327
+ case 1:
328
+ found = _a.sent();
329
+ if (found.length === 0) {
330
+ data = {};
331
+ return [2 /*return*/, new table_doc_1.TableDocImpl(this.name, '', data, this.db)];
332
+ }
333
+ return [2 /*return*/, found[0]];
334
+ }
335
+ });
336
+ });
274
337
  };
275
338
  /**
276
339
  * Get a specific document by its ID
277
340
  * @param doc_id The document ID to retrieve
278
341
  * @returns A TableDoc instance with the specific document data
279
342
  */
280
- Table.prototype.getDoc = function (doc_id) {
343
+ Table.prototype.getByDocId = function (doc_id) {
281
344
  return __awaiter(this, void 0, void 0, function () {
282
- var url, response, e_6;
345
+ var url, response, e_5;
283
346
  return __generator(this, function (_a) {
284
347
  switch (_a.label) {
285
348
  case 0:
@@ -287,7 +350,7 @@ var Table = /** @class */ (function () {
287
350
  _a.label = 1;
288
351
  case 1:
289
352
  _a.trys.push([1, 3, , 4]);
290
- return [4 /*yield*/, axios_1["default"].get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
353
+ return [4 /*yield*/, (0, utils_1.get)(url, this.db.auth)];
291
354
  case 2:
292
355
  response = _a.sent();
293
356
  if (!response.data.success) {
@@ -296,8 +359,9 @@ var Table = /** @class */ (function () {
296
359
  // Create a TableDoc instance with the document data
297
360
  return [2 /*return*/, new table_doc_1.TableDocImpl(this.name, doc_id, response.data.data, this.db)];
298
361
  case 3:
299
- e_6 = _a.sent();
300
- throw new Error("Something went wrong with getDoc operation: ".concat(e_6.message || String(e_6)));
362
+ e_5 = _a.sent();
363
+ // throw new Error(`Something went wrong with getDoc operation: ${e.message || String(e)}`)
364
+ throw new Error(e_5);
301
365
  case 4: return [2 /*return*/];
302
366
  }
303
367
  });
@@ -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>;