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.
@@ -0,0 +1,3 @@
1
+ export { ChainDB, connect } from './features/chain-db';
2
+ export { Access } from './features/access';
3
+ export { BasicResponse, SignedUserAccount } from './features/types';
@@ -0,0 +1,19 @@
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
+ exports.__esModule = true;
14
+ exports.Access = exports.connect = exports.ChainDB = void 0;
15
+ var chain_db_1 = require("./features/chain-db");
16
+ __createBinding(exports, chain_db_1, "ChainDB");
17
+ __createBinding(exports, chain_db_1, "connect");
18
+ var access_1 = require("./features/access");
19
+ __createBinding(exports, access_1, "Access");
@@ -0,0 +1,6 @@
1
+ export declare class Access {
2
+ user: string;
3
+ password: string;
4
+ constructor(user: string, password: string);
5
+ parse: (data_base: string, table_name: string) => string;
6
+ }
@@ -0,0 +1,16 @@
1
+ import sha256 from 'sha256';
2
+ var Access = /** @class */ (function () {
3
+ function Access(user, password) {
4
+ var _this = this;
5
+ this.user = '';
6
+ this.password = '';
7
+ this.parse = function (data_base, table_name) {
8
+ var access_info = "".concat(data_base).concat(table_name).concat(_this.user).concat(_this.password);
9
+ return sha256(access_info);
10
+ };
11
+ this.user = user;
12
+ this.password = password;
13
+ }
14
+ return Access;
15
+ }());
16
+ export { 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,317 @@
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 sha256 from 'sha256';
38
+ import axios from 'axios';
39
+ import { Access } from './access';
40
+ import { API, CHECK_USER_NAME, CREATE_USER_ACCOUNT, GET_ALL_TRANSFER_BY_USER_ID, GET_TRANSFER_BY_USER_ID, GET_USER_ACCOUNT, GET_USER_ACCOUNT_BY_ID, TRANSFER_UNITS, } from './constants';
41
+ import { post } from './utils';
42
+ import * as table from './table';
43
+ var ChainDB = /** @class */ (function () {
44
+ function ChainDB() {
45
+ this.api = '';
46
+ this.name = '';
47
+ this.access = null;
48
+ this.access_key = '';
49
+ }
50
+ /**
51
+ * Connection information.
52
+ * @param server Server location. If the `server` parameter is empty, then "http://localhost" will be used.
53
+ * @param data_base Data base name
54
+ * @param user User to access the data base
55
+ * @param password Password to access the data base
56
+ */
57
+ ChainDB.prototype.connect = function (server, data_base, user, password) {
58
+ var key_data = "".concat(data_base).concat(user).concat(password);
59
+ var key = sha256(key_data);
60
+ this.api = server || API;
61
+ this.name = data_base;
62
+ this.access = new Access(user, password);
63
+ this.access_key = key;
64
+ };
65
+ /**
66
+ * Create a new user account inside the connected table
67
+ * @param user_name
68
+ * @param password
69
+ * @param units
70
+ * @param password_hint
71
+ * @returns
72
+ */
73
+ ChainDB.prototype.create_user_account = function (user_name, password, units, password_hint) {
74
+ return __awaiter(this, void 0, void 0, function () {
75
+ var url, body, response, _a;
76
+ return __generator(this, function (_b) {
77
+ switch (_b.label) {
78
+ case 0:
79
+ url = "".concat(this.api).concat(CREATE_USER_ACCOUNT);
80
+ body = {
81
+ db_access_key: this.access_key,
82
+ user_name: user_name,
83
+ password: password,
84
+ password_hint: password_hint,
85
+ units: units
86
+ };
87
+ _b.label = 1;
88
+ case 1:
89
+ _b.trys.push([1, 4, , 5]);
90
+ return [4 /*yield*/, post(url, body)];
91
+ case 2:
92
+ response = _b.sent();
93
+ return [4 /*yield*/, response.data];
94
+ case 3: return [2 /*return*/, _b.sent()];
95
+ case 4:
96
+ _a = _b.sent();
97
+ throw new Error('Something went wrong!');
98
+ case 5: return [2 /*return*/];
99
+ }
100
+ });
101
+ });
102
+ };
103
+ /**
104
+ * Get user account info (login method)
105
+ * @param user_name
106
+ * @param password
107
+ * @returns
108
+ */
109
+ ChainDB.prototype.get_user_account = function (user_name, password) {
110
+ return __awaiter(this, void 0, void 0, function () {
111
+ var url, response, _a;
112
+ return __generator(this, function (_b) {
113
+ switch (_b.label) {
114
+ case 0:
115
+ url = "".concat(this.api).concat(GET_USER_ACCOUNT, "/").concat(user_name, "/").concat(password, "/").concat(this.access_key);
116
+ _b.label = 1;
117
+ case 1:
118
+ _b.trys.push([1, 4, , 5]);
119
+ return [4 /*yield*/, axios.get(url)];
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 by its id
134
+ * @param user_id
135
+ * @returns
136
+ */
137
+ ChainDB.prototype.get_user_account_by_id = function (user_id) {
138
+ return __awaiter(this, void 0, void 0, function () {
139
+ var url, response, _a;
140
+ return __generator(this, function (_b) {
141
+ switch (_b.label) {
142
+ case 0:
143
+ url = "".concat(this.api).concat(GET_USER_ACCOUNT_BY_ID, "/").concat(user_id, "/").concat(this.access_key);
144
+ _b.label = 1;
145
+ case 1:
146
+ _b.trys.push([1, 4, , 5]);
147
+ return [4 /*yield*/, axios.get(url)];
148
+ case 2:
149
+ response = _b.sent();
150
+ return [4 /*yield*/, response.data];
151
+ case 3: return [2 /*return*/, _b.sent()];
152
+ case 4:
153
+ _a = _b.sent();
154
+ throw new Error('Something went wrong!');
155
+ case 5: return [2 /*return*/];
156
+ }
157
+ });
158
+ });
159
+ };
160
+ /**
161
+ * Check if user_name is already taken
162
+ * @param user_name
163
+ */
164
+ ChainDB.prototype.check_user_name = function (user_name) {
165
+ return __awaiter(this, void 0, void 0, function () {
166
+ var url, response, _a;
167
+ return __generator(this, function (_b) {
168
+ switch (_b.label) {
169
+ case 0:
170
+ url = "".concat(this.api).concat(CHECK_USER_NAME, "/").concat(user_name, "/").concat(this.access_key);
171
+ _b.label = 1;
172
+ case 1:
173
+ _b.trys.push([1, 4, , 5]);
174
+ return [4 /*yield*/, axios.get(url)];
175
+ case 2:
176
+ response = _b.sent();
177
+ return [4 /*yield*/, response.data];
178
+ case 3: return [2 /*return*/, _b.sent()];
179
+ case 4:
180
+ _a = _b.sent();
181
+ throw new Error('Something went wrong!');
182
+ case 5: return [2 /*return*/];
183
+ }
184
+ });
185
+ });
186
+ };
187
+ /**
188
+ * Transfer units between users
189
+ * @param from user_id
190
+ * @param to user_id
191
+ * @param units
192
+ * @returns
193
+ */
194
+ ChainDB.prototype.transfer_units = function (from, to, units) {
195
+ return __awaiter(this, void 0, void 0, function () {
196
+ var url, body, response, _a;
197
+ return __generator(this, function (_b) {
198
+ switch (_b.label) {
199
+ case 0:
200
+ url = "".concat(this.api).concat(TRANSFER_UNITS);
201
+ body = {
202
+ db_access_key: this.access_key,
203
+ from: from,
204
+ to: to,
205
+ units: units
206
+ };
207
+ _b.label = 1;
208
+ case 1:
209
+ _b.trys.push([1, 4, , 5]);
210
+ return [4 /*yield*/, post(url, body)];
211
+ case 2:
212
+ response = _b.sent();
213
+ return [4 /*yield*/, response.data];
214
+ case 3: return [2 /*return*/, _b.sent()];
215
+ case 4:
216
+ _a = _b.sent();
217
+ throw new Error('Something went wrong!');
218
+ case 5: return [2 /*return*/];
219
+ }
220
+ });
221
+ });
222
+ };
223
+ /**
224
+ * Fetch the last Transference of units Records by User
225
+ * @param user_id
226
+ * @returns
227
+ */
228
+ ChainDB.prototype.get_transfer_by_user_id = function (user_id) {
229
+ return __awaiter(this, void 0, void 0, function () {
230
+ var url, response, _a;
231
+ return __generator(this, function (_b) {
232
+ switch (_b.label) {
233
+ case 0:
234
+ url = "".concat(this.api).concat(GET_TRANSFER_BY_USER_ID, "/").concat(user_id, "/").concat(this.access_key);
235
+ _b.label = 1;
236
+ case 1:
237
+ _b.trys.push([1, 4, , 5]);
238
+ return [4 /*yield*/, axios.get(url)];
239
+ case 2:
240
+ response = _b.sent();
241
+ return [4 /*yield*/, response.data];
242
+ case 3: return [2 /*return*/, _b.sent()];
243
+ case 4:
244
+ _a = _b.sent();
245
+ throw new Error('Something went wrong!');
246
+ case 5: return [2 /*return*/];
247
+ }
248
+ });
249
+ });
250
+ };
251
+ /**
252
+ * Fetch all Transference of units Records by User
253
+ * @param user_id
254
+ * @returns
255
+ */
256
+ ChainDB.prototype.get_all_transfers_by_user_id = function (user_id) {
257
+ return __awaiter(this, void 0, void 0, function () {
258
+ var url, response, _a;
259
+ return __generator(this, function (_b) {
260
+ switch (_b.label) {
261
+ case 0:
262
+ url = "".concat(this.api).concat(GET_ALL_TRANSFER_BY_USER_ID, "/").concat(user_id, "/").concat(this.access_key);
263
+ _b.label = 1;
264
+ case 1:
265
+ _b.trys.push([1, 4, , 5]);
266
+ return [4 /*yield*/, axios.get(url)];
267
+ case 2:
268
+ response = _b.sent();
269
+ return [4 /*yield*/, response.data];
270
+ case 3: return [2 /*return*/, _b.sent()];
271
+ case 4:
272
+ _a = _b.sent();
273
+ throw new Error('Something went wrong!');
274
+ case 5: return [2 /*return*/];
275
+ }
276
+ });
277
+ });
278
+ };
279
+ /**
280
+ * Initialize a table, fetching its more updated data
281
+ */
282
+ ChainDB.prototype.get_table = function (table_name, model) {
283
+ return __awaiter(this, void 0, void 0, function () {
284
+ var chainDbCopy, table_data;
285
+ return __generator(this, function (_a) {
286
+ switch (_a.label) {
287
+ case 0:
288
+ chainDbCopy = connect(this.api, this.name, this.access.user, this.access.password);
289
+ return [4 /*yield*/, table.get(chainDbCopy, table_name, model)
290
+ // NOTE: Although only the "table" and "persist" properties are displayed by
291
+ // the lint, all Table properties are being exposed.
292
+ // This is due to a javascript limitation on classes.
293
+ //
294
+ // There was an attempt to return a new object with only the required
295
+ // data, but this generates an error in the "this" instance of the Table.
296
+ ];
297
+ case 1:
298
+ table_data = _a.sent();
299
+ // NOTE: Although only the "table" and "persist" properties are displayed by
300
+ // the lint, all Table properties are being exposed.
301
+ // This is due to a javascript limitation on classes.
302
+ //
303
+ // There was an attempt to return a new object with only the required
304
+ // data, but this generates an error in the "this" instance of the Table.
305
+ return [2 /*return*/, table_data];
306
+ }
307
+ });
308
+ });
309
+ };
310
+ return ChainDB;
311
+ }());
312
+ export { ChainDB };
313
+ export var connect = function (server, data_base, user, password) {
314
+ var chainDb = new ChainDB();
315
+ chainDb.connect(server, data_base, user, password);
316
+ return chainDb;
317
+ };
@@ -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,11 @@
1
+ // Contants
2
+ export var API = 'http://localhost:2818';
3
+ export var CONTRACT_PAYLOAD = '/get_last_contract_transaction';
4
+ export var CONTRACT_TRANSACTION = '/post_contract_transaction';
5
+ export var CREATE_USER_ACCOUNT = '/create_user_account';
6
+ export var GET_USER_ACCOUNT = '/get_user_account';
7
+ export var GET_USER_ACCOUNT_BY_ID = '/get_user_account_by_id';
8
+ export var TRANSFER_UNITS = '/transfer_units';
9
+ export var GET_TRANSFER_BY_USER_ID = '/get_transfer_by_user_id';
10
+ export var GET_ALL_TRANSFER_BY_USER_ID = '/get_all_transfers_by_user_id';
11
+ export var 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,109 @@
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 { CONTRACT_PAYLOAD, CONTRACT_TRANSACTION } from './constants';
39
+ import { TransactionType } from './types';
40
+ import { post } from './utils';
41
+ var Table = /** @class */ (function () {
42
+ function Table(table, contract_id, db) {
43
+ this.contract_id = '';
44
+ this.table = table;
45
+ this.contract_id = contract_id;
46
+ this.db = db;
47
+ }
48
+ /**
49
+ * Persist table data on chain
50
+ */
51
+ Table.prototype.persist = function () {
52
+ return __awaiter(this, void 0, void 0, function () {
53
+ var url, contract_data, body, _a;
54
+ return __generator(this, function (_b) {
55
+ switch (_b.label) {
56
+ case 0:
57
+ url = "".concat(this.db.api).concat(CONTRACT_TRANSACTION);
58
+ contract_data = JSON.stringify(this.table);
59
+ body = {
60
+ tx_type: TransactionType.CONTRACT,
61
+ contract_id: this.contract_id,
62
+ db_access_key: this.db.access_key,
63
+ data: contract_data
64
+ };
65
+ _b.label = 1;
66
+ case 1:
67
+ _b.trys.push([1, 3, , 4]);
68
+ return [4 /*yield*/, post(url, body)];
69
+ case 2:
70
+ _b.sent();
71
+ return [3 /*break*/, 4];
72
+ case 3:
73
+ _a = _b.sent();
74
+ throw new Error('Something went wrong!');
75
+ case 4: return [2 /*return*/];
76
+ }
77
+ });
78
+ });
79
+ };
80
+ return Table;
81
+ }());
82
+ export var get = function (db, table_name, model) { return __awaiter(void 0, void 0, void 0, function () {
83
+ var contract_id, url, contract_response, contract_data_json, table_1, table, _a;
84
+ return __generator(this, function (_b) {
85
+ switch (_b.label) {
86
+ case 0:
87
+ contract_id = db.access.parse(db.name, table_name);
88
+ url = "".concat(db.api).concat(CONTRACT_PAYLOAD, "/").concat(contract_id, "/").concat(db.access_key);
89
+ _b.label = 1;
90
+ case 1:
91
+ _b.trys.push([1, 3, , 4]);
92
+ return [4 /*yield*/, axios.get(url)];
93
+ case 2:
94
+ contract_response = _b.sent();
95
+ contract_data_json = contract_response.data;
96
+ // If there's already a table (contract) with data, then, fetch its data
97
+ if (contract_data_json.tx_type === TransactionType.CONTRACT) {
98
+ table_1 = new Table(contract_data_json.data, contract_id, db);
99
+ return [2 /*return*/, table_1];
100
+ }
101
+ table = new Table(model, contract_id, db);
102
+ return [2 /*return*/, table];
103
+ case 3:
104
+ _a = _b.sent();
105
+ throw new Error('Something went wrong!');
106
+ case 4: return [2 /*return*/];
107
+ }
108
+ });
109
+ }); };
@@ -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,7 @@
1
+ export var TransactionType;
2
+ (function (TransactionType) {
3
+ TransactionType["NONE"] = "NONE";
4
+ TransactionType["ACCOUNT"] = "ACCOUNT";
5
+ TransactionType["CONTRACT"] = "CONTRACT";
6
+ TransactionType["TRANSFER"] = "TRANSFER";
7
+ })(TransactionType || (TransactionType = {}));
@@ -0,0 +1 @@
1
+ export declare const post: (url: string, body: any) => Promise<import("axios").AxiosResponse<any, any>>;
@@ -0,0 +1,4 @@
1
+ import axios from 'axios';
2
+ export var post = function (url, body) {
3
+ return axios.post(url, body, { headers: { 'content-type': 'application/json' } });
4
+ };
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { ChainDB, connect } from './features/chain-db';
2
+ export { Access } from './features/access';
3
+ export { BasicResponse, SignedUserAccount } from './features/types';
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { ChainDB, connect } from './features/chain-db';
2
+ export { Access } from './features/access';
package/nodemon.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "watch": ["src"],
3
+ "ext": ".ts,.js",
4
+ "ignore": [],
5
+ "exec": "npx ts-node ./src/index.ts"
6
+ }