2. Models¶
Models are used to describe the data types you wish to use.
2.1. Base Model¶
The base model
2.2. Variables¶
Variables within a Model
-
class
dstore.var.
Variable
(name, default=None, mods=None)[source]¶ The base class used for creating Variable types This also provides a validate method which iterates over the modifiers and runs their validate method on this variable instance.
Parameters: - name – The name of the Variable instance inside a Model Class
- default – The default value to give this variable
- mods – A list of modifiers, i.e. dstore.mod.NotNull()
2.2.1. RowID¶
RowID denotes an instance ID, and is not a Class but an instance of dstore.var.Number:
from dstore import var, mod
RowID = Number( "id", mods = [ mod.AutoIncrement(), mod.PrimaryKey(), mod.Unique() ])
In terms of a MySQL it equates to:
id INT NOT NULL AUTO_INCREMENT,
UNIQUE (id),
PRIMARY KEY (id)
Usage
from dstore import Model, var
class Car( Model ):
_namespace = "cars.make"
_vars = [
var.RowID
]
2.2.2. Number¶
-
class
dstore.var.
Number
(name, default=None, mods=None)[source]¶ This variable type is used to store an integer value.
Parameters: - name – The name of the Variable instance inside a Model Class
- default – The default value to give this variable
- mods – A list of modifiers, i.e. dstore.mod.NotNull()
Usage:
from dstore import var, mod var.Number( "year", 1950, mods = [ mod.NotNull() ] )
2.2.3. Boolean¶
-
class
dstore.var.
Boolean
(name, default=None, mods=None)[source]¶ This variable is used to store a boolean value.
Parameters: - name – The name of the Variable instance inside a Model Class
- default – The default value to give this variable
- mods – A list of modifiers, i.e. dstore.mod.NotNull()
Usage:
from dstore import var var.Boolean( "is_available", False )
2.2.4. Character¶
-
class
dstore.var.
Character
(name, length, default=None, mods=None)[source]¶ This variable is used to store a string of static length.
Parameters: - name – The name of the Variable instance inside a Model Class
- length – The length of the string
- default – The default value to give this variable
- mods – A list of modifiers. The mod dstore.mod.Length( length ) is always added to this list
Usage:
from dstore import var var.Character( "is_available", 4 )
2.2.5. Binary¶
-
class
dstore.var.
Binary
(name, length, default=None, mods=None)[source]¶ This variable is used to store binary data.
Parameters: - name – The name of the Variable instance inside a Model Class
- length – The length of the data
- default – The default value to give this variable
- mods – A list of modifiers. The mod dstore.mod.Length( length ) is always added to this list
Usage:
from dstore import var var.Binary( "data", 32 )
2.2.6. String¶
-
class
dstore.var.
String
(name, length, default=None, mods=None)[source]¶ This variable is used to store a string of variable length.
Parameters: - name – The name of the Variable instance inside a Model Class
- length – The length of the string
- default – The default value to give this variable
- mods – A list of modifiers. The mod dstore.mod.Length( length ) is always added to this list
Usage:
from dstore import var var.String( "username", 32 )
2.2.7. Text¶
-
class
dstore.var.
Text
(name, default=None, mods=None)[source]¶ This variable is used to store text.
Parameters: - name – The name of the Variable instance inside a Model Class
- default – The default value to give this variable
- mods – A list of modifiers, i.e. dstore.mod.NotNull()
Usage:
from dstore import var var.Text( "description" )
2.2.8. Float¶
-
class
dstore.var.
Float
(name, default=None, mods=None)[source]¶ This variable is used to store a floating point integer.
Parameters: - name – The name of the Variable instance inside a Model Class
- default – The default value to give this variable
- mods – A list of modifiers, i.e. dstore.mod.NotNull()
Usage:
from dstore import var var.Float( "price" )
2.2.9. Enum¶
-
class
dstore.var.
Enum
(name, values, default=None, mods=None)[source]¶ This variable is used to store a an enum.
Parameters: - name (str) – The name of the Variable instance inside a Model Class
- values (list[str]) – The list of available choices
- default (str or None) – The default value to give this variable
- mods (list[dstore.mod.Mod]) – A list of modifiers. The mod dstore.mod.InEnum( values ) is always added to this list
Usage:
from dstore import var, mod var.Enum( "car_type", [ "truck", "bus", "ute", "car", "motorbike" ], mods = [ mod.NotNull() ] ) # or var.Enum( name = "car_type", values = [ "truck", "bus", "ute", "car", "motorbike" ], default = "car", mods = [ mod.NotNull() ] )
2.2.10. ForeignKey¶
-
class
dstore.var.
ForeignKey
(namespace)[source]¶ This variable is used to link the Model Instance to another Model Instance
Parameters: namespace – The namespace of the Model Class this variable references This variable type is a subclass of dstore.var.Number, with the following mod:
- dstore.mod.ForeignKey
The name given to this variable is the name of the Model Class being referenced with a suffix of ‘_id”
For example:
from dstore import var var.ForeignKey( "cars.make" )
Is the same as:
from dstore import var var.Number( "cars_make_id", mods = [ dstore.mod.ForeignKey( "cars.make" ) )
2.2.11. Date¶
-
class
dstore.var.
Date
(name, default=None, mods=None)[source]¶ This variable is used to store a date value (without a timezone offset).
Parameters: - name – The name of the Variable instance inside a Model Class
- default – The default value to give this variable
- mods – A list of modifiers, i.e. dstore.mod.NotNull()
Usage:
from dstore import var var.Date( "build_date" )
2.2.12. Time¶
-
class
dstore.var.
Time
(name, default=None, mods=None)[source]¶ This variable is used to store a time value (without a timezone offset).
Parameters: - name – The name of the Variable instance inside a Model Class
- default – The default value to give this variable
- mods – A list of modifiers, i.e. dstore.mod.NotNull()
Usage:
from dstore import var var.Time( "build_time" )
2.2.13. DateTime¶
-
class
dstore.var.
DateTime
(name, default=None, mods=None)[source]¶ This variable is used to store a date and time value (without a timezone offset).
Parameters: - name – The name of the Variable instance inside a Model Class
- default – The default value to give this variable
- mods – A list of modifiers, i.e. dstore.mod.NotNull()
Usage:
from dstore import var var.DateTime( "built_on" )
2.3. Modifiers¶
-
class
dstore.mod.
Mod
[source]¶ The base class used for creating a Modifier.
A Modifier is a way to validate Model instance Variables upon add and/or update.