jupitern/cosmosdb
Composer 安装命令:
composer require jupitern/cosmosdb
包简介
PHP wrapper for Azure Cosmos DB (formerly known as azure documentdb) using SQL rest api
README 文档
README
PHP wrapper for Azure Cosmos DB
https://docs.microsoft.com/pt-pt/rest/api/cosmos-db/common-tasks-using-the-cosmosdb-rest-api
Installation
Include jupitern/cosmosdb in your project, by adding it to your composer.json file.
{
"require": {
"jupitern/cosmosdb": "2.*"
}
}
Changelog
v2.7.0
- adding support for PATCH verb (CosmosDb add-on PATCH API) with Add, Set, Replace, Remove, Increment and Move operations, including "getPatchOp[OP]" helper functions
v2.6.0
- code refactor. min PHP verion supported is now 8.0
- selectCollection no longer creates a colletion if not exist. use createCollection for that
- bug fixes
v2.5.0
- support partitioned queries using new method "setPartitionValue()"
- support creating partitioned collections
- support for nested partition keys
v2.0.0
- support for cross partition queries
- selectCollection method removed from all methods for performance improvements
v1.4.4
- replaced pear package http_request2 by guzzle
- added method to provide guzzle configuration
v1.3.0
- added support for parameterized queries
Note
This package adds additional functionalities to the AzureDocumentDB-PHP package. All other functionality exists in this package as well.
Limitations
- Use of
limit()ororder()in cross-partition queries is currently not supported. - Multi-document patch using transaction based requests is currently not supported.
Usage
Connecting
$conn = new \Jupitern\CosmosDb\CosmosDb('https://localhost:8081', 'primaryKey'); $conn->setHttpClientOptions(['verify' => false]); # optional: set guzzle client options. $db = $conn->selectDB('testdb'); # create a new collection $collection = $db->createCollection('Users', 'country'); # select existing collection $collection = $db->selectCollection('Users');
Inserting Records
$rid = \Jupitern\CosmosDb\QueryBuilder::instance() ->setCollection($collection) ->setPartitionKey('country') ->save([ 'id' => '2', 'name' => 'Jane doe', 'age' => 35, 'country' => 'Portugal' ]);
Updating Records
$rid = \Jupitern\CosmosDb\QueryBuilder::instance() ->setCollection($collection) ->setPartitionKey('country') ->save([ "_rid" => $rid, 'id' => '2', 'name' => 'Jane Doe Something', 'age' => 36, 'country' => 'Portugal' ]);
Patching Records
# Patch operations: ADD | SET | REPLACE | REMOVE | INCR | MOVE # https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update#similarities-and-differences # Where a PartitionKey is in use, the PartitionValue should be set on the QueryBuilder instance # prior to making any PATCH operations, as by the nature of PATCH, there is no document body to find the value in, # and the value is taken from the class property when the request is made. $rid_doc is also required because PATCH is an item-level operation. # Example starting document (as array) # [ # "_rid" => $rid, # 'id' => '2', # 'name' => 'Jane Doe Something', # 'age' => 36, # 'country' => 'Portugal' # ] $res = \Jupitern\CosmosDb\QueryBuilder::instance() ->setCollection($collection) ->setPartitionKey('country') ->setPartitionValue('Portugal'); # Patch operations can be batched, so the $operations argument is an array of arrays # Batch patch operations are limited to 10 operations per request $operations[] = $res->getPatchOpSet('/age', 38); $operations[] = $res->getPatchOpAdd('/region' 'Algarve'); $rid_doc = $res->patch($rid_doc, $operations); # Example patched document (as array) # [ # "_rid" => $rid, # 'id' => '2', # 'name' => 'Jane Doe Something', # 'age' => 38, # 'country' => 'Portugal' # 'region' => 'Algarve' # ]
Querying Records
# cross partition query to get a single document and return it as an array $res = \Jupitern\CosmosDb\QueryBuilder::instance() ->setCollection($collection) ->select("c.id, c.name") ->where("c.age > @age and c.country = @country") ->params(['@age' => 10, '@country' => 'Portugal']) ->find(true) # pass true if is cross partition query ->toArray(); # query a document using a known partition value # and return as an array. note: setting a known # partition value will result in a more efficient # query against your database as it will not rely # on cross-partition querying $res = \Jupitern\CosmosDb\QueryBuilder::instance() ->setCollection($collection) ->setPartitionValue('Portugal') ->select("c.id, c.name") ->where("c.age > @age") ->params(['@age' => 10]) ->find() ->toArray(); # query to get the top 5 documents as an array, with the # document ID as the array key. # note: refer to limitations section $res = \Jupitern\CosmosDb\QueryBuilder::instance() ->setCollection($collection) ->select("c.id, c.name") ->where("c.age > @age and c.country = @country") ->params(['@age' => 10, '@country' => 'Portugal']) ->limit(5) ->findAll() ->toArray('id'); # query a document using a collection alias and cross partition query $res = \Jupitern\CosmosDb\QueryBuilder::instance() ->setCollection($collection) ->select("TestColl.id, TestColl.name") ->from("TestColl") ->where("TestColl.age > @age") ->params(['@age' => 10]) ->findAll(true) # pass true if is cross partition query ->toArray();
Deleting Records
# delete one document that matches criteria (single partition) $res = \Jupitern\CosmosDb\QueryBuilder::instance() ->setCollection($collection) ->setPartitionKey('country') ->where("c.age > 20 and c.country = 'Portugal'") ->delete(); # delete all documents that match criteria (cross partition) $res = \Jupitern\CosmosDb\QueryBuilder::instance() ->setCollection($collection) ->setPartitionKey('country') ->where("c.age > 20") ->deleteAll(true);
Error handling
try { $res = QueryBuilder::instance() ->setCollection($collection) ->deleteAll(true); } catch (\GuzzleHttp\Exception\ClientException $e) { $response = json_decode($e->getResponse()->getBody()); echo "ERROR: ".$response->code ." => ". $response->message .PHP_EOL.PHP_EOL; echo $e->getTraceAsString(); }
Contributing
- welcome to discuss a bugs, features and ideas.
License
jupitern/cosmosdb is release under the MIT license.
You are free to use, modify and distribute this software, as long as the copyright header is left intact
jupitern/cosmosdb 适用场景与选型建议
jupitern/cosmosdb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 73.32k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2018 年 09 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「documentdb」 「azure cosmosdb」 「cosmosdb」 「azure documentdb」 「cosmosdb rest」 「cosmosdb sql api rest」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jupitern/cosmosdb 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jupitern/cosmosdb 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jupitern/cosmosdb 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A simple PHP SDK for Microsoft Azure DocumentDB.
Small library to access Microsoft Windows Azure Blob Storage with a Service or a StreamWrapper.
Adds Silverstripe Flysystem support for using the Azure Blob adapter
PHP wrapper for Azure Cosmos DB (formerly known as azure documentdb) using SQL rest api. Changed few things on this package to the https://github.com/jupitern/cosmosdb
PHP wrapper for Azure Cosmos DB using SQL rest api
Laravel 5 Queue Driver for Microsoft Azure Storage Queue
统计信息
- 总下载量: 73.32k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-25