Skip to content
Wyatt Greenway edited this page Oct 12, 2022 · 24 revisions

class Type

Base type class for all other field types.

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::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 Model and Models type 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. eslint-disable-next-line no-unused-vars.

Return value: undefined


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