chain-db-ts 0.0.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/.prettierrc +7 -0
- package/dist/cjs/features/access.d.ts +6 -0
- package/dist/cjs/features/access.js +22 -0
- package/dist/cjs/features/chain-db.d.ts +74 -0
- package/dist/cjs/features/chain-db.js +347 -0
- package/dist/cjs/features/constants.d.ts +10 -0
- package/dist/cjs/features/constants.js +14 -0
- package/dist/cjs/features/table.d.ts +13 -0
- package/dist/cjs/features/table.js +116 -0
- package/dist/cjs/features/types.d.ts +27 -0
- package/dist/cjs/features/types.js +10 -0
- package/dist/cjs/features/utils.d.ts +1 -0
- package/dist/cjs/features/utils.js +11 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +19 -0
- package/features/access.d.ts +6 -0
- package/features/access.js +16 -0
- package/features/chain-db.d.ts +74 -0
- package/features/chain-db.js +317 -0
- package/features/constants.d.ts +10 -0
- package/features/constants.js +11 -0
- package/features/table.d.ts +13 -0
- package/features/table.js +109 -0
- package/features/types.d.ts +27 -0
- package/features/types.js +7 -0
- package/features/utils.d.ts +1 -0
- package/features/utils.js +4 -0
- package/index.d.ts +3 -0
- package/index.js +2 -0
- package/nodemon.json +6 -0
- package/package.json +40 -0
- package/readme.md +186 -0
- package/tsconfig.json +57 -0
- package/tsconfig.prod.json +6 -0
package/.prettierrc
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
exports.Access = void 0;
|
|
7
|
+
var sha256_1 = __importDefault(require("sha256"));
|
|
8
|
+
var Access = /** @class */ (function () {
|
|
9
|
+
function Access(user, password) {
|
|
10
|
+
var _this = this;
|
|
11
|
+
this.user = '';
|
|
12
|
+
this.password = '';
|
|
13
|
+
this.parse = function (data_base, table_name) {
|
|
14
|
+
var access_info = "".concat(data_base).concat(table_name).concat(_this.user).concat(_this.password);
|
|
15
|
+
return (0, sha256_1["default"])(access_info);
|
|
16
|
+
};
|
|
17
|
+
this.user = user;
|
|
18
|
+
this.password = password;
|
|
19
|
+
}
|
|
20
|
+
return Access;
|
|
21
|
+
}());
|
|
22
|
+
exports.Access = Access;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Access } from './access';
|
|
2
|
+
import { BasicResponse, SignedUserAccount, TransferUnitsRegistry } from './types';
|
|
3
|
+
export declare class ChainDB {
|
|
4
|
+
api: string;
|
|
5
|
+
name: string;
|
|
6
|
+
access: Access | null;
|
|
7
|
+
access_key: string;
|
|
8
|
+
/**
|
|
9
|
+
* Connection information.
|
|
10
|
+
* @param server Server location. If the `server` parameter is empty, then "http://localhost" will be used.
|
|
11
|
+
* @param data_base Data base name
|
|
12
|
+
* @param user User to access the data base
|
|
13
|
+
* @param password Password to access the data base
|
|
14
|
+
*/
|
|
15
|
+
connect(server: string | null, data_base: string, user: string, password: string): void;
|
|
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[]>>;
|
|
63
|
+
/**
|
|
64
|
+
* Initialize a table, fetching its more updated data
|
|
65
|
+
*/
|
|
66
|
+
get_table<Model>(table_name: string, model: Model): Promise<{
|
|
67
|
+
table: Model;
|
|
68
|
+
/**
|
|
69
|
+
* Persist table data on chain
|
|
70
|
+
*/
|
|
71
|
+
persist: () => Promise<void>;
|
|
72
|
+
}>;
|
|
73
|
+
}
|
|
74
|
+
export declare const connect: (server: string | null, data_base: string, user: string, password: string) => ChainDB;
|
|
@@ -0,0 +1,347 @@
|
|
|
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
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
35
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
36
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
37
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
38
|
+
function step(op) {
|
|
39
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
40
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
41
|
+
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;
|
|
42
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
43
|
+
switch (op[0]) {
|
|
44
|
+
case 0: case 1: t = op; break;
|
|
45
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
46
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
47
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
48
|
+
default:
|
|
49
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
50
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
51
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
52
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
53
|
+
if (t[2]) _.ops.pop();
|
|
54
|
+
_.trys.pop(); continue;
|
|
55
|
+
}
|
|
56
|
+
op = body.call(thisArg, _);
|
|
57
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
58
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
62
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
63
|
+
};
|
|
64
|
+
exports.__esModule = true;
|
|
65
|
+
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
|
+
var constants_1 = require("./constants");
|
|
70
|
+
var utils_1 = require("./utils");
|
|
71
|
+
var table = __importStar(require("./table"));
|
|
72
|
+
var ChainDB = /** @class */ (function () {
|
|
73
|
+
function ChainDB() {
|
|
74
|
+
this.api = '';
|
|
75
|
+
this.name = '';
|
|
76
|
+
this.access = null;
|
|
77
|
+
this.access_key = '';
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Connection information.
|
|
81
|
+
* @param server Server location. If the `server` parameter is empty, then "http://localhost" will be used.
|
|
82
|
+
* @param data_base Data base name
|
|
83
|
+
* @param user User to access the data base
|
|
84
|
+
* @param password Password to access the data base
|
|
85
|
+
*/
|
|
86
|
+
ChainDB.prototype.connect = function (server, data_base, user, password) {
|
|
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) {
|
|
167
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
168
|
+
var url, response, _a;
|
|
169
|
+
return __generator(this, function (_b) {
|
|
170
|
+
switch (_b.label) {
|
|
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) {
|
|
290
|
+
case 0:
|
|
291
|
+
url = "".concat(this.api).concat(constants_1.GET_ALL_TRANSFER_BY_USER_ID, "/").concat(user_id, "/").concat(this.access_key);
|
|
292
|
+
_b.label = 1;
|
|
293
|
+
case 1:
|
|
294
|
+
_b.trys.push([1, 4, , 5]);
|
|
295
|
+
return [4 /*yield*/, axios_1["default"].get(url)];
|
|
296
|
+
case 2:
|
|
297
|
+
response = _b.sent();
|
|
298
|
+
return [4 /*yield*/, response.data];
|
|
299
|
+
case 3: return [2 /*return*/, _b.sent()];
|
|
300
|
+
case 4:
|
|
301
|
+
_a = _b.sent();
|
|
302
|
+
throw new Error('Something went wrong!');
|
|
303
|
+
case 5: return [2 /*return*/];
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
};
|
|
308
|
+
/**
|
|
309
|
+
* Initialize a table, fetching its more updated data
|
|
310
|
+
*/
|
|
311
|
+
ChainDB.prototype.get_table = function (table_name, model) {
|
|
312
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
313
|
+
var chainDbCopy, table_data;
|
|
314
|
+
return __generator(this, function (_a) {
|
|
315
|
+
switch (_a.label) {
|
|
316
|
+
case 0:
|
|
317
|
+
chainDbCopy = (0, exports.connect)(this.api, this.name, this.access.user, this.access.password);
|
|
318
|
+
return [4 /*yield*/, table.get(chainDbCopy, table_name, model)
|
|
319
|
+
// NOTE: Although only the "table" and "persist" properties are displayed by
|
|
320
|
+
// the lint, all Table properties are being exposed.
|
|
321
|
+
// This is due to a javascript limitation on classes.
|
|
322
|
+
//
|
|
323
|
+
// There was an attempt to return a new object with only the required
|
|
324
|
+
// data, but this generates an error in the "this" instance of the Table.
|
|
325
|
+
];
|
|
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
|
+
}
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
};
|
|
339
|
+
return ChainDB;
|
|
340
|
+
}());
|
|
341
|
+
exports.ChainDB = ChainDB;
|
|
342
|
+
var connect = function (server, data_base, user, password) {
|
|
343
|
+
var chainDb = new ChainDB();
|
|
344
|
+
chainDb.connect(server, data_base, user, password);
|
|
345
|
+
return chainDb;
|
|
346
|
+
};
|
|
347
|
+
exports.connect = connect;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const API = "http://localhost:2818";
|
|
2
|
+
export declare const CONTRACT_PAYLOAD = "/get_last_contract_transaction";
|
|
3
|
+
export declare const CONTRACT_TRANSACTION = "/post_contract_transaction";
|
|
4
|
+
export declare const CREATE_USER_ACCOUNT = "/create_user_account";
|
|
5
|
+
export declare const GET_USER_ACCOUNT = "/get_user_account";
|
|
6
|
+
export declare const GET_USER_ACCOUNT_BY_ID = "/get_user_account_by_id";
|
|
7
|
+
export declare const TRANSFER_UNITS = "/transfer_units";
|
|
8
|
+
export declare const GET_TRANSFER_BY_USER_ID = "/get_transfer_by_user_id";
|
|
9
|
+
export declare const GET_ALL_TRANSFER_BY_USER_ID = "/get_all_transfers_by_user_id";
|
|
10
|
+
export declare const CHECK_USER_NAME = "/check_user_name";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.CHECK_USER_NAME = exports.GET_ALL_TRANSFER_BY_USER_ID = exports.GET_TRANSFER_BY_USER_ID = exports.TRANSFER_UNITS = exports.GET_USER_ACCOUNT_BY_ID = exports.GET_USER_ACCOUNT = exports.CREATE_USER_ACCOUNT = exports.CONTRACT_TRANSACTION = exports.CONTRACT_PAYLOAD = exports.API = void 0;
|
|
4
|
+
// Contants
|
|
5
|
+
exports.API = 'http://localhost:2818';
|
|
6
|
+
exports.CONTRACT_PAYLOAD = '/get_last_contract_transaction';
|
|
7
|
+
exports.CONTRACT_TRANSACTION = '/post_contract_transaction';
|
|
8
|
+
exports.CREATE_USER_ACCOUNT = '/create_user_account';
|
|
9
|
+
exports.GET_USER_ACCOUNT = '/get_user_account';
|
|
10
|
+
exports.GET_USER_ACCOUNT_BY_ID = '/get_user_account_by_id';
|
|
11
|
+
exports.TRANSFER_UNITS = '/transfer_units';
|
|
12
|
+
exports.GET_TRANSFER_BY_USER_ID = '/get_transfer_by_user_id';
|
|
13
|
+
exports.GET_ALL_TRANSFER_BY_USER_ID = '/get_all_transfers_by_user_id';
|
|
14
|
+
exports.CHECK_USER_NAME = '/check_user_name';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ChainDB } from './chain-db';
|
|
2
|
+
declare class Table<Model> {
|
|
3
|
+
table: Model;
|
|
4
|
+
private contract_id;
|
|
5
|
+
private db;
|
|
6
|
+
constructor(table: Model, contract_id: string, db: ChainDB);
|
|
7
|
+
/**
|
|
8
|
+
* Persist table data on chain
|
|
9
|
+
*/
|
|
10
|
+
persist(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export declare const get: <Model>(db: ChainDB, table_name: string, model: Model) => Promise<Table<Model>>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
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;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
exports.__esModule = true;
|
|
42
|
+
exports.get = void 0;
|
|
43
|
+
var axios_1 = __importDefault(require("axios"));
|
|
44
|
+
var constants_1 = require("./constants");
|
|
45
|
+
var types_1 = require("./types");
|
|
46
|
+
var utils_1 = require("./utils");
|
|
47
|
+
var Table = /** @class */ (function () {
|
|
48
|
+
function Table(table, contract_id, db) {
|
|
49
|
+
this.contract_id = '';
|
|
50
|
+
this.table = table;
|
|
51
|
+
this.contract_id = contract_id;
|
|
52
|
+
this.db = db;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Persist table data on chain
|
|
56
|
+
*/
|
|
57
|
+
Table.prototype.persist = function () {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
59
|
+
var url, contract_data, body, _a;
|
|
60
|
+
return __generator(this, function (_b) {
|
|
61
|
+
switch (_b.label) {
|
|
62
|
+
case 0:
|
|
63
|
+
url = "".concat(this.db.api).concat(constants_1.CONTRACT_TRANSACTION);
|
|
64
|
+
contract_data = JSON.stringify(this.table);
|
|
65
|
+
body = {
|
|
66
|
+
tx_type: types_1.TransactionType.CONTRACT,
|
|
67
|
+
contract_id: this.contract_id,
|
|
68
|
+
db_access_key: this.db.access_key,
|
|
69
|
+
data: contract_data
|
|
70
|
+
};
|
|
71
|
+
_b.label = 1;
|
|
72
|
+
case 1:
|
|
73
|
+
_b.trys.push([1, 3, , 4]);
|
|
74
|
+
return [4 /*yield*/, (0, utils_1.post)(url, body)];
|
|
75
|
+
case 2:
|
|
76
|
+
_b.sent();
|
|
77
|
+
return [3 /*break*/, 4];
|
|
78
|
+
case 3:
|
|
79
|
+
_a = _b.sent();
|
|
80
|
+
throw new Error('Something went wrong!');
|
|
81
|
+
case 4: return [2 /*return*/];
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
return Table;
|
|
87
|
+
}());
|
|
88
|
+
var get = function (db, table_name, model) { return __awaiter(void 0, void 0, void 0, function () {
|
|
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;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type BasicResponse<D> = {
|
|
2
|
+
success: boolean;
|
|
3
|
+
error_msg: string;
|
|
4
|
+
data?: D;
|
|
5
|
+
};
|
|
6
|
+
export type SignedUserAccount = {
|
|
7
|
+
id: string;
|
|
8
|
+
user_name: string;
|
|
9
|
+
units: number;
|
|
10
|
+
};
|
|
11
|
+
export type TransferUnitsRegistry = {
|
|
12
|
+
from: string;
|
|
13
|
+
to: string;
|
|
14
|
+
units: number;
|
|
15
|
+
};
|
|
16
|
+
export declare enum TransactionType {
|
|
17
|
+
NONE = "NONE",
|
|
18
|
+
ACCOUNT = "ACCOUNT",
|
|
19
|
+
CONTRACT = "CONTRACT",
|
|
20
|
+
TRANSFER = "TRANSFER"
|
|
21
|
+
}
|
|
22
|
+
export type ContractTransactionData<Model> = {
|
|
23
|
+
tx_type: TransactionType;
|
|
24
|
+
contract_id: string;
|
|
25
|
+
timestamp?: number;
|
|
26
|
+
data: Model;
|
|
27
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.TransactionType = void 0;
|
|
4
|
+
var TransactionType;
|
|
5
|
+
(function (TransactionType) {
|
|
6
|
+
TransactionType["NONE"] = "NONE";
|
|
7
|
+
TransactionType["ACCOUNT"] = "ACCOUNT";
|
|
8
|
+
TransactionType["CONTRACT"] = "CONTRACT";
|
|
9
|
+
TransactionType["TRANSFER"] = "TRANSFER";
|
|
10
|
+
})(TransactionType = exports.TransactionType || (exports.TransactionType = {}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const post: (url: string, body: any) => Promise<import("axios").AxiosResponse<any, any>>;
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
exports.post = void 0;
|
|
7
|
+
var axios_1 = __importDefault(require("axios"));
|
|
8
|
+
var post = function (url, body) {
|
|
9
|
+
return axios_1["default"].post(url, body, { headers: { 'content-type': 'application/json' } });
|
|
10
|
+
};
|
|
11
|
+
exports.post = post;
|