Skip to content
Wyatt Greenway edited this page Dec 13, 2022 · 24 revisions

class Type 📜

Base type class for all other field types.

static property Type::_isMythixFieldType 📜


static property Type::clone 📜


method Type::castToType(
    context: CastToTypeContext,
): any
📜

Cast a value to the underlying field type.

This method is implemented differently for every field type. Its job is to cast any value provided to the type. For many types, if a null or undefined value is provided, then that value will simply be returned.

Interface:

  • interface CastToTypeContext {
      connection: Connection;  // The connection of the model.
      field: Field;            // The field descriptor that has this type.
      Model: class Model;      // The parent Model class of the field.
      self: Model;             // The model instance.
      value: any;              // The value that should be cast.
    };

Arguments:

  • context: CastToTypeContext

    The "context" passed to castToType. This contains all that any type should need to cast a value.

Return value: any


method Type::clone(): Type 📜

Clone this field type.

Return value: Type

This field type, cloned to a new instance.


method Type::constructor(
    ...args: Array<any>,
): Type
📜

Instantiate a new field Type.

Arguments:

  • ...args: Array<any>

    All field types "record" their constructor arguments, always. Mythix ORM requires that all field types, when calling super in their constructor, provide the base Type class all arguments provided to their constructor. This is so that the field type can later be cloned or serialized.

Return value: Type


method Type::deserialize(
    value: any,
    connection?: Connection,
): any
📜

Deserialize the field's value. This is the opposite of Type.serialize.

The job of this method to serialize the field's value for any serialize operation, generally to JSON. If a connection is provided as the second argument, then it is assumed by the field type that it is being serialized for the database. In this case the connection generally assists with the serializing of the value. This is used for example with DateType and DateTimeType types, when they need to be serialized to or from a database operation.

Arguments:

  • value: any

    The field's value to deserialize.

  • connection?: Connection

    An optional connection, which if provided will likely change how the value is deserialized.

Return value: any

The deserialized value for the field.

See also: Type.serialize


method Type::exposeToModel(): boolean 📜

Used to decide if a field should be exposed on the model or not.

Some fields, such as types like ModelType and ModelsType do not expose themselves on the model as model fields. Instead they only expose themselves via the relationship methods they inject onto the model instance. Most fields do expose themselves on the model, but any field that overrides this method and returns false will not be exposed on the model instance.

Notes:

  • This method is also a static method on the type class.

Return value: boolean

If true is returned, then the field owning this type will be added to the model instance, and accessible by the user. If false is returned, then the field will be "hidden", and not exposed to the user on model instances.


method Type::getDisplayName(): string 📜

Get the "display name" for this type instance.

This will return a "human friendly" name for the type, generally used for logging or debugging. It will change based on the type it is called upon. For example, a BooleanType will return the string value 'BOOLEAN'.

Notes:

  • This method is also a static method on the type class.

Return value: string

A string representing the type. This is not the same as the database equivalent type. Rather, it is simply used to represent the type for logging or debugging.


method Type::getField() 📜

Get the field that owns this type.


method Type::getModel() 📜

Get the model class that owns this field type.


method Type::initialize(
    connection: Connection,
    self: Model,
): undefined
📜

Initialize a model instance against this type.

Initialize is called whenever a model instance is created. Note that the type instance is shared across all model instances. initialize is still called for every model instance that is created however, because the type class can modify the model it exists on. For example, the ModelType and ModelsType types inject custom relational methods onto each model instance.

Arguments:

  • connection: Connection

    The database connection of the calling model instance.

  • self: Model

    The actual model instance that is calling this method.

Return value: undefined


method Type::isDirty(
    context: CheckDirtyContext,
): boolean
📜

Check if the field value is dirty.

This is only used by some field types, such as SerializedType. It will respond with true if the field is dirty, or false if it isn't.

Interface:

  • interface CheckDirtyContext {
      value: any; // The field's current value
      field: Field; // The field that is being checked
      fieldName: string; // The fieldName of the field that is being checked
      self: Model; // The model instance the field is from
      connection: ConnectionBase; // The current connection
    }

Arguments:

  • context: CheckDirtyContext

    The context provided to the call.

Return value: boolean

Returns true if the field is dirty, or false otherwise.


method Type::isForeignKey(): boolean 📜

Check if this field type is a foreign key type.

Notes:

  • This method is also a static method on the type class.

Return value: boolean

Return true if this field type defines a foreign key, or false otherwise.


method Type::isRelational(): boolean 📜

Check if this field type is a relational field type.

Notes:

  • This method is also a static method on the type class.

Return value: boolean

Return true if this field type defines a relation, instead of a direct value. Field types such as ModelType and ModelsType are examples of relational field types. ForeignKeyType is not a relational type, but is instead considered a "concrete" type. Even though it does technically define a relationship, it is also a direct field, that actually has a concrete value, backed by database storage.


method Type::isRemote(): boolean 📜

Check if this field's value is driven by the database.

For example, given an AUTOINCREMENTING id field, this would return true, because the value of the field is provided by the database itself. This can be true for nearly any field type, but is generally only true for auto-incrementing ids, and date/time types.

Return value: boolean

Return true for any field type that has a value provided directly by the underlying database, or false otherwise.


method Type::isValidValue(
    value: any,
): boolean
📜

Check if the value provided is a valid value for this field type.

This is used to check if any value would be a valid value for this type. For example, if this type happens to be a BooleanType, then isValidValue would only return true if the provided value was either true or false. All field types have their own custom isValidValue implementation, to verify values against their type.

Arguments:

  • value: any

    Any value to check.

Return value: boolean

Return true if the provided value is a valid value for the field type this was called upon, or false otherwise.


method Type::isVirtual(): boolean 📜

Check if this field type is a virtual field type.

Notes:

  • This method is also a static method on the type class.

Return value: boolean

Return true if this field type is a virtual field type. Virtual fields are fields that don't directly store a concrete value themselves. They often pull and combine values from other fields, or other models. Currently the only virtual fields in Mythix ORM are ModelType and ModelsType.


method Type::onSetFieldValue(
    context: SetFieldValueContext,
): undefined
📜

This method is called any time a field's value is set on a model.

It is only used by the SerializedType to update its internal "dirty" cache. It can be used by any field type however to detect when a model's field's value changes.

Interface:

  • interface SetFieldValueContext {
      value: any;
      field: Field;
      fieldName: string;
      self: Model;
    }

Arguments:

  • context: SetFieldValueContext

    The context provided to the call.

Return value: undefined

This method returns nothing.


method Type::serialize(
    value: any,
    connection?: Connection,
): any | string
📜

Serialize the field's value. This is the opposite of Type.deserialize.

This is used mostly by the Model.toJSON method to serialize values for JSON format. However, it is also used in some other cases, such as formatting for DateType and DateTimeType, and for checking dirtiness in the SerializedType.

The job of this method is to serialize the field's value for any serialize operation, generally to JSON. If a connection is provided as the second argument, then it is assumed by the field type that it is being serialized for the database. In this case the connection generally assists with the serializing of the value. This is used for example with DateType and DateTimeType types, when they need to be serialized to or from a database operation.

Arguments:

  • value: any

    The field's value to serialize.

  • connection?: Connection

    An optional connection, which if provided will likely change how the value is serialized.

Return value: any | string

Though the return value will generally be a string, it isn't required to be.

See also: Type.deserialize


method Type::setField(
    field: Field,
)
📜

Set the field that owns this type.

Arguments:

  • field: Field

    The field that owns this type.


method Type::setModel(
    Model: class Model,
)
📜

Set the model class that this type belongs to.

Arguments:

  • Model: class Model

    The model class that owns this field type.


static method Type::instantiateType(
    _type,
): Type
📜

Instantiate this field type.

This method will instantiate the type class it is called from. It won't instantiate the type if it is already instantiated, and it will call the type wrapper if one was provided (see Type.static wrapConstructor). It may not need to instantiate the type if the user already did so (for example type: new Types.StringType(), or type: Types.STRING()). It may need to instantiate however if the user didn't do so, for example type: Types.StringType, or type: Types.STRING.

Notes:

  • If the type is already instantiated, then it will be cloned instead using Type.clone.

Arguments:

  • _type

Return value: Type

The instantiated type instance.


static method Type::isSameType(
    value: Type,
): boolean
📜

Check to see if the provided type is the same as this type.

This checks if the two types are the same type by comparing the type names. For example, a Types.BooleanType.isSameType(new BooleanType()) would return true.

Arguments:

  • value: Type

    A type instance to check. This must be a type instance, not a type class.

Return value: boolean

Return true if the provided value is the same type instance as this type class. Return false otherwise. Similarity is based on the name of the types. If the class names between the two match, then the types are considered the same type.


static method Type::isType(
    value: any,
): boolean
📜

Check to see if the provided value is an instance of a Mythix ORM Type. Unlike Type.static isTypeClass, which checks if a class is a Type, this will check to see if an instance is an instance of a Mythix ORM Type. It will return true if the provided value is an instanceof Type, or if the value's constructor property has a truthy _isMythixFieldType property (value.constructor._isMythixFieldType)

Arguments:

  • value: any

    Value to check.

Return value: boolean


static method Type::isTypeClass(
    value: Function,
): boolean
📜

Use this method to check if a class is a Mythix ORM field type. It will return true if the provided value is a class that inherits from Type, or if the provided value has an attribute named _isMythixFieldType that is truthy.

Arguments:

  • value: Function

    Value to check.

Return value: boolean


static method Type::wrapConstructor(
    TypeKlass: class Type,
): Function
📜

This method is used to create the "shortcut" types for Mythix ORM. For example, a "string" type can be created either via new Types.StringType(), or via Types.STRING(). Both are equivalent. Types.STRING was created by calling Types.STRING = Type.wrapConstructor(Types.StringType).

This method "wraps" a type constructor, providing a more convenient way to create and use field types. It works by wrapping the provided class's constructor in a callable method. If the method is not called (i.e. type: Types.STRING) then the type system still knows what to do, because the wrapping method will be called by Mythix ORM to instantiate the type when the model's fields are first fetched.

Arguments:

  • TypeKlass: class Type

    A Type class to wrap.

Return value: Function

A callable function to instantiate the type class. If not called by the user, then Mythix ORM will call the method when the type is being created to fetch the type instance.


method Type::toConnectionType(
    connection: Connection,
    options?: object,
): string
📜

Convert this type to the underlying type of the database driver.

This is generally called by a QueryGenerator instance to convert the field's type to the underlying type of the database. If a connection argument is provided, then it will proxy the call to Connection.typeToString. If no connection argument is provided, then it will simply return the "common" SQL string representation for this type.

Arguments:

  • connection: Connection

    The database driver connection this type is being converted to.

  • options?: object

    Any options to pass through with the type conversion. This options object will be passed to the Connection.typeToString call.

Return value: string

A string type representing the underlying database driver type, or the "common" SQL type if not connection is provided.



Clone this wiki locally