Skip to content

NumericType

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

class NumericType extends Type 📜

NUMERIC type. Also known as NUMBER, or DECIMAL in some databases.

This represents a "real" number of high precision and accuracy in the underlying database driver. Unlike the RealType, which suffers from precision and rounding errors, this will be precise to very large and very small values in most databases.

The precision, and scale arguments are named after PostgreSQL, and need some explaining, as their names do not match their intent very well. precision is the total number of digits a number can have. For example, the number 123.456 has a precision of 6, because it contains six digits. The scale specifies the number of digits that are desired after the decimal place. A scale of 0 would essentially mean that you want an integer (no decimal places), whereas a scale of 2 would mean you want two decimal places. It is important to call out that different database drivers may interpret and use these values differently.

Example:

  • class Numeric extends Model {
      static fields = {
        numeric1: Types.NUMERIC(20, 6),
        numeric2: Types.NUMERIC,
        numeric3: new Types.NumericType(10, 3),
      };
    }

See also: Type

method NumericType::castToType(
    context: CastToTypeContext,
): number | null | undefined
📜

Cast provided value to underlying type.

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

See Type.castToType for a more detailed description.

Arguments:

Return value: number | null | undefined

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


method NumericType::constructor(
    precision?: number = 20,
    scale?: number = 6,
): NumericType
📜

Construct a new NUMERIC type.

The precision argument specifies the total number of digits (including decimal places) allowed for each number. The scale argument specifies the number of digits allowed after the decimal place. A scale of 0 is valid, meaning you want no digits after the decimal point.

Arguments:

  • precision?: number (Default: 20)

    The total number of allowed digits in the number. This includes decimal places. So a number of 123.456 would need a precision of 6, since it contains six total digits.

  • scale?: number (Default: 6)

    How many digits are allowed after the decimal point. A value of 2 would allow two digits after the decimal point. A value of 0 would allow no digits after the decimal point, essentially turning this into an integer.

Return value: NumericType


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

Check if the provided value is valid.

This will check if the provided value is a number instance, and if it is finite. If both conditions are true, then it will return true.

Arguments:

  • value: any

    The value to check.

Return value: boolean


static method NumericType::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 'NUMERIC'


method NumericType::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 'NUMERIC'.

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