Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

updating how-to-get-table-information with kv example #804

Merged
merged 6 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions docs/how-to-guides/09_how-to-get-table-information.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,66 @@ In the example shown below, the `contract` smart contract's table `profiles` is
}));
})();
```

## Query Data using the Key-Value API (KV API)
The KV API is a new api which allows smart contract developers to create datastore key value tables on-chain. KV tables can have muliple indexes, unique index and non-unique indexes. The table must have at least one unique index. If the smart contract uses KV tables use the get_kv_table_rows RPC call to query data.
halsaphi marked this conversation as resolved.
Show resolved Hide resolved

In the example shown below, the `contract` smart contract's kv table `profiles` is queried via the index named `users` for the row with primary key `testacc`. The `limit` is **1** which implies that only 1 row with value `testacc` will be returned.
```javascript
(async () => {
console.log(await rpc.get_kv_table_rows({
json: false, // Get the response as json
code: 'contract', // Contract that we target
table: 'profiles', // Tablename
indexName: 'users', // The name of the index name
lowerBound: 'testacc', // Table primary key value
upperBound: 'testacc', // Table primary key value
limit: 1, // Here we limit to 1 to get only the single row with primary key equal to 'testacc'
reverse: false, // Optional: Get reversed data
show_payer: false, // Optional: Show ram payer
expReturn
}));
})();
```
Above we console log the response from the EOSIO network. An example of an expected response is shown below.
```javascript
{
"rows": [{
"user": "testacc",
"age": 21,
"surname": "Martin"
}
],
"more": false
}
```

If the KV table has an additional indexes these can be used to query the data. The example shown below, is based on the previous example however in this case an index called `ages` is defined. This index is used to query the table for records where the persons age is 17.
```javascript
(async () => {
console.log(await rpc.get_kv_table_rows({
json: false, // Get the response as json
code: 'contract', // Contract that we target
table: 'profiles', // Tablename
indexName: 'ages', // The name of the index name
lowerBound: '17', // Table primary key value
upperBound: '17', // Table primary key value
limit: 1, // Here we limit to 1 to get only the single row with primary key equal to 'testacc'
reverse: false, // Optional: Get reversed data
show_payer: false, // Optional: Show ram payer
expReturn
}));
})();
```
Above we console log the response from the EOSIO network. An example of an expected response is shown below.
```javascript
{
"rows": [{
"user": "otheracc",
"age": 17,
"surname": "Dubious"
}
],
"more": false
}
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
"@blockone/eslint-config-blockone": "^3.0.0",
"@types/elliptic": "^6.4.12",
"@types/jest": "^26.0.14",
"@types/node": "^14.11.2",
"@types/node": "^14.11.4",
"@types/pako": "^1.0.1",
"cypress": "^4.12.1",
"eosjs-ecc": "^4.0.7",
"eslint": "^6.8.0",
"jest": "^26.4.2",
"jest": "^26.5.0",
"jest-fetch-mock": "^3.0.3",
"ts-jest": "^26.4.1",
"ts-loader": "^7.0.5",
Expand Down
Loading