Skip to content

XIDType

Wyatt Greenway edited this page Oct 12, 2022 · 17 revisions

class XIDType extends UUIDBaseType

XID type.

This represents a string based XID. 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 an XID, plus the length of any prefix you provide.

This type uses the xid-js module to generate XIDs.

Example:

  • class XIDs extends Model {
      static fields = {
        xid1: Types.XID({
          prefix: 'USER_',
        }),
        xid2: new Types.XIDType({
          prefix: 'USER_',
        }),
        xidWithDefault: {
          type: Types.XID({ ... }),
          defaultValue: Types.XID.Default.XID,
        },
      };
    }

property XIDType::Default: object = { XID }

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


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

The provided value will be validated to ensure it is a valid base32 XID value.

If your XID is prefixed with the prefix option, then that will be stripped off first before the provided value is validated. The prefix will be added back after validation.

Notes:

  • If a valid XID 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 XIDType::isValidValue(value: any): boolean

Check if the provided value is valid.

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

Arguments:

  • value: any

    The value to check.

Return value: boolean


static method XIDType::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 'XID'



Clone this wiki locally