Files
morethantext/src/document/session.rs
Jeff Baskin c7054e4306
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s
Fixed the ignored session tests.
2026-03-08 16:02:08 -04:00

69 lines
2.3 KiB
Rust

use crate::{
action::{Action, CalcValue, Calculation, Delete, FieldType, Operand, Query, Update},
document::{
clock::Clock,
create::IndexType,
definition::{DocDef, DocFuncType},
},
message::wrapper::Message,
name::{Name, NameType},
queue::{
data_director::{Include, Path, RegMsg, Register},
router::Queue,
},
};
use std::{sync::mpsc::channel, time::Duration};
pub struct Session;
impl Session {
pub fn doc_names() -> Vec<Name> {
let mut names = Vec::new();
names.push(Name::english("session"));
names
}
pub fn document_definition() -> DocDef {
let mut docdef = DocDef::with_names(Self::doc_names());
let mut calc = Calculation::new(Operand::Add);
calc.add_value(FieldType::DateTime).unwrap();
calc.add_value(Duration::from_hours(1)).unwrap();
let name_id = Name::english("id");
docdef.add_field(name_id.clone(), FieldType::Uuid);
docdef.set_default(&name_id, FieldType::Uuid).unwrap();
docdef.add_index(&name_id, IndexType::Unique).unwrap();
let name_expire = Name::english("expire");
docdef.add_field(name_expire.clone(), FieldType::DateTime);
docdef.set_default(&name_expire, calc.clone()).unwrap();
docdef.add_index(&name_expire, IndexType::Index).unwrap();
let mut update = Update::new(Session::doc_names()[0].clone());
update
.get_values_mut()
.add_field(name_expire.clone(), calc.clone());
let path = Path::new(
Include::All,
Include::Just(Session::doc_names()[0].clone().into()),
Include::Just(Action::OnQuery),
);
let query_action = DocFuncType::ExistingQuery(update.into());
docdef.add_route(path, query_action);
let mut delete = Delete::new(Session::doc_names()[0].clone());
let delete_qry = delete.get_query_mut();
let mut delete_calc = Calculation::new(Operand::LessThan);
delete_calc
.add_value(CalcValue::Existing(FieldType::DateTime))
.unwrap();
delete_calc.add_value(FieldType::DateTime).unwrap();
delete_qry.add(name_expire.clone(), delete_calc);
let delete_func = DocFuncType::Trigger(delete.into());
docdef.add_route(Clock::get_path(), delete_func);
docdef
}
}