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/features/types.js
CHANGED
|
@@ -1,7 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Operators for the advanced criteria
|
|
3
|
+
* @see https://docs.chaindb.com/docs/query-language/ (TODO)
|
|
4
|
+
*/
|
|
5
|
+
export var Operators = {
|
|
6
|
+
// (==)
|
|
7
|
+
EQUAL: 'Eq',
|
|
8
|
+
// (!=)
|
|
9
|
+
NOT_EQUAL: 'Ne',
|
|
10
|
+
// (>)
|
|
11
|
+
GREATER_THAN: 'Gt',
|
|
12
|
+
// (>=)
|
|
13
|
+
GREATER_THAN_OR_EQUAL: 'Ge',
|
|
14
|
+
// (<)
|
|
15
|
+
LESS_THAN: 'Lt',
|
|
16
|
+
// (<=)
|
|
17
|
+
LESS_THAN_OR_EQUAL: 'Le',
|
|
18
|
+
// (for strings and arrays)
|
|
19
|
+
CONTAINS: 'Contains',
|
|
20
|
+
// (for strings)
|
|
21
|
+
STARTS_WITH: 'StartsWith',
|
|
22
|
+
// (for strings)
|
|
23
|
+
ENDS_WITH: 'EndsWith'
|
|
24
|
+
};
|
|
25
|
+
// Events
|
|
26
|
+
export var EventTypes = {
|
|
27
|
+
TABLE_PERSIST: 'TablePersist',
|
|
28
|
+
TABLE_UPDATE: 'TableUpdate'
|
|
29
|
+
};
|
package/features/utils.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { BasicResponse } from './types';
|
|
2
|
+
export declare const post: (url: string, body: any, auth?: string) => Promise<import("axios").AxiosResponse<BasicResponse<any>, any>>;
|
package/features/utils.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
|
-
export var post = function (url, body) {
|
|
3
|
-
|
|
2
|
+
export var post = function (url, body, auth) {
|
|
3
|
+
if (auth === void 0) { auth = ''; }
|
|
4
|
+
return axios.post(url, body, {
|
|
5
|
+
headers: { 'content-type': 'application/json', Authorization: "Basic ".concat(auth) }
|
|
6
|
+
});
|
|
4
7
|
};
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { ChainDB, connect } from './features/chain-db';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
2
|
+
export { BasicResponse, Operators, CriteriaAdvanced, Criteria, TableDoc, EventTypes, EventData, EventCallback, } from './features/types';
|
|
3
|
+
export { default as Events } from './features/events';
|
|
4
|
+
export { TableDocImpl } from './features/table-doc';
|
package/index.js
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
export { ChainDB, connect } from './features/chain-db';
|
|
2
|
-
export {
|
|
2
|
+
export { Operators, EventTypes, } from './features/types';
|
|
3
|
+
export { default as Events } from './features/events';
|
|
4
|
+
export { TableDocImpl } from './features/table-doc';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chain-db-ts",
|
|
3
|
-
"version": "0.0.2",
|
|
3
|
+
"version": "1.0.0-rc.2",
|
|
4
4
|
"description": "Chain DB Client for Javascript/Typescript Node apps.",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"license": "MIT",
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@types/node": "^18.15.5",
|
|
29
|
-
"@types/
|
|
29
|
+
"@types/ws": "^8.5.14",
|
|
30
30
|
"nodemon": "^3.0.1",
|
|
31
31
|
"ts-node": "^10.9.1",
|
|
32
32
|
"tslib": "^2.5.0",
|
|
@@ -35,6 +35,8 @@
|
|
|
35
35
|
"repository": "git://github.com/wpdas/chain-db-ts.git",
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"axios": "^1.4.0",
|
|
38
|
-
"
|
|
38
|
+
"bufferutil": "^4.0.9",
|
|
39
|
+
"utf-8-validate": "^6.0.5",
|
|
40
|
+
"ws": "^8.18.1"
|
|
39
41
|
}
|
|
40
42
|
}
|
package/readme.md
CHANGED
|
@@ -1,186 +1,314 @@
|
|
|
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
|
-
|
|
7
|
+
```bash
|
|
8
|
+
npm install chain-db-ts
|
|
9
|
+
# or
|
|
10
|
+
yarn add chain-db-ts
|
|
11
|
+
```
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
## Usage
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
### Connecting to Chain DB
|
|
12
16
|
|
|
13
|
-
|
|
17
|
+
```typescript
|
|
18
|
+
import { connect } from 'chain-db-ts'
|
|
14
19
|
|
|
15
|
-
|
|
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
|
+
```
|
|
16
30
|
|
|
17
|
-
|
|
18
|
-
npm install chain-db-ts
|
|
31
|
+
### Working with Tables
|
|
19
32
|
|
|
20
|
-
|
|
33
|
+
Define your table structure using TypeScript interfaces:
|
|
21
34
|
|
|
22
|
-
|
|
35
|
+
```typescript
|
|
36
|
+
// Define your table structure
|
|
37
|
+
interface GreetingTable {
|
|
38
|
+
greeting: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Define a more complex table
|
|
42
|
+
interface UserTable {
|
|
43
|
+
id: number
|
|
44
|
+
name: string
|
|
45
|
+
email: string
|
|
46
|
+
active: boolean
|
|
47
|
+
createdAt: string
|
|
48
|
+
}
|
|
23
49
|
```
|
|
24
50
|
|
|
25
|
-
|
|
51
|
+
### Getting a Table
|
|
26
52
|
|
|
27
|
-
|
|
53
|
+
```typescript
|
|
54
|
+
// Get a table instance
|
|
55
|
+
// If the table already exists in the chain, its data will be loaded. (data from the last record stored in the table)
|
|
56
|
+
const greetingTable = await db.getTable<GreetingTable>('greeting')
|
|
28
57
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
58
|
+
// Access the current document data (the last record stored in the table)
|
|
59
|
+
console.log(greetingTable.currentDoc) // e.g., { greeting: 'Hello' }
|
|
60
|
+
```
|
|
32
61
|
|
|
33
|
-
|
|
62
|
+
### Modifying and Persisting Data
|
|
34
63
|
|
|
35
|
-
|
|
64
|
+
```typescript
|
|
65
|
+
// Modify the current document data
|
|
66
|
+
greetingTable.currentDoc.greeting = 'Hello, Chain DB!'
|
|
36
67
|
|
|
37
|
-
|
|
68
|
+
// Persist changes to database (creates a new record with a new doc_id)
|
|
69
|
+
const result = await greetingTable.persist()
|
|
38
70
|
|
|
39
|
-
|
|
71
|
+
// The persist method returns the created document with its doc_id
|
|
72
|
+
console.log(result.doc_id) // e.g., '550e8400-e29b-41d4-a716-446655440000'
|
|
40
73
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
public greeting = 'Hi'
|
|
45
|
-
}
|
|
74
|
+
// You can also access the current document's ID directly
|
|
75
|
+
const currentDocId = greetingTable.getCurrentDocId()
|
|
76
|
+
console.log(currentDocId) // Same as result.doc_id
|
|
46
77
|
```
|
|
47
78
|
|
|
48
|
-
|
|
49
|
-
import { connect } from 'chain-db-ts'
|
|
50
|
-
import { GreetingTable } from './tables'
|
|
51
|
-
|
|
52
|
-
const main async () {
|
|
53
|
-
// server | db-name | user | password
|
|
54
|
-
// If the `server` parameter is empty(null), then "http://localhost:2818" will be used.
|
|
55
|
-
const db = connect('http://localhost:2818', 'test-db', 'root', '1234')
|
|
79
|
+
### Updating Item
|
|
56
80
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
console.log(greetingTable.table) // { greeting: 'Hi' }
|
|
81
|
+
```typescript
|
|
82
|
+
// To update a specific document, first get it by ID
|
|
83
|
+
const docId = '550e8400-e29b-41d4-a716-446655440000'
|
|
84
|
+
const specificDoc = await greetingTable.getDoc(docId)
|
|
62
85
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
// See the most updated values of the table
|
|
68
|
-
console.log(greetingTable.table) // { greeting: 'Hello my dear!' }
|
|
69
|
-
}
|
|
70
|
-
main()
|
|
86
|
+
// Then update its data
|
|
87
|
+
specificDoc.doc.greeting = 'Updated greeting'
|
|
88
|
+
await specificDoc.update()
|
|
71
89
|
```
|
|
72
90
|
|
|
73
|
-
|
|
91
|
+
### Getting a Specific Document
|
|
74
92
|
|
|
75
|
-
|
|
93
|
+
```typescript
|
|
94
|
+
// Get a specific document by its ID (assuming we know a document ID)
|
|
95
|
+
// The document ID is generated by ChainDB when data is persisted
|
|
96
|
+
const docId = '550e8400-e29b-41d4-a716-446655440000' // Example ID
|
|
97
|
+
const specificDoc = await greetingTable.getDoc(docId)
|
|
76
98
|
|
|
77
|
-
|
|
99
|
+
// Access the document data
|
|
100
|
+
console.log(specificDoc.doc) // e.g., { greeting: 'Hello from specific doc', doc_id: '550e8400-e29b-41d4-a716-446655440000' }
|
|
78
101
|
|
|
79
|
-
|
|
102
|
+
// The doc_id is also available directly in the document object
|
|
103
|
+
console.log(specificDoc.doc.doc_id) // '550e8400-e29b-41d4-a716-446655440000'
|
|
80
104
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
105
|
+
// Update the specific document
|
|
106
|
+
specificDoc.doc.greeting = 'Updated greeting for specific doc'
|
|
107
|
+
await specificDoc.update() // Updates only this specific document
|
|
84
108
|
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
// {
|
|
92
|
-
// id: 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3',
|
|
93
|
-
// user_name: 'wenderson.fake',
|
|
94
|
-
// units: 2
|
|
95
|
-
// }
|
|
96
|
-
}
|
|
109
|
+
// Refetch the document data if it might have been updated elsewhere
|
|
110
|
+
await specificDoc.refetch()
|
|
111
|
+
console.log(specificDoc.doc) // Updated data from the database
|
|
112
|
+
|
|
113
|
+
// Get the table name this document belongs to
|
|
114
|
+
const tableName = specificDoc.getTableName() // 'greeting'
|
|
97
115
|
```
|
|
98
116
|
|
|
99
|
-
|
|
117
|
+
Note: The `TableDoc` instance returned by `getDoc()` is a simplified version of a table that only allows updating the specific document. It cannot create new records with `persist()` or perform other table operations.
|
|
100
118
|
|
|
101
|
-
|
|
119
|
+
### Getting Table History
|
|
102
120
|
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
//
|
|
109
|
-
//
|
|
110
|
-
//
|
|
111
|
-
//
|
|
112
|
-
//
|
|
121
|
+
```typescript
|
|
122
|
+
// Get the last 100 changes to the table
|
|
123
|
+
const history = await greetingTable.getHistory(100)
|
|
124
|
+
console.log(history)
|
|
125
|
+
// Example output:
|
|
126
|
+
// [
|
|
127
|
+
// { greeting: 'Hello, Chain DB!' },
|
|
128
|
+
// { greeting: 'Hello' },
|
|
129
|
+
// { greeting: 'Hi there' },
|
|
130
|
+
// ...
|
|
131
|
+
// ]
|
|
113
132
|
```
|
|
114
133
|
|
|
115
|
-
###
|
|
134
|
+
### Real-time Events with WebSockets
|
|
135
|
+
|
|
136
|
+
Chain DB supports real-time updates through WebSockets. You can subscribe to table events to get notified when data changes:
|
|
116
137
|
|
|
117
|
-
|
|
138
|
+
```typescript
|
|
139
|
+
import { EventTypes, EventData } from 'chain-db-ts'
|
|
118
140
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
console.log(
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
//
|
|
126
|
-
|
|
127
|
-
|
|
141
|
+
// Subscribe to table update events
|
|
142
|
+
db.events().subscribe(EventTypes.TABLE_UPDATE, (eventData: EventData) => {
|
|
143
|
+
console.log('Table updated:', eventData.table)
|
|
144
|
+
console.log('New data:', eventData.data)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
// Subscribe to new data persistence events
|
|
148
|
+
db.events().subscribe(EventTypes.TABLE_PERSIST, (eventData: EventData) => {
|
|
149
|
+
console.log('New data added to table:', eventData.table)
|
|
150
|
+
console.log('Data:', eventData.data)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
// Unsubscribe from an event
|
|
154
|
+
const myCallback = (eventData: EventData) => {
|
|
155
|
+
// Handle event
|
|
156
|
+
}
|
|
157
|
+
db.events().subscribe(EventTypes.TABLE_UPDATE, myCallback)
|
|
158
|
+
// Later, when you want to unsubscribe:
|
|
159
|
+
db.events().unsubscribe(EventTypes.TABLE_UPDATE, myCallback)
|
|
160
|
+
|
|
161
|
+
// Close WebSocket connection when done
|
|
162
|
+
db.events().closeEvents()
|
|
128
163
|
```
|
|
129
164
|
|
|
130
|
-
|
|
165
|
+
The `EventData` object contains:
|
|
131
166
|
|
|
132
|
-
|
|
167
|
+
- `event_type`: The type of event (TableUpdate, TablePersist)
|
|
168
|
+
- `database`: The database name
|
|
169
|
+
- `table`: The table name
|
|
170
|
+
- `data`: The data associated with the event
|
|
171
|
+
- `timestamp`: When the event occurred
|
|
133
172
|
|
|
134
|
-
|
|
173
|
+
### Querying Data
|
|
135
174
|
|
|
136
|
-
|
|
137
|
-
const wenderson_id = 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3'
|
|
138
|
-
const suly_id = '136c406933d98e5c8bb4820f5145869bb5ad40647b768de4e9adb2a52d0dea2f'
|
|
175
|
+
#### Basic Queries
|
|
139
176
|
|
|
140
|
-
|
|
141
|
-
|
|
177
|
+
```typescript
|
|
178
|
+
// Find items with exact matches
|
|
179
|
+
const users = await userTable.findWhere(
|
|
180
|
+
{ active: true, name: 'John' }, // criteria
|
|
181
|
+
10, // limit (default: 1000)
|
|
182
|
+
true // reverse order (default: true)
|
|
183
|
+
)
|
|
184
|
+
```
|
|
142
185
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
186
|
+
#### Advanced Queries
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import { Operators } from 'chain-db-ts'
|
|
190
|
+
|
|
191
|
+
// Find items with advanced criteria
|
|
192
|
+
const users = await userTable.findWhereAdvanced(
|
|
193
|
+
[
|
|
194
|
+
{
|
|
195
|
+
field: 'name',
|
|
196
|
+
operator: Operators.CONTAINS,
|
|
197
|
+
value: 'John',
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
field: 'age',
|
|
201
|
+
operator: Operators.GREATER_THAN,
|
|
202
|
+
value: 25,
|
|
203
|
+
},
|
|
204
|
+
],
|
|
205
|
+
10, // limit
|
|
206
|
+
true // reverse order
|
|
207
|
+
)
|
|
148
208
|
```
|
|
149
209
|
|
|
150
|
-
|
|
210
|
+
Available operators:
|
|
211
|
+
|
|
212
|
+
- `EQUAL` (==)
|
|
213
|
+
- `NOT_EQUAL` (!=)
|
|
214
|
+
- `GREATER_THAN` (>)
|
|
215
|
+
- `GREATER_THAN_OR_EQUAL` (>=)
|
|
216
|
+
- `LESS_THAN` (<)
|
|
217
|
+
- `LESS_THAN_OR_EQUAL` (<=)
|
|
218
|
+
- `CONTAINS` (for strings and arrays)
|
|
219
|
+
- `STARTS_WITH` (for strings)
|
|
220
|
+
- `ENDS_WITH` (for strings)
|
|
221
|
+
|
|
222
|
+
### Other Table Methods
|
|
151
223
|
|
|
152
|
-
|
|
224
|
+
```typescript
|
|
225
|
+
// Check if a table is empty
|
|
226
|
+
const isEmpty = greetingTable.isEmpty()
|
|
153
227
|
|
|
154
|
-
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
// from: 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3',
|
|
160
|
-
// to: '136c406933d98e5c8bb4820f5145869bb5ad40647b768de4e9adb2a52d0dea2f',
|
|
161
|
-
// units: 2
|
|
162
|
-
// }
|
|
228
|
+
// Get the table name
|
|
229
|
+
const tableName = greetingTable.getName()
|
|
230
|
+
|
|
231
|
+
// Refetch the table data from the database
|
|
232
|
+
await greetingTable.refetch()
|
|
163
233
|
```
|
|
164
234
|
|
|
165
|
-
|
|
235
|
+
## Complete Example
|
|
166
236
|
|
|
167
|
-
|
|
237
|
+
```typescript
|
|
238
|
+
import { connect, EventTypes, EventData } from 'chain-db-ts'
|
|
168
239
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
//
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
//
|
|
184
|
-
|
|
185
|
-
//
|
|
240
|
+
// Define table structure
|
|
241
|
+
interface GreetingTable {
|
|
242
|
+
greeting: string
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async function main() {
|
|
246
|
+
// Connect to Chain DB
|
|
247
|
+
const db = await connect({
|
|
248
|
+
server: 'http://localhost:2818',
|
|
249
|
+
database: 'test-db',
|
|
250
|
+
user: 'root',
|
|
251
|
+
password: '1234',
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
// Get the "greeting" table
|
|
255
|
+
const greetingTable = await db.getTable<GreetingTable>('greeting')
|
|
256
|
+
console.log(greetingTable.currentDoc) // e.g., { greeting: 'Hi' }
|
|
257
|
+
|
|
258
|
+
// Subscribe to table update events
|
|
259
|
+
db.events().subscribe(EventTypes.TABLE_UPDATE, (eventData: EventData) => {
|
|
260
|
+
if (eventData.table === 'greeting') {
|
|
261
|
+
console.log('Greeting table updated:', eventData.data)
|
|
262
|
+
}
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
// Modify and persist data
|
|
266
|
+
greetingTable.currentDoc.greeting = 'Hello my dear!'
|
|
267
|
+
const persistResult = await greetingTable.persist() // Data is persisted on database
|
|
268
|
+
|
|
269
|
+
// Get the doc_id of the newly created document
|
|
270
|
+
console.log('New document ID:', persistResult.doc_id)
|
|
271
|
+
|
|
272
|
+
// You can also get the current document ID directly from the table
|
|
273
|
+
const currentDocId = greetingTable.getCurrentDocId()
|
|
274
|
+
console.log('Current document ID:', currentDocId)
|
|
275
|
+
|
|
276
|
+
// See the updated values
|
|
277
|
+
console.log(greetingTable.currentDoc) // { greeting: 'Hello my dear!', doc_id: '...' }
|
|
278
|
+
|
|
279
|
+
// Get a specific document by its ID
|
|
280
|
+
// We can use the ID we just got from the persist operation
|
|
281
|
+
const specificDoc = await greetingTable.getDoc(currentDocId)
|
|
282
|
+
|
|
283
|
+
// Access the document data and ID
|
|
284
|
+
console.log(specificDoc.doc) // { greeting: 'Hello my dear!', doc_id: '...' }
|
|
285
|
+
console.log(specificDoc.doc.doc_id) // Same as currentDocId
|
|
286
|
+
|
|
287
|
+
// Update a specific document
|
|
288
|
+
specificDoc.doc.greeting = 'Updated specific document'
|
|
289
|
+
await specificDoc.update() // Updates only this specific document
|
|
290
|
+
|
|
291
|
+
// Refetch the document to get the latest data
|
|
292
|
+
await specificDoc.refetch()
|
|
293
|
+
console.log(specificDoc.doc) // Latest data from the database
|
|
294
|
+
|
|
295
|
+
// Get the last 100 changes
|
|
296
|
+
const greetingHistory = await greetingTable.getHistory(100)
|
|
297
|
+
console.log(greetingHistory)
|
|
298
|
+
// [
|
|
299
|
+
// { greeting: 'Updated specific document' },
|
|
300
|
+
// { greeting: 'Hello my dear!' },
|
|
301
|
+
// { greeting: 'Hi' },
|
|
302
|
+
// ...
|
|
303
|
+
// ]
|
|
304
|
+
|
|
305
|
+
// Close WebSocket connection when done
|
|
306
|
+
db.events().closeEvents()
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
main().catch(console.error)
|
|
186
310
|
```
|
|
311
|
+
|
|
312
|
+
## License
|
|
313
|
+
|
|
314
|
+
MIT
|
|
@@ -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 };
|