feat: more models implemented
This commit is contained in:
18
entity/src/group.rs
Normal file
18
entity/src/group.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "groups")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
#[serde(skip_deserializing)]
|
||||
pub id: i32,
|
||||
#[sea_orm(index = "group_names")]
|
||||
pub name: String,
|
||||
// permissions: many-to-many to Permission.
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
65
entity/src/group_permission.rs
Normal file
65
entity/src/group_permission.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
|
||||
pub struct Entity;
|
||||
|
||||
impl EntityName for Entity {
|
||||
fn table_name(&self) -> &str {
|
||||
"group_permission"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub group_id: i32,
|
||||
pub permission_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
||||
pub enum Column {
|
||||
GroupId,
|
||||
PermissionId,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
|
||||
pub enum PrimaryKey {
|
||||
GroupId,
|
||||
PermissionId,
|
||||
}
|
||||
|
||||
impl PrimaryKeyTrait for PrimaryKey {
|
||||
type ValueType = (i32, i32);
|
||||
|
||||
fn auto_increment() -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl ColumnTrait for Column {
|
||||
type EntityName = Entity;
|
||||
|
||||
fn def(&self) -> ColumnDef {
|
||||
match self {
|
||||
Self::GroupId => ColumnType::Integer.def(),
|
||||
Self::PermissionId => ColumnType::Integer.def(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::group::Entity",
|
||||
from = "Column::GroupId",
|
||||
to = "super::group::Column::Id"
|
||||
)]
|
||||
Group,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::permission::Entity",
|
||||
from = "Column::PermissionId",
|
||||
to = "super::permission::Column::Id"
|
||||
)]
|
||||
Permission,
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
@@ -1,5 +1,13 @@
|
||||
pub mod user;
|
||||
pub mod group;
|
||||
pub mod group_permission;
|
||||
pub mod permission;
|
||||
pub mod user;
|
||||
pub mod user_group;
|
||||
pub mod user_permission;
|
||||
|
||||
pub use user::Entity as User;
|
||||
pub use group::Entity as Group;
|
||||
pub use group_permission::Entity as GroupPermission;
|
||||
pub use permission::Entity as Permission;
|
||||
pub use user::Entity as User;
|
||||
pub use user_group::Entity as UserGroup;
|
||||
pub use user_permission::Entity as UserPermission;
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use sea_orm::{DbConn, EntityTrait, Schema, DatabaseConnection, Database, ConnectionTrait};
|
||||
use sea_orm::{ConnectionTrait, Database, DatabaseConnection, DbConn, EntityTrait, Schema};
|
||||
|
||||
mod user;
|
||||
mod group;
|
||||
mod group_permission;
|
||||
mod permission;
|
||||
mod user;
|
||||
mod user_group;
|
||||
mod user_permission;
|
||||
|
||||
async fn create_table<E>(db: &DbConn, entity: E)
|
||||
where
|
||||
@@ -12,7 +16,7 @@ where
|
||||
|
||||
let mut table_create_statement = schema.create_table_from_entity(entity);
|
||||
// we need to shadow the mutable instance X, because if_not_exists() returns &mut X
|
||||
let table_create_statement = table_create_statement.if_not_exists();
|
||||
let table_create_statement = table_create_statement.if_not_exists();
|
||||
// we need to reborrow after dereferencing, which transforms our &mut X into &X
|
||||
let stmt = backend.build(&*table_create_statement);
|
||||
|
||||
@@ -31,7 +35,10 @@ pub async fn create_tables(db: &DbConn) {
|
||||
async fn main() {
|
||||
// Running Entities manually creates the tables from the entities in their latest incarnation.
|
||||
println!("Connecting to database...");
|
||||
let db: DatabaseConnection = Database::connect("postgresql://miniweb:miniweb@localhost:54321/miniweb").await.unwrap();
|
||||
let db: DatabaseConnection =
|
||||
Database::connect("postgresql://miniweb:miniweb@localhost:54321/miniweb")
|
||||
.await
|
||||
.unwrap();
|
||||
println!("Creating tables for entities...");
|
||||
create_tables(&db).await;
|
||||
}
|
||||
|
||||
@@ -9,10 +9,12 @@ pub struct Model {
|
||||
pub id: i32,
|
||||
#[sea_orm(index = "permission_names")]
|
||||
pub name: String,
|
||||
#[sea_orm(index = "permission_codes")]
|
||||
pub codename: String,
|
||||
pub level: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
@@ -1,18 +1,58 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::group::Entity as Group;
|
||||
use super::permission::Entity as Permission;
|
||||
use super::user_group::Relation as UserGroupRelation;
|
||||
use super::user_permission::Relation as UserPermissionRelation;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "users")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
#[serde(skip_deserializing)]
|
||||
pub id: i64,
|
||||
#[sea_orm(index = "user_usernames")]
|
||||
pub username: String,
|
||||
#[sea_orm(column_type = "Text")]
|
||||
pub description: Option<String>,
|
||||
pub password: String,
|
||||
|
||||
pub first_name: Option<String>,
|
||||
pub last_name: Option<String>,
|
||||
pub email: Option<String>,
|
||||
|
||||
// groups: many-to-many to Group
|
||||
// user_permissions: many-to-many to Permission
|
||||
pub is_staff: bool,
|
||||
pub is_active: bool,
|
||||
pub is_superuser: bool,
|
||||
pub last_login: Option<DateTimeWithTimeZone>,
|
||||
pub date_joined: Option<DateTimeWithTimeZone>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
impl Related<Group> for Entity {
|
||||
// The final relation is User -> UserGroup -> Group
|
||||
fn to() -> RelationDef {
|
||||
UserGroupRelation::Group.def()
|
||||
}
|
||||
|
||||
fn via() -> Option<RelationDef> {
|
||||
// The original relation is UserGroup -> User,
|
||||
// after `rev` it becomes User -> UserGroup
|
||||
Some(UserGroupRelation::Group.def().rev())
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<Permission> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
UserPermissionRelation::Permission.def()
|
||||
}
|
||||
|
||||
fn via() -> Option<RelationDef> {
|
||||
Some(UserPermissionRelation::Permission.def().rev())
|
||||
}
|
||||
}
|
||||
|
||||
65
entity/src/user_group.rs
Normal file
65
entity/src/user_group.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
|
||||
pub struct Entity;
|
||||
|
||||
impl EntityName for Entity {
|
||||
fn table_name(&self) -> &str {
|
||||
"user_group"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub user_id: i32,
|
||||
pub group_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
||||
pub enum Column {
|
||||
UserId,
|
||||
GroupId,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
|
||||
pub enum PrimaryKey {
|
||||
UserId,
|
||||
GroupId,
|
||||
}
|
||||
|
||||
impl PrimaryKeyTrait for PrimaryKey {
|
||||
type ValueType = (i32, i32);
|
||||
|
||||
fn auto_increment() -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl ColumnTrait for Column {
|
||||
type EntityName = Entity;
|
||||
|
||||
fn def(&self) -> ColumnDef {
|
||||
match self {
|
||||
Self::UserId => ColumnType::Integer.def(),
|
||||
Self::GroupId => ColumnType::Integer.def(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::user::Entity",
|
||||
from = "Column::UserId",
|
||||
to = "super::user::Column::Id"
|
||||
)]
|
||||
User,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::group::Entity",
|
||||
from = "Column::GroupId",
|
||||
to = "super::group::Column::Id"
|
||||
)]
|
||||
Group,
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
65
entity/src/user_permission.rs
Normal file
65
entity/src/user_permission.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
|
||||
pub struct Entity;
|
||||
|
||||
impl EntityName for Entity {
|
||||
fn table_name(&self) -> &str {
|
||||
"user_permission"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub user_id: i32,
|
||||
pub permission_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
||||
pub enum Column {
|
||||
UserId,
|
||||
PermissionId,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
|
||||
pub enum PrimaryKey {
|
||||
UserId,
|
||||
PermissionId,
|
||||
}
|
||||
|
||||
impl PrimaryKeyTrait for PrimaryKey {
|
||||
type ValueType = (i32, i32);
|
||||
|
||||
fn auto_increment() -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl ColumnTrait for Column {
|
||||
type EntityName = Entity;
|
||||
|
||||
fn def(&self) -> ColumnDef {
|
||||
match self {
|
||||
Self::UserId => ColumnType::Integer.def(),
|
||||
Self::PermissionId => ColumnType::Integer.def(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::user::Entity",
|
||||
from = "Column::UserId",
|
||||
to = "super::user::Column::Id"
|
||||
)]
|
||||
User,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::permission::Entity",
|
||||
from = "Column::PermissionId",
|
||||
to = "super::permission::Column::Id"
|
||||
)]
|
||||
Permission,
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
Reference in New Issue
Block a user