Skip to content

UUIDV4Type

Wyatt Greenway edited this page Nov 23, 2022 · 20 revisions

class UUIDV4Type extends UUIDBaseType

UUID (version 4) type.

This represents a string based UUID, using UUID version 4. The underlying database type is the same as StringType, which is usually a "VARCHAR" type.

An optional "prefix" can be specified for this type, which will allow you to prefix your ids.

This type will automatically decide its own "length" based on the length of a UUIDV4, plus the length of any prefix you provide.

See the uuid module to properly understand the options for this field type. 📜

Example:

  • class UUIDs extends Model {
      static fields = {
        uuid1: Types.UUIDV4({
          prefix: 'USER_',
          random: ...,
          rng: ...,
          buffer: ...,
          offset: ...,
        }),
        uuid2: new Types.UUIDV4Type({
          prefix: 'USER_',
          ...,
        }),
        uuidWithDefault: {
          type: Types.UUIDV4({ ... }),
          defaultValue: Types.UUIDV4.Default.UUIDV4,
        },
      };
    }

See also: Type

property UUIDV4Type::Default: object = { UUIDV4 }

UUIDV4 is a method that can be used as the defaultValue of a Field to have this field auto-generate UUIDs for new models.


method UUIDV4Type::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 UUIDV4 string primitive. A null or undefined value will simply be returned.

The provided value will be given to uuid.validate to validate that it is a correct UUIDV4. If the provided value is not a valid UUIDV4, then this method will throw an exception.

If your UUID is prefixed with the prefix option, then that will be stripped off first before the provided value is handed off to uuid.validate for validation. The prefix will be added back after validation.

Notes:

  • If a valid UUID is given without a prefix, then the specified prefix (if any) will simply be added to the returned result.

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 UUIDV4Type::getArgsForUUID(options: object): Array<any> 📜

This is an internal method that is used by the type. It prepares the arguments needed for the uuid module based on the options provided to the type when created. It will return arguments that can be properly passed to uuid.v1.

Arguments:

  • options: object

    The "options" object provided to the type when it was initially created.

Return value: Array<any>

An array of arguments to pass to uuid.v1


method UUIDV4Type::isValidValue(value: any): boolean 📜

Check if the provided value is valid.

This will check if the provided value is a valid UUIDV4, ignoring any prefix.

Arguments:

  • value: any

    The value to check.

Return value: boolean


static method UUIDV4Type::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 'UUIDV4'


method UUIDV4Type::validateOptions(): undefined 📜

This is an internal method that is used by the type. It validates the arguments that will be passed to the uuid.v4 method. If incorrect options were provided to the type when the type was specified, then this will throw a validation exception.

Notes:

  • The provided "options" are not validated until the first UUID is generated for the first time.

Return value: undefined



Clone this wiki locally