use dbnexus::db_entity;
use sea_orm::entity::prelude::DateTimeUtc;
use sea_orm::entity::prelude::*;
#[db_entity(table_name = "limiteron_quotas", primary_key = "id")]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "limiteron_quotas")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i64,
#[sea_orm(column_name = "user_id")]
pub user_id: String,
pub resource: String,
#[sea_orm(unique)]
#[sea_orm(column_name = "quota_key")]
pub quota_key: String,
pub limit: u64,
pub consumed: u64,
#[sea_orm(column_name = "window_start")]
pub window_start: DateTimeUtc,
#[sea_orm(column_name = "window_end")]
pub window_end: DateTimeUtc,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
pub fn create_table_ddl() -> &'static str {
r#"
CREATE TABLE IF NOT EXISTS limiteron_quotas (
id BIGSERIAL PRIMARY KEY,
user_id VARCHAR(255) NOT NULL,
resource VARCHAR(255) NOT NULL,
quota_key VARCHAR(511) NOT NULL UNIQUE,
limit BIGINT NOT NULL,
consumed BIGINT NOT NULL DEFAULT 0,
window_start TIMESTAMP WITH TIME ZONE NOT NULL,
window_end TIMESTAMP WITH TIME ZONE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
)
"#
}
pub fn create_quota_key(user_id: &str, resource: &str) -> String {
format!("{}:{}", user_id, resource)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_quota_key() {
assert_eq!(
create_quota_key("user1", "api_requests"),
"user1:api_requests"
);
assert_eq!(create_quota_key("user2", "storage_mb"), "user2:storage_mb");
}
#[test]
fn test_create_table_ddl() {
let ddl = create_table_ddl();
assert!(ddl.contains("limiteron_quotas"));
assert!(ddl.contains("user_id"));
assert!(ddl.contains("quota_key"));
assert!(ddl.contains("consumed"));
}
}