定制 drewlabs/http-query 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

drewlabs/http-query

最新稳定版本:v0.3.16

Composer 安装命令:

composer require drewlabs/http-query

包简介

HTTP Api query client sdk

README 文档

README

This library provides a client side implementation of HTTP query (similar to graphql) that allows developpers to easily customize data requested from web resources (that support the server side language implementation) and directly sending complex query parameters.

Usage

Query builder

The library comes with a query builder class that provides a fluent interface for building and compiling queries. An example is as follow:

  • Creating a query object
use Drewlabs\Query\Http\Query;

// Instruct the query object to use `http://127.0.0.1:8080` as base domain
$b = Query::new('http://127.0.0.1:8080')

            // Select the api path (path to the resource being queried)
            ->from('api/v1/examples')

            // Add a Bearer Authorization header to the request
            ->withAuthorization(BEARER_TOKEN);

As seen above, the API comes with numerous fluent methods for defining query intention:

  • eq

The eq method allow developpers to build a COLUMN=VALUE like query:

$b = $b->eq('title', 'Lorem Ipsum');
  • neq

The neq is the inverse of the eq query method:

$b = $b->neq('title', 'Lorem Ipsum');
  • lte / lt

The lte and lt respectively allow developper to construct a query that check if a column value is less than (less than or equal to) a given value.

$b = $b->lt('title', 'Lorem Ipsum')
       ->lte('title', 'Lorem Ipsum');
  • gte / gt

The gte and gt respectively allow developper to construct a query that check if a column value is greater than (greater than or equal to) a given value.

$b = $b->gt('title', 'Lorem Ipsum')
       ->gte('title', 'Lorem Ipsum');
  • in

The in clause search for value that exists in a list of provided values:

$b = $b->in('rates', [3, 3.5, 9]);
  • exists

The exists clause allow to query for relationship existance in the database.

$b = $b->exists('comments')
        ->exists('comments', new SubQuery('where', ['likes', '>', 1000]));
  • sort

The sort clause allow developpers to apply a sort by column on the result set:

Note The sort order is defined by an integer value. Any value greater than 0 get converted to ASC while any value less than 0 is converted to DESC

$b = $b->sort('created_at', -1); // ['sort' => ['by' => 'created_at', 'order' => 'DESC']]
  • select

The select clause allows developpers to specify the list of columns to select from the query server:

$b = $b->eq('title', 'Lorem Ipsum')->select('*', 'comments');

Note As shown in the examples above, query object provides a fluent api for building complex queries using PHP function calls.

Executing composed query

After building query using the fluent API, developpers can send query requests to backend servers using the execute method:

use Drewlabs\Query\Http\Query;

$b = Query::new('http://127.0.0.1:8080')
            ->from('api/v1/examples')
            ->withAuthorization(BEARER_TOKEN);

$b->date('created_at', '>', '2023-12-01')
    ->date('created_at', '<', '2025-02-01')
    ->exists('comments', fn($b) => $b->in('tags', [1, 4]));

// Executing the query
$result = $b->limit($limit)->execute(); // Calling execute runs the query against the HTTP api and return an instance of \Drewlabs\Query\Http\QueryResult object

print_r($result->getBody()); // returns the actual body of the query

print_r($result->first()); // return the first element of the list of items returned by the query

Aggregation framework

The query api also support some aggregation method for performing basic mathematic computation on the resource being selected instead of returning an entire collection:

  • min(string column, string relation): return the minimum value of the column in the selected resource table

  • max(string column, string relation): return the maximum value of the column in the selected resource table

  • sum(string column, string relation): computes and return the sum of all values in the given column in the selected resource table

  • avg(string column, string relation): computes and return the average of all values in the given column in the selected resource table

  • count(string column, string relation): Count the total elements matching matching the provided query parameters.

Note All aggregation method support a second parameter allowing developpers to query a releated resource in the application database.

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-11