chain-db-ts 1.0.0-rc.2 → 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 +1 -0
- package/CHANGELOG.md +6 -0
- package/dist/cjs/features/chain-db.d.ts +8 -2
- package/dist/cjs/features/chain-db.js +40 -35
- package/dist/cjs/features/table-doc.js +5 -7
- package/dist/cjs/features/table.d.ts +49 -19
- package/dist/cjs/features/table.js +131 -65
- package/dist/cjs/features/types.d.ts +5 -0
- package/dist/cjs/features/utils.d.ts +2 -1
- package/dist/cjs/features/utils.js +9 -1
- package/dist/cjs/index.d.ts +2 -1
- package/dist/cjs/index.js +4 -1
- package/features/chain-db.d.ts +8 -2
- package/features/chain-db.js +38 -34
- package/features/table-doc.js +6 -5
- package/features/table.d.ts +49 -19
- package/features/table.js +132 -63
- package/features/types.d.ts +5 -0
- package/features/utils.d.ts +2 -1
- package/features/utils.js +7 -0
- package/index.d.ts +2 -1
- package/index.js +2 -1
- package/package.json +1 -1
- package/readme.md +66 -162
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
18.0.0
|
package/CHANGELOG.md
CHANGED
|
@@ -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):
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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(
|
|
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*/,
|
|
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(
|
|
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,28 +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
|
+
/**
|
|
6
|
+
* The current document data
|
|
7
|
+
*/
|
|
5
8
|
private name;
|
|
6
9
|
private db;
|
|
7
10
|
constructor(name: string, db: ChainDB);
|
|
8
11
|
/**
|
|
9
|
-
*
|
|
12
|
+
* Create a new document in the table
|
|
10
13
|
*/
|
|
11
|
-
|
|
14
|
+
new(data: Model): Promise<TableDoc<Model>>;
|
|
12
15
|
/**
|
|
13
16
|
* Get the history of changes. A list of transactions from the most recent to the most old
|
|
14
17
|
* in a range of depth
|
|
15
|
-
* @param limit
|
|
16
|
-
*/
|
|
17
|
-
getHistory(limit: number): Promise<DocId<Model>[]>;
|
|
18
|
-
/**
|
|
19
|
-
* Refetch the table data
|
|
18
|
+
* @param limit Maximum number of items to return (default: 100)
|
|
20
19
|
*/
|
|
21
|
-
|
|
20
|
+
getHistory(limit?: number): Promise<TableDocImpl<DocId<Model>>[]>;
|
|
22
21
|
/**
|
|
23
|
-
*
|
|
22
|
+
* Get the last record stored in the table
|
|
24
23
|
*/
|
|
25
|
-
|
|
24
|
+
last(): Promise<TableDocImpl<DocId<Model>>>;
|
|
26
25
|
/**
|
|
27
26
|
* Get the table's name
|
|
28
27
|
*/
|
|
@@ -31,7 +30,7 @@ declare class Table<Model> {
|
|
|
31
30
|
* Find items in the table using basic criteria with exact matches
|
|
32
31
|
* @param criteria Object with fields and values to match exactly, e.g.: {age: 44, name: "john"}
|
|
33
32
|
* @param limit Maximum number of items to return (default: 1000)
|
|
34
|
-
* @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)
|
|
35
34
|
* @returns Array of found items matching the criteria
|
|
36
35
|
* @example
|
|
37
36
|
* // Find items where age is 44
|
|
@@ -47,9 +46,16 @@ declare class Table<Model> {
|
|
|
47
46
|
* score: 100
|
|
48
47
|
* })
|
|
49
48
|
*/
|
|
50
|
-
findWhere(criteria: Criteria<Model>, limit?: number, reverse?: boolean): Promise<DocId<Model
|
|
49
|
+
findWhere(criteria: Criteria<Model>, limit?: number, reverse?: boolean): Promise<TableDocImpl<DocId<Model>>[]>;
|
|
50
|
+
/**
|
|
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>>>;
|
|
51
57
|
/**
|
|
52
|
-
* Find
|
|
58
|
+
* Find the first item in the table using advanced criteria with operators
|
|
53
59
|
* @param criteria Array of criteria to filter items. Each criteria contains:
|
|
54
60
|
* - field: The field name to filter
|
|
55
61
|
* - operator: The operator to use in comparison. Available operators:
|
|
@@ -64,7 +70,7 @@ declare class Table<Model> {
|
|
|
64
70
|
* - EndsWith: Check if field ends with value (for strings)
|
|
65
71
|
* - value: The value to compare against
|
|
66
72
|
* @param limit Maximum number of items to return (default: 1000)
|
|
67
|
-
* @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)
|
|
68
74
|
* @returns Array of found items matching the criteria
|
|
69
75
|
* @example
|
|
70
76
|
* // Find items where greeting contains "arg"
|
|
@@ -76,16 +82,40 @@ declare class Table<Model> {
|
|
|
76
82
|
* }
|
|
77
83
|
* ])
|
|
78
84
|
*/
|
|
79
|
-
findWhereAdvanced(criteria: CriteriaAdvanced<Model>[], limit?: number, reverse?: boolean): Promise<DocId<Model
|
|
85
|
+
findWhereAdvanced(criteria: CriteriaAdvanced<Model>[], limit?: number, reverse?: boolean): Promise<TableDocImpl<DocId<Model>>[]>;
|
|
80
86
|
/**
|
|
81
|
-
*
|
|
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
|
+
* ])
|
|
82
112
|
*/
|
|
83
|
-
|
|
113
|
+
findAdvanced(criteria: CriteriaAdvanced<Model>[], reverse?: boolean): Promise<TableDocImpl<DocId<Model>>>;
|
|
84
114
|
/**
|
|
85
115
|
* Get a specific document by its ID
|
|
86
116
|
* @param doc_id The document ID to retrieve
|
|
87
117
|
* @returns A TableDoc instance with the specific document data
|
|
88
118
|
*/
|
|
89
|
-
|
|
119
|
+
getByDocId(doc_id: string): Promise<TableDoc<Model>>;
|
|
90
120
|
}
|
|
91
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
|
-
*
|
|
54
|
+
* Create a new document in the table
|
|
55
55
|
*/
|
|
56
|
-
Table.prototype
|
|
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 (
|
|
60
|
-
switch (
|
|
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:
|
|
65
|
+
data: data
|
|
65
66
|
};
|
|
66
|
-
|
|
67
|
+
_b.label = 1;
|
|
67
68
|
case 1:
|
|
68
|
-
|
|
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 =
|
|
72
|
+
response = _b.sent();
|
|
72
73
|
if (!response.data.success) {
|
|
73
74
|
throw new Error(response.data.message);
|
|
74
75
|
}
|
|
75
|
-
|
|
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 =
|
|
78
|
-
throw new Error(
|
|
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*/,
|
|
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
|
-
|
|
106
|
-
return [2 /*return*/,
|
|
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(
|
|
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
|
-
*
|
|
122
|
+
* Get the last record stored in the table
|
|
117
123
|
*/
|
|
118
|
-
Table.prototype.
|
|
124
|
+
Table.prototype.last = function () {
|
|
125
|
+
var _a;
|
|
119
126
|
return __awaiter(this, void 0, void 0, function () {
|
|
120
|
-
var url, response,
|
|
121
|
-
return __generator(this, function (
|
|
122
|
-
switch (
|
|
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
|
-
|
|
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
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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,
|
|
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
|
-
|
|
197
|
-
return [2 /*return*/,
|
|
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
|
-
|
|
200
|
-
throw new Error(
|
|
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
|
|
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,
|
|
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,30 +279,70 @@ var Table = /** @class */ (function () {
|
|
|
254
279
|
if (!response.data.success) {
|
|
255
280
|
throw new Error(response.data.message);
|
|
256
281
|
}
|
|
257
|
-
|
|
258
|
-
return [2 /*return*/,
|
|
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
|
-
|
|
261
|
-
throw new Error(
|
|
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
|
-
*
|
|
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.
|
|
271
|
-
|
|
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
|
+
});
|
|
272
337
|
};
|
|
273
338
|
/**
|
|
274
339
|
* Get a specific document by its ID
|
|
275
340
|
* @param doc_id The document ID to retrieve
|
|
276
341
|
* @returns A TableDoc instance with the specific document data
|
|
277
342
|
*/
|
|
278
|
-
Table.prototype.
|
|
343
|
+
Table.prototype.getByDocId = function (doc_id) {
|
|
279
344
|
return __awaiter(this, void 0, void 0, function () {
|
|
280
|
-
var url, response,
|
|
345
|
+
var url, response, e_5;
|
|
281
346
|
return __generator(this, function (_a) {
|
|
282
347
|
switch (_a.label) {
|
|
283
348
|
case 0:
|
|
@@ -285,7 +350,7 @@ var Table = /** @class */ (function () {
|
|
|
285
350
|
_a.label = 1;
|
|
286
351
|
case 1:
|
|
287
352
|
_a.trys.push([1, 3, , 4]);
|
|
288
|
-
return [4 /*yield*/,
|
|
353
|
+
return [4 /*yield*/, (0, utils_1.get)(url, this.db.auth)];
|
|
289
354
|
case 2:
|
|
290
355
|
response = _a.sent();
|
|
291
356
|
if (!response.data.success) {
|
|
@@ -294,8 +359,9 @@ var Table = /** @class */ (function () {
|
|
|
294
359
|
// Create a TableDoc instance with the document data
|
|
295
360
|
return [2 /*return*/, new table_doc_1.TableDocImpl(this.name, doc_id, response.data.data, this.db)];
|
|
296
361
|
case 3:
|
|
297
|
-
|
|
298
|
-
throw new Error(
|
|
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);
|
|
299
365
|
case 4: return [2 /*return*/];
|
|
300
366
|
}
|
|
301
367
|
});
|