chain-db-ts 0.0.2 → 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 +110 -0
- package/dist/cjs/features/chain-db.d.ts +23 -64
- package/dist/cjs/features/chain-db.js +100 -263
- package/dist/cjs/features/constants.d.ts +11 -11
- package/dist/cjs/features/constants.js +22 -12
- package/dist/cjs/features/events.d.ts +28 -0
- package/dist/cjs/features/events.js +89 -0
- package/dist/cjs/features/table-doc.d.ts +37 -0
- package/dist/cjs/features/table-doc.js +135 -0
- package/dist/cjs/features/table.d.ts +81 -9
- package/dist/cjs/features/table.js +226 -70
- package/dist/cjs/features/types.d.ts +79 -21
- 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 +3 -2
- package/dist/cjs/index.js +8 -3
- package/features/chain-db.d.ts +23 -64
- package/features/chain-db.js +101 -241
- package/features/constants.d.ts +11 -11
- package/features/constants.js +14 -11
- package/features/events.d.ts +28 -0
- package/features/events.js +84 -0
- package/features/table-doc.d.ts +37 -0
- package/features/table-doc.js +129 -0
- package/features/table.d.ts +81 -9
- package/features/table.js +227 -69
- package/features/types.d.ts +79 -21
- package/features/types.js +29 -7
- package/features/utils.d.ts +2 -1
- package/features/utils.js +5 -2
- package/index.d.ts +3 -2
- package/index.js +3 -1
- package/package.json +5 -3
- package/readme.md +260 -132
- 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,129 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
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;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
import axios from 'axios';
|
|
38
|
+
import { UPDATE_ITEM, GET_DOC } from './constants';
|
|
39
|
+
import { post } from './utils';
|
|
40
|
+
/**
|
|
41
|
+
* Implementation of TableDoc interface
|
|
42
|
+
* Represents a specific document from a table
|
|
43
|
+
*/
|
|
44
|
+
var TableDocImpl = /** @class */ (function () {
|
|
45
|
+
function TableDocImpl(tableName, doc_id, data, db) {
|
|
46
|
+
this.tableName = tableName;
|
|
47
|
+
this.doc_id = doc_id;
|
|
48
|
+
this.doc = data;
|
|
49
|
+
this.db = db;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Update the document data
|
|
53
|
+
* This will update the specific document without creating a new one
|
|
54
|
+
*/
|
|
55
|
+
TableDocImpl.prototype.update = function () {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
57
|
+
var url, body, response, e_1;
|
|
58
|
+
return __generator(this, function (_a) {
|
|
59
|
+
switch (_a.label) {
|
|
60
|
+
case 0:
|
|
61
|
+
url = "".concat(this.db.server).concat(UPDATE_ITEM(this.tableName));
|
|
62
|
+
body = {
|
|
63
|
+
data: this.doc,
|
|
64
|
+
doc_id: this.doc_id
|
|
65
|
+
};
|
|
66
|
+
_a.label = 1;
|
|
67
|
+
case 1:
|
|
68
|
+
_a.trys.push([1, 3, , 4]);
|
|
69
|
+
return [4 /*yield*/, post(url, body, this.db.auth)];
|
|
70
|
+
case 2:
|
|
71
|
+
response = _a.sent();
|
|
72
|
+
if (!response.data.success) {
|
|
73
|
+
throw new Error(response.data.message);
|
|
74
|
+
}
|
|
75
|
+
return [3 /*break*/, 4];
|
|
76
|
+
case 3:
|
|
77
|
+
e_1 = _a.sent();
|
|
78
|
+
throw new Error("Something went wrong updating document ".concat(this.doc_id, ": ").concat(e_1.message || String(e_1)));
|
|
79
|
+
case 4: return [2 /*return*/];
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Refetch the document data from the database
|
|
86
|
+
* Useful when the document might have been updated by another application
|
|
87
|
+
*/
|
|
88
|
+
TableDocImpl.prototype.refetch = function () {
|
|
89
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
90
|
+
var url, response, e_2;
|
|
91
|
+
return __generator(this, function (_a) {
|
|
92
|
+
switch (_a.label) {
|
|
93
|
+
case 0:
|
|
94
|
+
url = "".concat(this.db.server).concat(GET_DOC(this.tableName, this.doc_id));
|
|
95
|
+
_a.label = 1;
|
|
96
|
+
case 1:
|
|
97
|
+
_a.trys.push([1, 3, , 4]);
|
|
98
|
+
return [4 /*yield*/, axios.get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
|
|
99
|
+
case 2:
|
|
100
|
+
response = _a.sent();
|
|
101
|
+
if (!response.data.success) {
|
|
102
|
+
throw new Error(response.data.message);
|
|
103
|
+
}
|
|
104
|
+
// Update the table data with the latest data from the database
|
|
105
|
+
this.doc = response.data.data;
|
|
106
|
+
return [3 /*break*/, 4];
|
|
107
|
+
case 3:
|
|
108
|
+
e_2 = _a.sent();
|
|
109
|
+
throw new Error("Something went wrong refetching document ".concat(this.doc_id, ": ").concat(e_2.message || String(e_2)));
|
|
110
|
+
case 4: return [2 /*return*/];
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Get the table name this document belongs to
|
|
117
|
+
*/
|
|
118
|
+
TableDocImpl.prototype.getTableName = function () {
|
|
119
|
+
return this.tableName;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Check if the table is empty
|
|
123
|
+
*/
|
|
124
|
+
TableDocImpl.prototype.isEmpty = function () {
|
|
125
|
+
return Object.keys(this.doc).length === 0;
|
|
126
|
+
};
|
|
127
|
+
return TableDocImpl;
|
|
128
|
+
}());
|
|
129
|
+
export { TableDocImpl };
|
package/features/table.d.ts
CHANGED
|
@@ -1,19 +1,91 @@
|
|
|
1
1
|
import { ChainDB } from './chain-db';
|
|
2
|
+
import { Criteria, CriteriaAdvanced, DocId, TableDoc } from './types';
|
|
2
3
|
declare class Table<Model> {
|
|
3
|
-
|
|
4
|
-
private
|
|
4
|
+
currentDoc: Model;
|
|
5
|
+
private name;
|
|
5
6
|
private db;
|
|
6
|
-
constructor(
|
|
7
|
+
constructor(name: string, db: ChainDB);
|
|
7
8
|
/**
|
|
8
|
-
* Persist table data
|
|
9
|
+
* Persist table's document data changes
|
|
9
10
|
*/
|
|
10
|
-
persist(): Promise<
|
|
11
|
+
persist(): Promise<DocId<Model>>;
|
|
11
12
|
/**
|
|
12
13
|
* Get the history of changes. A list of transactions from the most recent to the most old
|
|
13
14
|
* in a range of depth
|
|
14
|
-
* @param
|
|
15
|
+
* @param limit
|
|
15
16
|
*/
|
|
16
|
-
getHistory(
|
|
17
|
+
getHistory(limit: number): Promise<DocId<Model>[]>;
|
|
18
|
+
/**
|
|
19
|
+
* Refetch the table data
|
|
20
|
+
*/
|
|
21
|
+
refetch(): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Check if the table is empty
|
|
24
|
+
*/
|
|
25
|
+
isEmpty(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Get the table's name
|
|
28
|
+
*/
|
|
29
|
+
getName(): string;
|
|
30
|
+
/**
|
|
31
|
+
* Find items in the table using basic criteria with exact matches
|
|
32
|
+
* @param criteria Object with fields and values to match exactly, e.g.: {age: 44, name: "john"}
|
|
33
|
+
* @param limit Maximum number of items to return (default: 1000)
|
|
34
|
+
* @param reverse If true, returns items in reverse order (default: true)
|
|
35
|
+
* @returns Array of found items matching the criteria
|
|
36
|
+
* @example
|
|
37
|
+
* // Find items where age is 44
|
|
38
|
+
* table.findWhere({
|
|
39
|
+
* age: 44,
|
|
40
|
+
* })
|
|
41
|
+
*
|
|
42
|
+
* // Find items with multiple criteria
|
|
43
|
+
* table.findWhere({
|
|
44
|
+
* age: 44,
|
|
45
|
+
* name: "john",
|
|
46
|
+
* active: true,
|
|
47
|
+
* score: 100
|
|
48
|
+
* })
|
|
49
|
+
*/
|
|
50
|
+
findWhere(criteria: Criteria<Model>, limit?: number, reverse?: boolean): Promise<DocId<Model>[]>;
|
|
51
|
+
/**
|
|
52
|
+
* Find items in the table using advanced criteria with operators
|
|
53
|
+
* @param criteria Array of criteria to filter items. Each criteria contains:
|
|
54
|
+
* - field: The field name to filter
|
|
55
|
+
* - operator: The operator to use in comparison. Available operators:
|
|
56
|
+
* - Eq (==) Equal
|
|
57
|
+
* - Ne (!=) Not Equal
|
|
58
|
+
* - Gt (>) Greater Than
|
|
59
|
+
* - Ge (>=) Greater Than or Equal
|
|
60
|
+
* - Lt (<) Less Than
|
|
61
|
+
* - Le (<=) Less Than or Equal
|
|
62
|
+
* - Contains: Check if field contains value (for strings and arrays)
|
|
63
|
+
* - StartsWith: Check if field starts with value (for strings)
|
|
64
|
+
* - EndsWith: Check if field ends with value (for strings)
|
|
65
|
+
* - value: The value to compare against
|
|
66
|
+
* @param limit Maximum number of items to return (default: 1000)
|
|
67
|
+
* @param reverse If true, returns items in reverse order (default: true)
|
|
68
|
+
* @returns Array of found items matching the criteria
|
|
69
|
+
* @example
|
|
70
|
+
* // Find items where greeting contains "arg"
|
|
71
|
+
* table.findWhereAdvanced([
|
|
72
|
+
* {
|
|
73
|
+
* field: "greeting",
|
|
74
|
+
* operator: Operators.Contains,
|
|
75
|
+
* value: "hello"
|
|
76
|
+
* }
|
|
77
|
+
* ])
|
|
78
|
+
*/
|
|
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>>;
|
|
17
90
|
}
|
|
18
|
-
export
|
|
19
|
-
export {};
|
|
91
|
+
export default Table;
|
package/features/table.js
CHANGED
|
@@ -35,43 +35,42 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
37
|
import axios from 'axios';
|
|
38
|
-
import {
|
|
39
|
-
import { TransactionType } from './types';
|
|
38
|
+
import { FIND_WHERE_ADVANCED, FIND_WHERE_BASIC, GET_HISTORY, GET_TABLE, PERSIST_NEW_DATA, GET_DOC } from './constants';
|
|
40
39
|
import { post } from './utils';
|
|
40
|
+
import { TableDocImpl } from './table-doc';
|
|
41
41
|
var Table = /** @class */ (function () {
|
|
42
|
-
function Table(
|
|
43
|
-
this.
|
|
44
|
-
this.
|
|
45
|
-
this.
|
|
42
|
+
function Table(name, db) {
|
|
43
|
+
this.name = '';
|
|
44
|
+
this.currentDoc = {};
|
|
45
|
+
this.name = name;
|
|
46
46
|
this.db = db;
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
|
-
* Persist table data
|
|
49
|
+
* Persist table's document data changes
|
|
50
50
|
*/
|
|
51
51
|
Table.prototype.persist = function () {
|
|
52
52
|
return __awaiter(this, void 0, void 0, function () {
|
|
53
|
-
var url,
|
|
54
|
-
return __generator(this, function (
|
|
55
|
-
switch (
|
|
53
|
+
var url, body, response, e_1;
|
|
54
|
+
return __generator(this, function (_a) {
|
|
55
|
+
switch (_a.label) {
|
|
56
56
|
case 0:
|
|
57
|
-
url = "".concat(this.db.
|
|
58
|
-
contract_data = JSON.stringify(this.table);
|
|
57
|
+
url = "".concat(this.db.server).concat(PERSIST_NEW_DATA(this.name));
|
|
59
58
|
body = {
|
|
60
|
-
|
|
61
|
-
contract_id: this.contract_id,
|
|
62
|
-
db_access_key: this.db.access_key,
|
|
63
|
-
data: contract_data
|
|
59
|
+
data: this.currentDoc
|
|
64
60
|
};
|
|
65
|
-
|
|
61
|
+
_a.label = 1;
|
|
66
62
|
case 1:
|
|
67
|
-
|
|
68
|
-
return [4 /*yield*/, post(url, body)];
|
|
63
|
+
_a.trys.push([1, 3, , 4]);
|
|
64
|
+
return [4 /*yield*/, post(url, body, this.db.auth)];
|
|
69
65
|
case 2:
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
response = _a.sent();
|
|
67
|
+
if (!response.data.success) {
|
|
68
|
+
throw new Error(response.data.message);
|
|
69
|
+
}
|
|
70
|
+
return [2 /*return*/, response.data.data];
|
|
72
71
|
case 3:
|
|
73
|
-
|
|
74
|
-
throw new Error(
|
|
72
|
+
e_1 = _a.sent();
|
|
73
|
+
throw new Error("Something went wrong with persist operation: ".concat(e_1.message || String(e_1)));
|
|
75
74
|
case 4: return [2 /*return*/];
|
|
76
75
|
}
|
|
77
76
|
});
|
|
@@ -80,32 +79,218 @@ var Table = /** @class */ (function () {
|
|
|
80
79
|
/**
|
|
81
80
|
* Get the history of changes. A list of transactions from the most recent to the most old
|
|
82
81
|
* in a range of depth
|
|
83
|
-
* @param
|
|
82
|
+
* @param limit
|
|
84
83
|
*/
|
|
85
|
-
Table.prototype.getHistory = function (
|
|
84
|
+
Table.prototype.getHistory = function (limit) {
|
|
86
85
|
return __awaiter(this, void 0, void 0, function () {
|
|
87
|
-
var url,
|
|
88
|
-
return __generator(this, function (
|
|
89
|
-
switch (
|
|
86
|
+
var url, response, e_2;
|
|
87
|
+
return __generator(this, function (_a) {
|
|
88
|
+
switch (_a.label) {
|
|
90
89
|
case 0:
|
|
91
|
-
url = "".concat(this.db.
|
|
92
|
-
|
|
90
|
+
url = "".concat(this.db.server).concat(GET_HISTORY(this.name, limit));
|
|
91
|
+
_a.label = 1;
|
|
93
92
|
case 1:
|
|
94
|
-
|
|
95
|
-
return [4 /*yield*/, axios.get(url)];
|
|
93
|
+
_a.trys.push([1, 3, , 4]);
|
|
94
|
+
return [4 /*yield*/, axios.get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
|
|
96
95
|
case 2:
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
// Return empty if theres no data
|
|
101
|
-
if (contract_data_json_list.length === 1 && contract_data_json_list[0].tx_type === TransactionType.NONE) {
|
|
102
|
-
return [2 /*return*/, []];
|
|
96
|
+
response = _a.sent();
|
|
97
|
+
if (!response.data.success) {
|
|
98
|
+
throw new Error(response.data.message);
|
|
103
99
|
}
|
|
104
100
|
// Return data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
|
|
105
|
-
return [2 /*return*/,
|
|
101
|
+
return [2 /*return*/, response.data.data];
|
|
102
|
+
case 3:
|
|
103
|
+
e_2 = _a.sent();
|
|
104
|
+
throw new Error("Something went wrong with getHistory operation: ".concat(e_2.message || String(e_2)));
|
|
105
|
+
case 4: return [2 /*return*/];
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Refetch the table data
|
|
112
|
+
*/
|
|
113
|
+
Table.prototype.refetch = function () {
|
|
114
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
115
|
+
var url, response, e_3;
|
|
116
|
+
return __generator(this, function (_a) {
|
|
117
|
+
switch (_a.label) {
|
|
118
|
+
case 0:
|
|
119
|
+
url = "".concat(this.db.server).concat(GET_TABLE(this.name));
|
|
120
|
+
_a.label = 1;
|
|
121
|
+
case 1:
|
|
122
|
+
_a.trys.push([1, 3, , 4]);
|
|
123
|
+
return [4 /*yield*/, axios.get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
|
|
124
|
+
case 2:
|
|
125
|
+
response = _a.sent();
|
|
126
|
+
this.currentDoc = response.data.data ? response.data.data : {};
|
|
127
|
+
return [3 /*break*/, 4];
|
|
106
128
|
case 3:
|
|
107
|
-
|
|
108
|
-
throw new Error(
|
|
129
|
+
e_3 = _a.sent();
|
|
130
|
+
throw new Error("Something went wrong with refetch operation: ".concat(e_3.message || String(e_3)));
|
|
131
|
+
case 4: return [2 /*return*/];
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Check if the table is empty
|
|
138
|
+
*/
|
|
139
|
+
Table.prototype.isEmpty = function () {
|
|
140
|
+
return Object.keys(this.currentDoc).length === 0;
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Get the table's name
|
|
144
|
+
*/
|
|
145
|
+
Table.prototype.getName = function () {
|
|
146
|
+
return this.name;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Find items in the table using basic criteria with exact matches
|
|
150
|
+
* @param criteria Object with fields and values to match exactly, e.g.: {age: 44, name: "john"}
|
|
151
|
+
* @param limit Maximum number of items to return (default: 1000)
|
|
152
|
+
* @param reverse If true, returns items in reverse order (default: true)
|
|
153
|
+
* @returns Array of found items matching the criteria
|
|
154
|
+
* @example
|
|
155
|
+
* // Find items where age is 44
|
|
156
|
+
* table.findWhere({
|
|
157
|
+
* age: 44,
|
|
158
|
+
* })
|
|
159
|
+
*
|
|
160
|
+
* // Find items with multiple criteria
|
|
161
|
+
* table.findWhere({
|
|
162
|
+
* age: 44,
|
|
163
|
+
* name: "john",
|
|
164
|
+
* active: true,
|
|
165
|
+
* score: 100
|
|
166
|
+
* })
|
|
167
|
+
*/
|
|
168
|
+
Table.prototype.findWhere = function (criteria, limit, reverse) {
|
|
169
|
+
if (limit === void 0) { limit = 1000; }
|
|
170
|
+
if (reverse === void 0) { reverse = true; }
|
|
171
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
172
|
+
var url, body, response, e_4;
|
|
173
|
+
return __generator(this, function (_a) {
|
|
174
|
+
switch (_a.label) {
|
|
175
|
+
case 0:
|
|
176
|
+
url = "".concat(this.db.server).concat(FIND_WHERE_BASIC(this.name));
|
|
177
|
+
body = {
|
|
178
|
+
criteria: criteria,
|
|
179
|
+
limit: limit,
|
|
180
|
+
reverse: reverse
|
|
181
|
+
};
|
|
182
|
+
_a.label = 1;
|
|
183
|
+
case 1:
|
|
184
|
+
_a.trys.push([1, 3, , 4]);
|
|
185
|
+
return [4 /*yield*/, post(url, body, this.db.auth)];
|
|
186
|
+
case 2:
|
|
187
|
+
response = _a.sent();
|
|
188
|
+
if (!response.data.success) {
|
|
189
|
+
throw new Error(response.data.message);
|
|
190
|
+
}
|
|
191
|
+
// Return found data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
|
|
192
|
+
return [2 /*return*/, response.data.data];
|
|
193
|
+
case 3:
|
|
194
|
+
e_4 = _a.sent();
|
|
195
|
+
throw new Error("Something went wrong with findWhere operation: ".concat(e_4.message || String(e_4)));
|
|
196
|
+
case 4: return [2 /*return*/];
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
};
|
|
201
|
+
/**
|
|
202
|
+
* Find items in the table using advanced criteria with operators
|
|
203
|
+
* @param criteria Array of criteria to filter items. Each criteria contains:
|
|
204
|
+
* - field: The field name to filter
|
|
205
|
+
* - operator: The operator to use in comparison. Available operators:
|
|
206
|
+
* - Eq (==) Equal
|
|
207
|
+
* - Ne (!=) Not Equal
|
|
208
|
+
* - Gt (>) Greater Than
|
|
209
|
+
* - Ge (>=) Greater Than or Equal
|
|
210
|
+
* - Lt (<) Less Than
|
|
211
|
+
* - Le (<=) Less Than or Equal
|
|
212
|
+
* - Contains: Check if field contains value (for strings and arrays)
|
|
213
|
+
* - StartsWith: Check if field starts with value (for strings)
|
|
214
|
+
* - EndsWith: Check if field ends with value (for strings)
|
|
215
|
+
* - value: The value to compare against
|
|
216
|
+
* @param limit Maximum number of items to return (default: 1000)
|
|
217
|
+
* @param reverse If true, returns items in reverse order (default: true)
|
|
218
|
+
* @returns Array of found items matching the criteria
|
|
219
|
+
* @example
|
|
220
|
+
* // Find items where greeting contains "arg"
|
|
221
|
+
* table.findWhereAdvanced([
|
|
222
|
+
* {
|
|
223
|
+
* field: "greeting",
|
|
224
|
+
* operator: Operators.Contains,
|
|
225
|
+
* value: "hello"
|
|
226
|
+
* }
|
|
227
|
+
* ])
|
|
228
|
+
*/
|
|
229
|
+
Table.prototype.findWhereAdvanced = function (criteria, limit, reverse) {
|
|
230
|
+
if (limit === void 0) { limit = 1000; }
|
|
231
|
+
if (reverse === void 0) { reverse = true; }
|
|
232
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
233
|
+
var url, body, response, e_5;
|
|
234
|
+
return __generator(this, function (_a) {
|
|
235
|
+
switch (_a.label) {
|
|
236
|
+
case 0:
|
|
237
|
+
url = "".concat(this.db.server).concat(FIND_WHERE_ADVANCED(this.name));
|
|
238
|
+
body = {
|
|
239
|
+
criteria: criteria,
|
|
240
|
+
limit: limit,
|
|
241
|
+
reverse: reverse
|
|
242
|
+
};
|
|
243
|
+
_a.label = 1;
|
|
244
|
+
case 1:
|
|
245
|
+
_a.trys.push([1, 3, , 4]);
|
|
246
|
+
return [4 /*yield*/, post(url, body, this.db.auth)];
|
|
247
|
+
case 2:
|
|
248
|
+
response = _a.sent();
|
|
249
|
+
if (!response.data.success) {
|
|
250
|
+
throw new Error(response.data.message);
|
|
251
|
+
}
|
|
252
|
+
// Return found data. Only table fields, e.g.: [{fieldA: 'Hi', filedB: 22}]
|
|
253
|
+
return [2 /*return*/, response.data.data];
|
|
254
|
+
case 3:
|
|
255
|
+
e_5 = _a.sent();
|
|
256
|
+
throw new Error("Something went wrong with findWhereAdvanced operation: ".concat(e_5.message || String(e_5)));
|
|
257
|
+
case 4: return [2 /*return*/];
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
};
|
|
262
|
+
/**
|
|
263
|
+
* Get the current document ID
|
|
264
|
+
*/
|
|
265
|
+
Table.prototype.getCurrentDocId = function () {
|
|
266
|
+
return this.currentDoc.doc_id;
|
|
267
|
+
};
|
|
268
|
+
/**
|
|
269
|
+
* Get a specific document by its ID
|
|
270
|
+
* @param doc_id The document ID to retrieve
|
|
271
|
+
* @returns A TableDoc instance with the specific document data
|
|
272
|
+
*/
|
|
273
|
+
Table.prototype.getDoc = function (doc_id) {
|
|
274
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
275
|
+
var url, response, e_6;
|
|
276
|
+
return __generator(this, function (_a) {
|
|
277
|
+
switch (_a.label) {
|
|
278
|
+
case 0:
|
|
279
|
+
url = "".concat(this.db.server).concat(GET_DOC(this.name, doc_id));
|
|
280
|
+
_a.label = 1;
|
|
281
|
+
case 1:
|
|
282
|
+
_a.trys.push([1, 3, , 4]);
|
|
283
|
+
return [4 /*yield*/, axios.get(url, { headers: { Authorization: "Basic ".concat(this.db.auth) } })];
|
|
284
|
+
case 2:
|
|
285
|
+
response = _a.sent();
|
|
286
|
+
if (!response.data.success) {
|
|
287
|
+
throw new Error(response.data.message);
|
|
288
|
+
}
|
|
289
|
+
// Create a TableDoc instance with the document data
|
|
290
|
+
return [2 /*return*/, new TableDocImpl(this.name, doc_id, response.data.data, this.db)];
|
|
291
|
+
case 3:
|
|
292
|
+
e_6 = _a.sent();
|
|
293
|
+
throw new Error("Something went wrong with getDoc operation: ".concat(e_6.message || String(e_6)));
|
|
109
294
|
case 4: return [2 /*return*/];
|
|
110
295
|
}
|
|
111
296
|
});
|
|
@@ -113,31 +298,4 @@ var Table = /** @class */ (function () {
|
|
|
113
298
|
};
|
|
114
299
|
return Table;
|
|
115
300
|
}());
|
|
116
|
-
export
|
|
117
|
-
var contract_id, url, contract_response, contract_data_json, table_1, table, _a;
|
|
118
|
-
return __generator(this, function (_b) {
|
|
119
|
-
switch (_b.label) {
|
|
120
|
-
case 0:
|
|
121
|
-
contract_id = db.access.parse(db.name, table_name);
|
|
122
|
-
url = "".concat(db.api).concat(CONTRACT_PAYLOAD, "/").concat(contract_id, "/").concat(db.access_key);
|
|
123
|
-
_b.label = 1;
|
|
124
|
-
case 1:
|
|
125
|
-
_b.trys.push([1, 3, , 4]);
|
|
126
|
-
return [4 /*yield*/, axios.get(url)];
|
|
127
|
-
case 2:
|
|
128
|
-
contract_response = _b.sent();
|
|
129
|
-
contract_data_json = contract_response.data;
|
|
130
|
-
// If there's already a table (contract) with data, then, fetch its data
|
|
131
|
-
if (contract_data_json.tx_type === TransactionType.CONTRACT) {
|
|
132
|
-
table_1 = new Table(contract_data_json.data, contract_id, db);
|
|
133
|
-
return [2 /*return*/, table_1];
|
|
134
|
-
}
|
|
135
|
-
table = new Table(model, contract_id, db);
|
|
136
|
-
return [2 /*return*/, table];
|
|
137
|
-
case 3:
|
|
138
|
-
_a = _b.sent();
|
|
139
|
-
throw new Error('Something went wrong!');
|
|
140
|
-
case 4: return [2 /*return*/];
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
}); };
|
|
301
|
+
export default Table;
|
package/features/types.d.ts
CHANGED
|
@@ -1,29 +1,87 @@
|
|
|
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
|
+
* 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;
|
|
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;
|
|
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;
|
|
21
81
|
}
|
|
22
|
-
export type
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
block_hash: string;
|
|
28
|
-
block_height: number;
|
|
82
|
+
export type DocId<Model> = Model & {
|
|
83
|
+
/**
|
|
84
|
+
* The document ID (unique identifier). Immutable.
|
|
85
|
+
*/
|
|
86
|
+
readonly doc_id: string;
|
|
29
87
|
};
|