from nosql_schema_check.model import Model
class Model_Class(Model):
Schema={field: value}
Validations={field: function -> True/False}
Default={field: function -> Default value}
Required=[keys, ...]
Optional variable -
collection=Collection object.
Model_Class.generate()
Schema = {
"key": 'type', -> change 'type' with type string.
"key1": {key: value, ...},
"key2": ('list', 'type') -> change 'type' with type string. ([value1, value2, ...])
}
Validations = {
"key": validate function for value,
...
validation for 'type' only.
}
Default = {
"key": function that returns default value,
...
default value for field only.
}
functions -
Model_Class.check_data
Model_Class.print_schema
Model_Class.compare_records
Model_Class.compare_record
Model_Class.update_records
Model_Class.update_record
Model_Class.collection
Example:
class Test(Model):
Schema={
"integer": "int",
"string": "str",
"list": ('list', "int"),
"dict": {
"key": "int",
"key1": ('list', {
"integer": "int",
"list": ('list', 'str')
})
},
"list_in_list": ('list', ('list', "str")),
"dict_in_dict": {
"a": 'int',
"b": {
"a": 'str'
}
}
}
Validations={
"integer": lambda i:True if i < 10 else print("Integer must be less than 10."),
"string": lambda s: len(s) < 10,
"list": (lambda l: len(l) < 4, lambda i: i < 10),
"dict":(lambda d: len(d) < 3,
{
"key": lambda i: i < 10,
"key1": (lambda l: len(l) < 2,)
}),
"list_in_list": lambda s: len(s) < 10,
"dict_in_dict": {
"b": {
"a": lambda s: len(s) < 20,
},
"a":lambda i: i < 20
}
}
Required=["integer", "list", ('dict', ["key", ("key1", ["integer"])])]
Default={"string":lambda:"Default_String"[:9]}
name="test"
Test.generate()
try:
data = Test.compare_record({"integer": 9, "list": [2,3,5], "dict": {"key": 9, "key1": [{"integer": 20, "list": ["asdd", "sfd"]}]}, "list_in_list": [["string1", "string2", "string3"]], "Something": "Extra_Thing"}, True)
print(data)
data.pop("Something")
print(Test.compare_records([data]))
print(Test.update_record({"integer": 5, "list": [10,20,30]}, data))
print(Test.update_records([{"integer": 5, "list": [10,20,30]},], [data,]))
except:
pass