bevy_sqlx

Crates.iobevy_sqlx
lib.rsbevy_sqlx
version
sourcesrc
created_at2024-09-17 13:27:22.707183
updated_at2024-10-30 04:00:23.917622
descriptionA SQLx database plugin for Bevy's ECS
homepage
repositoryhttps://github.com/nixpulvis/bevy_sqlx
max_upload_size
id1377446
Cargo.toml error:TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Nathan Lilienthal (nixpulvis)

documentation

README

Bevy SQLx


Bevy SQLx is a database plugin for Bevy's ECS which allows for SQL queries to be performed and data entities to be spawned.

Usage

use std::env;
use bevy::prelude::*;
use sqlx::{FromRow, Sqlite, SqlitePool};
use bevy_sqlx::{SqlxPlugin, SqlxDatabase, SqlxPrimaryKey, SqlxEvent};

#[derive(Component, FromRow, Debug)]
struct MyRecord {
    id: u32,
    flag: bool,
    text: String,
}

impl SqlxPrimaryKey for MyRecord {
    type Column = u32;

    fn primary_key(&self) -> Self::Column {
        self.id
    }
}

fn main() {
    let url = "sqlite:db/sqlite.db";
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(SqlxPlugin::<Sqlite, MyRecord>::from_url(&url))
        .add_systems(Startup, insert)
        .add_systems(Update, query)
        .run();
}

fn insert(mut events: EventWriter<SqlxEvent<Sqlite, MyRecord>>) {
    let sql = "INSERT INTO foos(text) VALUES ('test') RETURNING *";
    events.send(SqlxEvent::<Sqlite, MyRecord>::query(sql))
}

fn query(mut my_records: Query<&MyRecord>) {
    for my_record in &my_records {
        dbg!(my_record);
    }
}

fn resource(db: Res<SqlxDatabase<Sqlite>>) {
    let record = bevy::tasks::block_on(async {
        sqlx::query!("SELECT (1) as id, 'test' as text")
            .fetch_one(&db.pool)
            .await.unwrap()
    });
}

Running the sqlite Example

Sqlite Example

cargo run --example sqlite-minimal \
    --features sqlx/sqlite,bevy/bevy_winit,bevy/wayland
Commit count: 99

cargo fmt