From 45745704383df934c530c760e35136dbc16638f5 Mon Sep 17 00:00:00 2001 From: Aaron Cox Date: Tue, 25 Jul 2023 22:55:32 -0700 Subject: [PATCH] Reworked query params --- src/contract/table.ts | 51 +++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/src/contract/table.ts b/src/contract/table.ts index ddb5725..d382690 100644 --- a/src/contract/table.ts +++ b/src/contract/table.ts @@ -2,7 +2,7 @@ import {ABI, ABIDef, API, APIClient, Name, NameType, Serializer} from '@greymass import {indexPositionInWords, wrapIndexValue} from '../utils' import {TableCursor} from './table-cursor' -export interface Query { +export interface QueryParams { index?: string scope?: NameType key_type?: keyof API.v1.TableIndexTypes @@ -103,35 +103,35 @@ export class Table { * - `limit`: Maximum number of rows to return. * @returns {TableCursor} Promise resolving to a `TableCursor` of the filtered table rows. */ - query(query: Query = {}): TableCursor { + query(params: QueryParams = {}): TableCursor { const tableRowsParams: any = { // Table query table: this.name, code: this.account, - scope: query.scope || this.account, + scope: params.scope || this.account, // Response tping type: this.rowType, // Filtering - key_type: query.key_type, - lower_bound: wrapIndexValue(query.from), - upper_bound: wrapIndexValue(query.to), - limit: query.rowsPerAPIRequest || this.defaultRowLimit, + key_type: params.key_type, + lower_bound: wrapIndexValue(params.from), + upper_bound: wrapIndexValue(params.to), + limit: params.rowsPerAPIRequest || this.defaultRowLimit, } - if (query.index) { + if (params.index) { const fieldToIndexMapping = this.getFieldToIndex() - if (!fieldToIndexMapping[query.index]) { - throw new Error(`Field ${query.index} is not a valid index.`) + if (!fieldToIndexMapping[params.index]) { + throw new Error(`Field ${params.index} is not a valid index.`) } - tableRowsParams.index_position = fieldToIndexMapping[query.index].index_position + tableRowsParams.index_position = fieldToIndexMapping[params.index].index_position } return new TableCursor({ abi: this.abi, client: this.client, - maxRows: query.maxRows, + maxRows: params.maxRows, params: tableRowsParams, }) } @@ -143,22 +143,21 @@ export class Table { * Each key-value pair in the queryParams object corresponds to a field and its expected value in the table. * @returns {Promise} Promise resolving to a single table row. */ - async get( - queryValue?: API.v1.TableIndexType | string, - {scope = this.account, index, key_type, json = false}: Query = {} - ): Promise { + async get(value?: API.v1.TableIndexType | string, params: QueryParams = {}): Promise { const fieldToIndexMapping = this.getFieldToIndex() const tableRowsParams = { table: this.name, code: this.account, - scope, + scope: params.scope || this.account, type: this.rowType!, limit: 1, - lower_bound: wrapIndexValue(queryValue), - upper_bound: wrapIndexValue(queryValue), - index_position: index ? fieldToIndexMapping[index].index_position : 'primary', - key_type: key_type, + lower_bound: wrapIndexValue(value), + upper_bound: wrapIndexValue(value), + index_position: params.index + ? fieldToIndexMapping[params.index].index_position + : 'primary', + key_type: params.key_type, json: false, } @@ -173,7 +172,7 @@ export class Table { }) } - if (json) { + if (params.json) { row = Serializer.objectify(row) } @@ -188,9 +187,9 @@ export class Table { * - `limit`: Maximum number of rows to return. * @returns {TableCursor} Promise resolving to a `TableCursor` of the table rows. */ - first(maxRows: number, query: Query = {}): TableCursor { + first(maxRows: number, params: QueryParams = {}): TableCursor { return this.query({ - ...query, + ...params, maxRows, }) } @@ -199,8 +198,8 @@ export class Table { * Returns all the rows from the table. * @returns {Promise} Promise resolving to an array of table rows. */ - async all(): Promise { - return this.query().all() + async all(params: QueryParams = {}): Promise { + return this.query(params).all() } getFieldToIndex() {