wizbii/mongo-bundle

安装量: 14 192

依赖项: 0

建议者: 0

安全性: 0

星级: 3

分支: 1

类型:symfony-bundle

3.3.0 2024-09-17 08:39 UTC

README

pipeline status coverage report

目标

此包旨在帮助开发人员以优雅的方式与mongodb进行通信。它减轻了必须操作数组、依赖于像Doctrine ODM这样的重型包的负担。它基于一个MongoClient类,该类提供了大多数基本mongodb操作,但它比基础mongodb实现要容易使用得多。此类实现了一个MongoClientInterface,该接口也被另外两个实现所实现

  • 一个InMemory实现,它被设计用于单元测试。大多数查询操作符和更新器都得到了支持。详细信息请见下文
  • 一个LocalFile实现,它被设计用于功能测试。它与InMemory实现使用相同的LocalEngine,因此支持相同的查询操作符和更新器

安装

composer require wizbii/mongo-bundle

生产模式下的使用

首先,创建一个包含您的模型的对象。请注意,此对象必须实现https://gitlab.com/wizbii-open-source/json-serializer-bundle中解释的ArraySerializable接口

<?php 

namespace App\Model;

use Wizbii\JsonSerializerBundle\ArraySerializable;

class SimpleObject implements ArraySerializable
{
    private string $id;
    private string $value;

    public function __construct(string $id, string $value)
    {
        $this->id = $id;
        $this->value = $value;
    }

    public function serialize(): array
        {
            return [
                '_id' => $this->id,
                'value' => $this->value,
            ];
        }
    
        public static function deserialize(array $contentAsArray)
        {
            return new self($contentAsArray['_id'], $contentAsArray['value']);
        }
}

然后,创建一个需要从mongodb操作此对象的服务

<?php 

namespace App\Repository;

use Wizbii\OpenSource\MongoBundle\MongoClientBuilderInterface;
use Wizbii\OpenSource\MongoBundle\MongoClientInterface;
use App\Model\SimpleObject;

class SimpleObjectRepository
{
    /** @phpstan-var MongoClientInterface<SimpleObject> */
    private MongoClientInterface $mongoClient;

    /** @phpstan-param MongoClientBuilderInterface<SimpleObject> $mongoClientBuilder */
    public function __construct(MongoClientBuilderInterface $mongoClientBuilder)
    {
        $this->mongoClient = $mongoClientBuilder->buildFor('test_database', 'simple_object_collection', SimpleObject::class);
    }

    public function readSimpleObject(string $simpleObjectId): SimpleObject
    {
        /** @var SimpleObject */
        $simpleObject = $this->mongoClient->get($simpleObjectId); 
        return $simpleObject;
    }

    /** @return SimpleObject[] */
    public function findSimpleObjectNamedRemi(int $rows = 10, int $offset = 0): array
    {
        return $this->mongoClient->findBy(['name' => 'Rémi'], $rows, $offset);
    }

    public function createSimpleObject(): string
    {
        $this->mongoClient->put(new SimpleObject('id-abcd', 'Rémi'));
    }
}

当然,您可以使用以下方式调整mongo配置

<?php 
namespace App\Repository;

use Wizbii\OpenSource\MongoBundle\MongoClientBuilderInterface;
use Wizbii\OpenSource\MongoBundle\MongoClientInterface;
use App\Model\SimpleObject;

class SimpleObjectRepository
{
    /** @phpstan-var MongoClientInterface<SimpleObject> */
    private MongoClientInterface $mongoClient;

    /** @phpstan-param MongoClientBuilderInterface<SimpleObject> $mongoClientBuilder */
    public function __construct(MongoClientBuilderInterface $mongoClientBuilder)
    {
        $this->mongoClient = $mongoClientBuilder
            ->overrideConfigurationFor('test_database', 'simple_object_collection', SimpleObject::class)
            ->setReadPreference('primary')
            ->setRetryWrites(true)
            //->...
            ->end()->build();
    }
}

开发模式下的使用

您的存储库代码没有改变:生产模式和开发模式下的代码相同。但当你执行单元测试时,用于MongoClientBuilderInterface构造函数参数的类将使用InMemory实现。

以下是一个为这样的存储库编写单元测试的示例

<?php

namespace App\Tests\Repository;

use App\Model\SimpleObject;
use App\Repository\SimpleObjectRepository;
use Tests\Wizbii\OpenSource\MongoBundle\MongoClientTestCase;

class SimpleObjectRepositoryTest extends MongoClientTestCase
{
    public function test_it_can_create_and_retrieve_simple_objects()
    {
        $simpleObjectRepository = new SimpleObjectRepository($this->getMongoClientBuilder());
        $simpleObjectRepository->createSimpleObject($this->getSimpleObject('remi', 'Rémi'));
        $simpleObject = $simpleObjectRepository->readSimpleObject('remi');
        $this->assertThat($simpleObject, $this->isInstanceOf(SimpleObject::class));
        $this->assertThat($simpleObject->getValue(), $this->equalTo('Rémi'));
    }

    public function test_it_can_find_simple_objects_by_name()
    {
        $simpleObjectRepository = new SimpleObjectRepository($this->getMongoClientBuilder());
        $simpleObjectRepository->create($this->getSimpleObject('remi-1', 'Rémi'));
        $simpleObjectRepository->create($this->getSimpleObject('mark', 'Mark'));
        $simpleObjectRepository->create($this->getSimpleObject('remi-2', 'Rémi'));
        $simpleObjectRepository->create($this->getSimpleObject('remi-3', 'Rémi'));
        $simpleObjects = $simpleObjectRepository->findSimpleObjectNamedRemi();
        $this->assertThat($simpleObjects, $this->countOf(3));
        $this->assertThat($simpleObjects[0]->getId(), $this->equalTo('remi-1'));
        $this->assertThat($simpleObjects[1]->getId(), $this->equalTo('remi-2'));
        $this->assertThat($simpleObjects[2]->getId(), $this->equalTo('remi-3'));
    }

    private function getSimpleObject(string $id, string $value): SimpleObject
    {
        return new SimpleObject($id, $value);
    }
}

支持的Mongo功能

查询操作符实际实现本地引擎注释
$eqOKOK
$gtOKOK
$gteOKOK
$inOKOK
$ltOKOK
$lteOKOK
$neOKOK
$ninOKOK
$andOKOK
$notOKOK
$norOKOK
$orOKOK
$existsOKOK
$typeOKOK某些类型不受支持。有关详细信息,请参阅TypeFilter
$exprOKKO
$jsonSchemaOKKO
$modOKKO
$regexpOKOK
$textOKKO
$whereOKKO
$geoIntersectsOKKO
$geoWithinOKKO
$nearOKKO
$nearSphereOKKO
$allOKOK
$elemMatchOKOK
$sizeOKOK
$bitsAllClearOKKO
$bitsAllSetOKKO
$bitsAnyClearOKKO
$bitsAnySetOKKO
$commentOKOK
更新器实际实现本地引擎注释
$currentDateOKKO
$incOKKO
$minOKKO
$maxOKKO
$mulOKKO
$renameOKKO
$setOKOK
$setOnInsertOKKO
$unsetOKKO
$addToSetOKKO
$popOKKO
$pullOKKO
$pushOKKO
$pushAllOKKO
$eachOKKO
$positionOKKO
$sliceOKKO
$sortOKKO
$bitOKKO

贡献

  1. 分支存储库
  2. 进行更改
  3. 使用composer dev:checks测试它们(它将运行测试、phpstan和cs:lint子命令)
  4. 创建合并请求