chain-db-ts 0.0.1 → 1.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/features/chain-db.d.ts +25 -60
- package/dist/cjs/features/chain-db.js +100 -263
- package/dist/cjs/features/constants.d.ts +10 -10
- package/dist/cjs/features/constants.js +20 -11
- package/dist/cjs/features/events.d.ts +28 -0
- package/dist/cjs/features/events.js +89 -0
- package/dist/cjs/features/table.d.ts +78 -5
- package/dist/cjs/features/table.js +236 -51
- package/dist/cjs/features/types.d.ts +46 -20
- package/dist/cjs/features/types.js +30 -8
- package/dist/cjs/features/utils.d.ts +2 -1
- package/dist/cjs/features/utils.js +5 -2
- package/dist/cjs/index.d.ts +1 -2
- package/dist/cjs/index.js +4 -3
- package/features/chain-db.d.ts +25 -60
- package/features/chain-db.js +101 -241
- package/features/constants.d.ts +10 -10
- package/features/constants.js +13 -10
- package/features/events.d.ts +28 -0
- package/features/events.js +84 -0
- package/features/table.d.ts +78 -5
- package/features/table.js +237 -50
- package/features/types.d.ts +46 -20
- package/features/types.js +29 -7
- package/features/utils.d.ts +2 -1
- package/features/utils.js +5 -2
- package/index.d.ts +1 -2
- package/index.js +1 -1
- package/package.json +5 -3
- package/readme.md +201 -134
- package/tsconfig.json +0 -33
- 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/readme.md
CHANGED
|
@@ -1,186 +1,253 @@
|
|
|
1
|
-
# Chain DB
|
|
1
|
+
# Chain DB TypeScript Client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A TypeScript client for [Chain DB](https://github.com/wpdas/chain-db), a simple database that maintains a history of changes, allowing you to track how your data evolves over time.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
The `unit` property present in each user's account can be anything the project wants, it can be a type of currency, item, resource.
|
|
10
|
-
|
|
11
|
-
Visit the [Chain DB repository](https://github.com/wpdas/chain-db) to get to know more.
|
|
12
|
-
|
|
13
|
-
## Install
|
|
14
|
-
|
|
15
|
-
Install using npm or yarn:
|
|
16
|
-
|
|
17
|
-
```sh
|
|
7
|
+
```bash
|
|
18
8
|
npm install chain-db-ts
|
|
19
|
-
|
|
20
9
|
# or
|
|
21
|
-
|
|
22
10
|
yarn add chain-db-ts
|
|
23
11
|
```
|
|
24
12
|
|
|
25
|
-
## Usage
|
|
13
|
+
## Usage
|
|
26
14
|
|
|
27
|
-
|
|
15
|
+
### Connecting to Chain DB
|
|
28
16
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
**data:** `D` (any expected data type depending on the request) <br/>
|
|
17
|
+
```typescript
|
|
18
|
+
import { connect } from 'chain-db-ts'
|
|
32
19
|
|
|
33
|
-
|
|
20
|
+
// Connect to Chain DB
|
|
21
|
+
// Parameters: server | database | user | password
|
|
22
|
+
// If the server parameter is null, "http://localhost:2818" will be used as default
|
|
23
|
+
const db = await connect({
|
|
24
|
+
server: 'http://localhost:2818',
|
|
25
|
+
database: 'my-database',
|
|
26
|
+
user: 'root',
|
|
27
|
+
password: '1234',
|
|
28
|
+
})
|
|
29
|
+
```
|
|
34
30
|
|
|
35
|
-
###
|
|
31
|
+
### Working with Tables
|
|
36
32
|
|
|
37
|
-
|
|
33
|
+
Define your table structure using TypeScript interfaces:
|
|
38
34
|
|
|
39
|
-
|
|
35
|
+
```typescript
|
|
36
|
+
// Define your table structure
|
|
37
|
+
interface GreetingTable {
|
|
38
|
+
greeting: string
|
|
39
|
+
}
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
// Define a more complex table
|
|
42
|
+
interface UserTable {
|
|
43
|
+
id: number
|
|
44
|
+
name: string
|
|
45
|
+
email: string
|
|
46
|
+
active: boolean
|
|
47
|
+
createdAt: string
|
|
45
48
|
}
|
|
46
49
|
```
|
|
47
50
|
|
|
48
|
-
|
|
49
|
-
import { connect } from 'chain-db-ts'
|
|
50
|
-
import { GreetingTable } from './tables'
|
|
51
|
+
### Getting a Table
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
```typescript
|
|
54
|
+
// Get a table instance
|
|
55
|
+
// If the table already exists in the chain, its data will be loaded
|
|
56
|
+
const greetingTable = await db.getTable<GreetingTable>('greeting')
|
|
56
57
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const greetingTable = await db.get_table('greeting', new GreetingTable())
|
|
61
|
-
console.log(greetingTable.table) // { greeting: 'Hi' }
|
|
58
|
+
// Access the table data
|
|
59
|
+
console.log(greetingTable.table) // e.g., { greeting: 'Hello' }
|
|
60
|
+
```
|
|
62
61
|
|
|
63
|
-
|
|
64
|
-
greetingTable.table.greeting = "Hello my dear!"
|
|
65
|
-
await greetingTable.persist() // Data is persisted on the blockchain
|
|
62
|
+
### Modifying and Persisting Data
|
|
66
63
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
64
|
+
```typescript
|
|
65
|
+
// Modify the table data
|
|
66
|
+
greetingTable.table.greeting = 'Hello, Chain DB!'
|
|
67
|
+
|
|
68
|
+
// Persist changes to database (creates a new record)
|
|
69
|
+
await greetingTable.persist()
|
|
71
70
|
```
|
|
72
71
|
|
|
73
|
-
|
|
72
|
+
### Updating the Last Item
|
|
74
73
|
|
|
75
|
-
|
|
74
|
+
```typescript
|
|
75
|
+
// Update the last item without creating a new one
|
|
76
|
+
greetingTable.table.greeting = 'Updated greeting'
|
|
77
|
+
await greetingTable.update()
|
|
78
|
+
```
|
|
76
79
|
|
|
77
|
-
|
|
80
|
+
### Getting Table History
|
|
78
81
|
|
|
79
|
-
|
|
82
|
+
```typescript
|
|
83
|
+
// Get the last 100 changes to the table
|
|
84
|
+
const history = await greetingTable.getHistory(100)
|
|
85
|
+
console.log(history)
|
|
86
|
+
// Example output:
|
|
87
|
+
// [
|
|
88
|
+
// { greeting: 'Hello, Chain DB!' },
|
|
89
|
+
// { greeting: 'Hello' },
|
|
90
|
+
// { greeting: 'Hi there' },
|
|
91
|
+
// ...
|
|
92
|
+
// ]
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Real-time Events with WebSockets
|
|
96
|
+
|
|
97
|
+
Chain DB supports real-time updates through WebSockets. You can subscribe to table events to get notified when data changes:
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { EventTypes, EventData } from 'chain-db-ts'
|
|
101
|
+
|
|
102
|
+
// Subscribe to table update events
|
|
103
|
+
db.events().subscribe(EventTypes.TABLE_UPDATE, (eventData: EventData) => {
|
|
104
|
+
console.log('Table updated:', eventData.table)
|
|
105
|
+
console.log('New data:', eventData.data)
|
|
106
|
+
})
|
|
80
107
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
108
|
+
// Subscribe to new data persistence events
|
|
109
|
+
db.events().subscribe(EventTypes.TABLE_PERSIST, (eventData: EventData) => {
|
|
110
|
+
console.log('New data added to table:', eventData.table)
|
|
111
|
+
console.log('Data:', eventData.data)
|
|
112
|
+
})
|
|
84
113
|
|
|
85
|
-
//
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
// user name | password | units (optional) | password hint (optional - may be used in the future versions)
|
|
89
|
-
const user = await db.create_user_account(user_name, user_pass, 2)
|
|
90
|
-
console.log(user.data)
|
|
91
|
-
// {
|
|
92
|
-
// id: 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3',
|
|
93
|
-
// user_name: 'wenderson.fake',
|
|
94
|
-
// units: 2
|
|
95
|
-
// }
|
|
114
|
+
// Unsubscribe from an event
|
|
115
|
+
const myCallback = (eventData: EventData) => {
|
|
116
|
+
// Handle event
|
|
96
117
|
}
|
|
118
|
+
db.events().subscribe(EventTypes.TABLE_UPDATE, myCallback)
|
|
119
|
+
// Later, when you want to unsubscribe:
|
|
120
|
+
db.events().unsubscribe(EventTypes.TABLE_UPDATE, myCallback)
|
|
121
|
+
|
|
122
|
+
// Close WebSocket connection when done
|
|
123
|
+
db.events().closeEvents()
|
|
97
124
|
```
|
|
98
125
|
|
|
99
|
-
|
|
126
|
+
The `EventData` object contains:
|
|
100
127
|
|
|
101
|
-
|
|
128
|
+
- `event_type`: The type of event (TableUpdate, TablePersist)
|
|
129
|
+
- `database`: The database name
|
|
130
|
+
- `table`: The table name
|
|
131
|
+
- `data`: The data associated with the event
|
|
132
|
+
- `timestamp`: When the event occurred
|
|
102
133
|
|
|
103
|
-
|
|
104
|
-
const user_name = 'wenderson.fake'
|
|
105
|
-
const user_pass = '1234'
|
|
106
|
-
const user = await db.get_user_account(user_name, user_pass)
|
|
107
|
-
console.log(user.data)
|
|
108
|
-
// {
|
|
109
|
-
// id: 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3',
|
|
110
|
-
// user_name: 'wenderson.fake',
|
|
111
|
-
// units: 2
|
|
112
|
-
// }
|
|
113
|
-
```
|
|
134
|
+
### Querying Data
|
|
114
135
|
|
|
115
|
-
|
|
136
|
+
#### Basic Queries
|
|
116
137
|
|
|
117
|
-
|
|
138
|
+
```typescript
|
|
139
|
+
// Find items with exact matches
|
|
140
|
+
const users = await userTable.findWhere(
|
|
141
|
+
{ active: true, name: 'John' }, // criteria
|
|
142
|
+
10, // limit (default: 1000)
|
|
143
|
+
true // reverse order (default: true)
|
|
144
|
+
)
|
|
145
|
+
```
|
|
118
146
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
147
|
+
#### Advanced Queries
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { Operators } from 'chain-db-ts'
|
|
151
|
+
|
|
152
|
+
// Find items with advanced criteria
|
|
153
|
+
const users = await userTable.findWhereAdvanced(
|
|
154
|
+
[
|
|
155
|
+
{
|
|
156
|
+
field: 'name',
|
|
157
|
+
operator: Operators.CONTAINS,
|
|
158
|
+
value: 'John',
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
field: 'age',
|
|
162
|
+
operator: Operators.GREATER_THAN,
|
|
163
|
+
value: 25,
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
10, // limit
|
|
167
|
+
true // reverse order
|
|
168
|
+
)
|
|
128
169
|
```
|
|
129
170
|
|
|
130
|
-
|
|
171
|
+
Available operators:
|
|
131
172
|
|
|
132
|
-
|
|
173
|
+
- `EQUAL` (==)
|
|
174
|
+
- `NOT_EQUAL` (!=)
|
|
175
|
+
- `GREATER_THAN` (>)
|
|
176
|
+
- `GREATER_THAN_OR_EQUAL` (>=)
|
|
177
|
+
- `LESS_THAN` (<)
|
|
178
|
+
- `LESS_THAN_OR_EQUAL` (<=)
|
|
179
|
+
- `CONTAINS` (for strings and arrays)
|
|
180
|
+
- `STARTS_WITH` (for strings)
|
|
181
|
+
- `ENDS_WITH` (for strings)
|
|
133
182
|
|
|
134
|
-
|
|
183
|
+
### Other Table Methods
|
|
135
184
|
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
const
|
|
185
|
+
```typescript
|
|
186
|
+
// Check if a table is empty
|
|
187
|
+
const isEmpty = greetingTable.isEmpty()
|
|
139
188
|
|
|
140
|
-
|
|
141
|
-
const
|
|
189
|
+
// Get the table name
|
|
190
|
+
const tableName = greetingTable.getName()
|
|
142
191
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
console.log(res.success)
|
|
146
|
-
// true / false
|
|
147
|
-
}
|
|
192
|
+
// Refetch the table data from the database
|
|
193
|
+
await greetingTable.refetch()
|
|
148
194
|
```
|
|
149
195
|
|
|
150
|
-
|
|
196
|
+
## Complete Example
|
|
151
197
|
|
|
152
|
-
|
|
198
|
+
```typescript
|
|
199
|
+
import { connect, EventTypes, EventData } from 'chain-db-ts'
|
|
153
200
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
// {
|
|
159
|
-
// from: 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3',
|
|
160
|
-
// to: '136c406933d98e5c8bb4820f5145869bb5ad40647b768de4e9adb2a52d0dea2f',
|
|
161
|
-
// units: 2
|
|
162
|
-
// }
|
|
163
|
-
```
|
|
201
|
+
// Define table structure
|
|
202
|
+
interface GreetingTable {
|
|
203
|
+
greeting: string
|
|
204
|
+
}
|
|
164
205
|
|
|
165
|
-
|
|
206
|
+
async function main() {
|
|
207
|
+
// Connect to Chain DB
|
|
208
|
+
const db = await connect({
|
|
209
|
+
server: 'http://localhost:2818',
|
|
210
|
+
database: 'test-db',
|
|
211
|
+
user: 'root',
|
|
212
|
+
password: '1234',
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
// Get the "greeting" table
|
|
216
|
+
const greetingTable = await db.getTable<GreetingTable>('greeting')
|
|
217
|
+
console.log(greetingTable.table) // e.g., { greeting: 'Hi' }
|
|
218
|
+
|
|
219
|
+
// Subscribe to table update events
|
|
220
|
+
db.events().subscribe(EventTypes.TABLE_UPDATE, (eventData: EventData) => {
|
|
221
|
+
if (eventData.table === 'greeting') {
|
|
222
|
+
console.log('Greeting table updated:', eventData.data)
|
|
223
|
+
}
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
// Modify and persist data
|
|
227
|
+
greetingTable.table.greeting = 'Hello my dear!'
|
|
228
|
+
await greetingTable.persist() // Data is persisted on database
|
|
229
|
+
|
|
230
|
+
// See the updated values
|
|
231
|
+
console.log(greetingTable.table) // { greeting: 'Hello my dear!' }
|
|
166
232
|
|
|
167
|
-
|
|
233
|
+
// Get the last 100 changes
|
|
234
|
+
const greetingHistory = await greetingTable.getHistory(100)
|
|
235
|
+
console.log(greetingHistory)
|
|
236
|
+
// [
|
|
237
|
+
// { greeting: 'Hello my dear!' },
|
|
238
|
+
// { greeting: 'Hi' },
|
|
239
|
+
// { greeting: 'Ei, sou eu :D' },
|
|
240
|
+
// { greeting: 'Heyo' },
|
|
241
|
+
// ...
|
|
242
|
+
// ]
|
|
243
|
+
|
|
244
|
+
// Close WebSocket connection when done
|
|
245
|
+
db.events().closeEvents()
|
|
246
|
+
}
|
|
168
247
|
|
|
169
|
-
|
|
170
|
-
const wenderson_id = 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3'
|
|
171
|
-
const all_units_transfers_record = await db.get_all_transfers_by_user_id(wenderson_id)
|
|
172
|
-
console.log(all_units_transfers_record.data)
|
|
173
|
-
// [
|
|
174
|
-
// {
|
|
175
|
-
// from: 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3',
|
|
176
|
-
// to: '136c406933d98e5c8bb4820f5145869bb5ad40647b768de4e9adb2a52d0dea2f',
|
|
177
|
-
// units: 2
|
|
178
|
-
// },
|
|
179
|
-
// {
|
|
180
|
-
// from: '136c406933d98e5c8bb4820f5145869bb5ad40647b768de4e9adb2a52d0dea2f',
|
|
181
|
-
// to: 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3',
|
|
182
|
-
// units: 6
|
|
183
|
-
// },
|
|
184
|
-
// ...
|
|
185
|
-
// ]
|
|
248
|
+
main().catch(console.error)
|
|
186
249
|
```
|
|
250
|
+
|
|
251
|
+
## License
|
|
252
|
+
|
|
253
|
+
MIT
|
package/tsconfig.json
CHANGED
|
@@ -22,36 +22,3 @@
|
|
|
22
22
|
"baseUrl": "."
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
// {
|
|
27
|
-
// "compilerOptions": {
|
|
28
|
-
// "target": "es5",
|
|
29
|
-
// "module": "ESNext",
|
|
30
|
-
// "lib": ["es6", "esnext", "es2016", "es2017", "es2022"],
|
|
31
|
-
// "allowJs": true,
|
|
32
|
-
// "outDir": "build",
|
|
33
|
-
// "rootDir": "src",
|
|
34
|
-
// "strict": true,
|
|
35
|
-
// "noImplicitAny": true,
|
|
36
|
-
// "esModuleInterop": true,
|
|
37
|
-
// "resolveJsonModule": true,
|
|
38
|
-
// "moduleResolution": "node"
|
|
39
|
-
// }
|
|
40
|
-
// }
|
|
41
|
-
|
|
42
|
-
// {
|
|
43
|
-
// "compilerOptions": {
|
|
44
|
-
// "target": "es6",
|
|
45
|
-
// "module": "commonjs",
|
|
46
|
-
// "lib": ["es6", "esnext", "es2016", "es2017", "es2022"],
|
|
47
|
-
// "allowJs": true,
|
|
48
|
-
// "outDir": "build",
|
|
49
|
-
// "rootDir": "src",
|
|
50
|
-
// "strict": true,
|
|
51
|
-
// "noImplicitAny": true,
|
|
52
|
-
// "esModuleInterop": true,
|
|
53
|
-
// "resolveJsonModule": true,
|
|
54
|
-
// "moduleResolution": "node",
|
|
55
|
-
// "declaration": true
|
|
56
|
-
// }
|
|
57
|
-
// }
|
|
@@ -1,22 +0,0 @@
|
|
|
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;
|
package/features/access.d.ts
DELETED
package/features/access.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
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 };
|