madewithlove/broadway-mongodb

此包已被废弃,不再维护。作者建议使用 broadway/event-store-mongodb 包。

Broadway 的 MongoDB 驱动程序

0.2.0 2016-04-22 20:23 UTC

This package is auto-updated.

Last update: 2019-11-15 15:26:02 UTC


README

Build Status Latest Stable Version Total Downloads Scrutinizer Quality Score Code Coverage

基于 MongoDBBroadway,并使用 mongodb/mongodb 开发的 MongoDB 驱动程序。

目标

安装

此包与 mongodb/mongodb 具有相同的依赖要求。

$ pecl install mongodb
$ echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

通过 Composer

$ composer require madewithlove/broadway-mongodb

使用方法

MongoDBClientFactory

此包包含一个工厂来构建 MongoDB\Client

使用默认值

$factory = new MongoDBClientFactory();
$client = $factory->create(['database' => 'foobar']);

为特定主机和端口创建客户端

$factory = new MongoDBClientFactory();
$client = $factory->create([
     'database' => 'foobar',
     'host' => 'my_host',
     'port' => 3000,
]);

// Or alternatively

$client = $factory->create([
     'database' => 'foobar',
     'host' => 'my_host:3000',
]);

为多个主机创建客户端

“hosts”选项也可以是用于多个主机的数组

$factory = new MongoDBClientFactory();
$client = $factory->create([
     'database' => 'foobar',
     'host' => ['my_host_1', 'my_host_2'],
]);

带有用户名和密码的客户端创建

如果您必须对 MongoDB 数据库进行身份验证,您可以传递用户名和密码

$factory = new MongoDBClientFactory();
$client = $factory->create([
     'database' => 'foobar',
     'username' => 'foo',
     'password' => 'bar',
]);

使用 DSN 字符串创建客户端

或者您可以通过传递 DSN 字符串来进行连接

$factory = new MongoDBClientFactory();
$client = $factory->create([
     'dsn' => 'mongodb://foo:200/foo',
]);

ReadModel

此包包含一个基本的 MongoDBRepository 类,您可以直接使用或扩展以构建自己的仓库。

为您的模型创建仓库的最简单方法是使用 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);

测试

$ composer test

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件