aiotu/terseq
Composer 安装命令:
composer require aiotu/terseq
包简介
Query Builder for AWS DynamoDB
README 文档
README
This document provides a comprehensive guide on how to utilize the Terseq library to build and execute queries on AWS DynamoDB using the AWS SDK for PHP.
Features
Terseq supports building queries for the following DynamoDB operations:
Single-item operations
Query operations
Transactions
Batch
Why I should use this library?
AWS SDK for PHP is a powerful tool for working with AWS services, but it can be challenging to use due to its complexity. It requires a lot of boilerplate code to build and execute queries. Terseq simplifies this process by providing a fluent interface to build queries for DynamoDB operations. It also supports single-table design, which is a recommended practice for DynamoDB.
Installation
To install the Terseq package, run the following command in your project directory using Composer:
composer require aiotu/terseq
Usage
Initialize
Create client by AWS SDK and DatabaseManager
$client = new \Aws\DynamoDb\DynamoDbClient([ 'region' => 'us-west-2', 'version' => 'latest', ]); $manager = new \Terseq\DatabaseManager($client, new Marshaler());
Operations
GetItem
$manager->getItem() ->table(['Books', 'BookId']) ->pk('super-cool-id') ->dispatch();
PutItem
$manager->putItem() ->table(['Books', 'BookId']) ->item([ 'BookId' => 'super-cool-id', 'Title' => 'Super Cool Book', 'Author' => 'Super Cool Author', ]) ->dispatch();
UpdateItem
$manager->updateItem() ->table(['Books', 'BookId']) ->pk('super-cool-id') ->set('Title', 'Super Cool Book Updated') ->set('Author', 'Super Cool Author Updated') ->dispatch();
DeleteItem
$manager->deleteItem() ->table(['Books', 'BookId']) ->pk('super-cool-id') ->dispatch();
Query
$result = $manager->query() ->table(['Books', 'BookId']) ->pk('super-cool-id') ->consistentRead() ->dispatch();
TransactGetItems
use Terseq\Builders\Operations\TransactGetItems\Operations\Get; $manager->transactGetItems() ->get( [ static fn (Get $get) => $get->pk('super-cool-id1'), static fn (Get $get) => $get->pk('super-cool-id2'), ], table: ['Books', 'BookId'], ) ->dispatch();
TransactWriteItems
use Terseq\Builders\Operations\TransactWriteItems\Operations\Delete; use Terseq\Builders\Operations\TransactWriteItems\Operations\Put; use Terseq\Builders\Operations\TransactWriteItems\Operations\Update; $manager->transactWriteItems() ->put( [ fn (Put $put) => $put->item([ 'BookId' => 'super-book1', 'Author' => 'Unknown', ]), fn (Put $put) => $put->item([ 'BookId' => 'super-book-2', 'Author' => 'Incognito', ]), ], table: ['Books', 'BookId'], ) ->update( fn (Update $update) => $update ->pk('super-book-3') ->set('Author', 'Incognito'), table: ['Books', 'BookId'], ) ->delete( fn (Delete $delete) => $delete->pk('super-book-4'), table: ['Books', 'BookId'], ) ->dispatch();
BatchGetItem
use Terseq\Builders\Operations\BatchGetItem\Operations\BatchGet; $manager->batchGetItem() ->get( fn (BatchGet $get) => $get ->pk('super-book-1') ->pk('super-book-2') ->composite('book-id', 'release-date'), table: ['Books', 'BookId'], ) ->dispatch();
BatchWriteItem
$manager->batchWriteItem() ->put( [ 'BookId' => 'super-book-1', 'Author' => 'Unknown', ], table: ['Books', 'BookId'], ) ->put( [ 'BookId' => 'super-book-2', 'Author' => 'Incognito', ], table: ['Books', 'BookId'], ) ->delete( 'super-book-3', table: ['Books', 'BookId'], ) ->dispatch();
Table
Table as object (recommended)
Example of using table object
use Terseq\Builders\Table; use Terseq\Builders\Keys; class Books extends Table { public function getTableName(): string { return 'Books'; } public function getKeys(): Keys { return new Keys(partitionKey: 'BookId', sortKey: null); } }
Example with secondary indexes
use Terseq\Builders\Table; use Terseq\Builders\Keys; class BooksTable extends Table { /** * Table name */ public function getTableName(): string { return 'Books'; } /** * Partition key and sort key (optional) */ public function getKeys(): Keys { return new Keys(partitionKey: 'BookId', sortKey: 'ReleaseDate'); } /** * Secondary index map (optional) also known as GSI and LSI */ public function getSecondaryIndexMap(): ?array { return [ 'AuthorIndex' => new Keys(partitionKey: 'AuthorId', sortKey: 'BornDate'), 'GenreIndex' => new Keys(partitionKey: 'GenreId', sortKey: 'GenreName'), 'LsiExample' => new Keys(partitionKey: 'BookId', sortKey: 'AuthorBornYear'), ]; } }
Table as array
table(table: ['TableName', 'PartitionKey', 'SortKey']);
OR
table(table: ['TableName', 'PartitionKey']); // Sort key by default is null
OR
table(table: ['TableName']); // throws exception, because PartitionKey is required
OR
table(table: [ 'table' => 'TableName', 'pk' => 'PartitionKey', 'sk' => 'SortKey', ]);
Single-table design (recommended)
Library supports single-table design.
Example of using single-table design
$manager = new \Terseq\DatabaseManager( client: $client, marshaler: new Marshaler(), singleTable: new class extends \Terseq\Builders\Table { public function getTableName(): string { return 'Books'; } public function getKeys(): Keys { return new Keys(partitionKey: 'BookId'); } }, );
That's all! Now you can build queries without passing table name and keys.
Usage
// Query $manager->getItem()->pk('super-cool-id')->dispatch(); $manager->batchWriteItem() ->put( [ 'BookId' => 'super-book-1', 'Author' => 'Unknown', ], ) ->put( [ 'BookId' => 'super-book-2', 'Author' => 'Incognito', ], ) ->delete('super-book-3') ->dispatch();
Comparison with AWS SDK
Example of using AWS SDK
$client->updateItem( [ 'TableName' => 'Books', 'UpdateExpression' => 'SET #Title = :title_0, #Author = :author_0', 'ExpressionAttributeNames' => [ '#Title' => 'Title', '#Author' => 'Author', ], 'ExpressionAttributeValues' => [ ':title_0' => [ 'S' => 'Super Cool Book Updated', ], ':author_0' => [ 'S' => 'Super Cool Author Updated', ], ], 'Key' => [ 'BookId' => [ 'S' => 'super-cool-id', ], ], ], );
Example of using Terseq for the same operation
$manager->updateItem() ->table(['Books', 'BookId']) ->pk('super-cool-id') ->set('Title', 'Super Cool Book Updated') ->set('Author', 'Super Cool Author Updated') ->dispatch();
aiotu/terseq 适用场景与选型建议
aiotu/terseq 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.09k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「query」 「aws」 「dynamodb」 「builder」 「terseq」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aiotu/terseq 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aiotu/terseq 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aiotu/terseq 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
AWS SDK for PHP - Use Amazon Web Services in your PHP project
AWS package containing services for the DreamFactory Platform 2.0.
This package includes a script and fail2ban configuration that allows you to use fail2ban when utilizing AWS elastic load balancer (ELB) and an apache webserver.
Elastic Driver for Laravel Scout
Query filtering in your frontend
Anax Database Active Record module for model classes.
统计信息
- 总下载量: 11.09k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-07