-
Notifications
You must be signed in to change notification settings - Fork 285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: wallet repository search methods #3931
Conversation
This pull request introduces 1 alert when merging 8df4d75 into 08d8c93 - view on LGTM.com new alerts:
|
I made two new api clients in Testing clients (covering |
…system/core into refactor/wallet-repository-search
Search can be described as searching through API resources. Its functions are tailored for API use and aren't part of wallet repository anymore. A lot changes were made to the way controllers, resources, and search services interact together. Most of them are breaking. Please point me to other repositories I may have to update.
API methods related to wallet repository were massively updated while methods related to blocks and transactions not so much. Similar concepts are implemented differently yet use the same name which might be confusing.
Example
Searching entities from magistrate package is perfect simple example to dissect.
Resource
To start create
EntityResource
type definition:packages/core-magistrate-api/src/resources/entity.ts
Resources aren't transformer classes anymore. They are types that are eventually returned by API controllers.
In addition to
EntityResource
three other resource classesWalletResource
,DelegateResource
, andLockResource
were replaced with similar type definition. WhileBlockResource
andTransactionResource
were left untouched, they are transformer classes still.Criteria
When searching
EntityCriteria
type is used to filterEntityResource
. GenericContracts.Search.StandardCriteriaOf
type is used to convert resource type into criteria type:packages/core-magistrate-api/src/resources/entity.ts
Standard criteria properties carry
AND
meaning, array itemsOR
. Here is an example resigned businesses and developers criteria:Search service
EntitySearchService
connects the two. It acceptsEntityCriteria
and returnsEntityResource
:packages/core-magistrate-api/src/services/entity-search-service.ts
getEntityResourceFromWallet
Both
getEntity
andgetEntitiesPage
rely on privategetEntityResourceFromWallet
method to create resource object:packages/core-magistrate-api/src/services/entity-search-service.ts
Previously wallet and delegate resources had meaningful
transform
method, single wallet can be transformed into single delegate. Wallet repository had to improvise when searching through delegates because it didn't had delegate object at hand which was created at the very end of controller. Single wallet cannot be transformed into single lock or entity, there might be few. Theirtransform
methods were empty. Creation happened within wallet repository itself breaking isolation. Wallet repository was aware ofcore-magistrate
.This
getEntityResourceFromWallet
method feels liketransform
and is incore-magistrate-api
package, but it's semantically closer to the way locks and entities were created.getEntity
getEntity
implementation is straightforward. It looks for wallet in"entities"
index and usesgetEntityResourceFromWallet
to return resource:packages/core-magistrate-api/src/services/entity-search-service.ts
*getEntities
To save resources (isn't implemented downstream yet)
getEntitiesPage
is accompanied by private generator method*getEntities
. It creates and immediately filters out entities that do not match criteria:packages/core-magistrate-api/src/services/entity-search-service.ts
Services.Search.StandardCriteriaService
knows how to test ifT
matches standard criteriaContracts.Search.StandardCriteriaOf<T>
.getEntitiesPage
Finally the last method that returns page of entities. It sets default sorting and then feeds filtered
Iterable<EntityResource>
into pagination service for sorting and slicing:packages/core-magistrate-api/src/services/entity-search-service.ts
Services.Search.PaginationService
can sort over multiple properties. It currently does not take advantage of generators to save memory and is merelyArray.from().sort().slice()
.Controller validation
Controller
index
method gets criteria from query string whilesearch
gets criteria from payload. Schemas are built from schema object. It is defined in the same file as resource so if one is updated it's difficult to miss the other:packages/core-magistrate-api/src/resources/entity.ts
Controller
index
andsearch
methods directly returnContracts.Search.ResultsPage<EntityResource>
fromgetEntitiesPage
which suitable for pagination plugin.show
method has to wrapEntityResource
into{ data: EntityResource }
.