|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of the event-engine/php-postgres-document-store. |
| 4 | + * (c) 2019 prooph software GmbH <contact@prooph.de> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +use EventEngine\DocumentStore\Filter; |
| 13 | +use EventEngine\DocumentStore\OrderBy; |
| 14 | +use EventEngine\DocumentStore; |
| 15 | + |
| 16 | +require_once __DIR__ .'/../vendor/autoload.php'; |
| 17 | + |
| 18 | +$dsn = getenv('PDO_DSN'); |
| 19 | +$usr = getenv('PDO_USER'); |
| 20 | +$pwd = getenv('PDO_PWD'); |
| 21 | + |
| 22 | +$pdo = new \PDO($dsn, $usr, $pwd); |
| 23 | +$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
| 24 | + |
| 25 | +$pdo->prepare("DROP TABLE IF EXISTS em_ds_docs")->execute(); |
| 26 | +$pdo->prepare("DROP TABLE IF EXISTS em_ds_animals")->execute(); |
| 27 | +$pdo->prepare("DROP TABLE IF EXISTS em_ds_test")->execute(); |
| 28 | + |
| 29 | +$documentStore = new \EventEngine\DocumentStore\Postgres\PostgresDocumentStore($pdo); |
| 30 | + |
| 31 | +$documentStore->addCollection('test'); |
| 32 | +$documentStore->addCollection('animals', |
| 33 | + DocumentStore\FieldIndex::forField('name', DocumentStore\FieldIndex::SORT_DESC, true), |
| 34 | + DocumentStore\MultiFieldIndex::forFields([ |
| 35 | + DocumentStore\FieldIndex::forField('character.friendly'), |
| 36 | + DocumentStore\FieldIndex::forField('pet') |
| 37 | + ]) |
| 38 | +); |
| 39 | + |
| 40 | +$collections = $documentStore->listCollections(); |
| 41 | + |
| 42 | +foreach ($collections as $col) { |
| 43 | + echo "Collection: $col\n"; |
| 44 | +} |
| 45 | + |
| 46 | +$collections = $documentStore->filterCollectionsByPrefix('do'); |
| 47 | + |
| 48 | +foreach ($collections as $col) { |
| 49 | + echo "Filtered Collection: $col\n"; |
| 50 | +} |
| 51 | + |
| 52 | +echo "Has test: " . ($documentStore->hasCollection('test')? 'YES' : 'NO') . "\n"; |
| 53 | +echo "Has dogs: " . ($documentStore->hasCollection('dogs')? 'YES' : 'NO') . "\n"; |
| 54 | + |
| 55 | +$documentStore->dropCollection('test'); |
| 56 | +echo "Table test dropped\n"; |
| 57 | +echo "Has test: " . ($documentStore->hasCollection('test')? 'YES' : 'NO') . "\n"; |
| 58 | + |
| 59 | +$docId1 = '0e2c39b1-0778-4e92-a06b-0fa1c6ae9c5d'; |
| 60 | +$docId2 = '0e2ec294-df90-4bd7-a934-74747752f0bb'; |
| 61 | +$docId3 = '5bd4fdee-bfe2-4e1d-a4bf-b27e57fa1686'; |
| 62 | + |
| 63 | +$documentStore->addDoc('animals', $docId1, [ |
| 64 | + 'animal' => 'dog', |
| 65 | + 'name' => 'Jack', |
| 66 | + 'age' => 5, |
| 67 | + 'character' => [ |
| 68 | + 'friendly' => 10, |
| 69 | + 'wild' => 6, |
| 70 | + 'docile' => 8 |
| 71 | + ] |
| 72 | +]); |
| 73 | + |
| 74 | +$jack = $documentStore->getDoc('animals', $docId1); |
| 75 | + |
| 76 | +echo "Jack: " . json_encode($jack) . "\n"; |
| 77 | + |
| 78 | +try { |
| 79 | + $documentStore->upsertDoc('animals', $docId2, [ |
| 80 | + 'animal' => 'cat', |
| 81 | + 'name' => 'Jack', |
| 82 | + 'age' => 5, |
| 83 | + 'character' => [ |
| 84 | + 'friendly' => 3, |
| 85 | + 'wild' => 7, |
| 86 | + 'docile' => 2 |
| 87 | + ] |
| 88 | + ]); |
| 89 | +} catch (PDOException $exception) { |
| 90 | + if($exception->errorInfo[0] === "23505") { |
| 91 | + echo "Cannot add Jack twice due to unqiue index on name.\n"; |
| 92 | + } else { |
| 93 | + throw $exception; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +$documentStore->upsertDoc('animals', $docId2, [ |
| 98 | + 'animal' => 'cat', |
| 99 | + 'name' => 'Tiger', |
| 100 | + 'age' => 5, |
| 101 | + 'character' => [ |
| 102 | + 'friendly' => 3, |
| 103 | + 'wild' => 7, |
| 104 | + 'docile' => 2 |
| 105 | + ] |
| 106 | +]); |
| 107 | + |
| 108 | +$documentStore->upsertDoc('animals', $docId2, [ |
| 109 | + 'age' => 3 |
| 110 | +]); |
| 111 | + |
| 112 | +$tiger = $documentStore->getDoc('animals', $docId2); |
| 113 | + |
| 114 | +echo "Tiger: " . json_encode($tiger) . "\n"; |
| 115 | + |
| 116 | +$documentStore->addDoc('animals', $docId3, [ |
| 117 | + 'animal' => 'cat', |
| 118 | + 'name' => 'Gini', |
| 119 | + 'age' => 5, |
| 120 | + 'character' => [ |
| 121 | + 'friendly' => 8, |
| 122 | + 'wild' => 3, |
| 123 | + 'docile' => 4 |
| 124 | + ] |
| 125 | +]); |
| 126 | + |
| 127 | +$cats = $documentStore->filterDocs('animals', new Filter\EqFilter('animal', 'cat')); |
| 128 | + |
| 129 | +foreach ($cats as $cat) { |
| 130 | + echo "Cat: " . json_encode($cat) . "\n"; |
| 131 | +} |
| 132 | + |
| 133 | +$tiNames = $documentStore->filterDocs( |
| 134 | + 'animals', |
| 135 | + new Filter\LikeFilter('name', 'Ti%') |
| 136 | +); |
| 137 | + |
| 138 | +foreach ($tiNames as $tiName) { |
| 139 | + echo "Animal name starting with Ti: " . json_encode($tiName) . "\n"; |
| 140 | +} |
| 141 | + |
| 142 | +$documentStore->updateMany('animals', new Filter\GteFilter('character.friendly', 5), ['pet' => true]); |
| 143 | + |
| 144 | +$pets = $documentStore->filterDocs('animals', new Filter\EqFilter('pet', true)); |
| 145 | + |
| 146 | +foreach ($pets as $pet) { |
| 147 | + echo "Pet: " . json_encode($pet) . "\n"; |
| 148 | +} |
| 149 | + |
| 150 | +$superFriendlyPets = $documentStore->filterDocs( |
| 151 | + 'animals', |
| 152 | + new Filter\AndFilter( |
| 153 | + new Filter\EqFilter('pet', true), |
| 154 | + new Filter\EqFilter('character.friendly', 10) |
| 155 | + ) |
| 156 | +); |
| 157 | + |
| 158 | +foreach ($superFriendlyPets as $pet) { |
| 159 | + echo "Super friendly pet: " . json_encode($pet) . "\n"; |
| 160 | +} |
| 161 | + |
| 162 | +$documentStore->updateDoc('animals', $docId1, ['color' => ['black', 'white']]); |
| 163 | +$documentStore->updateDoc('animals', $docId2, ['color' => ['grey', 'black', 'white']]); |
| 164 | +$documentStore->updateDoc('animals', $docId3, ['color' => ['brown', 'red', 'white']]); |
| 165 | + |
| 166 | +$petsWithBlack = $documentStore->filterDocs( |
| 167 | + 'animals', |
| 168 | + new Filter\AndFilter( |
| 169 | + new Filter\EqFilter('pet', true), |
| 170 | + new Filter\InArrayFilter('color', 'black') |
| 171 | + ) |
| 172 | +); |
| 173 | + |
| 174 | +foreach ($petsWithBlack as $pet) { |
| 175 | + echo "Pet with black: " . json_encode($pet) . "\n"; |
| 176 | +} |
| 177 | + |
| 178 | +$noPets = $documentStore->filterDocs( |
| 179 | + 'animals', |
| 180 | + new Filter\NotFilter(new Filter\ExistsFilter('pet')) |
| 181 | +); |
| 182 | + |
| 183 | +foreach ($noPets as $pet) { |
| 184 | + echo "Not a pet: " . json_encode($pet) . "\n"; |
| 185 | +} |
| 186 | + |
| 187 | +$oldestAnimals = $documentStore->filterDocs( |
| 188 | + 'animals', |
| 189 | + new Filter\AnyFilter(), |
| 190 | + null, |
| 191 | + 2, |
| 192 | + OrderBy\AndOrder::by( |
| 193 | + OrderBy\Desc::byProp('age'), |
| 194 | + OrderBy\Asc::byProp('name') |
| 195 | + ) |
| 196 | +); |
| 197 | + |
| 198 | +foreach ($oldestAnimals as $animal) { |
| 199 | + echo "Old animal: " . json_encode($animal) . "\n"; |
| 200 | +} |
| 201 | + |
| 202 | +$sortedAnimals = $documentStore->filterDocs( |
| 203 | + 'animals', |
| 204 | + new Filter\AnyFilter(), |
| 205 | + 1, |
| 206 | + 2, |
| 207 | + OrderBy\Asc::byProp('name') |
| 208 | +); |
| 209 | + |
| 210 | +foreach ($sortedAnimals as $animal) { |
| 211 | + echo "Sorted animal: " . json_encode($animal) . "\n"; |
| 212 | +} |
| 213 | + |
| 214 | + |
| 215 | +$documentStore->deleteDoc('animals', $docId2); |
| 216 | +echo "Deleted doc with id $docId2\n"; |
| 217 | + |
| 218 | +$animalsWithWhite = $documentStore->filterDocs( |
| 219 | + 'animals', |
| 220 | + New Filter\InArrayFilter('color', 'white') |
| 221 | +); |
| 222 | + |
| 223 | +foreach ($animalsWithWhite as $animal) { |
| 224 | + echo "Animal with color white: " . json_encode($animal) . "\n"; |
| 225 | +} |
| 226 | + |
| 227 | +$documentStore->deleteMany('animals', new Filter\InArrayFilter('color', 'white')); |
| 228 | + |
| 229 | +echo "Deleted animals with color white\n"; |
| 230 | + |
| 231 | +$animalsWithWhite = $documentStore->filterDocs( |
| 232 | + 'animals', |
| 233 | + New Filter\InArrayFilter('color', 'white') |
| 234 | +); |
| 235 | + |
| 236 | +foreach ($animalsWithWhite as $animal) { |
| 237 | + echo "Animal with color white: " . json_encode($animal) . "\n"; |
| 238 | +} |
0 commit comments