@@ -32,6 +32,7 @@ import {
32
32
Mutation_BodyWrapper ,
33
33
DocumentDatabaseMutation ,
34
34
EventDatabaseMutation ,
35
+ AddIndexMutation ,
35
36
} from '../proto/db3_mutation_v2'
36
37
37
38
import { Client , ReadClient } from '../client/types'
@@ -43,10 +44,22 @@ import { Index } from '../proto/db3_database_v2'
43
44
* Create an event database to store contract events
44
45
*
45
46
* ```ts
46
- * const {db, result} = await createEventDatabase(client, "my_db")
47
+ * const {db, result} = await createEventDatabase(client,
48
+ * "my contract event db",
49
+ * "0x...",
50
+ * ["DepositEvent"],
51
+ * "{...}",
52
+ * "wss://xxxxx",
53
+ * "100000"
54
+ * )
47
55
* ```
48
- * @param client - the db3 client instance
49
- * @param desc - the description for the database
56
+ * @param client - the client instance
57
+ * @param desc - the description for the event database
58
+ * @param contractAddress - the contract address
59
+ * @param tables - the contract event list
60
+ * @param abi - the json abi of contract
61
+ * @param evmNodeUrl - the websocket url of evm node
62
+ * @param startBlock - the start block to subscribe, 0 start from the latest block
50
63
* @returns the {@link CreateDBResult}
51
64
*
52
65
**/
@@ -111,6 +124,65 @@ export async function createEventDatabase(
111
124
throw new Error ( 'fail to create database' )
112
125
}
113
126
}
127
+ /**
128
+ *
129
+ * Add index the existing Collection
130
+ *
131
+ * ```ts
132
+ *
133
+ * const index:Index = {
134
+ * path:'/city', // a top level field name 'city' and the path will be '/city'
135
+ * indexType: IndexType.StringKey
136
+ * }
137
+ * const result = await addIndex(collection, [index])
138
+ * ```
139
+ * @param client - the db3 client instance
140
+ * @param indexes - the index list of {@link Index}
141
+ * @returns the {@link MutationResult}
142
+ *
143
+ **/
144
+ export async function addIndex ( collection : Collection , indexes : Index [ ] ) {
145
+ if ( indexes . filter ( ( item ) => ! item . path . startsWith ( '/' ) ) . length > 0 ) {
146
+ throw new Error ( 'the index path must start with /' )
147
+ }
148
+ const addIndexMutation : AddIndexMutation = {
149
+ collectionName : collection . name ,
150
+ indexFields : indexes ,
151
+ }
152
+
153
+ const body : Mutation_BodyWrapper = {
154
+ body : { oneofKind : 'addIndexMutation' , addIndexMutation } ,
155
+ dbAddress : fromHEX ( collection . db . addr ) ,
156
+ }
157
+
158
+ const dm : Mutation = {
159
+ action : MutationAction . AddIndex ,
160
+ bodies : [ body ] ,
161
+ }
162
+ const payload = Mutation . toBinary ( dm )
163
+ try {
164
+ const response = await collection . db . client . provider . sendMutation (
165
+ payload ,
166
+ collection . db . client . nonce . toString ( )
167
+ )
168
+ if ( response . code == 0 ) {
169
+ return {
170
+ result : {
171
+ id : response . id ,
172
+ block : response . block ,
173
+ order : response . order ,
174
+ } as MutationResult ,
175
+ }
176
+ } else {
177
+ throw new Error ( 'fail to add index with err ' + response . msg )
178
+ }
179
+ } catch ( e ) {
180
+ throw e
181
+ } finally {
182
+ collection . db . client . nonce += 1
183
+ }
184
+ }
185
+
114
186
/**
115
187
*
116
188
* Create a document database to group the collections
@@ -163,12 +235,12 @@ export async function createDocumentDatabase(client: Client, desc: string) {
163
235
* Get the collection by an db address and collection name
164
236
*
165
237
* ```ts
166
- * const database = await getCollection("0x....", "col1", client)
238
+ * const collection = await getCollection("0x....", "col1", client)
167
239
* ```
168
- * @param addr - a hex format string address
240
+ * @param addr - a hex format string database address
169
241
* @param name - the name of collection
170
- * @param client- the db3 client instance
171
- * @returns the {@link Database}[]
242
+ * @param client- the client instance
243
+ * @returns the {@link Collection}
172
244
*
173
245
**/
174
246
export async function getCollection (
@@ -187,6 +259,7 @@ export async function getCollection(
187
259
)
188
260
}
189
261
}
262
+
190
263
/**
191
264
*
192
265
* Get the database by an address
@@ -271,7 +344,7 @@ export async function showDatabase(owner: string, client: Client | ReadClient) {
271
344
*
272
345
* @param db - the instance of database
273
346
* @param name - the name of collection
274
- * @param indexFields - the fields for index
347
+ * @param indexFields - the fields for { @link Index}
275
348
* @returns the {@link CreateCollectionResult}
276
349
*
277
350
**/
0 commit comments