From c8c774db1527513c0515145655168c37158d620f Mon Sep 17 00:00:00 2001 From: Daniel Fugere Date: Tue, 30 Jan 2024 09:31:13 -0800 Subject: [PATCH] add reverse param to query method (#67) * enhancement: add reverse param to query method closes issue #46 * Added test for reverse --------- Co-authored-by: Aaron Cox --- src/contract/table.ts | 4 ++++ ...68b42051d704bf454c567035d80e7be11dfaf.json | 19 +++++++++++++++++++ test/tests/table.ts | 10 ++++++++++ 3 files changed, 33 insertions(+) create mode 100644 test/data/requests/74068b42051d704bf454c567035d80e7be11dfaf.json diff --git a/src/contract/table.ts b/src/contract/table.ts index b204d1c..9d675a5 100644 --- a/src/contract/table.ts +++ b/src/contract/table.ts @@ -14,6 +14,7 @@ export interface QueryParams { to?: API.v1.TableIndexType | string | number maxRows?: number rowsPerAPIRequest?: number + reverse?: boolean } interface FieldToIndex { @@ -123,6 +124,7 @@ export class Table { lower_bound: wrapIndexValue(params.from), upper_bound: wrapIndexValue(params.to), limit: params.rowsPerAPIRequest || this.defaultRowLimit, + reverse: params.reverse, } if (params.index) { @@ -173,6 +175,7 @@ export class Table { index_position: params.index_position, key_type: params.key_type, json: false, + reverse: params.reverse, } if (params.index) { @@ -259,6 +262,7 @@ export class Table { lower_bound: wrapIndexValue(params.from), upper_bound: wrapIndexValue(params.to), limit: params.rowsPerAPIRequest || this.defaultRowLimit, + reverse: params.reverse, } return new TableScopeCursor({ diff --git a/test/data/requests/74068b42051d704bf454c567035d80e7be11dfaf.json b/test/data/requests/74068b42051d704bf454c567035d80e7be11dfaf.json new file mode 100644 index 0000000..190a8c9 --- /dev/null +++ b/test/data/requests/74068b42051d704bf454c567035d80e7be11dfaf.json @@ -0,0 +1,19 @@ +{ + "request": { + "path": "https://eos.greymass.com/v1/chain/get_table_rows", + "params": { + "method": "POST", + "body": "{\"json\":false,\"limit\":1000,\"table\":\"trending\",\"code\":\"decentiumorg\",\"scope\":\"decentiumorg\",\"key_type\":\"i64\",\"lower_bound\":\"5\",\"upper_bound\":\"6\",\"reverse\":true}" + } + }, + "status": 200, + "json": { + "rows": [ + "06000000000000000895010000000000104d76cca58c6534ca009b2e9baba662bfbfed5c000000006c958ee2037f9a9c039e4c50d4f9473dc7972fa19a2808d37d988f24cba96d0ff3b41474754673da3801598bc003cc70fbcc0a5dfc134b91db86ecbae70d497fd2f2e8bc32e31aa1aaee41c4e61d010000000000000000000000000000", + "05000000000000004190010000000000a02ecd5845b73055a02ecd5845b730557d41ec5c000090dae5a9904a039c9e9903de1a09302017b9bbe1ba8aec85617dd6513aaf4bc65e5e1e3663be34cd9cfaac00010000000000000000000000000000" + ], + "more": false, + "next_key": "" + }, + "text": "{\"rows\":[\"06000000000000000895010000000000104d76cca58c6534ca009b2e9baba662bfbfed5c000000006c958ee2037f9a9c039e4c50d4f9473dc7972fa19a2808d37d988f24cba96d0ff3b41474754673da3801598bc003cc70fbcc0a5dfc134b91db86ecbae70d497fd2f2e8bc32e31aa1aaee41c4e61d010000000000000000000000000000\",\"05000000000000004190010000000000a02ecd5845b73055a02ecd5845b730557d41ec5c000090dae5a9904a039c9e9903de1a09302017b9bbe1ba8aec85617dd6513aaf4bc65e5e1e3663be34cd9cfaac00010000000000000000000000000000\"],\"more\":false,\"next_key\":\"\"}" +} \ No newline at end of file diff --git a/test/tests/table.ts b/test/tests/table.ts index 150e1c4..844da43 100644 --- a/test/tests/table.ts +++ b/test/tests/table.ts @@ -302,6 +302,16 @@ suite('Table', () => { assert.equal(rowsSellTxtScope.length, 348) }) }) + + test('reverse', async function () { + const tableRowCursor = decentiumTrendingTable.query({from: 5, to: 6, reverse: true}) + tableRowCursor.reset() + + assert.deepEqual( + Serializer.objectify(await tableRowCursor.next()).map((row) => row.id), + [6, 5] + ) + }) }) suite('get', () => {