Skip to content

Commit adf06cc

Browse files
committed
add database in document & change repository manager api
1 parent e3d9156 commit adf06cc

9 files changed

Lines changed: 69 additions & 26 deletions

src/Attribute/Document.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
{
1212
public function __construct(
1313
public string $collection,
14+
public string|null $database = null,
1415
) {
1516
}
1617
}

src/Metadata/AttributeDocumentMetadataFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public function metadata(string $className): DocumentMetadata
2929
throw new ClassIsNotAnDocument($className);
3030
}
3131

32-
$collection = $attributes[0]->newInstance()->collection;
32+
$attribute = $attributes[0]->newInstance();
33+
34+
$collection = $attribute->collection;
35+
$database = $attribute->database;
3336
$idProperty = null;
3437

3538
foreach ($reflection->getProperties() as $reflectionProperty) {
@@ -52,6 +55,7 @@ public function metadata(string $className): DocumentMetadata
5255

5356
return new DocumentMetadata(
5457
$className,
58+
$database,
5559
$collection,
5660
$idProperty,
5761
$this->indexes($reflection),

src/Metadata/DocumentMetadata.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
public function __construct(
1717
public string $className,
18+
public string|null $database,
1819
public string $collection,
1920
public string $idProperty,
2021
public array $indexes = [],

src/Repository/MongoDBRepository.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function remove(string $id): void
5959
$this->collection()->deleteOne(['_id' => $id]);
6060
}
6161

62+
/** @return iterable<T> */
6263
public function findAll(): iterable
6364
{
6465
$cursor = $this->collection()->find([], [
@@ -70,6 +71,12 @@ public function findAll(): iterable
7071
}
7172
}
7273

74+
/**
75+
* @param array<string, mixed> $filter
76+
* @param array<string, 'asc'|'desc'>|null $orderBy
77+
*
78+
* @return iterable<T>
79+
*/
7380
public function findBy(array $filter, array|null $orderBy = null, int|null $limit = null, int|null $offset = null): iterable
7481
{
7582
$options = [];
@@ -98,6 +105,12 @@ public function findBy(array $filter, array|null $orderBy = null, int|null $limi
98105
}
99106
}
100107

108+
/**
109+
* @param array<string, mixed> $filter
110+
* @param array<string, 'asc'|'desc'>|null $orderBy
111+
*
112+
* @return T|null
113+
*/
101114
public function findOneBy(array $filter = [], array|null $orderBy = null): object|null
102115
{
103116
$options = [

src/Repository/MongoDBRepositoryManager.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Patchlevel\ODM\Repository;
66

7-
use MongoDB\Database;
7+
use MongoDB\Client;
88
use Patchlevel\Hydrator\CoreExtension;
99
use Patchlevel\Hydrator\Extension;
1010
use Patchlevel\Hydrator\Hydrator;
@@ -19,9 +19,10 @@ final class MongoDBRepositoryManager implements RepositoryManager
1919
private array $repositories = [];
2020

2121
public function __construct(
22-
private readonly Database $database,
22+
private readonly Client $client,
2323
private readonly DocumentMetadataFactory $metadataFactory,
2424
private readonly Hydrator $hydrator,
25+
private readonly string $defaultDatabase = 'default',
2526
) {
2627
}
2728

@@ -38,17 +39,19 @@ public function get(string $documentClass): MongoDBRepository
3839
return $this->repositories[$documentClass];
3940
}
4041

42+
$metadata = $this->metadataFactory->metadata($documentClass);
43+
4144
$this->repositories[$documentClass] = new MongoDBRepository(
42-
$this->database,
43-
$this->metadataFactory->metadata($documentClass),
45+
$this->client->getDatabase($metadata->database ?: $this->defaultDatabase),
46+
$metadata,
4447
$this->hydrator,
4548
);
4649

4750
return $this->repositories[$documentClass];
4851
}
4952

5053
/** @param list<Extension> $extensions */
51-
public static function create(Database $database, array $extensions = []): self
54+
public static function create(Client $client, array $extensions = []): self
5255
{
5356
$metadataFactory = new AttributeDocumentMetadataFactory();
5457

@@ -60,6 +63,6 @@ public static function create(Database $database, array $extensions = []): self
6063
$builder->useExtension($extension);
6164
}
6265

63-
return new self($database, $metadataFactory, $builder->build());
66+
return new self($client, $metadataFactory, $builder->build());
6467
}
6568
}

src/Repository/RangoRepository.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function remove(string $id): void
5656
$this->collection()->deleteOne(['_id' => $id]);
5757
}
5858

59+
/** @return iterable<T> */
5960
public function findAll(): iterable
6061
{
6162
$cursor = $this->collection()->find();
@@ -65,6 +66,12 @@ public function findAll(): iterable
6566
}
6667
}
6768

69+
/**
70+
* @param array<string, mixed> $filter
71+
* @param array<string, 'asc'|'desc'>|null $orderBy
72+
*
73+
* @return iterable<T>
74+
*/
6875
public function findBy(array $filter, array|null $orderBy = null, int|null $limit = null, int|null $offset = null): iterable
6976
{
7077
$options = [];
@@ -91,6 +98,12 @@ public function findBy(array $filter, array|null $orderBy = null, int|null $limi
9198
}
9299
}
93100

101+
/**
102+
* @param array<string, mixed> $filter
103+
* @param array<string, 'asc'|'desc'>|null $orderBy
104+
*
105+
* @return T|null
106+
*/
94107
public function findOneBy(array $filter = [], array|null $orderBy = null): object|null
95108
{
96109
$options = ['limit' => 1];

src/Repository/RangoRepositoryManager.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@
1111
use Patchlevel\ODM\Hydrator\ODMExtension;
1212
use Patchlevel\ODM\Metadata\AttributeDocumentMetadataFactory;
1313
use Patchlevel\ODM\Metadata\DocumentMetadataFactory;
14-
use Patchlevel\Rango\Database;
14+
use Patchlevel\Rango\Client;
1515

1616
final class RangoRepositoryManager implements RepositoryManager
1717
{
1818
/** @var array<string, RangoRepository<object>> */
1919
private array $repositories = [];
2020

2121
public function __construct(
22-
private readonly Database $database,
22+
private readonly Client $client,
2323
private readonly DocumentMetadataFactory $metadataFactory,
2424
private readonly Hydrator $hydrator,
25+
private readonly string $defaultDatabase = 'public',
2526
) {
2627
}
2728

@@ -38,8 +39,10 @@ public function get(string $documentClass): RangoRepository
3839
return $this->repositories[$documentClass];
3940
}
4041

42+
$metadata = $this->metadataFactory->metadata($documentClass);
43+
4144
$this->repositories[$documentClass] = new RangoRepository(
42-
$this->database,
45+
$this->client->selectDatabase($metadata->database ?: $this->defaultDatabase),
4346
$this->metadataFactory->metadata($documentClass),
4447
$this->hydrator,
4548
);
@@ -48,7 +51,7 @@ public function get(string $documentClass): RangoRepository
4851
}
4952

5053
/** @param list<Extension> $extensions */
51-
public function create(Database $database, array $extensions = []): self
54+
public function create(Client $client, array $extensions = []): self
5255
{
5356
$metadataFactory = new AttributeDocumentMetadataFactory();
5457

@@ -60,6 +63,6 @@ public function create(Database $database, array $extensions = []): self
6063
$builder->useExtension($extension);
6164
}
6265

63-
return new self($database, $metadataFactory, $builder->build());
66+
return new self($client, $metadataFactory, $builder->build());
6467
}
6568
}

tests/Integration/MongoDBRepositoryTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Patchlevel\ODM\Tests\Integration;
66

77
use MongoDB\Client;
8-
use MongoDB\Database;
98
use MongoDB\Driver\Exception\BulkWriteException;
109
use Patchlevel\Hydrator\CoreExtension;
1110
use Patchlevel\Hydrator\StackHydratorBuilder;
@@ -20,17 +19,17 @@
2019

2120
use function array_map;
2221
use function getenv;
23-
use function iterator_to_array;
2422
use function is_array;
23+
use function iterator_to_array;
2524

2625
class MongoDBRepositoryTest extends TestCase
2726
{
2827
protected MongoDBRepositoryManager $repositoryManager;
29-
private Database $database;
28+
private Client $client;
3029

3130
public function setUp(): void
3231
{
33-
$client = new Client(getenv('MONGODB_URI'));
32+
$this->client = new Client(getenv('MONGODB_URI'));
3433

3534
$documentMetadataFactory = new AttributeDocumentMetadataFactory();
3635

@@ -39,19 +38,19 @@ public function setUp(): void
3938
->useExtension(new ODMExtension($documentMetadataFactory))
4039
->build();
4140

42-
$this->database = $client->selectDatabase('patchlevel');
43-
$this->database->drop();
41+
$this->client->dropDatabase('patchlevel');
4442

4543
$this->repositoryManager = new MongoDBRepositoryManager(
46-
$this->database,
44+
$this->client,
4745
$documentMetadataFactory,
4846
$hydrator,
47+
'patchlevel',
4948
);
5049
}
5150

5251
protected function tearDown(): void
5352
{
54-
$this->database->drop();
53+
$this->client->dropDatabase('patchlevel');
5554
}
5655

5756
public function testSave(): void

tests/Integration/RangoRepositoryTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Patchlevel\ODM\Tests\Integration\Fixtures\Status;
1515
use Patchlevel\ODM\Tests\Integration\Fixtures\UniqueProfile;
1616
use Patchlevel\Rango\Client;
17-
use Patchlevel\Rango\Database;
1817
use Patchlevel\Rango\Exception\QueryException;
1918
use PHPUnit\Framework\TestCase;
2019

@@ -25,11 +24,17 @@
2524
class RangoRepositoryTest extends TestCase
2625
{
2726
protected RangoRepositoryManager $repositoryManager;
28-
private Database $database;
27+
private Client $client;
2928

3029
public function setUp(): void
3130
{
32-
$client = new Client(getenv('POSTGRES_URI'));
31+
$uri = getenv('POSTGRES_URI');
32+
33+
if (!$uri) {
34+
self::markTestSkipped('POSTGRES_URI is not set');
35+
}
36+
37+
$this->client = new Client($uri);
3338

3439
$documentMetadataFactory = new AttributeDocumentMetadataFactory();
3540

@@ -38,18 +43,19 @@ public function setUp(): void
3843
->useExtension(new ODMExtension($documentMetadataFactory))
3944
->build();
4045

41-
$this->database = $client->selectDatabase('patchlevel');
46+
$this->client->dropDatabase('patchlevel');
4247

4348
$this->repositoryManager = new RangoRepositoryManager(
44-
$this->database,
49+
$this->client,
4550
$documentMetadataFactory,
4651
$hydrator,
52+
'patchlevel',
4753
);
4854
}
4955

5056
protected function tearDown(): void
5157
{
52-
$this->database->drop();
58+
$this->client->dropDatabase('patchlevel');
5359
}
5460

5561
public function testSave(): void

0 commit comments

Comments
 (0)