chain-db-ts 1.0.0-rc.2 → 1.0.0-rc.4
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/.nvmrc +1 -0
- package/CHANGELOG.md +6 -0
- package/dist/cjs/features/chain-db.d.ts +8 -2
- package/dist/cjs/features/chain-db.js +40 -35
- package/dist/cjs/features/table-doc.js +5 -7
- package/dist/cjs/features/table.d.ts +49 -19
- package/dist/cjs/features/table.js +131 -65
- package/dist/cjs/features/types.d.ts +5 -0
- package/dist/cjs/features/utils.d.ts +2 -1
- package/dist/cjs/features/utils.js +9 -1
- package/dist/cjs/index.d.ts +2 -1
- package/dist/cjs/index.js +4 -1
- package/features/chain-db.d.ts +8 -2
- package/features/chain-db.js +38 -34
- package/features/table-doc.js +6 -5
- package/features/table.d.ts +49 -19
- package/features/table.js +132 -63
- package/features/types.d.ts +5 -0
- package/features/utils.d.ts +2 -1
- package/features/utils.js +7 -0
- package/index.d.ts +2 -1
- package/index.js +2 -1
- package/package.json +1 -1
- package/readme.md +66 -162
|
@@ -9,6 +9,11 @@ export type Connection = {
|
|
|
9
9
|
user: string;
|
|
10
10
|
password: string;
|
|
11
11
|
};
|
|
12
|
+
export type ConnectionWithToken = {
|
|
13
|
+
server: string | null;
|
|
14
|
+
database: string;
|
|
15
|
+
token: string;
|
|
16
|
+
};
|
|
12
17
|
export type Criteria<Model> = Partial<Record<keyof Model, string | number | boolean>>;
|
|
13
18
|
export type CriteriaAdvanced<Model> = {
|
|
14
19
|
field: Partial<keyof Model>;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { BasicResponse } from './types';
|
|
2
|
-
export declare const post: (url: string, body: any, auth?: string) => Promise<import("axios").AxiosResponse<BasicResponse<any>, any>>;
|
|
2
|
+
export declare const post: (url: string, body: any, auth?: string) => Promise<import("axios").AxiosResponse<BasicResponse<any>, any, {}>>;
|
|
3
|
+
export declare const get: (url: string, auth?: string) => Promise<import("axios").AxiosResponse<BasicResponse<any>, any, {}>>;
|
|
@@ -3,8 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
exports.__esModule = true;
|
|
6
|
-
exports.post = void 0;
|
|
6
|
+
exports.get = exports.post = void 0;
|
|
7
7
|
var axios_1 = __importDefault(require("axios"));
|
|
8
|
+
// Using Axios
|
|
8
9
|
var post = function (url, body, auth) {
|
|
9
10
|
if (auth === void 0) { auth = ''; }
|
|
10
11
|
return axios_1["default"].post(url, body, {
|
|
@@ -12,3 +13,10 @@ var post = function (url, body, auth) {
|
|
|
12
13
|
});
|
|
13
14
|
};
|
|
14
15
|
exports.post = post;
|
|
16
|
+
var get = function (url, auth) {
|
|
17
|
+
if (auth === void 0) { auth = ''; }
|
|
18
|
+
return axios_1["default"].get(url, {
|
|
19
|
+
headers: { Authorization: "Basic ".concat(auth) }
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
exports.get = get;
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { ChainDB, connect } from './features/chain-db';
|
|
1
|
+
export { ChainDB, connect, connectWithToken } from './features/chain-db';
|
|
2
2
|
export { BasicResponse, Operators, CriteriaAdvanced, Criteria, TableDoc, EventTypes, EventData, EventCallback, } from './features/types';
|
|
3
3
|
export { default as Events } from './features/events';
|
|
4
4
|
export { TableDocImpl } from './features/table-doc';
|
|
5
|
+
export { default as Table } from './features/table';
|
package/dist/cjs/index.js
CHANGED
|
@@ -11,10 +11,11 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
13
|
exports.__esModule = true;
|
|
14
|
-
exports.TableDocImpl = exports.Events = exports.EventTypes = exports.Operators = exports.connect = exports.ChainDB = void 0;
|
|
14
|
+
exports.Table = exports.TableDocImpl = exports.Events = exports.EventTypes = exports.Operators = exports.connectWithToken = exports.connect = exports.ChainDB = void 0;
|
|
15
15
|
var chain_db_1 = require("./features/chain-db");
|
|
16
16
|
__createBinding(exports, chain_db_1, "ChainDB");
|
|
17
17
|
__createBinding(exports, chain_db_1, "connect");
|
|
18
|
+
__createBinding(exports, chain_db_1, "connectWithToken");
|
|
18
19
|
var types_1 = require("./features/types");
|
|
19
20
|
__createBinding(exports, types_1, "Operators");
|
|
20
21
|
__createBinding(exports, types_1, "EventTypes");
|
|
@@ -22,3 +23,5 @@ var events_1 = require("./features/events");
|
|
|
22
23
|
__createBinding(exports, events_1, "default", "Events");
|
|
23
24
|
var table_doc_1 = require("./features/table-doc");
|
|
24
25
|
__createBinding(exports, table_doc_1, "TableDocImpl");
|
|
26
|
+
var table_1 = require("./features/table");
|
|
27
|
+
__createBinding(exports, table_1, "default", "Table");
|
package/features/chain-db.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Table from './table';
|
|
2
|
-
import { Connection, EventCallback } from './types';
|
|
2
|
+
import { Connection, ConnectionWithToken, EventCallback } from './types';
|
|
3
3
|
export declare class ChainDB {
|
|
4
4
|
server: string;
|
|
5
5
|
database: string;
|
|
@@ -13,10 +13,15 @@ export declare class ChainDB {
|
|
|
13
13
|
* @param password Password to access the data base
|
|
14
14
|
*/
|
|
15
15
|
connect(connection: Connection): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Connect to Chain DB with a token (Synchronously). This is useful if you don't want to use a promise to connect to Chain DB.
|
|
18
|
+
* @param connection Connection information with token
|
|
19
|
+
*/
|
|
20
|
+
connectWithToken(connection: ConnectionWithToken): void;
|
|
16
21
|
/**
|
|
17
22
|
* Initialize a table, fetching its more updated data
|
|
18
23
|
*/
|
|
19
|
-
getTable<Model>(table_name: string):
|
|
24
|
+
getTable<Model>(table_name: string): Table<Model>;
|
|
20
25
|
events(): {
|
|
21
26
|
/**
|
|
22
27
|
* Subscribe to an event
|
|
@@ -37,3 +42,4 @@ export declare class ChainDB {
|
|
|
37
42
|
};
|
|
38
43
|
}
|
|
39
44
|
export declare const connect: (connection: Connection) => Promise<ChainDB>;
|
|
45
|
+
export declare const connectWithToken: (connection: ConnectionWithToken) => ChainDB;
|
package/features/chain-db.js
CHANGED
|
@@ -84,44 +84,43 @@ var ChainDB = /** @class */ (function () {
|
|
|
84
84
|
});
|
|
85
85
|
});
|
|
86
86
|
};
|
|
87
|
+
/**
|
|
88
|
+
* Connect to Chain DB with a token (Synchronously). This is useful if you don't want to use a promise to connect to Chain DB.
|
|
89
|
+
* @param connection Connection information with token
|
|
90
|
+
*/
|
|
91
|
+
ChainDB.prototype.connectWithToken = function (connection) {
|
|
92
|
+
var server = connection.server, database = connection.database, token = connection.token;
|
|
93
|
+
this.server = server || DEFAULT_API_SERVER;
|
|
94
|
+
this.database = database;
|
|
95
|
+
this.auth = token;
|
|
96
|
+
};
|
|
87
97
|
/**
|
|
88
98
|
* Initialize a table, fetching its more updated data
|
|
89
99
|
*/
|
|
90
100
|
ChainDB.prototype.getTable = function (table_name) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
// /**
|
|
115
|
-
// * Get the history of changes. A list of transactions from the most recent to the most old
|
|
116
|
-
// * in a range of depth
|
|
117
|
-
// * @param depth
|
|
118
|
-
// */
|
|
119
|
-
// getHistory: (depth: number) => Promise<Model[]>
|
|
120
|
-
// }
|
|
121
|
-
];
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
});
|
|
101
|
+
var tableData = new Table(table_name, this);
|
|
102
|
+
// await tableData.refetch()
|
|
103
|
+
return tableData;
|
|
104
|
+
// const table_data = await table.get<Model>(this, table_name, model)
|
|
105
|
+
// NOTE: Although only the "table" and "persist" properties are displayed by
|
|
106
|
+
// the lint, all Table properties are being exposed.
|
|
107
|
+
// This is due to a javascript limitation on classes.
|
|
108
|
+
//
|
|
109
|
+
// There was an attempt to return a new object with only the required
|
|
110
|
+
// data, but this generates an error in the "this" instance of the Table.
|
|
111
|
+
// return table_data as {
|
|
112
|
+
// table: Model
|
|
113
|
+
// /**
|
|
114
|
+
// * Persist table data on chain
|
|
115
|
+
// */
|
|
116
|
+
// persist: () => Promise<void>
|
|
117
|
+
// /**
|
|
118
|
+
// * Get the history of changes. A list of transactions from the most recent to the most old
|
|
119
|
+
// * in a range of depth
|
|
120
|
+
// * @param depth
|
|
121
|
+
// */
|
|
122
|
+
// getHistory: (depth: number) => Promise<Model[]>
|
|
123
|
+
// }
|
|
125
124
|
};
|
|
126
125
|
ChainDB.prototype.events = function () {
|
|
127
126
|
var _this = this;
|
|
@@ -175,3 +174,8 @@ export var connect = function (connection) { return __awaiter(void 0, void 0, vo
|
|
|
175
174
|
}
|
|
176
175
|
});
|
|
177
176
|
}); };
|
|
177
|
+
export var connectWithToken = function (connection) {
|
|
178
|
+
var chainDb = new ChainDB();
|
|
179
|
+
chainDb.connectWithToken(connection);
|
|
180
|
+
return chainDb;
|
|
181
|
+
};
|
package/features/table-doc.js
CHANGED
|
@@ -34,9 +34,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
34
34
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
|
-
import axios from 'axios';
|
|
38
37
|
import { UPDATE_ITEM, GET_DOC } from './constants';
|
|
39
|
-
import { post } from './utils';
|
|
38
|
+
import { get, post } from './utils';
|
|
40
39
|
/**
|
|
41
40
|
* Implementation of TableDoc interface
|
|
42
41
|
* Represents a specific document from a table
|
|
@@ -75,7 +74,8 @@ var TableDocImpl = /** @class */ (function () {
|
|
|
75
74
|
return [3 /*break*/, 4];
|
|
76
75
|
case 3:
|
|
77
76
|
e_1 = _a.sent();
|
|
78
|
-
throw new Error(
|
|
77
|
+
// throw new Error(`Something went wrong updating document ${this.doc_id}: ${e.message || String(e)}`)
|
|
78
|
+
throw new Error(e_1);
|
|
79
79
|
case 4: return [2 /*return*/];
|
|
80
80
|
}
|
|
81
81
|
});
|
|
@@ -95,7 +95,7 @@ var TableDocImpl = /** @class */ (function () {
|
|
|
95
95
|
_a.label = 1;
|
|
96
96
|
case 1:
|
|
97
97
|
_a.trys.push([1, 3, , 4]);
|
|
98
|
-
return [4 /*yield*/,
|
|
98
|
+
return [4 /*yield*/, get(url, this.db.auth)];
|
|
99
99
|
case 2:
|
|
100
100
|
response = _a.sent();
|
|
101
101
|
if (!response.data.success) {
|
|
@@ -106,7 +106,8 @@ var TableDocImpl = /** @class */ (function () {
|
|
|
106
106
|
return [3 /*break*/, 4];
|
|
107
107
|
case 3:
|
|
108
108
|
e_2 = _a.sent();
|
|
109
|
-
throw new Error(
|
|
109
|
+
// throw new Error(`Something went wrong refetching document ${this.doc_id}: ${e.message || String(e)}`)
|
|
110
|
+
throw new Error(e_2);
|
|
110
111
|
case 4: return [2 /*return*/];
|
|
111
112
|
}
|
|
112
113
|
});
|
package/features/table.d.ts
CHANGED
|
@@ -1,28 +1,27 @@
|
|
|
1
1
|
import { ChainDB } from './chain-db';
|
|
2
2
|
import { Criteria, CriteriaAdvanced, DocId, TableDoc } from './types';
|
|
3
|
+
import { TableDocImpl } from './table-doc';
|
|
3
4
|
declare class Table<Model> {
|
|
4
|
-
|
|
5
|
+
/**
|
|
6
|
+
* The current document data
|
|
7
|
+
*/
|
|
5
8
|
private name;
|
|
6
9
|
private db;
|
|
7
10
|
constructor(name: string, db: ChainDB);
|
|
8
11
|
/**
|
|
9
|
-
*
|
|
12
|
+
* Create a new document in the table
|
|
10
13
|
*/
|
|
11
|
-
|
|
14
|
+
new(data: Model): Promise<TableDoc<Model>>;
|
|
12
15
|
/**
|
|
13
16
|
* Get the history of changes. A list of transactions from the most recent to the most old
|
|
14
17
|
* in a range of depth
|
|
15
|
-
* @param limit
|
|
16
|
-
*/
|
|
17
|
-
getHistory(limit: number): Promise<DocId<Model>[]>;
|
|
18
|
-
/**
|
|
19
|
-
* Refetch the table data
|
|
18
|
+
* @param limit Maximum number of items to return (default: 100)
|
|
20
19
|
*/
|
|
21
|
-
|
|
20
|
+
getHistory(limit?: number): Promise<TableDocImpl<DocId<Model>>[]>;
|
|
22
21
|
/**
|
|
23
|
-
*
|
|
22
|
+
* Get the last record stored in the table
|
|
24
23
|
*/
|
|
25
|
-
|
|
24
|
+
last(): Promise<TableDocImpl<DocId<Model>>>;
|
|
26
25
|
/**
|
|
27
26
|
* Get the table's name
|
|
28
27
|
*/
|
|
@@ -31,7 +30,7 @@ declare class Table<Model> {
|
|
|
31
30
|
* Find items in the table using basic criteria with exact matches
|
|
32
31
|
* @param criteria Object with fields and values to match exactly, e.g.: {age: 44, name: "john"}
|
|
33
32
|
* @param limit Maximum number of items to return (default: 1000)
|
|
34
|
-
* @param reverse If true, returns items in reverse order (default: true)
|
|
33
|
+
* @param reverse If true, returns items in reverse order, newer items first (default: true)
|
|
35
34
|
* @returns Array of found items matching the criteria
|
|
36
35
|
* @example
|
|
37
36
|
* // Find items where age is 44
|
|
@@ -47,9 +46,16 @@ declare class Table<Model> {
|
|
|
47
46
|
* score: 100
|
|
48
47
|
* })
|
|
49
48
|
*/
|
|
50
|
-
findWhere(criteria: Criteria<Model>, limit?: number, reverse?: boolean): Promise<DocId<Model
|
|
49
|
+
findWhere(criteria: Criteria<Model>, limit?: number, reverse?: boolean): Promise<TableDocImpl<DocId<Model>>[]>;
|
|
50
|
+
/**
|
|
51
|
+
* Find the first item in the table using basic criteria with exact matches
|
|
52
|
+
* @param criteria Object with fields and values to match exactly, e.g.: {age: 44, name: "john"}
|
|
53
|
+
* @param reverse If true, returns items in reverse order, newer items first (default: true)
|
|
54
|
+
* @returns The first found item matching the criteria or null if no item is found
|
|
55
|
+
*/
|
|
56
|
+
find(criteria: Criteria<Model>, reverse?: boolean): Promise<TableDocImpl<DocId<Model>>>;
|
|
51
57
|
/**
|
|
52
|
-
* Find
|
|
58
|
+
* Find the first item in the table using advanced criteria with operators
|
|
53
59
|
* @param criteria Array of criteria to filter items. Each criteria contains:
|
|
54
60
|
* - field: The field name to filter
|
|
55
61
|
* - operator: The operator to use in comparison. Available operators:
|
|
@@ -64,7 +70,7 @@ declare class Table<Model> {
|
|
|
64
70
|
* - EndsWith: Check if field ends with value (for strings)
|
|
65
71
|
* - value: The value to compare against
|
|
66
72
|
* @param limit Maximum number of items to return (default: 1000)
|
|
67
|
-
* @param reverse If true, returns items in reverse order (default: true)
|
|
73
|
+
* @param reverse If true, returns items in reverse order, newer items first (default: true)
|
|
68
74
|
* @returns Array of found items matching the criteria
|
|
69
75
|
* @example
|
|
70
76
|
* // Find items where greeting contains "arg"
|
|
@@ -76,16 +82,40 @@ declare class Table<Model> {
|
|
|
76
82
|
* }
|
|
77
83
|
* ])
|
|
78
84
|
*/
|
|
79
|
-
findWhereAdvanced(criteria: CriteriaAdvanced<Model>[], limit?: number, reverse?: boolean): Promise<DocId<Model
|
|
85
|
+
findWhereAdvanced(criteria: CriteriaAdvanced<Model>[], limit?: number, reverse?: boolean): Promise<TableDocImpl<DocId<Model>>[]>;
|
|
80
86
|
/**
|
|
81
|
-
*
|
|
87
|
+
* Find the first single item in the table using advanced criteria with operators
|
|
88
|
+
* @param criteria Array of criteria to filter items. Each criteria contains:
|
|
89
|
+
* - field: The field name to filter
|
|
90
|
+
* - operator: The operator to use in comparison. Available operators:
|
|
91
|
+
* - Eq (==) Equal
|
|
92
|
+
* - Ne (!=) Not Equal
|
|
93
|
+
* - Gt (>) Greater Than
|
|
94
|
+
* - Ge (>=) Greater Than or Equal
|
|
95
|
+
* - Lt (<) Less Than
|
|
96
|
+
* - Le (<=) Less Than or Equal
|
|
97
|
+
* - Contains: Check if field contains value (for strings and arrays)
|
|
98
|
+
* - StartsWith: Check if field starts with value (for strings)
|
|
99
|
+
* - EndsWith: Check if field ends with value (for strings)
|
|
100
|
+
* - value: The value to compare against
|
|
101
|
+
* @param reverse If true, returns items in reverse order, newer items first (default: true)
|
|
102
|
+
* @returns The first found item matching the criteria or null if no item is found
|
|
103
|
+
* @example
|
|
104
|
+
* // Find items where greeting contains "arg"
|
|
105
|
+
* table.findAdvanced([
|
|
106
|
+
* {
|
|
107
|
+
* field: "greeting",
|
|
108
|
+
* operator: Operators.Contains,
|
|
109
|
+
* value: "hello"
|
|
110
|
+
* }
|
|
111
|
+
* ])
|
|
82
112
|
*/
|
|
83
|
-
|
|
113
|
+
findAdvanced(criteria: CriteriaAdvanced<Model>[], reverse?: boolean): Promise<TableDocImpl<DocId<Model>>>;
|
|
84
114
|
/**
|
|
85
115
|
* Get a specific document by its ID
|
|
86
116
|
* @param doc_id The document ID to retrieve
|
|
87
117
|
* @returns A TableDoc instance with the specific document data
|
|
88
118
|
*/
|
|
89
|
-
|
|
119
|
+
getByDocId(doc_id: string): Promise<TableDoc<Model>>;
|
|
90
120
|
}
|
|
91
121
|
export default Table;
|