Entity Relational Mapper (built on bevy)
A bevy plugin designed to aid database access using an Entity Component System architecture.
Individual user requests are passed into the ECS as events. The systems can use a database query that allows loading of entities in the ECS from a database. Once entities have been modified in memory they are flushed back to the database upong a flush event for that request. Each request opens a new database transaction maintaining isolation between requests.
┌────────────────────────────────────────────────────┐
│ │
│ Web Server │
│ │
│ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Requests │ │ Responses │ │
│ │ │ │ │ │
│ │ ┌───────┐ │ │ ┌───────┐ │ │
│ │ │ │ │ │ │ │ │ │
│ │ └───────┘ │ │ └───────┘ │ │
│ │ │ │ │ │
│ │ ┌───────┐ │ │ ┌───────┐ │ │
│ │ │ │ │ │ │ │ │ │
│ │ └───────┘ │ │ └───────┘ │ │
│ │ │ │ │ │
│ │ ┌───────┐ │ │ ┌───────┐ │ │
│ │ │ │ │ │ │ │ │ │
│ │ └───────┘ │ │ └───────┘ │ │
│ │ │ │ │ │
│ └───┬─────────────┘ └─────────────▲───┘ │
│ │ │ │
│ │ │ │
└────────┼──────────────────────────────────┼────────┘
│ │
Requests│ │Events
create │ ┌────────┐ │create
events │ │ │ │response
│ ┌─┴──────┐ │ │
│ │ │ │ │
└──────────► Event │ ├────────────┘
│ │ │
│ ├─┘
└───┬──▲─┘
Systems │ │
process │ │
events │ │
┌───▼──┴──────────┐
│ │
┌───┴───────────────┐ │
│ Business Logic │ │
│ Systems │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ ├─┘
└───┬─────────────▲─┘
│ │
Queries │ │ Queries
access the│ │ return
database │ │ components
┌─────────────▼┐ ┌┴─────────────────────────────────────┐
│ Database │ loaded │ Entities │
│ │ in as │ │
│ │ components│ ┌────────┐ ┌────────┐ │
│ ├───────────┤ Entity 1: │ Comp 1 │ │ Comp 2 │ │
│ │ │ ───────┴────────┴───┴────────┘ │
│ │ │ │
│ │ │ ┌────────┐ │
│ │ │ Entity 2: │ Comp 2 │ │
│ │ │ ─────────────────────┴────────┘ │
└──────────────┘ │ │
└──────────────────────────────────────┘
Usage
Currently only SQLite database has been developed as a Database Resource implementation. So this is all for a SQLite database. Feel free to add an issue for an new database resource.
Use the DBQueryDerive derive marco to generate the component mapper to map the database row into the component. Each attribute
on the struct is treated as column in the SQLite database.
Eg. Create a User component
use async_trait;
use *;
use *;
use DBQueryDerive;
For example if you have a webserver resource then you can pull messages from it. Then start a new transaction to get the name of the user.
The DatabaseResource trait defines how the query access the actual database.
A Sqlite Database Connector using sqlx is already under SqlxSqliteDatabaseResource
use *;
use *;
Add a system to query users to handle the GetUserNameEvent.
Printing the users name.
use *;
use *;
Setup the bevy App
Examples
Found in ./examples