chain-db-ts 0.0.1 → 1.0.0-rc.1
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/dist/cjs/features/chain-db.d.ts +25 -60
- package/dist/cjs/features/chain-db.js +100 -263
- package/dist/cjs/features/constants.d.ts +10 -10
- package/dist/cjs/features/constants.js +20 -11
- package/dist/cjs/features/events.d.ts +28 -0
- package/dist/cjs/features/events.js +89 -0
- package/dist/cjs/features/table.d.ts +78 -5
- package/dist/cjs/features/table.js +236 -51
- package/dist/cjs/features/types.d.ts +46 -20
- package/dist/cjs/features/types.js +30 -8
- package/dist/cjs/features/utils.d.ts +2 -1
- package/dist/cjs/features/utils.js +5 -2
- package/dist/cjs/index.d.ts +1 -2
- package/dist/cjs/index.js +4 -3
- package/features/chain-db.d.ts +25 -60
- package/features/chain-db.js +101 -241
- package/features/constants.d.ts +10 -10
- package/features/constants.js +13 -10
- package/features/events.d.ts +28 -0
- package/features/events.js +84 -0
- package/features/table.d.ts +78 -5
- package/features/table.js +237 -50
- package/features/types.d.ts +46 -20
- package/features/types.js +29 -7
- package/features/utils.d.ts +2 -1
- package/features/utils.js +5 -2
- package/index.d.ts +1 -2
- package/index.js +1 -1
- package/package.json +5 -3
- package/readme.md +201 -134
- package/tsconfig.json +0 -33
- package/dist/cjs/features/access.d.ts +0 -6
- package/dist/cjs/features/access.js +0 -22
- package/features/access.d.ts +0 -6
- package/features/access.js +0 -16
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
exports.__esModule = true;
|
|
6
|
+
var ws_1 = __importDefault(require("ws"));
|
|
7
|
+
var Events = /** @class */ (function () {
|
|
8
|
+
function Events(url, auth) {
|
|
9
|
+
var _this = this;
|
|
10
|
+
this.eventListeners = new Map();
|
|
11
|
+
this.connected = false;
|
|
12
|
+
this.socket = new ws_1["default"](url, {
|
|
13
|
+
headers: {
|
|
14
|
+
Authorization: "Basic ".concat(auth)
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
//
|
|
18
|
+
this.socket.onopen = function () {
|
|
19
|
+
_this.connected = true;
|
|
20
|
+
console.log('ChainDB: WebSocket connection established');
|
|
21
|
+
};
|
|
22
|
+
this.socket.on('message', function (data) {
|
|
23
|
+
try {
|
|
24
|
+
var parsedData_1 = JSON.parse(data.toString());
|
|
25
|
+
if (parsedData_1.event_type && _this.eventListeners.has(parsedData_1.event_type)) {
|
|
26
|
+
var listeners = _this.eventListeners.get(parsedData_1.event_type) || [];
|
|
27
|
+
listeners.forEach(function (callback) { return callback(parsedData_1); });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
console.error('Error parsing WebSocket message:', error);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
this.socket.on('close', function () {
|
|
35
|
+
_this.connected = false;
|
|
36
|
+
console.log('ChainDB: WebSocket connection closed');
|
|
37
|
+
});
|
|
38
|
+
this.socket.on('error', function (error) {
|
|
39
|
+
console.error('WebSocket error:', error);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Subscribe to an event
|
|
44
|
+
* @param event Event name to subscribe to
|
|
45
|
+
* @param callback Function to call when the event is received
|
|
46
|
+
*/
|
|
47
|
+
Events.prototype.subscribe = function (event, callback) {
|
|
48
|
+
if (!this.eventListeners.has(event)) {
|
|
49
|
+
this.eventListeners.set(event, []);
|
|
50
|
+
}
|
|
51
|
+
var listeners = this.eventListeners.get(event) || [];
|
|
52
|
+
listeners.push(callback);
|
|
53
|
+
this.eventListeners.set(event, listeners);
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Unsubscribe from an event
|
|
57
|
+
* @param event Event name to unsubscribe from
|
|
58
|
+
* @param callback Optional callback to remove. If not provided, all callbacks for the event will be removed.
|
|
59
|
+
*/
|
|
60
|
+
Events.prototype.unsubscribe = function (event, callback) {
|
|
61
|
+
if (!this.eventListeners.has(event)) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (callback) {
|
|
65
|
+
// Remove specific callback
|
|
66
|
+
var listeners = this.eventListeners.get(event) || [];
|
|
67
|
+
var updatedListeners = listeners.filter(function (cb) { return cb !== callback; });
|
|
68
|
+
this.eventListeners.set(event, updatedListeners);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
// Remove all callbacks for this event
|
|
72
|
+
this.eventListeners["delete"](event);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Close the WebSocket connection
|
|
77
|
+
*/
|
|
78
|
+
Events.prototype.close = function () {
|
|
79
|
+
this.socket.close();
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Check if the WebSocket connection is established
|
|
83
|
+
*/
|
|
84
|
+
Events.prototype.isConnected = function () {
|
|
85
|
+
return this.connected;
|
|
86
|
+
};
|
|
87
|
+
return Events;
|
|
88
|
+
}());
|
|
89
|
+
exports["default"] = Events;
|
|
@@ -1,13 +1,86 @@
|
|
|
1
1
|
import { ChainDB } from './chain-db';
|
|
2
|
+
import { Criteria, CriteriaAdvanced } from './types';
|
|
2
3
|
declare class Table<Model> {
|
|
3
4
|
table: Model;
|
|
4
|
-
private
|
|
5
|
+
private name;
|
|
5
6
|
private db;
|
|
6
|
-
constructor(
|
|
7
|
+
constructor(name: string, db: ChainDB);
|
|
7
8
|
/**
|
|
8
|
-
* Persist table data
|
|
9
|
+
* Persist table data changes
|
|
9
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>;
|
|
17
|
+
/**
|
|
18
|
+
* Get the history of changes. A list of transactions from the most recent to the most old
|
|
19
|
+
* in a range of depth
|
|
20
|
+
* @param limit
|
|
21
|
+
*/
|
|
22
|
+
getHistory(limit: number): Promise<Model[]>;
|
|
23
|
+
/**
|
|
24
|
+
* Refetch the table data
|
|
25
|
+
*/
|
|
26
|
+
refetch(): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Check if the table is empty
|
|
29
|
+
*/
|
|
30
|
+
isEmpty(): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Get the table's name
|
|
33
|
+
*/
|
|
34
|
+
getName(): string;
|
|
35
|
+
/**
|
|
36
|
+
* Find items in the table using basic criteria with exact matches
|
|
37
|
+
* @param criteria Object with fields and values to match exactly, e.g.: {age: 44, name: "john"}
|
|
38
|
+
* @param limit Maximum number of items to return (default: 1000)
|
|
39
|
+
* @param reverse If true, returns items in reverse order (default: true)
|
|
40
|
+
* @returns Array of found items matching the criteria
|
|
41
|
+
* @example
|
|
42
|
+
* // Find items where age is 44
|
|
43
|
+
* table.findWhere({
|
|
44
|
+
* age: 44,
|
|
45
|
+
* })
|
|
46
|
+
*
|
|
47
|
+
* // Find items with multiple criteria
|
|
48
|
+
* table.findWhere({
|
|
49
|
+
* age: 44,
|
|
50
|
+
* name: "john",
|
|
51
|
+
* active: true,
|
|
52
|
+
* score: 100
|
|
53
|
+
* })
|
|
54
|
+
*/
|
|
55
|
+
findWhere(criteria: Criteria<Model>, limit?: number, reverse?: boolean): Promise<Model[]>;
|
|
56
|
+
/**
|
|
57
|
+
* Find items in the table using advanced criteria with operators
|
|
58
|
+
* @param criteria Array of criteria to filter items. Each criteria contains:
|
|
59
|
+
* - field: The field name to filter
|
|
60
|
+
* - operator: The operator to use in comparison. Available operators:
|
|
61
|
+
* - Eq (==) Equal
|
|
62
|
+
* - Ne (!=) Not Equal
|
|
63
|
+
* - Gt (>) Greater Than
|
|
64
|
+
* - Ge (>=) Greater Than or Equal
|
|
65
|
+
* - Lt (<) Less Than
|
|
66
|
+
* - Le (<=) Less Than or Equal
|
|
67
|
+
* - Contains: Check if field contains value (for strings and arrays)
|
|
68
|
+
* - StartsWith: Check if field starts with value (for strings)
|
|
69
|
+
* - EndsWith: Check if field ends with value (for strings)
|
|
70
|
+
* - value: The value to compare against
|
|
71
|
+
* @param limit Maximum number of items to return (default: 1000)
|
|
72
|
+
* @param reverse If true, returns items in reverse order (default: true)
|
|
73
|
+
* @returns Array of found items matching the criteria
|
|
74
|
+
* @example
|
|
75
|
+
* // Find items where greeting contains "arg"
|
|
76
|
+
* table.findWhereAdvanced([
|
|
77
|
+
* {
|
|
78
|
+
* field: "greeting",
|
|
79
|
+
* operator: Operators.Contains,
|
|
80
|
+
* value: "hello"
|
|
81
|
+
* }
|
|
82
|
+
* ])
|
|
83
|
+
*/
|
|
84
|
+
findWhereAdvanced(criteria: CriteriaAdvanced<Model>[], limit?: number, reverse?: boolean): Promise<Model[]>;
|
|
11
85
|
}
|
|
12
|
-
export
|
|
13
|
-
export {};
|
|
86
|
+
export default Table;
|
|
@@ -39,45 +39,258 @@ 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.get = void 0;
|
|
43
42
|
var axios_1 = __importDefault(require("axios"));
|
|
44
43
|
var constants_1 = require("./constants");
|
|
45
|
-
var types_1 = require("./types");
|
|
46
44
|
var utils_1 = require("./utils");
|
|
47
45
|
var Table = /** @class */ (function () {
|
|
48
|
-
function Table(
|
|
49
|
-
this.
|
|
50
|
-
this.table =
|
|
51
|
-
this.
|
|
46
|
+
function Table(name, db) {
|
|
47
|
+
this.name = '';
|
|
48
|
+
this.table = {};
|
|
49
|
+
this.name = name;
|
|
52
50
|
this.db = db;
|
|
53
51
|
}
|
|
54
52
|
/**
|
|
55
|
-
* Persist table data
|
|
53
|
+
* Persist table data changes
|
|
56
54
|
*/
|
|
57
55
|
Table.prototype.persist = function () {
|
|
58
56
|
return __awaiter(this, void 0, void 0, function () {
|
|
59
|
-
var url,
|
|
60
|
-
return __generator(this, function (
|
|
61
|
-
switch (
|
|
57
|
+
var url, body, response, e_1;
|
|
58
|
+
return __generator(this, function (_a) {
|
|
59
|
+
switch (_a.label) {
|
|
62
60
|
case 0:
|
|
63
|
-
url = "".concat(this.db.
|
|
64
|
-
contract_data = JSON.stringify(this.table);
|
|
61
|
+
url = "".concat(this.db.server).concat((0, constants_1.PERSIST_NEW_DATA)(this.name));
|
|
65
62
|
body = {
|
|
66
|
-
|
|
67
|
-
contract_id: this.contract_id,
|
|
68
|
-
db_access_key: this.db.access_key,
|
|
69
|
-
data: contract_data
|
|
63
|
+
data: this.table
|
|
70
64
|
};
|
|
71
|
-
|
|
65
|
+
_a.label = 1;
|
|
72
66
|
case 1:
|
|
73
|
-
|
|
74
|
-
return [4 /*yield*/, (0, utils_1.post)(url, body)];
|
|
67
|
+
_a.trys.push([1, 3, , 4]);
|
|
68
|
+
return [4 /*yield*/, (0, utils_1.post)(url, body, this.db.auth)];
|
|
75
69
|
case 2:
|
|
76
|
-
|
|
70
|
+
response = _a.sent();
|
|
71
|
+
if (!response.data.success) {
|
|
72
|
+
throw new Error(response.data.message);
|
|
73
|
+
}
|
|
77
74
|
return [3 /*break*/, 4];
|
|
78
75
|
case 3:
|
|
79
|
-
|
|
80
|
-
throw new Error(
|
|
76
|
+
e_1 = _a.sent();
|
|
77
|
+
throw new Error("Something went wrong with persist operation: ".concat(e_1.message || String(e_1)));
|
|
78
|
+
case 4: return [2 /*return*/];
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
};
|
|
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
|
+
/**
|
|
116
|
+
* Get the history of changes. A list of transactions from the most recent to the most old
|
|
117
|
+
* in a range of depth
|
|
118
|
+
* @param limit
|
|
119
|
+
*/
|
|
120
|
+
Table.prototype.getHistory = function (limit) {
|
|
121
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
122
|
+
var url, response, e_3;
|
|
123
|
+
return __generator(this, function (_a) {
|
|
124
|
+
switch (_a.label) {
|
|
125
|
+
case 0:
|
|
126
|
+
url = "".concat(this.db.server).concat((0, constants_1.GET_HISTORY)(this.name, limit));
|
|
127
|
+
_a.label = 1;
|
|
128
|
+
case 1:
|
|
129
|
+
_a.trys.push([1, 3, , 4]);
|
|
130
|
+
return [4 /*yield*/, axios_1["default"].get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
|
|
131
|
+
case 2:
|
|
132
|
+
response = _a.sent();
|
|
133
|
+
if (!response.data.success) {
|
|
134
|
+
throw new Error(response.data.message);
|
|
135
|
+
}
|
|
136
|
+
// Return data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
|
|
137
|
+
return [2 /*return*/, response.data.data];
|
|
138
|
+
case 3:
|
|
139
|
+
e_3 = _a.sent();
|
|
140
|
+
throw new Error("Something went wrong with getHistory operation: ".concat(e_3.message || String(e_3)));
|
|
141
|
+
case 4: return [2 /*return*/];
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* Refetch the table data
|
|
148
|
+
*/
|
|
149
|
+
Table.prototype.refetch = function () {
|
|
150
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
151
|
+
var url, response, e_4;
|
|
152
|
+
return __generator(this, function (_a) {
|
|
153
|
+
switch (_a.label) {
|
|
154
|
+
case 0:
|
|
155
|
+
url = "".concat(this.db.server).concat((0, constants_1.GET_TABLE)(this.name));
|
|
156
|
+
_a.label = 1;
|
|
157
|
+
case 1:
|
|
158
|
+
_a.trys.push([1, 3, , 4]);
|
|
159
|
+
return [4 /*yield*/, axios_1["default"].get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
|
|
160
|
+
case 2:
|
|
161
|
+
response = _a.sent();
|
|
162
|
+
this.table = response.data.data ? response.data.data : {};
|
|
163
|
+
return [3 /*break*/, 4];
|
|
164
|
+
case 3:
|
|
165
|
+
e_4 = _a.sent();
|
|
166
|
+
throw new Error("Something went wrong with refetch operation: ".concat(e_4.message || String(e_4)));
|
|
167
|
+
case 4: return [2 /*return*/];
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Check if the table is empty
|
|
174
|
+
*/
|
|
175
|
+
Table.prototype.isEmpty = function () {
|
|
176
|
+
return Object.keys(this.table).length === 0;
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Get the table's name
|
|
180
|
+
*/
|
|
181
|
+
Table.prototype.getName = function () {
|
|
182
|
+
return this.name;
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Find items in the table using basic criteria with exact matches
|
|
186
|
+
* @param criteria Object with fields and values to match exactly, e.g.: {age: 44, name: "john"}
|
|
187
|
+
* @param limit Maximum number of items to return (default: 1000)
|
|
188
|
+
* @param reverse If true, returns items in reverse order (default: true)
|
|
189
|
+
* @returns Array of found items matching the criteria
|
|
190
|
+
* @example
|
|
191
|
+
* // Find items where age is 44
|
|
192
|
+
* table.findWhere({
|
|
193
|
+
* age: 44,
|
|
194
|
+
* })
|
|
195
|
+
*
|
|
196
|
+
* // Find items with multiple criteria
|
|
197
|
+
* table.findWhere({
|
|
198
|
+
* age: 44,
|
|
199
|
+
* name: "john",
|
|
200
|
+
* active: true,
|
|
201
|
+
* score: 100
|
|
202
|
+
* })
|
|
203
|
+
*/
|
|
204
|
+
Table.prototype.findWhere = function (criteria, limit, reverse) {
|
|
205
|
+
if (limit === void 0) { limit = 1000; }
|
|
206
|
+
if (reverse === void 0) { reverse = true; }
|
|
207
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
208
|
+
var url, body, response, e_5;
|
|
209
|
+
return __generator(this, function (_a) {
|
|
210
|
+
switch (_a.label) {
|
|
211
|
+
case 0:
|
|
212
|
+
url = "".concat(this.db.server).concat((0, constants_1.FIND_WHERE_BASIC)(this.name));
|
|
213
|
+
body = {
|
|
214
|
+
criteria: criteria,
|
|
215
|
+
limit: limit,
|
|
216
|
+
reverse: reverse
|
|
217
|
+
};
|
|
218
|
+
_a.label = 1;
|
|
219
|
+
case 1:
|
|
220
|
+
_a.trys.push([1, 3, , 4]);
|
|
221
|
+
return [4 /*yield*/, (0, utils_1.post)(url, body, this.db.auth)];
|
|
222
|
+
case 2:
|
|
223
|
+
response = _a.sent();
|
|
224
|
+
if (!response.data.success) {
|
|
225
|
+
throw new Error(response.data.message);
|
|
226
|
+
}
|
|
227
|
+
// Return found data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
|
|
228
|
+
return [2 /*return*/, response.data.data];
|
|
229
|
+
case 3:
|
|
230
|
+
e_5 = _a.sent();
|
|
231
|
+
throw new Error("Something went wrong with findWhere operation: ".concat(e_5.message || String(e_5)));
|
|
232
|
+
case 4: return [2 /*return*/];
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
};
|
|
237
|
+
// TODO: Implement documentation
|
|
238
|
+
/**
|
|
239
|
+
* Find items in the table using advanced criteria with operators
|
|
240
|
+
* @param criteria Array of criteria to filter items. Each criteria contains:
|
|
241
|
+
* - field: The field name to filter
|
|
242
|
+
* - operator: The operator to use in comparison. Available operators:
|
|
243
|
+
* - Eq (==) Equal
|
|
244
|
+
* - Ne (!=) Not Equal
|
|
245
|
+
* - Gt (>) Greater Than
|
|
246
|
+
* - Ge (>=) Greater Than or Equal
|
|
247
|
+
* - Lt (<) Less Than
|
|
248
|
+
* - Le (<=) Less Than or Equal
|
|
249
|
+
* - Contains: Check if field contains value (for strings and arrays)
|
|
250
|
+
* - StartsWith: Check if field starts with value (for strings)
|
|
251
|
+
* - EndsWith: Check if field ends with value (for strings)
|
|
252
|
+
* - value: The value to compare against
|
|
253
|
+
* @param limit Maximum number of items to return (default: 1000)
|
|
254
|
+
* @param reverse If true, returns items in reverse order (default: true)
|
|
255
|
+
* @returns Array of found items matching the criteria
|
|
256
|
+
* @example
|
|
257
|
+
* // Find items where greeting contains "arg"
|
|
258
|
+
* table.findWhereAdvanced([
|
|
259
|
+
* {
|
|
260
|
+
* field: "greeting",
|
|
261
|
+
* operator: Operators.Contains,
|
|
262
|
+
* value: "hello"
|
|
263
|
+
* }
|
|
264
|
+
* ])
|
|
265
|
+
*/
|
|
266
|
+
Table.prototype.findWhereAdvanced = function (criteria, limit, reverse) {
|
|
267
|
+
if (limit === void 0) { limit = 1000; }
|
|
268
|
+
if (reverse === void 0) { reverse = true; }
|
|
269
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
270
|
+
var url, body, response, e_6;
|
|
271
|
+
return __generator(this, function (_a) {
|
|
272
|
+
switch (_a.label) {
|
|
273
|
+
case 0:
|
|
274
|
+
url = "".concat(this.db.server).concat((0, constants_1.FIND_WHERE_ADVANCED)(this.name));
|
|
275
|
+
body = {
|
|
276
|
+
criteria: criteria,
|
|
277
|
+
limit: limit,
|
|
278
|
+
reverse: reverse
|
|
279
|
+
};
|
|
280
|
+
_a.label = 1;
|
|
281
|
+
case 1:
|
|
282
|
+
_a.trys.push([1, 3, , 4]);
|
|
283
|
+
return [4 /*yield*/, (0, utils_1.post)(url, body, this.db.auth)];
|
|
284
|
+
case 2:
|
|
285
|
+
response = _a.sent();
|
|
286
|
+
if (!response.data.success) {
|
|
287
|
+
throw new Error(response.data.message);
|
|
288
|
+
}
|
|
289
|
+
// Return found data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
|
|
290
|
+
return [2 /*return*/, response.data.data];
|
|
291
|
+
case 3:
|
|
292
|
+
e_6 = _a.sent();
|
|
293
|
+
throw new Error("Something went wrong with findWhereAdvanced operation: ".concat(e_6.message || String(e_6)));
|
|
81
294
|
case 4: return [2 /*return*/];
|
|
82
295
|
}
|
|
83
296
|
});
|
|
@@ -85,32 +298,4 @@ var Table = /** @class */ (function () {
|
|
|
85
298
|
};
|
|
86
299
|
return Table;
|
|
87
300
|
}());
|
|
88
|
-
|
|
89
|
-
var contract_id, url, contract_response, contract_data_json, table_1, table, _a;
|
|
90
|
-
return __generator(this, function (_b) {
|
|
91
|
-
switch (_b.label) {
|
|
92
|
-
case 0:
|
|
93
|
-
contract_id = db.access.parse(db.name, table_name);
|
|
94
|
-
url = "".concat(db.api).concat(constants_1.CONTRACT_PAYLOAD, "/").concat(contract_id, "/").concat(db.access_key);
|
|
95
|
-
_b.label = 1;
|
|
96
|
-
case 1:
|
|
97
|
-
_b.trys.push([1, 3, , 4]);
|
|
98
|
-
return [4 /*yield*/, axios_1["default"].get(url)];
|
|
99
|
-
case 2:
|
|
100
|
-
contract_response = _b.sent();
|
|
101
|
-
contract_data_json = contract_response.data;
|
|
102
|
-
// If there's already a table (contract) with data, then, fetch its data
|
|
103
|
-
if (contract_data_json.tx_type === types_1.TransactionType.CONTRACT) {
|
|
104
|
-
table_1 = new Table(contract_data_json.data, contract_id, db);
|
|
105
|
-
return [2 /*return*/, table_1];
|
|
106
|
-
}
|
|
107
|
-
table = new Table(model, contract_id, db);
|
|
108
|
-
return [2 /*return*/, table];
|
|
109
|
-
case 3:
|
|
110
|
-
_a = _b.sent();
|
|
111
|
-
throw new Error('Something went wrong!');
|
|
112
|
-
case 4: return [2 /*return*/];
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
}); };
|
|
116
|
-
exports.get = get;
|
|
301
|
+
exports["default"] = Table;
|
|
@@ -1,27 +1,53 @@
|
|
|
1
1
|
export type BasicResponse<D> = {
|
|
2
2
|
success: boolean;
|
|
3
|
-
|
|
3
|
+
message: string;
|
|
4
4
|
data?: D;
|
|
5
5
|
};
|
|
6
|
-
export type
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
export type Connection = {
|
|
7
|
+
server: string | null;
|
|
8
|
+
database: string;
|
|
9
|
+
user: string;
|
|
10
|
+
password: string;
|
|
10
11
|
};
|
|
11
|
-
export type
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
export type Criteria<Model> = Partial<Record<keyof Model, string | number | boolean>>;
|
|
13
|
+
export type CriteriaAdvanced<Model> = {
|
|
14
|
+
field: Partial<keyof Model>;
|
|
15
|
+
/**
|
|
16
|
+
* @see Operators
|
|
17
|
+
*/
|
|
18
|
+
operator: string;
|
|
19
|
+
value: string | number | boolean;
|
|
15
20
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Operators for the advanced criteria
|
|
23
|
+
* @see https://docs.chaindb.com/docs/query-language/ (TODO)
|
|
24
|
+
*/
|
|
25
|
+
export declare const Operators: {
|
|
26
|
+
EQUAL: string;
|
|
27
|
+
NOT_EQUAL: string;
|
|
28
|
+
GREATER_THAN: string;
|
|
29
|
+
GREATER_THAN_OR_EQUAL: string;
|
|
30
|
+
LESS_THAN: string;
|
|
31
|
+
LESS_THAN_OR_EQUAL: string;
|
|
32
|
+
CONTAINS: string;
|
|
33
|
+
STARTS_WITH: string;
|
|
34
|
+
ENDS_WITH: string;
|
|
27
35
|
};
|
|
36
|
+
export declare const EventTypes: {
|
|
37
|
+
TABLE_PERSIST: string;
|
|
38
|
+
TABLE_UPDATE: string;
|
|
39
|
+
};
|
|
40
|
+
export type EventData = {
|
|
41
|
+
event_type: string;
|
|
42
|
+
database: string;
|
|
43
|
+
table: string;
|
|
44
|
+
/**
|
|
45
|
+
* Data of the event (also to/from the table)
|
|
46
|
+
*/
|
|
47
|
+
data: Record<string, any>;
|
|
48
|
+
/**
|
|
49
|
+
* Timestamp of the event
|
|
50
|
+
*/
|
|
51
|
+
timestamp: number;
|
|
52
|
+
};
|
|
53
|
+
export type EventCallback = (data: EventData) => void;
|
|
@@ -1,10 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
exports.__esModule = true;
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
exports.EventTypes = exports.Operators = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Operators for the advanced criteria
|
|
6
|
+
* @see https://docs.chaindb.com/docs/query-language/ (TODO)
|
|
7
|
+
*/
|
|
8
|
+
exports.Operators = {
|
|
9
|
+
// (==)
|
|
10
|
+
EQUAL: 'Eq',
|
|
11
|
+
// (!=)
|
|
12
|
+
NOT_EQUAL: 'Ne',
|
|
13
|
+
// (>)
|
|
14
|
+
GREATER_THAN: 'Gt',
|
|
15
|
+
// (>=)
|
|
16
|
+
GREATER_THAN_OR_EQUAL: 'Ge',
|
|
17
|
+
// (<)
|
|
18
|
+
LESS_THAN: 'Lt',
|
|
19
|
+
// (<=)
|
|
20
|
+
LESS_THAN_OR_EQUAL: 'Le',
|
|
21
|
+
// (for strings and arrays)
|
|
22
|
+
CONTAINS: 'Contains',
|
|
23
|
+
// (for strings)
|
|
24
|
+
STARTS_WITH: 'StartsWith',
|
|
25
|
+
// (for strings)
|
|
26
|
+
ENDS_WITH: 'EndsWith'
|
|
27
|
+
};
|
|
28
|
+
// Events
|
|
29
|
+
exports.EventTypes = {
|
|
30
|
+
TABLE_PERSIST: 'TablePersist',
|
|
31
|
+
TABLE_UPDATE: 'TableUpdate'
|
|
32
|
+
};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { BasicResponse } from './types';
|
|
2
|
+
export declare const post: (url: string, body: any, auth?: string) => Promise<import("axios").AxiosResponse<BasicResponse<any>, any>>;
|
|
@@ -5,7 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
exports.__esModule = true;
|
|
6
6
|
exports.post = void 0;
|
|
7
7
|
var axios_1 = __importDefault(require("axios"));
|
|
8
|
-
var post = function (url, body) {
|
|
9
|
-
|
|
8
|
+
var post = function (url, body, auth) {
|
|
9
|
+
if (auth === void 0) { auth = ''; }
|
|
10
|
+
return axios_1["default"].post(url, body, {
|
|
11
|
+
headers: { 'content-type': 'application/json', Authorization: "Basic ".concat(auth) }
|
|
12
|
+
});
|
|
10
13
|
};
|
|
11
14
|
exports.post = post;
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
1
|
export { ChainDB, connect } from './features/chain-db';
|
|
2
|
-
export {
|
|
3
|
-
export { BasicResponse, SignedUserAccount } from './features/types';
|
|
2
|
+
export { BasicResponse, Operators, CriteriaAdvanced, Criteria, EventTypes, EventData, EventCallback, } from './features/types';
|
package/dist/cjs/index.js
CHANGED
|
@@ -11,9 +11,10 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
13
|
exports.__esModule = true;
|
|
14
|
-
exports.
|
|
14
|
+
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
|
-
var
|
|
19
|
-
__createBinding(exports,
|
|
18
|
+
var types_1 = require("./features/types");
|
|
19
|
+
__createBinding(exports, types_1, "Operators");
|
|
20
|
+
__createBinding(exports, types_1, "EventTypes");
|