mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/compatibility/mongodb-laravel-integration
MIT License
7k stars 1.42k forks source link

PHPORM-206 Add model schema version feature #3021

Closed florianJacques closed 2 months ago

florianJacques commented 3 months ago

Added a feature for managing mongoDB document versioning. The default key is "schema_version" If the version is not specified, t is automatically set to 1

Usage

use MongoDB\Laravel\Eloquent\HasDocumentVersion;
use MongoDB\Laravel\Eloquent\Model as Eloquent;

/** @property int $schema_version */
class SchemaVersionned extends Eloquent
{
    use HasSchemaVersion;

    public const int SCHEMA_VERSION = 1;

    protected $connection       = 'mongodb';
    protected $collection       = 'documentVersion';
    protected static $unguarded = true;
}

For overwrite version key:

/** @property int $__v */
class DocumentVersion extends Eloquent
{
    use HasSchemaVersion;

    protected $connection       = 'mongodb';
    protected $collection       = 'documentVersion';
    protected static $unguarded = true;

    public static function getSchemaVersionKey(): string
    {
       return '__v';
    }
}

For update document version:

/** @property int $schema_version */
class DocumentVersion extends Eloquent
{
    use HasSchemaVersion;

    public const int SCHEMA_VERSION = 2;

    protected $connection       = 'mongodb';
    protected $collection       = 'documentVersion';
    protected static $unguarded = true;
}
florianJacques commented 2 months ago

Thanks for your work !

GromNaN commented 2 months ago

Thank you @florianJacques