Source: client/query-request.js

  1. /*
  2. * Copyright 2023, TeamDev. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Redistribution and use in source and/or binary forms, with or without
  11. * modification, must retain the above copyright notice and the following
  12. * disclaimer.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. import {OrderBy} from "../proto/spine/client/query_pb";
  27. import {FilteringRequest} from "./filtering-request";
  28. /**
  29. * A request to retrieve entities of the given type.
  30. *
  31. * Allows to post the query data to the Spine backend and receive the entity states as `Promise`.
  32. *
  33. * A usage example:
  34. * ```
  35. * const customers =
  36. * client.select(Customer.class)
  37. * .byId(westCoastCustomerIds())
  38. * .withMask("name", "address", "email")
  39. * .where([Filters.eq("type", "permanent"),
  40. * Filters.eq("discount_percent", 10),
  41. * Filters.eq("company_size", Company.Size.SMALL)])
  42. * .orderBy("name", OrderBy.Direction.ASCENDING)
  43. * .limit(20)
  44. * .run(); // The returned type is `Promise<Customer[]>`.
  45. * ```
  46. *
  47. * All of the called filtering methods are optional. If none of them are specified, all entities
  48. * of type will be retrieved.
  49. *
  50. * @template <T> the query target type
  51. */
  52. export class QueryRequest extends FilteringRequest {
  53. /**
  54. * @param {!Class<Message>} targetType the target type of entities
  55. * @param {!Client} client the client which initiated the request
  56. * @param {!ActorRequestFactory} actorRequestFactory the request factory
  57. */
  58. constructor(targetType, client, actorRequestFactory) {
  59. super(targetType, client, actorRequestFactory)
  60. }
  61. /**
  62. * Sets the sorting order for the retrieved results.
  63. *
  64. * @param {!String} column the column to order by
  65. * @param {!OrderBy.Direction} direction the ascending/descending direction
  66. * @return {this} self for method chaining
  67. */
  68. orderBy(column, direction) {
  69. if (direction === OrderBy.Direction.ASCENDING) {
  70. this._builder().orderAscendingBy(column);
  71. } else {
  72. this._builder().orderDescendingBy(column);
  73. }
  74. return this._self();
  75. }
  76. /**
  77. * Sets the maximum number of returned entities.
  78. *
  79. * Can only be used in conjunction with the {@link #orderBy} condition.
  80. *
  81. * @param {number} count the max number of response entities
  82. * @return {this} self for method chaining
  83. */
  84. limit(count) {
  85. this._builder().limit(count);
  86. return this._self();
  87. }
  88. /**
  89. * Builds a `Query` instance based on currently specified filters.
  90. *
  91. * @return {spine.client.Query} a `Query` instance
  92. */
  93. query() {
  94. return this._builder().build();
  95. }
  96. /**
  97. * Runs the query and obtains the results.
  98. *
  99. * @return {Promise<Message[]>} the asynchronously resolved query results
  100. */
  101. run() {
  102. const query = this.query();
  103. return this._client.read(query);
  104. }
  105. /**
  106. * @inheritDoc
  107. */
  108. _newBuilderFn() {
  109. return requestFactory => requestFactory.query().select(this.targetType);
  110. }
  111. /**
  112. * @inheritDoc
  113. */
  114. _self() {
  115. return this;
  116. }
  117. }