Skip to content

Commit

Permalink
enhancement: passing lower and upper_bound without data wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Jul 7, 2023
1 parent 371dfb9 commit ab236e2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/contract/table-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ export class TableCursor<TableRow> {
table: this.table,
tableParams: {
...this.tableParams,
lower_bound: wrapIndexValue(query.from) || this.tableParams.lower_bound,
upper_bound: wrapIndexValue(query.to) || this.tableParams.upper_bound,
lower_bound: query.from || this.tableParams.lower_bound,
upper_bound: query.to || this.tableParams.upper_bound,
},
})
}
Expand Down
33 changes: 20 additions & 13 deletions src/contract/table.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import {ABI, ABISerializableConstructor, API, Name, NameType, UInt64} from '@wharfkit/session'
import {ABI, ABISerializableConstructor, API, Name, NameType} from '@wharfkit/session'
import type {Contract} from '../contract'
import {indexPositionInWords, wrapIndexValue} from '../utils'
import {indexPositionInWords} from '../utils'
import {TableCursor} from './table-cursor'

export interface QueryOptions {
index?: string
scope?: NameType
index_type?: API.v1.TableIndexType
key_type?: keyof API.v1.TableIndexTypes
}

export interface WhereQueryOptions extends QueryOptions {
limit?: number
}

export interface WhereQuery {
from: number | string | UInt64
to: number | string | UInt64
from: API.v1.TableIndexType | string
to: API.v1.TableIndexType | string
}

interface FieldToIndex {
Expand Down Expand Up @@ -95,7 +95,7 @@ export class Table<TableRow extends ABISerializableConstructor = ABISerializable
*/
query(
query: WhereQuery,
{limit = 10, scope = this.contract.account, index, index_type}: WhereQueryOptions = {}
{limit = 10, scope = this.contract.account, index, key_type}: WhereQueryOptions = {}
): TableCursor<TableRow> {
if (!query) {
throw new Error('Index value range must be provided')
Expand All @@ -113,8 +113,9 @@ export class Table<TableRow extends ABISerializableConstructor = ABISerializable
scope,
type: this.rowType,
limit,
lower_bound: wrapIndexValue(from),
upper_bound: wrapIndexValue(from),
lower_bound: from,
upper_bound: to,
key_type: key_type,
}

return new TableCursor({
Expand All @@ -131,24 +132,30 @@ export class Table<TableRow extends ABISerializableConstructor = ABISerializable
* Each key-value pair in the queryParams object corresponds to a field and its expected value in the table.
* @returns {Promise<TableRow>} Promise resolving to a single table row.
*/
async get(query, queryOptions?: QueryOptions): Promise<TableRow> {
if (!query) {
async get(
queryValue: API.v1.TableIndexType | string,
queryOptions?: QueryOptions
): Promise<TableRow> {
if (!queryValue) {
throw new Error('Index value must be provided')
}

const fieldToIndexMapping = await this.getFieldToIndex()

console.log({queryValue})

const tableRowsParams = {
table: this.name,
code: this.contract.account,
scope: this.contract.account,
type: this.rowType,
type: this.rowType!,
limit: 1,
lower_bound: typeof query === 'string' ? Name.from(query) : UInt64.from(query),
upper_bound: typeof query === 'string' ? Name.from(query) : UInt64.from(query),
lower_bound: queryValue,
upper_bound: queryValue,
index_position: queryOptions?.index
? fieldToIndexMapping[queryOptions?.index].index_position
: 'primary',
key_type: queryOptions?.key_type,
}

const {rows} = await this.contract.client!.v1.chain.get_table_rows(tableRowsParams)
Expand Down
28 changes: 28 additions & 0 deletions test/data/requests/209e329f05655a9957f80aa9b1c231a159d5f9fe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"request": {
"path": "https://eos.greymass.com/v1/chain/get_table_rows",
"params": {
"method": "POST",
"body": "{\"table\":\"trending\",\"code\":\"decentiumorg\",\"scope\":\"decentiumorg\",\"limit\":1,\"lower_bound\":\"5\",\"upper_bound\":\"5\",\"index_position\":\"primary\",\"key_type\":\"name\",\"json\":true}"
}
},
"headers": {
"access-control-allow-headers": "X-Requested-With,Accept,Content-Type,Origin",
"access-control-allow-methods": "GET, POST, OPTIONS",
"access-control-allow-origin": "*",
"connection": "close",
"content-length": "38",
"content-type": "application/json",
"date": "Fri, 07 Jul 2023 14:04:10 GMT",
"host": "eos.greymass.com",
"server": "nginx",
"x-cached": "MISS"
},
"status": 200,
"json": {
"rows": [],
"more": false,
"next_key": ""
},
"text": "{\"rows\":[],\"more\":false,\"next_key\":\"\"}"
}
8 changes: 6 additions & 2 deletions test/tests/table.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {assert} from 'chai'

import {UInt64} from '@wharfkit/session'

import {Table, Contract} from '$lib'

import {makeClient} from '../utils/mock-client'
Expand Down Expand Up @@ -91,9 +93,11 @@ suite('Table', () => {
})
})

suite('find', () => {
suite('get', () => {
test('should fetch table row correctly when filtering by primary index is used', async () => {
const row = await decentiumTrendingTable.get(5)
const row = await decentiumTrendingTable.get(5, {key_type: 'i64'})

console.log({row})

assert.deepEqual(row, {
id: 5,
Expand Down

0 comments on commit ab236e2

Please sign in to comment.