1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
// TODO technically all field types are nullable...is that okay?

// TODO also finish refactoring this library

// TODO how do we do transactions? Will the IC simply take care of that for us? The answer is no, the IC will not take care of that for us
// TODO How much type checking and enforcing should sudodb do? Perhaps I should just leave that up to sudograph for now?

// TODO I think I should do some primitive type checking in here...such as if you try to update a field
// TODO that you did not initialize the type with...like creating or updating fields that you did not initialize the type with

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::error::Error;
mod create;
mod read;
mod update;
mod delete;

pub use create::create;
pub use read::read;
pub use update::update;
pub use delete::delete;

use ic_cdk;

pub type ObjectTypeStore = BTreeMap<ObjectTypeName, ObjectType>;

type ObjectTypeName = String;

#[derive(Debug)]
pub struct ObjectType {
    object_type_name: String,
    field_types_store: FieldTypesStore,
    field_values_store: FieldValuesStore,
    // field_indexes_store: FieldIndexStore
    // TODO the indexes will go here
}

pub type FieldTypesStore = BTreeMap<FieldName, FieldType>;

pub type FieldName = String;

// TODO time to get relations working!!!
// TODO it might be nice to have a FieldType Scalar that is itself an enum of the scalar types, or something
#[derive(Debug, Clone)]
pub enum FieldType {
    Boolean,
    Date,
    Float, // TODO do we need to split this into sizes? What should the default be?
    Int, // TODO do we need to split this into sizes? What should the default be?
    RelationMany(FieldTypeRelationInfo),
    RelationOne(FieldTypeRelationInfo),
    String
}

#[derive(Debug, Clone)]
pub struct FieldTypeRelationInfo {
    pub object_name: String,
    pub opposing_object_name: String,
    pub opposing_field_name: Option<String>
    // pub relation_name: Option<String>
}

type FieldValuesStore = BTreeMap<PrimaryKey, FieldValueStore>;

type PrimaryKey = String;

type FieldValueStore = BTreeMap<FieldName, FieldValue>;

#[derive(Debug)]
#[derive(Clone)]
pub enum FieldValue {
    Scalar(Option<FieldValueScalar>),
    RelationMany(Option<FieldValueRelationMany>),
    RelationOne(Option<FieldValueRelationOne>)
}

// TODO do we want ID to be a scalar type as well?
#[derive(Clone, Debug)]
pub enum FieldValueScalar {
    Boolean(bool),
    Date(String),
    Float(f32),
    Int(i32),
    String(String)
}

#[derive(Clone, Debug)]
pub struct FieldValueRelationMany {
    pub relation_object_type_name: ObjectTypeName,
    pub relation_primary_keys: Vec<PrimaryKey>,
    pub relation_primary_keys_to_remove: Vec<PrimaryKey> // TODO this is a really bad way of doing this, what we really need to do is have the FieldInput have its own types, and we can have a specific type for removing fields
}

#[derive(Clone, Debug)]
pub struct FieldValueRelationOne {
    pub relation_object_type_name: ObjectTypeName,
    pub relation_primary_key: PrimaryKey
}

// type FieldIndexStore = BTreeMap<FieldValue, PrimaryKey>;

#[derive(Clone, Debug)]
pub enum ReadInputOperation {
    Contains,
    EndsWith,
    Equals,
    GreaterThan,
    GreaterThanOrEqualTo,
    In, // TODO this is just not implented for strings right now
    LessThan,
    LessThanOrEqualTo,
    StartsWith
    // TODO we have not implemented or yet, and we have not done arbitrarily nested ands and ors
}

// TODO think if we are using the best structure below
// TODO some of these are redundant depending on what we're doing
// TODO should we have a ReadInputScalar and ReadInputRelation?
#[derive(Debug)]
pub struct ReadInput {
    // TODO not sure we need input_type since FieldValue has that information inside of it
    pub input_type: ReadInputType, // TODO I think we might not need this
    pub input_operation: ReadInputOperation,
    pub field_name: String,
    pub field_value: FieldValue,
    pub relation_object_type_name: ObjectTypeName, // TODO this field is not necessary for scalars
    pub relation_read_inputs: Vec<ReadInput>, // TODO this field is not necessary for scalars
    pub and: Vec<ReadInput>, // TODO should we make and and or options?
    pub or: Vec<ReadInput>
    // TODO I think I will need the field type here
}

// TODO we might want to get rid of this type
#[derive(Debug)]
pub enum ReadInputType {
    Scalar,
    Relation
}

#[derive(Debug)]
pub struct FieldInput {
    pub field_name: String,
    pub field_value: FieldValue
}

#[derive(Debug)]
pub struct FieldTypeInput {
    pub field_name: String,
    pub field_type: FieldType
}

// TODO make sure we are doing our error handling in the best way possible
#[derive(Debug)]
pub struct SudodbError {
    message: String
}

impl Error for SudodbError {

}

impl std::fmt::Display for SudodbError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        return write!(f, "{}", self.message);
    }
}

pub type JSONString = String;

// TODO we should do some type checking on relations
// TODO it may be slightly difficult though, because we do not know the order the user will do relations in
// TODO perhaps, once done inserting into the map, just loop through and check that all relations are accounted for
// TODO keep a copy of the original or just abort/panic if there is a problem, this should roll back the state on the IC
pub fn init_object_type(
    object_type_store: &mut ObjectTypeStore,
    object_type_name: &str,
    field_type_inputs: Vec<FieldTypeInput>
) -> Result<(), Box<dyn Error>> {
    ic_cdk::println!("{:?}", object_type_name);
    ic_cdk::println!("{:?}", field_type_inputs);

    let mut field_types_store = BTreeMap::new();

    for field_type_input in field_type_inputs {
        field_types_store.insert(
            field_type_input.field_name,
            field_type_input.field_type
        );
    }

    object_type_store.insert(
        String::from(object_type_name),
        ObjectType {
            object_type_name: String::from(object_type_name),
            field_values_store: BTreeMap::new(),
            field_types_store
        }
    );

    return Ok(());
}

// TODO create a selection set object
// TODO it could be very simple, just a map with keys that are fields...map to an option, the option has another map

// type SelectionSet = HashMap<FieldName, Option<SelectionSet>>;

#[derive(Debug, Clone)]
pub struct SelectionSet(pub Option<HashMap<FieldName, SelectionSet>>);

pub fn convert_field_value_store_to_json_string(
    object_type_store: &ObjectTypeStore,
    field_value_store: &FieldValueStore,
    selection_set: &SelectionSet
) -> JSONString {
    if let Some(selection_set_hash_map) = &selection_set.0 {
        let inner_json = selection_set_hash_map.iter().enumerate().fold(String::from(""), |result, (i, (key, value))| {

            let field_value = field_value_store.get(key).unwrap();

            match field_value {
                FieldValue::Scalar(field_value_scalar_option) => {
                    return format!(
                        "{result}\"{key}\":{value}{comma}",
                        result = result,
                        key = key,
                        value = match field_value_scalar_option {
                            Some(field_value_scalar) => match field_value_scalar {
                                FieldValueScalar::Boolean(field_value_scalar_boolean) => format!("{}", field_value_scalar_boolean),
                                FieldValueScalar::Date(field_value_scalar_string) => format!("\"{}\"", field_value_scalar_string),
                                FieldValueScalar::Float(field_value_scalar_int) => format!("{}", field_value_scalar_int),
                                FieldValueScalar::Int(field_value_scalar_int) => format!("{}", field_value_scalar_int),
                                FieldValueScalar::String(field_value_scalar_string) => format!("\"{}\"", field_value_scalar_string)
                            },
                            None => String::from("null")
                        },
                        comma = if i == selection_set_hash_map.iter().len() - 1 { "" } else { "," }
                    );
                },
                FieldValue::RelationMany(field_value_relation_many_option) => {
                    ic_cdk::println!("FieldValue::RelationMany");

                    if let Some(field_value_relation_many) = field_value_relation_many_option {
                        ic_cdk::println!("{:?}", field_value_relation_many);
                        // TODO we simply need to go retrieve the relation and serialize it...in fact, I think we can
                        // TODO just do this recursively and call this function again, and it will automatically resolve arbitrarily nested relations
                        // let relation_field_value_store = 
                    
                        if let Some(relation_object_type) = object_type_store.get(&field_value_relation_many.relation_object_type_name) {
                            ic_cdk::println!("{:?}", relation_object_type);
                            // let relation_field_value_store = relation_object_type.field_values_store.get();
                        
                            // TODO evil mutations of course
                            let mut relation_string = String::from("[");
                            
                            for (index, relation_primary_key) in field_value_relation_many.relation_primary_keys.iter().enumerate() {
                                // let relation_json_string = 
                                // let relation_field_value_store = relation_object_type.field_values_store.get(relation_primary_key);
                            
                                if let Some(relation_field_value_store) = relation_object_type.field_values_store.get(relation_primary_key) {
                                    let relation_json_string = convert_field_value_store_to_json_string(
                                        object_type_store,
                                        relation_field_value_store,
                                        value
                                    );

                                    ic_cdk::println!("relation_json_string");
                                    ic_cdk::println!("{:?}", relation_json_string);
    
                                    relation_string.push_str(&relation_json_string);
                                    relation_string.push_str(if index == field_value_relation_many.relation_primary_keys.iter().len() - 1 { "" } else { "," });
                                }
                                else {
                                    return result; // TODO this should probably be an error
                                }
                            }
    
                            relation_string.push_str("]");
    
                            return format!(
                                "{result}\"{key}\":{value}{comma}",
                                result = result,
                                key = key,
                                value = relation_string,
                                comma = if i == selection_set_hash_map.iter().len() - 1 { "" } else { "," }
                            );
                        }
                        else {
                            // return result; // TODO this should probably return an error
                            panic!();
                        }
                    }
                    else {
                        return format!(
                            "{result}\"{key}\":{value}{comma}",
                            result = result,
                            key = key,
                            value = String::from("[]"),
                            comma = if i == selection_set_hash_map.iter().len() - 1 { "" } else { "," }
                        );
                    }
                },
                FieldValue::RelationOne(field_value_relation_one_option) => {
                    if let Some(field_value_relation_one) = field_value_relation_one_option {
                        if let Some(relation_object_type) = object_type_store.get(&field_value_relation_one.relation_object_type_name) {
                            if let Some(relation_field_value_store) = relation_object_type.field_values_store.get(&field_value_relation_one.relation_primary_key) {
                                
                                ic_cdk::println!("relation_field_value_store");
                                ic_cdk::println!("{:?}", relation_field_value_store);
                                
                                let relation_json_string = convert_field_value_store_to_json_string(
                                    object_type_store,
                                    relation_field_value_store,
                                    value
                                );
    
                                ic_cdk::println!("relation_json_string");
                                ic_cdk::println!("{}", relation_json_string);
    
                                // TODO we need some sort of selection setting here
                            
                                return format!(
                                    "{result}\"{key}\":{value}{comma}",
                                    result = result,
                                    key = key,
                                    value = relation_json_string,
                                    comma = if i == selection_set_hash_map.iter().len() - 1 { "" } else { "," }
                                );
                            }
                            else {
                                return format!(
                                    "{result}\"{key}\":{value}{comma}",
                                    result = result,
                                    key = key,
                                    value = String::from("null"),
                                    comma = if i == selection_set_hash_map.iter().len() - 1 { "" } else { "," }
                                );
                            }
                        }
                        else {
                            panic!();
                        }
                    }
                    else {
                        return format!(
                            "{result}\"{key}\":{value}{comma}",
                            result = result,
                            key = key,
                            value = String::from("null"),
                            comma = if i == selection_set_hash_map.iter().len() - 1 { "" } else { "," }
                        );
                    }
                }
            };
        });
        
        let full_json = format!(
            "{{{inner_json}}}",
            inner_json = inner_json
        );
        
        ic_cdk::println!("full_json");
        ic_cdk::println!("{}", full_json);

        return full_json;
    }
    else {
        return String::from("");
    }
}

// TODO actually, we absolutely need some sort of selection set mechanism here, otherwise we will grab all relations
// TODO and there could be 100s or 1000s or millions
// TODO figure out how to print this better maybe...
// TODO for now I am just going to serialize all fields of all records...there is not concept of a selection or selection set
// TODO I believe most of the inneficiency will just be in the serialization to the string, and not in the fetching itself
// TODO this is really where the retrieval is done
// TODO this only works for string values right now, and only scalar values as well
// TODO We will need to add support for numbers, null, undefined, and relations
pub fn old_convert_field_value_store_to_json_string(
    object_type_store: &ObjectTypeStore,
    field_value_store: &FieldValueStore,
    selection_set: SelectionSet
) -> String {
    let inner_json = field_value_store.iter().enumerate().fold(String::from(""), |result, (i, (key, value))| {
        match value {
            FieldValue::Scalar(field_value_scalar_option) => {
                return format!(
                    "{result}\"{key}\":{value}{comma}",
                    result = result,
                    key = key,
                    value = match field_value_scalar_option {
                        Some(field_value_scalar) => match field_value_scalar {
                            FieldValueScalar::Boolean(field_value_scalar_boolean) => format!("{}", field_value_scalar_boolean),
                            FieldValueScalar::Date(field_value_scalar_string) => format!("\"{}\"", field_value_scalar_string),
                            FieldValueScalar::Float(field_value_scalar_int) => format!("{}", field_value_scalar_int),
                            FieldValueScalar::Int(field_value_scalar_int) => format!("{}", field_value_scalar_int),
                            FieldValueScalar::String(field_value_scalar_string) => format!("\"{}\"", field_value_scalar_string)
                        },
                        None => String::from("null")
                    },
                    comma = if i == field_value_store.iter().len() - 1 { "" } else { "," }
                );
            },
            FieldValue::RelationMany(field_value_relation_many_option) => {
                if let Some(field_value_relation_many) = field_value_relation_many_option {
                    // TODO we simply need to go retrieve the relation and serialize it...in fact, I think we can
                    // TODO just do this recursively and call this function again, and it will automatically resolve arbitrarily nested relations
                    // let relation_field_value_store = 
                
                    if let Some(relation_object_type) = object_type_store.get(&field_value_relation_many.relation_object_type_name) {
                        // let relation_field_value_store = relation_object_type.field_values_store.get();
                    
                        // TODO evil mutations of course
                        let mut relation_string = String::from("[");
                        
                        for (index, relation_primary_key) in field_value_relation_many.relation_primary_keys.iter().enumerate() {
                            // let relation_json_string = 
                            // let relation_field_value_store = relation_object_type.field_values_store.get(relation_primary_key);
                        
                            if let Some(relation_field_value_store) = relation_object_type.field_values_store.get(relation_primary_key) {
                                let relation_json_string = old_convert_field_value_store_to_json_string(
                                    object_type_store,
                                    relation_field_value_store,
                                    selection_set.clone()
                                );

                                relation_string.push_str(&relation_json_string);
                                relation_string.push_str(if index == field_value_relation_many.relation_primary_keys.iter().len() - 1 { "" } else { "," });
                            }
                            else {
                                return result; // TODO this should probably be an error
                            }
                        }

                        relation_string.push_str("]");

                        return format!(
                            "{result}\"{key}\":\"{value}\"{comma}",
                            result = result,
                            key = key,
                            value = relation_string,
                            comma = if i == field_value_store.iter().len() - 1 { "" } else { "," }
                        );
                    }
                    else {
                        // return result; // TODO this should probably return an error
                        panic!();
                    }
                }
                else {
                    return format!(
                        "{result}\"{key}\":{value}{comma}",
                        result = result,
                        key = key,
                        value = String::from("[]"),
                        comma = if i == field_value_store.iter().len() - 1 { "" } else { "," }
                    );
                }
            },
            FieldValue::RelationOne(field_value_relation_one_option) => {
                if let Some(field_value_relation_one) = field_value_relation_one_option {
                    if let Some(relation_object_type) = object_type_store.get(&field_value_relation_one.relation_object_type_name) {
                        if let Some(relation_field_value_store) = relation_object_type.field_values_store.get(&field_value_relation_one.relation_primary_key) {
                            
                            ic_cdk::println!("relation_field_value_store");
                            ic_cdk::println!("{:?}", relation_field_value_store);
                            
                            let relation_json_string = old_convert_field_value_store_to_json_string(
                                object_type_store,
                                relation_field_value_store,
                                selection_set.clone()
                            );

                            ic_cdk::println!("relation_json_string");
                            ic_cdk::println!("{}", relation_json_string);

                            // TODO we need some sort of selection setting here
                        
                            return format!(
                                "{result}\"{key}\":{value}{comma}",
                                result = result,
                                key = key,
                                value = relation_json_string,
                                comma = if i == field_value_store.iter().len() - 1 { "" } else { "," }
                            );
                        }
                        else {
                            return format!(
                                "{result}\"{key}\":{value}{comma}",
                                result = result,
                                key = key,
                                value = String::from("null"),
                                comma = if i == field_value_store.iter().len() - 1 { "" } else { "," }
                            );
                        }
                    }
                    else {
                        panic!();
                    }
                }
                else {
                    return format!(
                        "{result}\"{key}\":{value}{comma}",
                        result = result,
                        key = key,
                        value = String::from("null"),
                        comma = if i == field_value_store.iter().len() - 1 { "" } else { "," }
                    );
                }
            }
        };
    });

    let full_json = format!(
        "{{{inner_json}}}",
        inner_json = inner_json
    );

    return full_json;
}

pub fn get_mutable_object_type(
    object_type_store: &mut ObjectTypeStore,
    object_type_name: String
) -> Result<&mut ObjectType, Box<dyn Error>> { // TODO not sure the result needs to be a reference
    // TODO it would be nice to use the ? syntax here
    let object_type_option = object_type_store.get_mut(&object_type_name);

    match object_type_option {
        Some(object_type) => {
            return Ok(object_type);
        },
        None => {
            return Err(Box::new(SudodbError {
                message: format!(
                    "Object type {object_type_name} not found in database",
                    object_type_name = object_type_name
                )
            }));
        }
    };
}

pub fn get_object_type(
    object_type_store: &ObjectTypeStore,
    object_type_name: String
) -> Result<&ObjectType, Box<dyn Error>> { // TODO not sure the result needs to be a reference
    // TODO it would be nice to use the ? syntax here
    let object_type_option = object_type_store.get(&object_type_name);

    match object_type_option {
        Some(object_type) => {
            return Ok(object_type);
        },
        None => {
            return Err(Box::new(SudodbError {
                message: format!(
                    "Object type {object_type_name} not found in database",
                    object_type_name = object_type_name
                )
            }));
        }
    };
}

pub fn get_mutable_field_value_store(
    object_type_store: &mut ObjectTypeStore,
    object_type_name: String,
    id: String // TODO consider using the name primary_key instead of id
) -> Result<&mut FieldValueStore, Box<dyn Error>> { // TODO not sure the result needs to be a reference
    let mutable_object_type = get_mutable_object_type(
        object_type_store,
        String::from(&object_type_name)
    )?;
    
    let mutable_field_value_store_option = mutable_object_type.field_values_store.get_mut(&id);

    match mutable_field_value_store_option {
        Some(mutable_field_value_store) => {
            return Ok(mutable_field_value_store);
        },
        None => {
            return Err(Box::new(SudodbError {
                message: format!(
                    "Field value store for id {id} on object type {object_type_name} not found in database",
                    id = id,
                    object_type_name = String::from(&object_type_name)
                )
            }));
        }
    };
}

pub fn get_field_value_store(
    object_type_store: &ObjectTypeStore,
    object_type_name: String,
    id: String // TODO consider using the name primary_key instead of id
) -> Result<&FieldValueStore, Box<dyn Error>> { // TODO not sure the result needs to be a reference
    let object_type = get_object_type(
        object_type_store,
        String::from(&object_type_name)
    )?;
    
    let field_value_store_option = object_type.field_values_store.get(&id);

    match field_value_store_option {
        Some(field_value_store) => {
            return Ok(field_value_store);
        },
        None => {
            return Err(Box::new(SudodbError {
                message: format!(
                    "Field value store for id {id} on object type {object_type_name} not found in database",
                    id = id,
                    object_type_name = String::from(&object_type_name)
                )
            }));
        }
    };
}

pub fn get_mutable_field_value(
    mutable_field_value_store: &mut FieldValueStore,
    object_type_name: String,
    field_name: String,
    id: String
) -> Result<&mut FieldValue, Box<dyn Error>> { // TODO not sure the result needs to be a reference
    let mutable_field_value_option = mutable_field_value_store.get_mut(&field_name);

    match mutable_field_value_option {
        Some(mutable_field_value) => {
            return Ok(mutable_field_value);
        },
        None => {
            return Err(Box::new(SudodbError {
                message: format!(
                    "field value for field name {field_name} and id {id} on object type {object_type_name} not found in database",
                    field_name = field_name,
                    id = id,
                    object_type_name = object_type_name
                )
            }));
        }
    };
}

// TODO we might want to pass in the field value store here
pub fn get_field_value(
    object_type_store: &ObjectTypeStore,
    object_type_name: String,
    field_name: String,
    id: String
) -> Result<&FieldValue, Box<dyn Error>> { // TODO not sure the result needs to be a reference
    let field_value_store = get_field_value_store(
        object_type_store,
        String::from(&object_type_name),
        String::from(&id)
    )?;

    let field_value_option = field_value_store.get(&field_name);

    match field_value_option {
        Some(field_value) => {
            return Ok(field_value);
        },
        None => {
            return Err(Box::new(SudodbError {
                message: format!(
                    "field value for field name {field_name} and id {id} on object type {object_type_name} not found in database",
                    field_name = field_name,
                    id = id,
                    object_type_name = object_type_name
                )
            }));
        }
    };
}

pub fn get_field_type_for_field_name(
    object_type_store: &ObjectTypeStore,
    object_type_name: String,
    field_name: String
) -> Result<FieldType, Box<dyn Error>> {
    // TODO only use mutable if necessary, make more functions for immutable
    let object_type = get_object_type(
        object_type_store,
        object_type_name
    )?;

    let field_type_option = object_type.field_types_store.get(&field_name);

    match field_type_option {
        Some(field_type) => {
            return Ok(field_type.clone());
        },
        None => {
            return Err(Box::new(SudodbError {
                message: format!(
                    "Field type for field {field_name} on object type {object_type_name} not found in database",
                    field_name = field_name,
                    object_type_name = object_type.object_type_name
                )
            }));
        }
    };
}