Skip to content

Commit a5b83a8

Browse files
authored
Merge pull request #30 from jupitern/development
Development
2 parents af88a9b + 30e63b0 commit a5b83a8

File tree

7 files changed

+428
-463
lines changed

7 files changed

+428
-463
lines changed

README.md

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
PHP wrapper for Azure Cosmos DB
44

5+
https://docs.microsoft.com/pt-pt/rest/api/cosmos-db/common-tasks-using-the-cosmosdb-rest-api
6+
57
## Installation
68

79
Include jupitern/cosmosdb in your project, by adding it to your composer.json file.
@@ -16,102 +18,91 @@ Include jupitern/cosmosdb in your project, by adding it to your composer.json fi
1618

1719
## Changelog
1820

19-
### v2.5.0
21+
### v2.6.0
22+
- code refactor. min PHP verion supported is now 8.0
23+
- selectCollection no longer creates a colletion if not exist. use createCollection for that
24+
- bug fixes
2025

26+
### v2.5.0
2127
- support partitioned queries using new method "setPartitionValue()"
22-
2328
- support creating partitioned collections
24-
2529
- support for nested partition keys
2630

2731
### v2.0.0
28-
2932
- support for cross partition queries
30-
3133
- selectCollection method removed from all methods for performance improvements
3234

3335
### v1.4.4
34-
3536
- replaced pear package http_request2 by guzzle
36-
3737
- added method to provide guzzle configuration
3838

3939
### v1.3.0
40-
4140
- added support for parameterized queries
4241

4342
## Note
44-
4543
This package adds additional functionalities to the [AzureDocumentDB-PHP](https://github.com/cocteau666/AzureDocumentDB-PHP) package. All other functionality exists in this package as well.
4644

4745
## Limitations
48-
4946
Use of `limit()` or `order()` in cross-partition queries is currently not supported.
5047

5148
## Usage
5249

5350
### Connecting
5451

5552
```php
56-
$conn = new \Jupitern\CosmosDb\CosmosDb('hostName', 'primaryKey');
57-
$conn->setHttpClientOptions(['verify' => false]); # optional: set guzzle client options.
58-
$db = $conn->selectDB('dbName');
59-
$collection = $db->selectCollection('collectionName');
60-
61-
# if a collection does not exist, it will be created when you
62-
# attempt to select the collection. however, if you have created
63-
# your database with shared throughput, then all collections require a partition key.
64-
# selectCollection() supports a second parameter for this purpose.
65-
$conn = new \Jupitern\CosmosDb\CosmosDb('hostName', 'primaryKey');
53+
$conn = new \Jupitern\CosmosDb\CosmosDb('https://localhost:8081', 'primaryKey');
6654
$conn->setHttpClientOptions(['verify' => false]); # optional: set guzzle client options.
67-
$db = $conn->selectDB('dbName');
68-
$collection = $db->selectCollection('collectionName', 'myPartitionKey');
55+
$db = $conn->selectDB('testdb');
56+
57+
# create a new collection
58+
$collection = $db->createCollection('Users', 'country');
59+
60+
# select existing collection
61+
$collection = $db->selectCollection('Users');
6962
```
7063

7164
### Inserting Records
7265

7366
```php
74-
75-
# consider a existing collection called "Users" with a partition key "country"
76-
77-
# insert a record
7867
$rid = \Jupitern\CosmosDb\QueryBuilder::instance()
7968
->setCollection($collection)
8069
->setPartitionKey('country')
81-
->save(['id' => '1', 'name' => 'John Doe', 'age' => 22, 'country' => 'Portugal']);
82-
83-
# insert a record against a collection with a nested partition key
84-
# note: this follows the same string format as is used when creating
85-
# a collection with a partition key via the Azure Portal
86-
$rid = \Jupitern\CosmosDb\QueryBuilder::instance()
87-
->setCollection($collection)
88-
->setPartitionKey('/form/person/country')
89-
->save(['id' => '2', 'name' => 'Jane doe', 'age' => 35, 'country' => 'Portugal']);
70+
->save([
71+
'id' => '2',
72+
'name' => 'Jane doe',
73+
'age' => 35,
74+
'country' => 'Portugal'
75+
]);
9076
```
9177

9278
### Updating Records
9379

9480
```php
95-
# update a record
9681
$rid = \Jupitern\CosmosDb\QueryBuilder::instance()
9782
->setCollection($collection)
9883
->setPartitionKey('country')
99-
->save(["_rid" => $rid, 'id' => '2', 'name' => 'Jane Doe Something', 'age' => 36, 'country' => 'Portugal']);
84+
->save([
85+
"_rid" => $rid,
86+
'id' => '2',
87+
'name' => 'Jane Doe Something',
88+
'age' => 36,
89+
'country' => 'Portugal'
90+
]);
10091
```
10192

10293
### Querying Records
10394

10495
```php
105-
# query a document and return it as an array
96+
# cross partition query to get a single document and return it as an array
10697
$res = \Jupitern\CosmosDb\QueryBuilder::instance()
10798
->setCollection($collection)
10899
->select("c.id, c.name")
109100
->where("c.age > @age and c.country = @country")
110-
->params(['@age' => 30, '@country' => 'Portugal'])
101+
->params(['@age' => 10, '@country' => 'Portugal'])
111102
->find(true) # pass true if is cross partition query
112103
->toArray();
113104

114-
# query a document using a known partition value,
105+
# query a document using a known partition value
115106
# and return as an array. note: setting a known
116107
# partition value will result in a more efficient
117108
# query against your database as it will not rely
@@ -120,29 +111,30 @@ $res = \Jupitern\CosmosDb\QueryBuilder::instance()
120111
->setCollection($collection)
121112
->setPartitionValue('Portugal')
122113
->select("c.id, c.name")
123-
->where("c.age > @age and c.country = @country")
124-
->params(['@age' => 30, '@country' => 'Portugal'])
114+
->where("c.age > @age")
115+
->params(['@age' => 10])
125116
->find()
126117
->toArray();
127118

128-
# query the top 5 documents as an array, with the
119+
# query to get the top 5 documents as an array, with the
129120
# document ID as the array key.
130121
# note: refer to limitations section
131122
$res = \Jupitern\CosmosDb\QueryBuilder::instance()
132123
->setCollection($collection)
133-
->select("c.id, c.username")
124+
->select("c.id, c.name")
134125
->where("c.age > @age and c.country = @country")
135126
->params(['@age' => 10, '@country' => 'Portugal'])
136127
->limit(5)
137-
->findAll() # cannot limit cross-partition queries
128+
->findAll()
138129
->toArray('id');
139130

140131
# query a document using a collection alias and cross partition query
141132
$res = \Jupitern\CosmosDb\QueryBuilder::instance()
142133
->setCollection($collection)
143134
->select("TestColl.id, TestColl.name")
144135
->from("TestColl")
145-
->where("TestColl.age > 30")
136+
->where("TestColl.age > @age")
137+
->params(['@age' => 10])
146138
->findAll(true) # pass true if is cross partition query
147139
->toArray();
148140
```
@@ -154,7 +146,7 @@ $res = \Jupitern\CosmosDb\QueryBuilder::instance()
154146
$res = \Jupitern\CosmosDb\QueryBuilder::instance()
155147
->setCollection($collection)
156148
->setPartitionKey('country')
157-
->where("c.age > 30 and c.country = 'Portugal'")
149+
->where("c.age > 20 and c.country = 'Portugal'")
158150
->delete();
159151

160152
# delete all documents that match criteria (cross partition)
@@ -164,3 +156,30 @@ $res = \Jupitern\CosmosDb\QueryBuilder::instance()
164156
->where("c.age > 20")
165157
->deleteAll(true);
166158
```
159+
160+
### Error handling
161+
162+
```php
163+
try {
164+
$res = QueryBuilder::instance()
165+
->setCollection($collection)
166+
->deleteAll(true);
167+
168+
} catch (\GuzzleHttp\Exception\ClientException $e) {
169+
$response = json_decode($e->getResponse()->getBody());
170+
echo "ERROR: ".$response->code ." => ". $response->message .PHP_EOL.PHP_EOL;
171+
172+
echo $e->getTraceAsString();
173+
}
174+
```
175+
176+
177+
## Contributing
178+
179+
- welcome to discuss a bugs, features and ideas.
180+
181+
## License
182+
183+
jupitern/cosmosdb is release under the MIT license.
184+
185+
You are free to use, modify and distribute this software, as long as the copyright header is left intact

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"source": "https://github.com/jupitern/cosmosdb",
99
"issues": "https://github.com/jupitern/cosmosdb/issues"
1010
},
11-
"require" :{
12-
"php":">=7.0",
11+
"require": {
12+
"php": ">=8.0",
13+
"ext-curl": "*",
1314
"guzzlehttp/guzzle": "^7.4"
1415
},
1516
"autoload": {

0 commit comments

Comments
 (0)