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
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## [1.0.0-rc.2] - 2025-03-05
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- New `TableDoc` interface for working with specific documents
|
|
10
|
+
- Added `refetch()` method to `TableDoc` to get the latest document data
|
|
11
|
+
- Added `getTableName()` method to `TableDoc` to get the table name
|
|
12
|
+
- Added `isEmpty()` method to `TableDoc` to check if the document is empty
|
|
13
|
+
- Added `DocId<Model>` type that adds a readonly `doc_id` property to models
|
|
14
|
+
- Added `getCurrentDocId()` method to `Table` to get the current document ID
|
|
15
|
+
- The `persist()` method now returns the created document with its `doc_id`
|
|
16
|
+
- Document IDs are now accessible directly via `doc_id` property in both `Table` and `TableDoc` instances
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- **BREAKING CHANGE**: Renamed `table` property to `currentDoc` in `Table` class for better semantics
|
|
21
|
+
- Improved error messages with more details about the operation and error
|
|
22
|
+
- Enhanced type safety with the `DocId<Model>` type
|
|
23
|
+
|
|
24
|
+
### Removed
|
|
25
|
+
|
|
26
|
+
- **BREAKING CHANGE**: Removed `update()` method from `Table` class
|
|
27
|
+
- To update documents, you must now use `getDoc()` to get a document reference and then call `update()` on that reference
|
|
28
|
+
- This change prevents accidental creation of duplicate records and makes the API more intuitive
|
|
29
|
+
|
|
30
|
+
### Migration Guide
|
|
31
|
+
|
|
32
|
+
#### Property Renaming
|
|
33
|
+
|
|
34
|
+
Before:
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
// Accessing table data
|
|
38
|
+
console.log(greetingTable.table)
|
|
39
|
+
|
|
40
|
+
// Accessing document data from TableDoc
|
|
41
|
+
console.log(specificDoc.table)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
After:
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
// Accessing current document data from Table
|
|
48
|
+
console.log(greetingTable.currentDoc)
|
|
49
|
+
|
|
50
|
+
// Accessing document data from TableDoc
|
|
51
|
+
console.log(specificDoc.doc)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
#### Updating Documents
|
|
55
|
+
|
|
56
|
+
Before:
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
// Update the last item
|
|
60
|
+
greetingTable.table.greeting = 'Updated greeting'
|
|
61
|
+
await greetingTable.update()
|
|
62
|
+
|
|
63
|
+
// Update a specific document by ID
|
|
64
|
+
greetingTable.table.greeting = 'Updated specific document'
|
|
65
|
+
await greetingTable.update('550e8400-e29b-41d4-a716-446655440000')
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
After:
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
// Get a specific document by ID
|
|
72
|
+
const specificDoc = await greetingTable.getDoc('550e8400-e29b-41d4-a716-446655440000')
|
|
73
|
+
|
|
74
|
+
// Update the document
|
|
75
|
+
specificDoc.doc.greeting = 'Updated greeting'
|
|
76
|
+
await specificDoc.update()
|
|
77
|
+
|
|
78
|
+
// Optionally, refetch the document to get the latest data
|
|
79
|
+
await specificDoc.refetch()
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
#### Accessing Document IDs
|
|
83
|
+
|
|
84
|
+
New in this version:
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
// When persisting data, you get the document ID in the result
|
|
88
|
+
const result = await greetingTable.persist()
|
|
89
|
+
console.log(result.doc_id) // The ID of the newly created document
|
|
90
|
+
|
|
91
|
+
// You can also get the current document ID directly from the table
|
|
92
|
+
const currentDocId = greetingTable.getCurrentDocId()
|
|
93
|
+
|
|
94
|
+
// When working with a specific document, the ID is available in multiple ways
|
|
95
|
+
const specificDoc = await greetingTable.getDoc(docId)
|
|
96
|
+
console.log(specificDoc.doc_id) // From the TableDoc instance
|
|
97
|
+
console.log(specificDoc.doc.doc_id) // From the document object
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## [1.0.0-rc.1] - 2025-03-01
|
|
101
|
+
|
|
102
|
+
Initial release candidate.
|
|
103
|
+
|
|
104
|
+
### Added
|
|
105
|
+
|
|
106
|
+
- Type-safe API with TypeScript generics
|
|
107
|
+
- Promise-based API with async/await
|
|
108
|
+
- WebSocket support for real-time updates
|
|
109
|
+
- Advanced query capabilities
|
|
110
|
+
- Complete history access
|
|
@@ -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,11 @@
|
|
|
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
|
|
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_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 GET_DOC: (table: string, doc_id: string) => string;
|
|
11
|
+
export declare const WEB_SOCKET_EVENTS: string;
|