From ac5cdbc07b8a0e319d75d7acab7b7f3f25f26075 Mon Sep 17 00:00:00 2001 From: dafuga Date: Thu, 6 Jul 2023 17:40:40 -0400 Subject: [PATCH] enhancement: renamed where to query and find to get --- contracts/decentiumorg.ts | 40 +++++++++++++++--------------- src/codegen/table.ts | 2 +- src/contract.ts | 2 +- src/contract/table-cursor.ts | 10 +++----- src/contract/table.ts | 8 +++--- src/example-contract.ts | 8 +++--- src/utils.ts | 21 +++++++++++----- test/codegen-samples/rewards.gm.ts | 16 ++++++------ test/data/contracts/token.ts | 16 ++++++------ test/tests/codegen.ts | 4 +-- test/tests/table.ts | 12 ++++----- 11 files changed, 72 insertions(+), 67 deletions(-) diff --git a/contracts/decentiumorg.ts b/contracts/decentiumorg.ts index 5532b7f..5916879 100644 --- a/contracts/decentiumorg.ts +++ b/contracts/decentiumorg.ts @@ -171,7 +171,7 @@ export namespace _Decentiumorg { export namespace tables { export class blogs { static fieldToIndex = {author: {type: 'name', index_position: 'primary'}} - static where( + static query( {limit = 10}, queryParams: _Decentiumorg.types.blogsQueryParams, client: APIClient @@ -183,9 +183,9 @@ export namespace _Decentiumorg { rowType: _Decentiumorg.types.Blog_row, fieldToIndex: blogs.fieldToIndex, }) - return blogsTable.where(queryParams) + return blogsTable.query(queryParams) } - static find( + static get( queryParams: _Decentiumorg.types.blogsQueryParams, client: APIClient ): Promise<_Decentiumorg.types.Blog_row> { @@ -196,7 +196,7 @@ export namespace _Decentiumorg { rowType: _Decentiumorg.types.Blog_row, fieldToIndex: blogs.fieldToIndex, }) - return blogsTable.find(queryParams) + return blogsTable.get(queryParams) } static all( queryParams: _Decentiumorg.types.blogsQueryParams, @@ -218,7 +218,7 @@ export namespace _Decentiumorg { from: {type: 'uint128', index_position: 'secondary'}, to: {type: 'uint128', index_position: 'tertiary'}, } - static where( + static query( {limit = 10}, queryParams: _Decentiumorg.types.linksQueryParams, client: APIClient @@ -230,9 +230,9 @@ export namespace _Decentiumorg { rowType: _Decentiumorg.types.Link_row, fieldToIndex: links.fieldToIndex, }) - return linksTable.where(queryParams) + return linksTable.query(queryParams) } - static find( + static get( queryParams: _Decentiumorg.types.linksQueryParams, client: APIClient ): Promise<_Decentiumorg.types.Link_row> { @@ -243,7 +243,7 @@ export namespace _Decentiumorg { rowType: _Decentiumorg.types.Link_row, fieldToIndex: links.fieldToIndex, }) - return linksTable.find(queryParams) + return linksTable.get(queryParams) } static all( queryParams: _Decentiumorg.types.linksQueryParams, @@ -264,7 +264,7 @@ export namespace _Decentiumorg { slug: {type: 'name', index_position: 'primary'}, updated: {type: 'uint64', index_position: 'secondary'}, } - static where( + static query( {limit = 10}, queryParams: _Decentiumorg.types.postsQueryParams, client: APIClient @@ -276,9 +276,9 @@ export namespace _Decentiumorg { rowType: _Decentiumorg.types.Post_row, fieldToIndex: posts.fieldToIndex, }) - return postsTable.where(queryParams) + return postsTable.query(queryParams) } - static find( + static get( queryParams: _Decentiumorg.types.postsQueryParams, client: APIClient ): Promise<_Decentiumorg.types.Post_row> { @@ -289,7 +289,7 @@ export namespace _Decentiumorg { rowType: _Decentiumorg.types.Post_row, fieldToIndex: posts.fieldToIndex, }) - return postsTable.find(queryParams) + return postsTable.get(queryParams) } static all( queryParams: _Decentiumorg.types.postsQueryParams, @@ -307,7 +307,7 @@ export namespace _Decentiumorg { } export class state { static fieldToIndex = {} - static where( + static query( {limit = 10}, queryParams: _Decentiumorg.types.stateQueryParams, client: APIClient @@ -319,9 +319,9 @@ export namespace _Decentiumorg { rowType: _Decentiumorg.types.State, fieldToIndex: state.fieldToIndex, }) - return stateTable.where(queryParams) + return stateTable.query(queryParams) } - static find( + static get( queryParams: _Decentiumorg.types.stateQueryParams, client: APIClient ): Promise<_Decentiumorg.types.State> { @@ -332,7 +332,7 @@ export namespace _Decentiumorg { rowType: _Decentiumorg.types.State, fieldToIndex: state.fieldToIndex, }) - return stateTable.find(queryParams) + return stateTable.get(queryParams) } static all( queryParams: _Decentiumorg.types.stateQueryParams, @@ -355,7 +355,7 @@ export namespace _Decentiumorg { cscore: {type: 'uint128', index_position: 'tertiary'}, permlink: {type: 'uint128', index_position: 'fourth'}, } - static where( + static query( {limit = 10}, queryParams: _Decentiumorg.types.trendingQueryParams, client: APIClient @@ -367,9 +367,9 @@ export namespace _Decentiumorg { rowType: _Decentiumorg.types.Trending_row, fieldToIndex: trending.fieldToIndex, }) - return trendingTable.where(queryParams) + return trendingTable.query(queryParams) } - static find( + static get( queryParams: _Decentiumorg.types.trendingQueryParams, client: APIClient ): Promise<_Decentiumorg.types.Trending_row> { @@ -380,7 +380,7 @@ export namespace _Decentiumorg { rowType: _Decentiumorg.types.Trending_row, fieldToIndex: trending.fieldToIndex, }) - return trendingTable.find(queryParams) + return trendingTable.get(queryParams) } static all( queryParams: _Decentiumorg.types.trendingQueryParams, diff --git a/src/codegen/table.ts b/src/codegen/table.ts index b857e84..56fada2 100644 --- a/src/codegen/table.ts +++ b/src/codegen/table.ts @@ -6,7 +6,7 @@ import {capitalize} from '../utils' export async function generateTableClass(contractName, namespaceName, table, abi) { const tableName = table.name - const struct = abi.structs.find((struct) => struct.name === table.type) + const struct = abi.structs.get((struct) => struct.name === table.type) const members: ts.ClassElement[] = [] const rowType = `${namespaceName}.types.${capitalize(struct.name)}` diff --git a/src/contract.ts b/src/contract.ts index 0be4390..b937446 100644 --- a/src/contract.ts +++ b/src/contract.ts @@ -104,7 +104,7 @@ export class Contract { async getTable(name: NameType): Promise { const tables = await this.getTables() - const table = tables.find((table) => table.name.equals(name)) + const table = tables.get((table) => table.name.equals(name)) if (!table) { throw new Error(`No table found with name ${name}`) diff --git a/src/contract/table-cursor.ts b/src/contract/table-cursor.ts index 7b25328..304f022 100644 --- a/src/contract/table-cursor.ts +++ b/src/contract/table-cursor.ts @@ -161,17 +161,13 @@ export class TableCursor { * * @returns A new cursor with updated parameters. */ - where(query: WhereQuery, queryOptions?: QueryOptions) { + query(query: WhereQuery, queryOptions?: QueryOptions) { return new TableCursor({ table: this.table, tableParams: { ...this.tableParams, - lower_bound: - wrapIndexValue(query.from, queryOptions?.index_type) || - this.tableParams.lower_bound, - upper_bound: - wrapIndexValue(query.to, queryOptions?.index_type) || - this.tableParams.upper_bound, + lower_bound: wrapIndexValue(query.from) || this.tableParams.lower_bound, + upper_bound: wrapIndexValue(query.to) || this.tableParams.upper_bound, }, }) } diff --git a/src/contract/table.ts b/src/contract/table.ts index 3781b01..de48cf9 100644 --- a/src/contract/table.ts +++ b/src/contract/table.ts @@ -93,7 +93,7 @@ export class Table} Promise resolving to a `TableCursor` of the filtered table rows. */ - where( + query( query: WhereQuery, {limit = 10, scope = this.contract.account, index, index_type}: WhereQueryOptions = {} ): TableCursor { @@ -113,8 +113,8 @@ export class Table} Promise resolving to a single table row. */ - async find(query, queryOptions?: QueryOptions): Promise { + async get(query, queryOptions?: QueryOptions): Promise { if (!query) { throw new Error('Index value must be provided') } diff --git a/src/example-contract.ts b/src/example-contract.ts index b02e965..f4071e2 100644 --- a/src/example-contract.ts +++ b/src/example-contract.ts @@ -48,7 +48,7 @@ export namespace _Blog { }, } - static where( + static query( queryParams: _Blog.types.UsersWhereQueryParams, {limit = 10} = {}, client: APIClient @@ -60,10 +60,10 @@ export namespace _Blog { fieldToIndex: Users.fieldToIndex, }) - return usersTable.where(queryParams, {limit}) + return usersTable.query(queryParams, {limit}) } - static async find( + static async get( queryParams: _Blog.types.UsersFindQueryParams, client: APIClient ): Promise<_Blog.types.UsersRow> { @@ -74,7 +74,7 @@ export namespace _Blog { fieldToIndex: Users.fieldToIndex, }) - return usersTable.find(queryParams) + return usersTable.get(queryParams) } static first(limit, client): TableCursor<_Blog.types.UsersRow> { diff --git a/src/utils.ts b/src/utils.ts index 9d77143..047fabd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,7 +3,11 @@ import { ABISerializableConstructor, ABISerializableObject, API, + isInstanceOf, Name, + UInt128, + UInt32, + UInt64, } from '@wharfkit/session' export function pascalCase(value: string): string { @@ -50,16 +54,21 @@ export function indexPositionInWords(index: number): string { ][index] } -export function wrapIndexValue( - value, - indexType?: API.v1.TableIndexType -): API.v1.TableIndexType | undefined { +export function wrapIndexValue(value): API.v1.TableIndexType | undefined { if (!value) { return } - if (indexType) { - return indexType.from(value) + if ( + isInstanceOf(value, UInt128) || + isInstanceOf(value, UInt64) || + isInstanceOf(value, UInt32) + ) { + return value + } + + if (typeof value === 'number') { + return UInt64.from(value) } return Name.from(value) diff --git a/test/codegen-samples/rewards.gm.ts b/test/codegen-samples/rewards.gm.ts index 4b8186f..4485a38 100644 --- a/test/codegen-samples/rewards.gm.ts +++ b/test/codegen-samples/rewards.gm.ts @@ -65,7 +65,7 @@ export namespace _RewardsGm { export namespace tables { export class config { static fieldToIndex = {} - static where( + static query( queryParams: _RewardsGm.types.ConfigWhereQueryParams, getTableRowsOptions: GetTableRowsOptions, client: APIClient @@ -76,9 +76,9 @@ export namespace _RewardsGm { rowType: _RewardsGm.types.Config, fieldToIndex: config.fieldToIndex, }) - return configTable.where(queryParams, getTableRowsOptions) + return configTable.query(queryParams, getTableRowsOptions) } - static find( + static get( queryParams: _RewardsGm.types.ConfigFindQueryParams, client: APIClient ): Promise<_RewardsGm.types.Config> { @@ -88,7 +88,7 @@ export namespace _RewardsGm { rowType: _RewardsGm.types.Config, fieldToIndex: config.fieldToIndex, }) - return configTable.find(queryParams) + return configTable.get(queryParams) } static first(limit: number, client: APIClient): TableCursor<_RewardsGm.types.Config> { const configTable = Table.from({ @@ -102,7 +102,7 @@ export namespace _RewardsGm { } export class users { static fieldToIndex = {} - static where( + static query( queryParams: _RewardsGm.types.UsersWhereQueryParams, getTableRowsOptions: GetTableRowsOptions, client: APIClient @@ -113,9 +113,9 @@ export namespace _RewardsGm { rowType: _RewardsGm.types.User_row, fieldToIndex: users.fieldToIndex, }) - return usersTable.where(queryParams, getTableRowsOptions) + return usersTable.query(queryParams, getTableRowsOptions) } - static find( + static get( queryParams: _RewardsGm.types.UsersFindQueryParams, client: APIClient ): Promise<_RewardsGm.types.User_row> { @@ -125,7 +125,7 @@ export namespace _RewardsGm { rowType: _RewardsGm.types.User_row, fieldToIndex: users.fieldToIndex, }) - return usersTable.find(queryParams) + return usersTable.get(queryParams) } static first(limit: number, client: APIClient): TableCursor<_RewardsGm.types.User_row> { const usersTable = Table.from({ diff --git a/test/data/contracts/token.ts b/test/data/contracts/token.ts index fc38e67..960535d 100644 --- a/test/data/contracts/token.ts +++ b/test/data/contracts/token.ts @@ -81,7 +81,7 @@ // export namespace tables { // export class accounts { // static fieldToIndex = {balance: {type: 'balance', index_position: 'primary'}} -// static where( +// static query( // queryParams: _EosioToken.types.AccountsWhereQueryParams, // getTableRowsOptions: GetTableRowsOptions, // client: APIClient @@ -92,9 +92,9 @@ // rowType: _EosioToken.types.Account, // fieldToIndex: accounts.fieldToIndex, // }) -// return accountsTable.where(queryParams, getTableRowsOptions) +// return accountsTable.query(queryParams, getTableRowsOptions) // } -// static find( +// static get( // queryParams: _EosioToken.types.AccountsFindQueryParams, // client: APIClient // ): Promise<_EosioToken.types.Account> { @@ -104,7 +104,7 @@ // rowType: _EosioToken.types.Account, // fieldToIndex: accounts.fieldToIndex, // }) -// return accountsTable.find(queryParams) +// return accountsTable.get(queryParams) // } // static first(limit: number, client: APIClient): TableCursor<_EosioToken.types.Account> { // const accountsTable = Table.from({ @@ -136,7 +136,7 @@ // } // export class stat { // static fieldToIndex = {supply: {type: 'supply', index_position: 'primary'}} -// static where( +// static query( // queryParams: _EosioToken.types.StatWhereQueryParams, // getTableRowsOptions: GetTableRowsOptions, // client: APIClient @@ -147,9 +147,9 @@ // rowType: _EosioToken.types.Currency_stats, // fieldToIndex: stat.fieldToIndex, // }) -// return statTable.where(queryParams, getTableRowsOptions) +// return statTable.query(queryParams, getTableRowsOptions) // } -// static find( +// static get( // queryParams: _EosioToken.types.StatFindQueryParams, // client: APIClient // ): Promise<_EosioToken.types.Currency_stats> { @@ -159,7 +159,7 @@ // rowType: _EosioToken.types.Currency_stats, // fieldToIndex: stat.fieldToIndex, // }) -// return statTable.find(queryParams) +// return statTable.get(queryParams) // } // static first( // limit: number, diff --git a/test/tests/codegen.ts b/test/tests/codegen.ts index 2ae6a01..bd8c3d2 100644 --- a/test/tests/codegen.ts +++ b/test/tests/codegen.ts @@ -58,7 +58,7 @@ suite('codegen', function () { suite('where', function () { test('returns a cursor that lets you filter rows', async function () { const rows = await _RewardsGm.tables.users - .where({from: 'dafuga.gm', to: 'tony.gm'}, {}, client) + .query({from: 'dafuga.gm', to: 'tony.gm'}, {}, client) .all() assert.equal(rows.length, 6) @@ -67,7 +67,7 @@ suite('codegen', function () { suite('find', function () { test('returns a single row', async function () { - const row = await _RewardsGm.tables.users.find('dafuga.gm', {}, client) + const row = await _RewardsGm.tables.users.get('dafuga.gm', {}, client) assert.equal(String(row.account), 'dafuga.gm') }) diff --git a/test/tests/table.ts b/test/tests/table.ts index 11a98f9..f7ae722 100644 --- a/test/tests/table.ts +++ b/test/tests/table.ts @@ -31,7 +31,7 @@ suite('Table', () => { suite('cursor', () => { suite('reset', () => { test('should allow you to reset the cursor', async () => { - const tableCursor = decentiumTrendingTable.where({from: 5, to: 6}) + const tableCursor = decentiumTrendingTable.query({from: 5, to: 6}) assert.deepEqual( (await tableCursor.next()).map((row) => row.id), @@ -56,7 +56,7 @@ suite('Table', () => { suite('where', () => { suite('all', () => { test('should fetch table rows correctly when filtering is used', async () => { - const tableCursor = decentiumTrendingTable.where( + const tableCursor = decentiumTrendingTable.query( { from: 101511, to: 105056, @@ -73,7 +73,7 @@ suite('Table', () => { }) test('should fetch correct number of table rows when limit option is used', async () => { - const tableCursor = decentiumTrendingTable.where({from: 5, to: 10}, {limit: 2}) + const tableCursor = decentiumTrendingTable.query({from: 5, to: 10}, {limit: 2}) assert.deepEqual( (await tableCursor.all()).map((row) => row.id), @@ -84,7 +84,7 @@ suite('Table', () => { suite('next', () => { test('should allow you to fetch more rows after first request', async () => { - const tableCursor = decentiumTrendingTable.where({from: 5}, {limit: 10000}) + const tableCursor = decentiumTrendingTable.query({from: 5}, {limit: 10000}) assert.equal((await tableCursor.next()).length, 235) assert.equal((await tableCursor.next()).length, 0) }) @@ -93,7 +93,7 @@ suite('Table', () => { suite('find', () => { test('should fetch table row correctly when filtering by primary index is used', async () => { - const row = await decentiumTrendingTable.find(5) + const row = await decentiumTrendingTable.get(5) assert.deepEqual(row, { id: 5, @@ -123,7 +123,7 @@ suite('Table', () => { }) test('should fetch table row correctly with default filtering', async () => { // curl http://eos.greymass.com/v1/chain/get_table_rows -d '{"table":"producers","limit":10,"code":"eosio","scope":"eosio","json":true, "lower_bound": "teamgreymass", "upper_bound": "teamgreymass"}' - const row = await producersTable.find('teamgreymass') + const row = await producersTable.get('teamgreymass') assert.deepEqual(row, { owner: 'teamgreymass',