#[macro_export]
macro_rules! create_model {
($service:expr, $database:expr, struct $sname:ident { $($fname:ident : $ftype:ty),* }) => {
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct $sname {
$(pub $fname : $ftype),*
}
impl $sname {
pub fn model_name() -> &'static str {
stringify!($sname)
}
pub fn field_names() -> &'static [&'static str] {
&[$(stringify!($fname)),*]
}
pub fn meta() -> Meta {
Meta {
database: $database.to_lowercase(),
collection: format!("{}__{}",
$service.to_lowercase(),
stringify!($sname).to_lowercase()
)
}
}
pub fn form_attrs() -> HashMap<String, Transport> {
let raw_attrs: HashMap<&str, Widget> = Self::widgets();
let mut clean_attrs: HashMap<String, Transport> = HashMap::new();
for (field, widget) in &raw_attrs {
clean_attrs.insert(field.to_string(), widget.clean_attrs(field));
}
clean_attrs
}
pub fn json_attrs() -> String {
let attrs: HashMap<String, Transport> = Self::form_attrs();
let mut json_text = String::new();
for (field, trans) in attrs {
let tmp = serde_json::to_string(&trans).unwrap();
if json_text.len() > 0 {
json_text = format!("{},\"{}\":{}", json_text, field, tmp);
} else {
json_text = format!("\"{}\":{}", field, tmp);
}
}
format!("{{{}}}", json_text)
}
pub fn form_html(action: &str, method: Option<&str>, enctype: Option<&str>) -> String {
Self::html(
Self::form_attrs(),
&stringify!($sname).to_lowercase(),
action,
if method.is_some() { method.unwrap().to_lowercase() } else { "get".to_string() },
if enctype.is_some() { enctype.unwrap() } else { "application/x-www-form-urlencoded" }
)
}
pub async fn migrat<'a>(keyword: &'a str, client: &Client) {
static MODEL_NAME: &'static str = stringify!($sname);
static FIELD_NAMES: &'static [&'static str] = &[$(stringify!($fname)),*];
if FIELD_NAMES.len() == 0 {
panic!("The model structure has no fields.");
}
let map_field_types: HashMap<&'static str, &'static str> =
FIELD_NAMES.iter().map(|item| item.to_owned())
.zip([$(stringify!($ftype)),*].iter().map(|item| item.to_owned())).collect();
let meta: Meta = Self::meta();
let mango_orm_keyword = format!("mango_orm_{}", keyword);
let attrs: HashMap<&'static str, Widget> = Self::widgets();
let database_names: Vec<String> =
client.list_database_names(None, None).await.unwrap();
let mut default_values: HashMap<&'static str, (&'static str, String)> = HashMap::new();
for (field, widget) in attrs {
if !FIELD_NAMES.contains(&field) {
panic!(
"Service: `{}` -> Model: `{}` -> raw_attrs() : `{}` - Incorrect field name.",
$service, MODEL_NAME, field
)
}
default_values.insert(field, (widget.value.get_data_type(), widget.value.get_raw_data()));
match widget.value {
FieldType::InputCheckBox(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputCheckBox` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.maxlength != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputCheckBox` : `maxlength` = only 0 (zero).",
$service, MODEL_NAME, field
)
} else if widget.other_attrs.contains("checked") {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputCheckBox` : `other_attrs` - must not contain the word `checked`.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputCheckBox` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "bool" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `bool`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputColor(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputColor` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputColor` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputDate(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputDate` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputDate` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputDateTime(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputDateTime` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputDateTime` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputEmail(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputEmail` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputEmail` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputFile => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputFile` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputFile` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputImage => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputImage` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputImage` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputNumberI32(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputNumber` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputNumber` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "i32" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `i32`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputNumberU32(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputNumber` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputNumber` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "u32" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `u32`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputNumberI64(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputNumber` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputNumber` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "i64" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `i64`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputNumberF64(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputNumber` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputNumber` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "f64" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `f64`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputPassword(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputPassword` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.value != FieldType::InputPassword(String::new()) {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputPassword` : `value` = only DataType::Text(String::new()).",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputPassword` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputRadio(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputRadio` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.maxlength != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputRadio` : `maxlength` = only 0 (zero).",
$service, MODEL_NAME, field
)
} else if widget.other_attrs.contains("checked") {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputRadio` : `other_attrs` - must not contain the word `checked`.",
$service, MODEL_NAME, field
)
} else if widget.select.len() == 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputRadio` : `select` - must not be an empty vec![]",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "bool" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `bool`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputRangeI32(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputRange` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputRange` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "i32" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `i32`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputRangeU32(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputRange` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputRange` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "u32" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `u32`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputRangeI64(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputRange` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputRange` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "i64" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `i64`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputRangeF64(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputRange` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputRange` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "f64" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `f64`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputTel(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputTel` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputTel` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputText(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputText` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputText` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::InputUrl(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputUrl` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `InputUrl` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::TextArea(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `TextArea` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `TextArea` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::SelectText(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `Select` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() == 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `Select` : `select` - Should not be empty.",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::SelectI32(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `Select` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() == 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `Select` : `select` - Should not be empty.",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "i32" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `i32`.",
$service, MODEL_NAME, field
)
}
}
FieldType::SelectU32(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `Select` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() == 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `Select` : `select` - Should not be empty.",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "u32" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `u32`.",
$service, MODEL_NAME, field
)
}
}
FieldType::SelectI64(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `Select` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() == 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `Select` : `select` - Should not be empty.",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "i64" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `i64`.",
$service, MODEL_NAME, field
)
}
}
FieldType::SelectF64(_) => {
if widget.relation_model != String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `Select` : `relation_model` = only blank string.",
$service, MODEL_NAME, field
)
} else if widget.select.len() == 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `Select` : `select` - Should not be empty.",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "f64" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `f64`.",
$service, MODEL_NAME, field
)
}
}
FieldType::ForeignKey => {
if widget.relation_model == String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `ForeignKey` : `relation_model` = <CategoryName>::meta().collection.to_string().",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `ForeignKey` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::ManyToMany => {
if widget.relation_model == String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `ManyToMany` : `relation_model` = <CategoryName>::meta().collection.to_string().",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `ManyToMany` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
FieldType::OneToOne => {
if widget.relation_model == String::new() {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `OneToOne` : `relation_model` = <CategoryName>::meta().collection.to_string().",
$service, MODEL_NAME, field
)
} else if widget.select.len() != 0 {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` -> FieldType `OneToOne` : `select` = only blank vec![].",
$service, MODEL_NAME, field
)
} else if map_field_types[field] != "String" {
panic!(
"Service: `{}` -> Model: `{}` -> Field: `{}` : Field type is not equal to `String`.",
$service, MODEL_NAME, field
)
}
}
_ => panic!("Service: `{}` -> Model: `{}` -> Field: `{}` : `field_type` - Non-existent field type.",
$service, MODEL_NAME, field),
}
}
let mango_orm_fnames: Vec<String> = {
let filter: Document = doc! {
"database": &meta.database, "collection": &meta.collection};
let model: Document = client.database(&mango_orm_keyword)
.collection("models").find_one(filter, None).await.unwrap().unwrap();
let fields: Vec<Bson> = model.get_array("fields").unwrap().to_vec();
fields.into_iter().map(|item: Bson| item.as_str().unwrap().to_string()).collect()
};
let mut run_document_modification: bool = false;
if FIELD_NAMES.len() != mango_orm_fnames.len() {
run_document_modification = true;
} else {
for item in FIELD_NAMES {
if mango_orm_fnames.iter().any(|item2| item2 != item) {
run_document_modification = true;
break;
}
}
}
if run_document_modification {
let db: Database = client.database(&meta.database);
let collection: Collection = db.collection(&meta.collection);
let mut cursor: Cursor = collection.find(None, None).await.unwrap();
while let Some(result) = cursor.next().await {
let curr_doc: Document = result.unwrap();
let mut tmp_doc = doc! {};
for field in FIELD_NAMES {
if curr_doc.contains_key(field) {
for item in curr_doc.iter() {
if item.0 == field {
tmp_doc.insert(field.to_string(), item.1);
break;
}
}
} else {
let value = &default_values[field];
tmp_doc.insert(field.to_string(), match value.0 {
"string" => Bson::String(value.1.clone()),
"i32" => Bson::Int32(value.1.parse::<i32>().unwrap()),
"u32" => Bson::Int64(value.1.parse::<i64>().unwrap()),
"i64" => Bson::Int64(value.1.parse::<i64>().unwrap()),
"f64" => Bson::Double(value.1.parse::<f64>().unwrap()),
"bool" => Bson::Boolean(value.1.parse::<bool>().unwrap()),
"none" => Bson::Null,
_ => panic!("Invalid data type."),
});
}
}
let query = doc! {"_id": curr_doc.get_object_id("_id").unwrap()};
let update = UpdateModifications::Document(tmp_doc);
collection.update_one(query, update, None).await.unwrap();
}
}
let db: Database = client.database(&meta.database);
if !database_names.contains(&meta.database) ||
!db.list_collection_names(None).await.unwrap().contains(&meta.collection) {
db.create_collection(&meta.collection, None).await.unwrap();
}
let db: Database = client.database(&mango_orm_keyword);
if !database_names.contains(&mango_orm_keyword) ||
!db.list_collection_names(None).await.unwrap().contains(&"models".to_owned()) {
panic!("For migration not used `models::Monitor.refresh()`.");
} else {
let collection = db.collection("models");
let filter = doc! {"database": &meta.database, "collection": &meta.collection};
let doc = doc!{
"database": &meta.database,
"collection": &meta.collection,
"fields": FIELD_NAMES,
"status": true
};
if collection.count_documents(filter.clone(), None).await.unwrap() == 0_i64 {
collection.insert_one(doc, None).await.unwrap();
} else {
let update = UpdateModifications::Document(doc);
collection.update_one(filter, update, None).await.unwrap();
}
}
}
}
}
}
#[cfg(test)]
mod tests {
}