Giter Club home page Giter Club logo

yadm's Introduction

Yadm is the fastest MongoDB ODM.

Build Status

The schema less ODM. It gives you the fastest hydration and persistent. Based on formapro/values lib.

Install

$ composer require formapro/yadm "mikemccabe/json-patch-php:dev-master as 0.1.1"

Storage example

Let's say we have an order model:

<?php

namespace Acme;

use function Formapro\Values\set_value;
use function Formapro\Values\get_value;

class Price
{
    private $values = [];
    
    public function setCurrency(string $value): void
    {
        set_value($this, 'currency', $value);
    }
    
    public function getCurrency(): string 
    {
        return get_value($this, 'currency');
    }
    
    public function setAmount(int $value): void
    {
        set_value($this, 'amount', $value);
    }
    
    public function getAmount(): string 
    {
        return get_value($this, 'amount');
    }
}
<?php
namespace Acme;

use function Formapro\Values\set_value;
use function Formapro\Values\get_value;
use function Formapro\Values\set_object;
use function Formapro\Values\get_object;

class Order
{
    private $values = [];
    
    public function setNumber(string $number): void
    {
        set_value($this, 'number', $number);
    }
    
    public function getNumber(): string 
    {
        return get_value($this, 'number');
    }
    
    public function setPrice(Price $price): void
    {
        set_object($this, 'price', $price);
    }
    
    public function getPrice(): Price
    {
        return get_object($this, 'price', Price::class);
    }
}
<?php
namespace Acme;

use MongoDB\Client;
use Formapro\Yadm\Hydrator;
use Formapro\Yadm\Storage;

$collection = (new Client())->selectCollection('acme_demo', 'orders');
$hydrator = new Hydrator(Order::class);
$storage = new Storage($collection, $hydrator);

$price = new Price();
$price->setAmount(123); # 1.23 USD
$price->setCurrency('USD');

$order = new Order();
$order->setNumber(1234);
$order->setPrice($price);

$storage->insert($order);

$foundOrder = $storage->find(['_id' => get_object_id($order)]);
$foundOrder->setNumber(4321);
$storage->update($foundOrder);

$storage->delete($foundOrder);

MongoDB special types usage

<?php
namespace Acme;

use MongoDB\Client;
use Formapro\Yadm\Hydrator;
use Formapro\Yadm\Storage;
use Formapro\Yadm\ConvertValues;
use Formapro\Yadm\Type\UuidType;
use Formapro\Yadm\Type\UTCDatetimeType;
use Formapro\Yadm\Uuid;
use function Formapro\Values\set_value;
use function Formapro\Values\get_value;

$convertValues = new ConvertValues([
    'id' => new UuidType(),
    'createdAt' => new UTCDatetimeType(),
]);

$collection = (new Client())->selectCollection('acme_demo', 'orders');
$hydrator = new Hydrator(Order::class);
$storage = new Storage($collection, $hydrator, null, null, $convertValues);
 

$order = new Order();
set_value($order, 'id', Uuid::generate()->toString());
set_value($order, 'createdAt', (new \DateTime())->format('U'));

$storage->insert($order);

$id = get_value($order, 'id');

// find by uuid
$anotherOrder = $storage->findOne(['id' => new Uuid($id)]);

// do not update id if not changed
$storage->update($anotherOrder);

// update on change
set_value($anotherOrder, 'id', Uuid::generate()->toString());
$storage->update($anotherOrder);

Other examples

In formapro/values repo you can find examples on how to build simple objects, object trees, hydrate and retrive data from\to object.

Benchmarks

License

MIT

yadm's People

Contributors

askozienko avatar makasim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

yadm's Issues

add findOneOrFail method

for now we can do only
$entity = $entity->findOne(['id' => $id]);
and if there is no entity we mush do additional check:

if (false == $entity) {
    throw new NotFoundHttpException('error message');
}

My suggestion is to add method:
$entity = $entity->findOneOrFail(['id' => $id]);
expected:
it returns $entity or throw NotFoundHttpException

testShouldTrackAddedValueToEmptyCollection not up to date?

I just patched our code because $set on empty array has created an object when using key.0 = val instead of key = [ val ] (makasim@8d61d69)

But I wonder, why the build does not fail, on my setup this test seems invalid now:

public function testShouldTrackAddedValueToEmptyCollection()
    {
        $obj = $this->createPersistedObject();
        $collector = new ChangesCollector();
        $collector->register($obj, get_values($obj));
        add_value($obj, 'aKey', 'aVal');
        self::assertEquals([
            '$set' => [
                'aKey.0' => 'aVal',
            ],
        ], $collector->changes(get_values($obj), $collector->getOriginalValues($obj)));
    }

Shouldn't it be changed to

            '$set' => [
                'aKey' => [ 'aVal' ],
            ],

?

Thanks for your great work
Gregor

usage example

Could you please write a guide for new release of basic CRUD operations?

aggregation framework

Hello
Your library can't work with mongo aggregation framework.
Will you integrate support this mongo functional?

Mongo collection update must be asc ordered. In other case there is an error

magento_data_1 | [MongoDB\Driver\Exception\BulkWriteException]
magento_data_1 | BulkWrite error :: cannot use the part (4 of data.products.2.media_gallery.
magento_data_1 | images.4.value_id) to traverse the element ({: null})

magento_data_1 | ["data.products.2.media_gallery.images.5.value_id"]=>
magento_data_1 | string(4) "2806"
magento_data_1 | ["data.products.2.media_gallery.images.5.file"]=>
magento_data_1 | string(14) "//-/-2_1.jpg"
magento_data_1 | ["data.products.2.media_gallery.images.5.product_id"]=>
magento_data_1 | string(3) "900"
magento_data_1 | ["data.products.2.media_gallery.images.5.label"]=>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.