2
2
3
3
PHP wrapper for Azure Cosmos DB
4
4
5
+ https://docs.microsoft.com/pt-pt/rest/api/cosmos-db/common-tasks-using-the-cosmosdb-rest-api
6
+
5
7
## Installation
6
8
7
9
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
16
18
17
19
## Changelog
18
20
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
20
25
26
+ ### v2.5.0
21
27
- support partitioned queries using new method "setPartitionValue()"
22
-
23
28
- support creating partitioned collections
24
-
25
29
- support for nested partition keys
26
30
27
31
### v2.0.0
28
-
29
32
- support for cross partition queries
30
-
31
33
- selectCollection method removed from all methods for performance improvements
32
34
33
35
### v1.4.4
34
-
35
36
- replaced pear package http_request2 by guzzle
36
-
37
37
- added method to provide guzzle configuration
38
38
39
39
### v1.3.0
40
-
41
40
- added support for parameterized queries
42
41
43
42
## Note
44
-
45
43
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.
46
44
47
45
## Limitations
48
-
49
46
Use of ` limit() ` or ` order() ` in cross-partition queries is currently not supported.
50
47
51
48
## Usage
52
49
53
50
### Connecting
54
51
55
52
``` 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');
66
54
$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');
69
62
```
70
63
71
64
### Inserting Records
72
65
73
66
``` php
74
-
75
- # consider a existing collection called "Users" with a partition key "country"
76
-
77
- # insert a record
78
67
$rid = \Jupitern\CosmosDb\QueryBuilder::instance()
79
68
->setCollection($collection)
80
69
->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
+ ]);
90
76
```
91
77
92
78
### Updating Records
93
79
94
80
``` php
95
- # update a record
96
81
$rid = \Jupitern\CosmosDb\QueryBuilder::instance()
97
82
->setCollection($collection)
98
83
->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
+ ]);
100
91
```
101
92
102
93
### Querying Records
103
94
104
95
``` 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
106
97
$res = \Jupitern\CosmosDb\QueryBuilder::instance()
107
98
->setCollection($collection)
108
99
->select("c.id, c.name")
109
100
->where("c.age > @age and c.country = @country")
110
- ->params(['@age' => 30 , '@country' => 'Portugal'])
101
+ ->params(['@age' => 10 , '@country' => 'Portugal'])
111
102
->find(true) # pass true if is cross partition query
112
103
->toArray();
113
104
114
- # query a document using a known partition value,
105
+ # query a document using a known partition value
115
106
# and return as an array. note: setting a known
116
107
# partition value will result in a more efficient
117
108
# query against your database as it will not rely
@@ -120,29 +111,30 @@ $res = \Jupitern\CosmosDb\QueryBuilder::instance()
120
111
->setCollection($collection)
121
112
->setPartitionValue('Portugal')
122
113
->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 ])
125
116
->find()
126
117
->toArray();
127
118
128
- # query the top 5 documents as an array, with the
119
+ # query to get the top 5 documents as an array, with the
129
120
# document ID as the array key.
130
121
# note: refer to limitations section
131
122
$res = \Jupitern\CosmosDb\QueryBuilder::instance()
132
123
->setCollection($collection)
133
- ->select("c.id, c.username ")
124
+ ->select("c.id, c.name ")
134
125
->where("c.age > @age and c.country = @country")
135
126
->params(['@age' => 10, '@country' => 'Portugal'])
136
127
->limit(5)
137
- ->findAll() # cannot limit cross-partition queries
128
+ ->findAll()
138
129
->toArray('id');
139
130
140
131
# query a document using a collection alias and cross partition query
141
132
$res = \Jupitern\CosmosDb\QueryBuilder::instance()
142
133
->setCollection($collection)
143
134
->select("TestColl.id, TestColl.name")
144
135
->from("TestColl")
145
- ->where("TestColl.age > 30")
136
+ ->where("TestColl.age > @age")
137
+ ->params(['@age' => 10])
146
138
->findAll(true) # pass true if is cross partition query
147
139
->toArray();
148
140
```
@@ -154,7 +146,7 @@ $res = \Jupitern\CosmosDb\QueryBuilder::instance()
154
146
$res = \Jupitern\CosmosDb\QueryBuilder::instance()
155
147
->setCollection($collection)
156
148
->setPartitionKey('country')
157
- ->where("c.age > 30 and c.country = 'Portugal'")
149
+ ->where("c.age > 20 and c.country = 'Portugal'")
158
150
->delete();
159
151
160
152
# delete all documents that match criteria (cross partition)
@@ -164,3 +156,30 @@ $res = \Jupitern\CosmosDb\QueryBuilder::instance()
164
156
->where("c.age > 20")
165
157
->deleteAll(true);
166
158
```
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
0 commit comments