Talisorm

An ORM for "ORMless" persistance of DDD-inspired domain models
Alternatives To Talisorm
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Prisma29,89744215 hours ago4,993September 24, 20222,780apache-2.0TypeScript
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
Objection.js6,9507073387 days ago195December 30, 2021153mitJavaScript
An SQL-friendly ORM for Node.js
Records6,944150287 days ago11February 21, 201964iscPython
SQL for Humans™
Sqlboiler5,666974 days ago95August 28, 202277bsd-3-clauseGo
Generate a Go ORM tailored to your database schema.
Deepkit Framework2,58540a day ago64August 02, 202277mitTypeScript
A new full-featured and high-performance TypeScript framework for enterprise applications.
Fullstack Starterkit1,075
a year ago2mitTypeScript
GraphQL first full-stack starter kit with Node, React. Powered by TypeScript
Gormigrate8745814 days ago29May 29, 202228mitGo
Minimalistic database migration helper for Gorm ORM
Iridium571983 months ago158April 07, 201841otherTypeScript
A high performance MongoDB ORM for Node.js
Dynamodb Onetable53825 days ago92September 09, 202213mitJavaScript
DynamoDB access and management for one table designs with NodeJS
Maghead4761117 months ago92August 14, 202299otherPHP
The fastest pure PHP database framework with a powerful static code generator, supports horizontal scale up, designed for PHP7
Alternatives To Talisorm
Select To Compare


Alternative Project Comparisons
Readme

Build Status Code Coverage

About TalisORM

A good design starts with some limitations. You can start simple and keep building until you have a large ORM like Doctrine. Or you can choose not to support a mapping configuration, table inheritance, combined write/read models, navigable object graphs, lazy-loading, etc. That's what I'm looking for with TalisOrm. The rules are:

  • You model a persistable domain object as an Aggregate: one (root) Entity, and optionally some Child entities.
  • The child entities themselves have no children.
  • You use the ORM for your write model only. That is, you don't need to fetch hundreds of these aggregates to show them to the user.
  • Your aggregate internally records domain events, which will automatically be released and dispatched after saving changes to the aggregate.

Furthermore:

  • You're going to write your own mapping code, which converts your values or Value objects to and from column values.

I explain more about the motivation for doing this in "ORMless; a Memento-like pattern for object persistence".

You can find some examples of how to use this library in test/TalisOrm/AggregateRepositoryTest/.

Recording and dispatching domain events

A domain event is a simple object indicating that something has happened inside an aggregate (usually this just means that something has changed). You can use the EventRecordingCapabilities trait to save yourself from rewriting a couple of simple lines over and over again.

Immediately after saving an aggregate, the AggregateRepository will call the aggregate's releaseEvents() method, which returns previously recorded domain events. It dispatches these events to an object that implements EventDispatcher. As a user of this library you have to provide your own implementation of this interface, which is very simple. Maybe you just want to forward the call to your favorite event dispatcher, or the one that ships with your framework.

Managing the database schema

Aggregates can implement SpecifiesSchema and, yes, specify their own schema. This can be useful if you want to use a tool to synchronize your current database schema with the schema that your aggregates expect, e.g. the Doctrine DBAL's own SingleDatabaseSynchronizer:

use Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer;
use TalisOrm\Schema\AggregateSchemaProvider;

// set up or reuse a Doctrine\DBAL\Connection instance
$connection = ...;

$schemaProvider = new AggregateSchemaProvider($connection, [
    // list all the aggregate class names of your application, e.g.
    User::class,
    Order::class
]);
$synchronizer = new SingleDatabaseSynchronizer($connection);
$synchronizer->createSchema($schemaProvider->createSchema());

You could also use Doctrine Migrations to automatically generate migrations based on schema changes. It may need a bit of setup, but once you have it working, you'll notice that this tool needs a SchemaProviderInterface instance (note: this interface is only available in recent versions of doctrine/migrations, which requires PHP 7). You can easily set up an adapter for AggregateSchemaProvider. For example:

final class AggregateMigrationsSchemaProvider implements SchemaProviderInterface
{
    /**
     * @var AggregateSchemaProvider
     */
    private $aggregateSchemaProvider;

    public function __construct(AggregateSchemaProvider $aggregateSchemaProvider)
    {
        $this->aggregateSchemaProvider = $aggregateSchemaProvider;
    }

    public function createSchema(): Schema
    {
        return $this->aggregateSchemaProvider->createSchema();
    }
}

Protecting against concurrent updates

Traditionally, we PHP developers aren't used to protect our aggregates against concurrent updates. Concurrent updates after all are a matter of chance. Maybe there aren't that many users who are working on the same aggregate in your project. But if you're worried that it might happen, there's an easy solution built-in to TalisORM: optimistic concurrency locking.

You need to take the following steps to make it work:

Make sure the table definition for your aggregate has an Aggregate::VERSION_COLUMN column, and that your fromState() and state() methods are aware of it. For example:

final class Order implements Aggregate, SpecifiesSchema
{
    /**
     * @var int
     */
    private $aggregateVersion;

    public function state(): array
    {
        // N.B. It's important to increment the version manually every time state() gets called!
        $this->aggregateVersion++;

        return [
            // ...
            Aggregate::VERSION_COLUMN => $this->aggregateVersion
        ];
    }

    public static function fromState(array $aggregateState, array $childEntityStatesByType): Aggregate
    {
        $order = new self();

        // ...

        $order->aggregateVersion = $aggregateState[Aggregate::VERSION_COLUMN];

        return $order;
    }

    /**
     * Only if your aggregate implements SpecifiesSchema:
     */
    public static function specifySchema(Schema $schema): void
    {
        $table = $schema->createTable('orders');

        // ...

        $table->addColumn(Aggregate::VERSION_COLUMN, 'integer');
    }
}

The above setup will protect your aggregate against concurrent updates between retrieving the aggregate from the database and saving it again. However, you may want to warn a user who's working with the aggregate's data in the user interface that once they store the object, someone else has modified it. To do this, you need to remember the version of the aggregate the user is looking at in the user's session. An outline of this solution:

final class Order implements Aggregate, SpecifiesSchema
{
    // ...

    public function setAggregateVersion(int $version): void
    {
        $this->aggregateVersion = $version;
    }

    public function aggregateVersion(): int
    {
        return $this->aggregateVersion;
    }
}

/*
 * Inside the controller which (for instance) renders a form, allowing the
 * user to modify some aspect of the aggregate:
 */
$order = $repository->getById($orderId);
$session->set('aggregate_version', $order->aggregateVersion());
// show form

/*
 * Inside the controller which modifies the aggregate based on the data the
 * user provided:
 */
$order = $repository->getById($orderId);
$order->setAggregateVersion($session->get('aggregate_version');

$order->makeSomeChange();

// This will compare the provided version to the version in the database:
$repository->save($order);
Popular Orm Projects
Popular Schema Projects
Popular Data Processing Categories

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Php
Schema
Orm
Doctrine
Concurrent