Skip to content

Commit

Permalink
Merge pull request #35 from wharfkit/first-row
Browse files Browse the repository at this point in the history
Make query in `get()` optional to just fetch first row
  • Loading branch information
aaroncox authored Jul 25, 2023
2 parents e2f6e00 + 53566cc commit 9b90b04
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/contract/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class Table<RowType = any> {
* @returns {Promise<TableRow>} Promise resolving to a single table row.
*/
async get(
queryValue: API.v1.TableIndexType | string,
queryValue?: API.v1.TableIndexType | string,
{scope = this.account, index, key_type}: QueryOptions = {}
): Promise<RowType> {
const fieldToIndexMapping = this.getFieldToIndex()
Expand Down
18 changes: 18 additions & 0 deletions test/data/requests/5d3db67a54d5cb0135682b1ad03efaa2560a95ff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"request": {
"path": "https://eos.greymass.com/v1/chain/get_table_rows",
"params": {
"method": "POST",
"body": "{\"table\":\"global\",\"code\":\"eosio\",\"scope\":\"eosio\",\"limit\":1,\"index_position\":\"primary\",\"key_type\":\"name\",\"json\":false}"
}
},
"status": 200,
"json": {
"rows": [
"000010000000000010270000000008000c000000f40100001400000064000000400d03000a000000f049020064000000100e00005802000080533b00ffff07000a000a00008c34915b000000cc968e630f000000d3fb69400a000000714fa458803b52ce49010600ff8a7a0e000000002983000200000000be2001001da5259d8a05000000e15fd7136f05001500ef04cfcfcf914244f4c4a358"
],
"more": false,
"next_key": ""
},
"text": "{\"rows\":[\"000010000000000010270000000008000c000000f40100001400000064000000400d03000a000000f049020064000000100e00005802000080533b00ffff07000a000a00008c34915b000000cc968e630f000000d3fb69400a000000714fa458803b52ce49010600ff8a7a0e000000002983000200000000be2001001da5259d8a05000000e15fd7136f05001500ef04cfcfcf914244f4c4a358\"],\"more\":false,\"next_key\":\"\"}"
}
8 changes: 8 additions & 0 deletions test/tests/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ContractKit, {Contract, Table, TableCursor} from '$lib'

import {Asset, Int64, Name, Serializer, Struct, TimePoint} from '@greymass/eosio'
import {makeClient} from '@wharfkit/mock-data'
import {EosioGlobalState} from '$test/data/structs/eosio'

const mockClient = makeClient('https://eos.greymass.com')

Expand Down Expand Up @@ -323,6 +324,13 @@ suite('Table', () => {
const row = await producersTable.get('teamgreymass')
assert.instanceOf(row.owner, Name)
})

test('should just get first row without params', async function () {
const table = eosio.table<EosioGlobalState>('global', EosioGlobalState)
const row = await table.get()
assert.instanceOf(row, EosioGlobalState)
assert.instanceOf(row.pervote_bucket, Int64)
})
})

suite('first', () => {
Expand Down

0 comments on commit 9b90b04

Please sign in to comment.