Skip to content

StringType

Wyatt Greenway edited this page Dec 13, 2022 · 15 revisions

class StringType extends Type 📜

STRING type.

This represents a "string" of any size for the underlying database driver, usually a "VARCHAR" type.

If no "length" is specified when you create the type, then a default length of 256 is assumed.

Example:

  • class Strings extends Model {
      static fields = {
        string1: Types.STRING(16),
        string2: Types.STRING, // default length = 256
        string3: new Types.StringType(64),
      };
    }

See also: Type

method StringType::castToType(
    context: CastToTypeContext,
): string | null | undefined
📜

Cast provided value to underlying type.

This will cast the incoming value to the underlying type of this field, a string primitive. A null or undefined value will simply be returned.

See Type.castToType for a more detailed description.

Arguments:

Return value: string | null | undefined

Return the incoming value, cast to this type. null and undefined are simply returned without casting.


method StringType::constructor(
    length?: number,
): StringType
📜

Construct a new STRING type.

The length argument specifies the number of characters to use for this string in the database.

Arguments:

  • length?: number

    How many characters to use in the underlying database to store the value.

Return value: StringType


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

Check if the provided value is valid.

This will check if the provided value is a string (or String) instance. Both an empty string, and a string that is nothing except whitespace are considered "valid".

Arguments:

  • value: any

    The value to check.

Return value: boolean


static method StringType::getDisplayName(): string 📜

Get the "display" name for this type.

This method is called from Model.toString when stringifying the model for representation.

Notes:

  • This is also an instance method that can be called from an instance of the type.

Return value: string

Return the string value 'STRING'


method StringType::toString(
    connection?: Connection,
): string
📜

Stringify the type itself.

If a connection argument is provided, then this will go through the connection to generate the type for the underlying database. If no connection is provided, then a "standard" SQL type will be returned for this type instead. The "standard" type returned when no connection is provided is 'VARCHAR'.

Arguments:

  • connection?: Connection

    An optional connection. If provided, send this type through Type.toConnectionType to have the connection itself generate the underlying type for the database. If connection is not provided, then this will simply return a "standard" generic matching SQL type.

Return value: string



Clone this wiki locally