Mojolicious-Plugin-Fondation-Model-DBIx-Async

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

                            dsn          => 'dbi:SQLite:dbname=data/app.db',
                            schema_class => 'MySchema',
                            workers      => 2,
                        },
                    ],
                    models => {
                        user => { source => 'User' },
                    },
                }},
            ],
        },
    }

    # In a controller
    sub list ($self) {
        $self->render_later;
        $self->model('user')->search({ active => 1 })->all
            ->on_done(sub {
                my @users = @_;
                $self->render(json => [ map { $_->get_columns } @users ]);
            })
            ->on_fail(sub { $self->reply->exception(shift) })
            ->retain;
    }

# DESCRIPTION

[Mojolicious::Plugin::Fondation::Model::DBIx::Async](https://metacpan.org/pod/Mojolicious%3A%3APlugin%3A%3AFondation%3A%3AModel%3A%3ADBIx%3A%3AAsync) is a [Fondation](https://metacpan.org/pod/Fondation) plugin
that exposes [DBIx::Class::Async](https://metacpan.org/pod/DBIx%3A%3AClass%3A%3AAsync) natively — no hashref CRUD wrapper, no
Future-to-Mojo::Promise conversion. Every call goes through a background worker
pool, keeping the [Mojolicious](https://metacpan.org/pod/Mojolicious) event loop responsive.

## Architecture

    ┌─────────────────────────────────────────────────┐
    │  Mojolicious Application                        │
    │  $c->model('user') → ResultSet                  │
    │  $c->schema         → DBIx::Class::Async::Schema│
    └──────────────┬──────────────────────────────────┘
                   │ IO::Async::Loop::Mojo
    ┌──────────────▼──────────────────────────────────┐
    │  Worker Pool (forked processes)                 │
    │  ┌────────┐ ┌────────┐ ┌────────┐              │
    │  │Worker 1│ │Worker 2│ │Worker N│              │
    │  └────────┘ └────────┘ └────────┘              │
    └──────────────┬──────────────────────────────────┘
                   │ DBI
    ┌──────────────▼──────────────────────────────────┐
    │  Database                                       │
    └─────────────────────────────────────────────────┘

Each backend is a separate [DBIx::Class::Async::Schema](https://metacpan.org/pod/DBIx%3A%3AClass%3A%3AAsync%3A%3ASchema) with its own
[IO::Async::Loop::Mojo](https://metacpan.org/pod/IO%3A%3AAsync%3A%3ALoop%3A%3AMojo) and worker pool. Workers are forked lazily on
the first schema access and are automatically stopped on process exit.

## Model discovery

During `fondation_finalyze`, the plugin scans every loaded Fondation plugin
for a `models` key in their configuration. Models declared by dependency
plugins are merged, with the application configuration taking priority.
Each model is validated to have a resolvable backend.

## Source registration

The `DBIx` action ([Mojolicious::Plugin::Fondation::Model::DBIx::Async::Action::DBIx](https://metacpan.org/pod/Mojolicious%3A%3APlugin%3A%3AFondation%3A%3AModel%3A%3ADBIx%3A%3AAsync%3A%3AAction%3A%3ADBIx))
auto-discovers `Result` and `ResultSet` classes under each plugin's
`Schema::Result::*` and `Schema::ResultSet::*` namespaces and registers them
on the native schema class _before_ workers are forked.

## Shutdown

An `END` block disconnects all schemas on clean process exit (Ctrl-C, `kill`,
`systemctl stop`), calling ["disconnect" in DBIx::Class::Async](https://metacpan.org/pod/DBIx%3A%3AClass%3A%3AAsync#disconnect) to gracefully
stop worker processes. The `before_server_stop` hook is also emitted so
other code can react to shutdown. Only `SIGKILL` bypasses this — no hook
can help there.

# CONFIGURATION

    'Fondation::Model::DBIx::Async' => {
        backends => [
            main => {
                dsn          => 'dbi:SQLite:dbname=data/app.db',
                schema_class => 'MySchema',
                user         => '',           # optional
                pass         => '',           # optional
                workers      => 2,            # default: 2
                dbi_attrs    => {},           # optional
            },
            logs => {
                dsn          => 'dbi:SQLite:dbname=data/logs.db',
                schema_class => 'MyLogSchema',
                workers      => 1,
            },
        ],
        default_backend => 'main',            # optional
        models => {
            user    => { source => 'User' },
            article => { source => 'articles', backend => 'main' },
            log     => { source => 'logs',    backend => 'logs' },
        },
    },

### backends

Array of name/config pairs (ordered). Each pair provides a backend name
followed by its configuration hash. Each backend requires `dsn` and
`schema_class`. Names are used by models and other plugins
to reference a specific connection.

Plain DSN strings are accepted as a shorthand and normalized to
`{ dsn => $dsn }`.

### default\_backend

Name of the default backend. When omitted, the first backend in the
`backends` array is used. Models without an explicit `backend` fall
back to this.

### models



( run in 2.077 seconds using v1.01-cache-2.11-cpan-f4a522933cf )