chain-db-ts 0.0.2 → 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 +23 -64
- package/dist/cjs/features/chain-db.js +100 -263
- package/dist/cjs/features/constants.d.ts +10 -11
- package/dist/cjs/features/constants.js +20 -12
- package/dist/cjs/features/events.d.ts +28 -0
- package/dist/cjs/features/events.js +89 -0
- package/dist/cjs/features/table.d.ts +74 -7
- package/dist/cjs/features/table.js +220 -69
- package/dist/cjs/features/types.d.ts +46 -22
- 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 +23 -64
- package/features/chain-db.js +101 -241
- package/features/constants.d.ts +10 -11
- package/features/constants.js +13 -11
- package/features/events.d.ts +28 -0
- package/features/events.js +84 -0
- package/features/table.d.ts +74 -7
- package/features/table.js +221 -68
- package/features/types.d.ts +46 -22
- 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/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
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import Table from './table';
|
|
2
|
+
import { Connection, EventCallback } from './types';
|
|
3
3
|
export declare class ChainDB {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
server: string;
|
|
5
|
+
database: string;
|
|
6
|
+
auth: string;
|
|
7
|
+
private _events;
|
|
8
8
|
/**
|
|
9
9
|
* Connection information.
|
|
10
10
|
* @param server Server location. If the `server` parameter is empty, then "http://localhost" will be used.
|
|
@@ -12,69 +12,28 @@ export declare class ChainDB {
|
|
|
12
12
|
* @param user User to access the data base
|
|
13
13
|
* @param password Password to access the data base
|
|
14
14
|
*/
|
|
15
|
-
connect(
|
|
16
|
-
/**
|
|
17
|
-
* Create a new user account inside the connected table
|
|
18
|
-
* @param user_name
|
|
19
|
-
* @param password
|
|
20
|
-
* @param units
|
|
21
|
-
* @param password_hint
|
|
22
|
-
* @returns
|
|
23
|
-
*/
|
|
24
|
-
create_user_account(user_name: string, password: string, units?: number, password_hint?: string): Promise<BasicResponse<SignedUserAccount>>;
|
|
25
|
-
/**
|
|
26
|
-
* Get user account info (login method)
|
|
27
|
-
* @param user_name
|
|
28
|
-
* @param password
|
|
29
|
-
* @returns
|
|
30
|
-
*/
|
|
31
|
-
get_user_account(user_name: string, password: string): Promise<BasicResponse<SignedUserAccount>>;
|
|
32
|
-
/**
|
|
33
|
-
* Get user account info by its id
|
|
34
|
-
* @param user_id
|
|
35
|
-
* @returns
|
|
36
|
-
*/
|
|
37
|
-
get_user_account_by_id(user_id: string): Promise<BasicResponse<SignedUserAccount>>;
|
|
38
|
-
/**
|
|
39
|
-
* Check if user_name is already taken
|
|
40
|
-
* @param user_name
|
|
41
|
-
*/
|
|
42
|
-
check_user_name(user_name: string): Promise<BasicResponse<string>>;
|
|
43
|
-
/**
|
|
44
|
-
* Transfer units between users
|
|
45
|
-
* @param from user_id
|
|
46
|
-
* @param to user_id
|
|
47
|
-
* @param units
|
|
48
|
-
* @returns
|
|
49
|
-
*/
|
|
50
|
-
transfer_units(from: string, to: string, units: number): Promise<BasicResponse<null>>;
|
|
51
|
-
/**
|
|
52
|
-
* Fetch the last Transference of units Records by User
|
|
53
|
-
* @param user_id
|
|
54
|
-
* @returns
|
|
55
|
-
*/
|
|
56
|
-
get_transfer_by_user_id(user_id: string): Promise<BasicResponse<TransferUnitsRegistry>>;
|
|
57
|
-
/**
|
|
58
|
-
* Fetch all Transference of units Records by User
|
|
59
|
-
* @param user_id
|
|
60
|
-
* @returns
|
|
61
|
-
*/
|
|
62
|
-
get_all_transfers_by_user_id(user_id: string): Promise<BasicResponse<TransferUnitsRegistry[]>>;
|
|
15
|
+
connect(connection: Connection): Promise<void>;
|
|
63
16
|
/**
|
|
64
17
|
* Initialize a table, fetching its more updated data
|
|
65
18
|
*/
|
|
66
|
-
|
|
67
|
-
|
|
19
|
+
getTable<Model>(table_name: string): Promise<Table<Model>>;
|
|
20
|
+
events(): {
|
|
21
|
+
/**
|
|
22
|
+
* Subscribe to an event
|
|
23
|
+
* @param event Event name to subscribe to @see {EventTypes}
|
|
24
|
+
* @param callback Function to call when the event is received
|
|
25
|
+
*/
|
|
26
|
+
subscribe: (event: string, callback: EventCallback) => void;
|
|
68
27
|
/**
|
|
69
|
-
*
|
|
28
|
+
* Unsubscribe from an event
|
|
29
|
+
* @param event Event name to unsubscribe from @see {EventTypes}
|
|
30
|
+
* @param callback Optional callback to remove. If not provided, all callbacks for the event will be removed.
|
|
70
31
|
*/
|
|
71
|
-
|
|
32
|
+
unsubscribe: (event: string, callback?: EventCallback) => void;
|
|
72
33
|
/**
|
|
73
|
-
*
|
|
74
|
-
* in a range of depth
|
|
75
|
-
* @param depth
|
|
34
|
+
* Close the events transmission
|
|
76
35
|
*/
|
|
77
|
-
|
|
78
|
-
}
|
|
36
|
+
closeEvents: () => void;
|
|
37
|
+
};
|
|
79
38
|
}
|
|
80
|
-
export declare const connect: (
|
|
39
|
+
export declare const connect: (connection: Connection) => Promise<ChainDB>;
|
|
@@ -1,27 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -63,18 +40,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
63
40
|
};
|
|
64
41
|
exports.__esModule = true;
|
|
65
42
|
exports.connect = exports.ChainDB = void 0;
|
|
66
|
-
var sha256_1 = __importDefault(require("sha256"));
|
|
67
|
-
var axios_1 = __importDefault(require("axios"));
|
|
68
|
-
var access_1 = require("./access");
|
|
69
43
|
var constants_1 = require("./constants");
|
|
70
44
|
var utils_1 = require("./utils");
|
|
71
|
-
var
|
|
45
|
+
var table_1 = __importDefault(require("./table"));
|
|
46
|
+
var events_1 = __importDefault(require("./events"));
|
|
72
47
|
var ChainDB = /** @class */ (function () {
|
|
73
48
|
function ChainDB() {
|
|
74
|
-
this.
|
|
75
|
-
this.
|
|
76
|
-
this.
|
|
77
|
-
this.
|
|
49
|
+
this.server = constants_1.DEFAULT_API_SERVER;
|
|
50
|
+
this.database = '';
|
|
51
|
+
this.auth = '';
|
|
52
|
+
this._events = null;
|
|
78
53
|
}
|
|
79
54
|
/**
|
|
80
55
|
* Connection information.
|
|
@@ -83,224 +58,34 @@ var ChainDB = /** @class */ (function () {
|
|
|
83
58
|
* @param user User to access the data base
|
|
84
59
|
* @param password Password to access the data base
|
|
85
60
|
*/
|
|
86
|
-
ChainDB.prototype.connect = function (
|
|
87
|
-
var key_data = "".concat(data_base).concat(user).concat(password);
|
|
88
|
-
var key = (0, sha256_1["default"])(key_data);
|
|
89
|
-
this.api = server || constants_1.API;
|
|
90
|
-
this.name = data_base;
|
|
91
|
-
this.access = new access_1.Access(user, password);
|
|
92
|
-
this.access_key = key;
|
|
93
|
-
};
|
|
94
|
-
/**
|
|
95
|
-
* Create a new user account inside the connected table
|
|
96
|
-
* @param user_name
|
|
97
|
-
* @param password
|
|
98
|
-
* @param units
|
|
99
|
-
* @param password_hint
|
|
100
|
-
* @returns
|
|
101
|
-
*/
|
|
102
|
-
ChainDB.prototype.create_user_account = function (user_name, password, units, password_hint) {
|
|
103
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
104
|
-
var url, body, response, _a;
|
|
105
|
-
return __generator(this, function (_b) {
|
|
106
|
-
switch (_b.label) {
|
|
107
|
-
case 0:
|
|
108
|
-
url = "".concat(this.api).concat(constants_1.CREATE_USER_ACCOUNT);
|
|
109
|
-
body = {
|
|
110
|
-
db_access_key: this.access_key,
|
|
111
|
-
user_name: user_name,
|
|
112
|
-
password: password,
|
|
113
|
-
password_hint: password_hint,
|
|
114
|
-
units: units
|
|
115
|
-
};
|
|
116
|
-
_b.label = 1;
|
|
117
|
-
case 1:
|
|
118
|
-
_b.trys.push([1, 4, , 5]);
|
|
119
|
-
return [4 /*yield*/, (0, utils_1.post)(url, body)];
|
|
120
|
-
case 2:
|
|
121
|
-
response = _b.sent();
|
|
122
|
-
return [4 /*yield*/, response.data];
|
|
123
|
-
case 3: return [2 /*return*/, _b.sent()];
|
|
124
|
-
case 4:
|
|
125
|
-
_a = _b.sent();
|
|
126
|
-
throw new Error('Something went wrong!');
|
|
127
|
-
case 5: return [2 /*return*/];
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
};
|
|
132
|
-
/**
|
|
133
|
-
* Get user account info (login method)
|
|
134
|
-
* @param user_name
|
|
135
|
-
* @param password
|
|
136
|
-
* @returns
|
|
137
|
-
*/
|
|
138
|
-
ChainDB.prototype.get_user_account = function (user_name, password) {
|
|
139
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
140
|
-
var url, response, _a;
|
|
141
|
-
return __generator(this, function (_b) {
|
|
142
|
-
switch (_b.label) {
|
|
143
|
-
case 0:
|
|
144
|
-
url = "".concat(this.api).concat(constants_1.GET_USER_ACCOUNT, "/").concat(user_name, "/").concat(password, "/").concat(this.access_key);
|
|
145
|
-
_b.label = 1;
|
|
146
|
-
case 1:
|
|
147
|
-
_b.trys.push([1, 4, , 5]);
|
|
148
|
-
return [4 /*yield*/, axios_1["default"].get(url)];
|
|
149
|
-
case 2:
|
|
150
|
-
response = _b.sent();
|
|
151
|
-
return [4 /*yield*/, response.data];
|
|
152
|
-
case 3: return [2 /*return*/, _b.sent()];
|
|
153
|
-
case 4:
|
|
154
|
-
_a = _b.sent();
|
|
155
|
-
throw new Error('Something went wrong!');
|
|
156
|
-
case 5: return [2 /*return*/];
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
};
|
|
161
|
-
/**
|
|
162
|
-
* Get user account info by its id
|
|
163
|
-
* @param user_id
|
|
164
|
-
* @returns
|
|
165
|
-
*/
|
|
166
|
-
ChainDB.prototype.get_user_account_by_id = function (user_id) {
|
|
61
|
+
ChainDB.prototype.connect = function (connection) {
|
|
167
62
|
return __awaiter(this, void 0, void 0, function () {
|
|
168
|
-
var
|
|
169
|
-
return __generator(this, function (
|
|
170
|
-
switch (
|
|
171
|
-
case 0:
|
|
172
|
-
url = "".concat(this.api).concat(constants_1.GET_USER_ACCOUNT_BY_ID, "/").concat(user_id, "/").concat(this.access_key);
|
|
173
|
-
_b.label = 1;
|
|
174
|
-
case 1:
|
|
175
|
-
_b.trys.push([1, 4, , 5]);
|
|
176
|
-
return [4 /*yield*/, axios_1["default"].get(url)];
|
|
177
|
-
case 2:
|
|
178
|
-
response = _b.sent();
|
|
179
|
-
return [4 /*yield*/, response.data];
|
|
180
|
-
case 3: return [2 /*return*/, _b.sent()];
|
|
181
|
-
case 4:
|
|
182
|
-
_a = _b.sent();
|
|
183
|
-
throw new Error('Something went wrong!');
|
|
184
|
-
case 5: return [2 /*return*/];
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
});
|
|
188
|
-
};
|
|
189
|
-
/**
|
|
190
|
-
* Check if user_name is already taken
|
|
191
|
-
* @param user_name
|
|
192
|
-
*/
|
|
193
|
-
ChainDB.prototype.check_user_name = function (user_name) {
|
|
194
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
195
|
-
var url, response, _a;
|
|
196
|
-
return __generator(this, function (_b) {
|
|
197
|
-
switch (_b.label) {
|
|
198
|
-
case 0:
|
|
199
|
-
url = "".concat(this.api).concat(constants_1.CHECK_USER_NAME, "/").concat(user_name, "/").concat(this.access_key);
|
|
200
|
-
_b.label = 1;
|
|
201
|
-
case 1:
|
|
202
|
-
_b.trys.push([1, 4, , 5]);
|
|
203
|
-
return [4 /*yield*/, axios_1["default"].get(url)];
|
|
204
|
-
case 2:
|
|
205
|
-
response = _b.sent();
|
|
206
|
-
return [4 /*yield*/, response.data];
|
|
207
|
-
case 3: return [2 /*return*/, _b.sent()];
|
|
208
|
-
case 4:
|
|
209
|
-
_a = _b.sent();
|
|
210
|
-
throw new Error('Something went wrong!');
|
|
211
|
-
case 5: return [2 /*return*/];
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
});
|
|
215
|
-
};
|
|
216
|
-
/**
|
|
217
|
-
* Transfer units between users
|
|
218
|
-
* @param from user_id
|
|
219
|
-
* @param to user_id
|
|
220
|
-
* @param units
|
|
221
|
-
* @returns
|
|
222
|
-
*/
|
|
223
|
-
ChainDB.prototype.transfer_units = function (from, to, units) {
|
|
224
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
225
|
-
var url, body, response, _a;
|
|
226
|
-
return __generator(this, function (_b) {
|
|
227
|
-
switch (_b.label) {
|
|
228
|
-
case 0:
|
|
229
|
-
url = "".concat(this.api).concat(constants_1.TRANSFER_UNITS);
|
|
230
|
-
body = {
|
|
231
|
-
db_access_key: this.access_key,
|
|
232
|
-
from: from,
|
|
233
|
-
to: to,
|
|
234
|
-
units: units
|
|
235
|
-
};
|
|
236
|
-
_b.label = 1;
|
|
237
|
-
case 1:
|
|
238
|
-
_b.trys.push([1, 4, , 5]);
|
|
239
|
-
return [4 /*yield*/, (0, utils_1.post)(url, body)];
|
|
240
|
-
case 2:
|
|
241
|
-
response = _b.sent();
|
|
242
|
-
return [4 /*yield*/, response.data];
|
|
243
|
-
case 3: return [2 /*return*/, _b.sent()];
|
|
244
|
-
case 4:
|
|
245
|
-
_a = _b.sent();
|
|
246
|
-
throw new Error('Something went wrong!');
|
|
247
|
-
case 5: return [2 /*return*/];
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
});
|
|
251
|
-
};
|
|
252
|
-
/**
|
|
253
|
-
* Fetch the last Transference of units Records by User
|
|
254
|
-
* @param user_id
|
|
255
|
-
* @returns
|
|
256
|
-
*/
|
|
257
|
-
ChainDB.prototype.get_transfer_by_user_id = function (user_id) {
|
|
258
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
259
|
-
var url, response, _a;
|
|
260
|
-
return __generator(this, function (_b) {
|
|
261
|
-
switch (_b.label) {
|
|
262
|
-
case 0:
|
|
263
|
-
url = "".concat(this.api).concat(constants_1.GET_TRANSFER_BY_USER_ID, "/").concat(user_id, "/").concat(this.access_key);
|
|
264
|
-
_b.label = 1;
|
|
265
|
-
case 1:
|
|
266
|
-
_b.trys.push([1, 4, , 5]);
|
|
267
|
-
return [4 /*yield*/, axios_1["default"].get(url)];
|
|
268
|
-
case 2:
|
|
269
|
-
response = _b.sent();
|
|
270
|
-
return [4 /*yield*/, response.data];
|
|
271
|
-
case 3: return [2 /*return*/, _b.sent()];
|
|
272
|
-
case 4:
|
|
273
|
-
_a = _b.sent();
|
|
274
|
-
throw new Error('Something went wrong!');
|
|
275
|
-
case 5: return [2 /*return*/];
|
|
276
|
-
}
|
|
277
|
-
});
|
|
278
|
-
});
|
|
279
|
-
};
|
|
280
|
-
/**
|
|
281
|
-
* Fetch all Transference of units Records by User
|
|
282
|
-
* @param user_id
|
|
283
|
-
* @returns
|
|
284
|
-
*/
|
|
285
|
-
ChainDB.prototype.get_all_transfers_by_user_id = function (user_id) {
|
|
286
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
287
|
-
var url, response, _a;
|
|
288
|
-
return __generator(this, function (_b) {
|
|
289
|
-
switch (_b.label) {
|
|
63
|
+
var server, database, user, password, response, e_1;
|
|
64
|
+
return __generator(this, function (_a) {
|
|
65
|
+
switch (_a.label) {
|
|
290
66
|
case 0:
|
|
291
|
-
|
|
292
|
-
|
|
67
|
+
server = connection.server, database = connection.database, user = connection.user, password = connection.password;
|
|
68
|
+
this.server = server || constants_1.DEFAULT_API_SERVER;
|
|
69
|
+
this.database = database;
|
|
70
|
+
_a.label = 1;
|
|
293
71
|
case 1:
|
|
294
|
-
|
|
295
|
-
return [4 /*yield*/,
|
|
72
|
+
_a.trys.push([1, 3, , 4]);
|
|
73
|
+
return [4 /*yield*/, (0, utils_1.post)("".concat(this.server).concat(constants_1.CONNECT), {
|
|
74
|
+
name: this.database,
|
|
75
|
+
user: user,
|
|
76
|
+
password: password
|
|
77
|
+
})];
|
|
296
78
|
case 2:
|
|
297
|
-
response =
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
case
|
|
79
|
+
response = _a.sent();
|
|
80
|
+
if (!response.data.success) {
|
|
81
|
+
throw new Error(response.data.message);
|
|
82
|
+
}
|
|
83
|
+
this.auth = response.data.data;
|
|
84
|
+
return [3 /*break*/, 4];
|
|
85
|
+
case 3:
|
|
86
|
+
e_1 = _a.sent();
|
|
87
|
+
throw new Error("Something went wrong! ".concat(e_1.message || String(e_1)));
|
|
88
|
+
case 4: return [2 /*return*/];
|
|
304
89
|
}
|
|
305
90
|
});
|
|
306
91
|
});
|
|
@@ -308,40 +93,92 @@ var ChainDB = /** @class */ (function () {
|
|
|
308
93
|
/**
|
|
309
94
|
* Initialize a table, fetching its more updated data
|
|
310
95
|
*/
|
|
311
|
-
ChainDB.prototype.
|
|
96
|
+
ChainDB.prototype.getTable = function (table_name) {
|
|
312
97
|
return __awaiter(this, void 0, void 0, function () {
|
|
313
|
-
var
|
|
98
|
+
var tableData;
|
|
314
99
|
return __generator(this, function (_a) {
|
|
315
100
|
switch (_a.label) {
|
|
316
101
|
case 0:
|
|
317
|
-
|
|
318
|
-
return [4 /*yield*/,
|
|
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)
|
|
319
108
|
// NOTE: Although only the "table" and "persist" properties are displayed by
|
|
320
109
|
// the lint, all Table properties are being exposed.
|
|
321
110
|
// This is due to a javascript limitation on classes.
|
|
322
111
|
//
|
|
323
112
|
// There was an attempt to return a new object with only the required
|
|
324
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
|
+
// }
|
|
325
127
|
];
|
|
326
|
-
case 1:
|
|
327
|
-
table_data = _a.sent();
|
|
328
|
-
// NOTE: Although only the "table" and "persist" properties are displayed by
|
|
329
|
-
// the lint, all Table properties are being exposed.
|
|
330
|
-
// This is due to a javascript limitation on classes.
|
|
331
|
-
//
|
|
332
|
-
// There was an attempt to return a new object with only the required
|
|
333
|
-
// data, but this generates an error in the "this" instance of the Table.
|
|
334
|
-
return [2 /*return*/, table_data];
|
|
335
128
|
}
|
|
336
129
|
});
|
|
337
130
|
});
|
|
338
131
|
};
|
|
132
|
+
ChainDB.prototype.events = function () {
|
|
133
|
+
var _this = this;
|
|
134
|
+
return {
|
|
135
|
+
/**
|
|
136
|
+
* Subscribe to an event
|
|
137
|
+
* @param event Event name to subscribe to @see {EventTypes}
|
|
138
|
+
* @param callback Function to call when the event is received
|
|
139
|
+
*/
|
|
140
|
+
subscribe: function (event, callback) {
|
|
141
|
+
var _a;
|
|
142
|
+
if (_this._events === null) {
|
|
143
|
+
var wsUrl = "".concat(_this.server.replace('http', 'ws')).concat(constants_1.WEB_SOCKET_EVENTS);
|
|
144
|
+
_this._events = new events_1["default"](wsUrl, _this.auth);
|
|
145
|
+
}
|
|
146
|
+
(_a = _this._events) === null || _a === void 0 ? void 0 : _a.subscribe(event, callback);
|
|
147
|
+
},
|
|
148
|
+
/**
|
|
149
|
+
* Unsubscribe from an event
|
|
150
|
+
* @param event Event name to unsubscribe from @see {EventTypes}
|
|
151
|
+
* @param callback Optional callback to remove. If not provided, all callbacks for the event will be removed.
|
|
152
|
+
*/
|
|
153
|
+
unsubscribe: function (event, callback) {
|
|
154
|
+
if (!_this._events || !_this._events.isConnected()) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
_this._events.unsubscribe(event, callback);
|
|
158
|
+
},
|
|
159
|
+
/**
|
|
160
|
+
* Close the events transmission
|
|
161
|
+
*/
|
|
162
|
+
closeEvents: function () {
|
|
163
|
+
var _a;
|
|
164
|
+
(_a = _this._events) === null || _a === void 0 ? void 0 : _a.close();
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
};
|
|
339
168
|
return ChainDB;
|
|
340
169
|
}());
|
|
341
170
|
exports.ChainDB = ChainDB;
|
|
342
|
-
var connect = function (
|
|
343
|
-
var chainDb
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
171
|
+
var connect = function (connection) { return __awaiter(void 0, void 0, void 0, function () {
|
|
172
|
+
var chainDb;
|
|
173
|
+
return __generator(this, function (_a) {
|
|
174
|
+
switch (_a.label) {
|
|
175
|
+
case 0:
|
|
176
|
+
chainDb = new ChainDB();
|
|
177
|
+
return [4 /*yield*/, chainDb.connect(connection)];
|
|
178
|
+
case 1:
|
|
179
|
+
_a.sent();
|
|
180
|
+
return [2 /*return*/, chainDb];
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}); };
|
|
347
184
|
exports.connect = connect;
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
export declare const
|
|
2
|
-
export declare const
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
5
|
-
export declare const
|
|
6
|
-
export declare const
|
|
7
|
-
export declare const
|
|
8
|
-
export declare const
|
|
9
|
-
export declare const
|
|
10
|
-
export declare const
|
|
11
|
-
export declare const CHECK_USER_NAME = "/check_user_name";
|
|
1
|
+
export declare const DEFAULT_API_SERVER = "http://localhost:2818";
|
|
2
|
+
export declare const API_BASE = "/api/v1";
|
|
3
|
+
export declare const CONNECT: string;
|
|
4
|
+
export declare const GET_TABLE: (table: string) => string;
|
|
5
|
+
export declare const UPDATE_LAST_ITEM: (table: string) => string;
|
|
6
|
+
export declare const PERSIST_NEW_DATA: (table: string) => string;
|
|
7
|
+
export declare const GET_HISTORY: (table: string, limit?: number) => string;
|
|
8
|
+
export declare const FIND_WHERE_BASIC: (table: string) => string;
|
|
9
|
+
export declare const FIND_WHERE_ADVANCED: (table: string) => string;
|
|
10
|
+
export declare const WEB_SOCKET_EVENTS: string;
|
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
exports.__esModule = true;
|
|
3
|
-
exports.
|
|
3
|
+
exports.WEB_SOCKET_EVENTS = exports.FIND_WHERE_ADVANCED = exports.FIND_WHERE_BASIC = exports.GET_HISTORY = exports.PERSIST_NEW_DATA = exports.UPDATE_LAST_ITEM = exports.GET_TABLE = exports.CONNECT = exports.API_BASE = exports.DEFAULT_API_SERVER = void 0;
|
|
4
4
|
// Contants
|
|
5
|
-
exports.
|
|
6
|
-
exports.
|
|
7
|
-
exports.
|
|
8
|
-
exports.
|
|
9
|
-
exports.
|
|
10
|
-
exports.
|
|
11
|
-
exports.
|
|
12
|
-
exports.
|
|
13
|
-
exports.
|
|
14
|
-
|
|
15
|
-
|
|
5
|
+
exports.DEFAULT_API_SERVER = 'http://localhost:2818';
|
|
6
|
+
exports.API_BASE = '/api/v1';
|
|
7
|
+
exports.CONNECT = "".concat(exports.API_BASE, "/database/connect");
|
|
8
|
+
var GET_TABLE = function (table) { return "".concat(exports.API_BASE, "/table/").concat(table); };
|
|
9
|
+
exports.GET_TABLE = GET_TABLE;
|
|
10
|
+
var UPDATE_LAST_ITEM = function (table) { return "".concat(exports.API_BASE, "/table/").concat(table, "/update"); };
|
|
11
|
+
exports.UPDATE_LAST_ITEM = UPDATE_LAST_ITEM;
|
|
12
|
+
var PERSIST_NEW_DATA = function (table) { return "".concat(exports.API_BASE, "/table/").concat(table, "/persist"); };
|
|
13
|
+
exports.PERSIST_NEW_DATA = PERSIST_NEW_DATA;
|
|
14
|
+
var GET_HISTORY = function (table, limit) {
|
|
15
|
+
if (limit === void 0) { limit = 25; }
|
|
16
|
+
return "".concat(exports.API_BASE, "/table/").concat(table, "/history?limit=").concat(limit);
|
|
17
|
+
};
|
|
18
|
+
exports.GET_HISTORY = GET_HISTORY;
|
|
19
|
+
var FIND_WHERE_BASIC = function (table) { return "".concat(exports.API_BASE, "/table/").concat(table, "/find"); };
|
|
20
|
+
exports.FIND_WHERE_BASIC = FIND_WHERE_BASIC;
|
|
21
|
+
var FIND_WHERE_ADVANCED = function (table) { return "".concat(exports.API_BASE, "/table/").concat(table, "/find-advanced"); };
|
|
22
|
+
exports.FIND_WHERE_ADVANCED = FIND_WHERE_ADVANCED;
|
|
23
|
+
exports.WEB_SOCKET_EVENTS = "".concat(exports.API_BASE, "/events");
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type EventCallback = (data: any) => void;
|
|
2
|
+
declare class Events {
|
|
3
|
+
private socket;
|
|
4
|
+
private eventListeners;
|
|
5
|
+
private connected;
|
|
6
|
+
constructor(url: string, auth: string);
|
|
7
|
+
/**
|
|
8
|
+
* Subscribe to an event
|
|
9
|
+
* @param event Event name to subscribe to
|
|
10
|
+
* @param callback Function to call when the event is received
|
|
11
|
+
*/
|
|
12
|
+
subscribe(event: string, callback: EventCallback): void;
|
|
13
|
+
/**
|
|
14
|
+
* Unsubscribe from an event
|
|
15
|
+
* @param event Event name to unsubscribe from
|
|
16
|
+
* @param callback Optional callback to remove. If not provided, all callbacks for the event will be removed.
|
|
17
|
+
*/
|
|
18
|
+
unsubscribe(event: string, callback?: EventCallback): void;
|
|
19
|
+
/**
|
|
20
|
+
* Close the WebSocket connection
|
|
21
|
+
*/
|
|
22
|
+
close(): void;
|
|
23
|
+
/**
|
|
24
|
+
* Check if the WebSocket connection is established
|
|
25
|
+
*/
|
|
26
|
+
isConnected(): boolean;
|
|
27
|
+
}
|
|
28
|
+
export default Events;
|