use wasm_dbms_api::prelude::{
Blob, Boolean, ColumnDef, Date, DateTime, Decimal, Int64, Json, Nullable, Table,
TableSchema as _, Text, Uint8, Uint32, Uint64, Uuid,
};
use wasm_dbms_macros::DatabaseSchema;
#[derive(Debug, Table, Clone, PartialEq, Eq)]
#[table = "users"]
pub(crate) struct User {
#[primary_key]
pub id: Uint32,
pub name: Text,
pub email: Nullable<Text>,
pub age: Uint8,
}
#[derive(Debug, Table, Clone, PartialEq, Eq)]
#[table = "posts"]
pub(crate) struct Post {
#[primary_key]
pub id: Uint32,
pub title: Text,
pub published: Boolean,
#[foreign_key(entity = "User", table = "users", column = "id")]
pub user_id: Uint32,
}
#[derive(Debug, Table, Clone, PartialEq, Eq)]
#[table = "comments"]
pub(crate) struct Comment {
#[primary_key]
pub id: Uint32,
pub body: Text,
#[foreign_key(entity = "Post", table = "posts", column = "id")]
pub post_id: Uint32,
}
#[derive(Debug, Table, Clone, PartialEq, Eq)]
#[table = "sales"]
pub(crate) struct Sale {
#[primary_key]
pub id: Uint32,
pub category: Text,
pub region: Text,
pub quantity: Uint32,
pub bonus: Nullable<Uint32>,
pub price: Decimal,
}
#[derive(Debug, Table, Clone, PartialEq, Eq)]
#[table = "events"]
pub(crate) struct Event {
#[primary_key]
#[autoincrement]
pub id: Uint64,
pub day: Date,
pub at: DateTime,
pub payload: Blob,
pub meta: Json,
pub delta: Int64,
pub token: Uuid,
}
#[derive(Debug, Clone, Copy, DatabaseSchema)]
#[tables(
User = "users",
Post = "posts",
Comment = "comments",
Sale = "sales",
Event = "events"
)]
pub(crate) struct TestSchema;
pub(crate) fn catalog(table: &str) -> Option<&'static [ColumnDef]> {
match table {
"users" => Some(User::columns()),
"posts" => Some(Post::columns()),
"comments" => Some(Comment::columns()),
"sales" => Some(Sale::columns()),
"events" => Some(Event::columns()),
_ => None,
}
}