madewithlove / broadway-mongodb
A mongodb driver for Broadway
Requires
- php: >=5.5.9
- broadway/broadway: ^0.9.0
- mongodb/mongodb: ^1.0
Requires (Dev)
- fabpot/php-cs-fixer: 2.0.*@dev
- mockery/mockery: ^0.9.4
- phpspec/phpspec: ^2.2
- phpunit/phpunit: ^4.7
- symfony/var-dumper: ^2.7
This package is auto-updated.
Last update: 2019-11-15 15:26:02 UTC
README
A MongoDB driver for Broadway based on mongodb/mongodb
.
Goals
Install
This package has the same requirements as mongodb/mongodb
.
$ pecl install mongodb $ echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
Via Composer
$ composer require madewithlove/broadway-mongodb
Usage
MongoDBClientFactory
This package ships with a factory to build a MongoDB\Client
.
Using default values
$factory = new MongoDBClientFactory(); $client = $factory->create(['database' => 'foobar']);
Creating a client for a specific host and port
$factory = new MongoDBClientFactory(); $client = $factory->create([ 'database' => 'foobar', 'host' => 'my_host', 'port' => 3000, ]); // Or alternatively $client = $factory->create([ 'database' => 'foobar', 'host' => 'my_host:3000', ]);
Creating a client for multiple hosts
The hosts
option can also be an array for multiple hosts
$factory = new MongoDBClientFactory(); $client = $factory->create([ 'database' => 'foobar', 'host' => ['my_host_1', 'my_host_2'], ]);
Creating a client for with username and password
If you have to authenticate to your MongoDB database you can pass the username and password
$factory = new MongoDBClientFactory(); $client = $factory->create([ 'database' => 'foobar', 'username' => 'foo', 'password' => 'bar', ]);
Creating a client using a dsn string
Alternatively you can pass a dsn string and it will be used to connect
$factory = new MongoDBClientFactory(); $client = $factory->create([ 'dsn' => 'mongodb://foo:200/foo', ]);
ReadModel
This package ships with a basic MongoDBRepository
class you can either use directly or extend to build your own repositories.
The easiest way to create a repository for your model is by using the ReadModel\Factory
:
$mongDBClientFactory = new MongoDBClientFactory(); $client = $factory->create(['database' => 'testing']); $factory = new ReadModel\Factory( new SimpleInterfaceSerializer(), $client->selectDatabase('testing') ); // 'my_projection' is the collection that will be used. $repository = $factory->create('my_projector'); // If you have a custom read model repository you can use the factory to create your own instances: $repository = $factory->create('my_projector', MyReadModelRepository::class);
Testing
$ composer test
License
The MIT License (MIT). Please see License File for more information.