@rvoh/dream
    Preparing search index...

    Class DreamSerializerBuilder<DataTypeForOpenapi, MaybeNullDataType, PassthroughDataType, DataType>

    Type Parameters

    Index

    Constructors

    Properties

    $typeForOpenapi: DataTypeForOpenapi
    attributes: (
        | InternalAnyTypedSerializerAttribute
        | InternalAnyTypedSerializerDelegatedAttribute
        | InternalAnyTypedSerializerCustomAttribute
        | InternalAnyTypedSerializerRendersOne<DataType, keyof DataType & string>
        | InternalAnyTypedSerializerRendersMany<DataType, keyof DataType & string>
    )[] = []
    passthroughData: PassthroughDataType = ...

    Methods

    • Includes an attribute from the data object in the serialized output.

      For DreamSerializer, OpenAPI types are automatically inferred from database columns. For json/jsonb columns and virtual attributes (getters), the openapi option is required.

      Parameters

      • name: Exclude<
            DataType["schema"][DataType["table"] & keyof DataType["schema"]]["nonJsonColumnNames"][number],
            keyof Dream,
        > & keyof DataType & "type"

        The attribute name from the data object

      • Optionaloptions: AutomaticSerializerAttributeOptionsForType

        Configuration options:

        • as - Rename the attribute key in the serialized output and OpenAPI shape
        • default - Value to use when the attribute is undefined (not available for type columns)
        • openapi - OpenAPI schema definition; required for json/jsonb columns and non-virtual getters, optional for standard columns (where it can supplement inferred types with a description), and optional for @deco.Virtual() columns (where the type is inferred from the decorator)
        • precision - Round decimal values to the specified number of decimal places (0–9) during rendering; does not affect the OpenAPI shape (not available for type or virtual columns)
        • required - Set to false to mark the attribute as optional in the OpenAPI schema; when omitted, attributes are required by default, meaning undefined values will serialize as null (not available for type columns)

      Returns DreamSerializerBuilder<
          DataTypeForOpenapi,
          MaybeNullDataType,
          PassthroughDataType,
          DataType,
      >

      The serializer builder for method chaining

      // With type inference (database column)
      .attribute('email')

      // With explicit OpenAPI type (virtual attribute or json column)
      .attribute('fullName', { openapi: { type: 'string' } })

      // With default value
      .attribute('status', { default: 'pending' })

      // Rename output key
      .attribute('email', { as: 'userEmail' })

      // Round decimal to 2 places
      .attribute('price', { precision: 2 })

      // Mark as optional in OpenAPI (omitted from response when undefined)
      .attribute('deletedAt', { required: false })
    • Includes an attribute from the data object in the serialized output.

      For DreamSerializer, OpenAPI types are automatically inferred from database columns. For json/jsonb columns and virtual attributes (getters), the openapi option is required.

      Parameters

      • name: DreamVirtualColumns<DataType>[number]

        The attribute name from the data object

      • Optionaloptions: SerializerAttributeOptionsForVirtualColumn

        Configuration options:

        • as - Rename the attribute key in the serialized output and OpenAPI shape
        • default - Value to use when the attribute is undefined (not available for type columns)
        • openapi - OpenAPI schema definition; required for json/jsonb columns and non-virtual getters, optional for standard columns (where it can supplement inferred types with a description), and optional for @deco.Virtual() columns (where the type is inferred from the decorator)
        • precision - Round decimal values to the specified number of decimal places (0–9) during rendering; does not affect the OpenAPI shape (not available for type or virtual columns)
        • required - Set to false to mark the attribute as optional in the OpenAPI schema; when omitted, attributes are required by default, meaning undefined values will serialize as null (not available for type columns)

      Returns DreamSerializerBuilder<
          DataTypeForOpenapi,
          MaybeNullDataType,
          PassthroughDataType,
          DataType,
      >

      The serializer builder for method chaining

      // With type inference (database column)
      .attribute('email')

      // With explicit OpenAPI type (virtual attribute or json column)
      .attribute('fullName', { openapi: { type: 'string' } })

      // With default value
      .attribute('status', { default: 'pending' })

      // Rename output key
      .attribute('email', { as: 'userEmail' })

      // Round decimal to 2 places
      .attribute('price', { precision: 2 })

      // Mark as optional in OpenAPI (omitted from response when undefined)
      .attribute('deletedAt', { required: false })
    • Includes an attribute from the data object in the serialized output.

      For DreamSerializer, OpenAPI types are automatically inferred from database columns. For json/jsonb columns and virtual attributes (getters), the openapi option is required.

      Type Parameters

      • MaybeAttributeName extends any
      • AttributeName extends string

      Parameters

      • name: AttributeName

        The attribute name from the data object

      • options: NonAutomaticSerializerAttributeOptionsWithPossibleDecimalRenderOption

        Configuration options:

        • as - Rename the attribute key in the serialized output and OpenAPI shape
        • default - Value to use when the attribute is undefined (not available for type columns)
        • openapi - OpenAPI schema definition; required for json/jsonb columns and non-virtual getters, optional for standard columns (where it can supplement inferred types with a description), and optional for @deco.Virtual() columns (where the type is inferred from the decorator)
        • precision - Round decimal values to the specified number of decimal places (0–9) during rendering; does not affect the OpenAPI shape (not available for type or virtual columns)
        • required - Set to false to mark the attribute as optional in the OpenAPI schema; when omitted, attributes are required by default, meaning undefined values will serialize as null (not available for type columns)

      Returns DreamSerializerBuilder<
          DataTypeForOpenapi,
          MaybeNullDataType,
          PassthroughDataType,
          DataType,
      >

      The serializer builder for method chaining

      // With type inference (database column)
      .attribute('email')

      // With explicit OpenAPI type (virtual attribute or json column)
      .attribute('fullName', { openapi: { type: 'string' } })

      // With default value
      .attribute('status', { default: 'pending' })

      // Rename output key
      .attribute('email', { as: 'userEmail' })

      // Round decimal to 2 places
      .attribute('price', { precision: 2 })

      // Mark as optional in OpenAPI (omitted from response when undefined)
      .attribute('deletedAt', { required: false })
    • Includes an attribute from the data object in the serialized output.

      For DreamSerializer, OpenAPI types are automatically inferred from database columns. For json/jsonb columns and virtual attributes (getters), the openapi option is required.

      Type Parameters

      • AttributeName extends any

      Parameters

      • name: AttributeName

        The attribute name from the data object

      • Optionaloptions: AutomaticSerializerAttributeOptions

        Configuration options:

        • as - Rename the attribute key in the serialized output and OpenAPI shape
        • default - Value to use when the attribute is undefined (not available for type columns)
        • openapi - OpenAPI schema definition; required for json/jsonb columns and non-virtual getters, optional for standard columns (where it can supplement inferred types with a description), and optional for @deco.Virtual() columns (where the type is inferred from the decorator)
        • precision - Round decimal values to the specified number of decimal places (0–9) during rendering; does not affect the OpenAPI shape (not available for type or virtual columns)
        • required - Set to false to mark the attribute as optional in the OpenAPI schema; when omitted, attributes are required by default, meaning undefined values will serialize as null (not available for type columns)

      Returns DreamSerializerBuilder<
          DataTypeForOpenapi,
          MaybeNullDataType,
          PassthroughDataType,
          DataType,
      >

      The serializer builder for method chaining

      // With type inference (database column)
      .attribute('email')

      // With explicit OpenAPI type (virtual attribute or json column)
      .attribute('fullName', { openapi: { type: 'string' } })

      // With default value
      .attribute('status', { default: 'pending' })

      // Rename output key
      .attribute('email', { as: 'userEmail' })

      // Round decimal to 2 places
      .attribute('price', { precision: 2 })

      // Mark as optional in OpenAPI (omitted from response when undefined)
      .attribute('deletedAt', { required: false })
    • Includes a computed value in the serialized output.

      Executes a callback function to generate a custom attribute value. The openapi option is always required since the return type cannot be inferred.

      Parameters

      • name: string

        The attribute name for the computed value

      • fn: () => unknown

        Callback function that returns the computed value

      • options: NonAutomaticSerializerAttributeOptions & { precision?: RoundingPrecision } & {
            flatten?: boolean;
        }

        Configuration options:

        • openapi - (required) OpenAPI schema definition for the computed value
        • as - Rename the attribute key in the serialized output and OpenAPI shape
        • default - Value to use when the callback returns undefined
        • flatten - When true, spreads the returned object's properties directly into the parent serialized output instead of nesting them under name; the openapi option should then define each flattened property individually
        • precision - Round decimal values to the specified number of decimal places (0–9) during rendering; does not affect the OpenAPI shape
        • required - Set to false to mark the attribute as optional in the OpenAPI schema; when omitted, attributes are required by default

      Returns DreamSerializerBuilder<
          DataTypeForOpenapi,
          MaybeNullDataType,
          PassthroughDataType,
          DataType,
      >

      The serializer builder for method chaining

      // Simple computed value
      .customAttribute('initials', () =>
      `${user.firstName?.[0]}${user.lastName?.[0]}`.toUpperCase(),
      { openapi: { type: 'string' } }
      )

      // Flattened object properties
      .customAttribute('coordinates', () => ({ lat: 40.7, lng: -74.0 }), {
      flatten: true,
      openapi: {
      lat: { type: 'number' },
      lng: { type: 'number' }
      }
      })

      // With decimal precision
      .customAttribute('averageRating', () => calculateAverage(ratings), {
      openapi: 'decimal',
      precision: 2
      })
    • Includes an attribute from a nested object in the serialized output.

      Accesses targetName.name on the data object. When the target object or the delegated attribute is null/undefined, the resolution order is:

      1. default if provided → renders the default value
      2. required: false → omits the key entirely from the rendered output
      3. otherwise → renders null

      When the target is a Dream model, OpenAPI types may be automatically inferred for standard database columns. For json/jsonb columns or non-Dream targets, the openapi option is required.

      optional and required are not aliases — they encode different things and can be used together:

      • optional: true is an OpenAPI-only marker (no runtime effect). It declares that the rendered value may be null (e.g. when delegating through a HasOne that may be missing). Use this when you want the key to always be present but the value to be nullable in the schema.
      • required: false affects both runtime and OpenAPI. At runtime, when the resolved value is undefined (which includes the case of a missing delegated association with no default), the key is omitted from the rendered output. In OpenAPI, the field is marked as not required.

      Type Parameters

      Parameters

      • targetName: TargetName

        The property name containing the target object (e.g., an association name)

      • name: TargetAttributeName

        The attribute name within the target object

      • Optionaloptions: AssociatedModelType extends Dream
            ? TargetAttributeName extends Exclude<
                AssociatedModelType["schema"][AssociatedModelType["table"] & keyof (
                    ...
                )[(...)]]["nonJsonColumnNames"][number],
                keyof Dream,
            > & keyof AssociatedModelType & "type"
                ? AutomaticSerializerAttributeOptionsForType & {
                    optional?: boolean;
                    required?: false;
                }
                : TargetAttributeName extends DreamVirtualColumns<AssociatedModelType>[number]
                    ? SerializerAttributeOptionsForVirtualColumn & { optional?: boolean }
                    : TargetAttributeName extends Exclude<
                        (...)[(...)][(...) & (...)]["nonJsonColumnNames"][number],
                        keyof Dream,
                    > & string & keyof AssociatedModelType
                        ?
                            | AutomaticSerializerAttributeOptions & { optional?: boolean }
                            | NonAutomaticSerializerAttributeOptions & {
                                precision?: RoundingPrecision;
                            } & { optional?: boolean }
                        : NonAutomaticSerializerAttributeOptions & {
                            precision?: RoundingPrecision;
                        } & { optional?: boolean }
            : NonAutomaticSerializerAttributeOptions & {
                precision?: RoundingPrecision;
            } & { optional?: boolean }

        Configuration options:

        • as - Rename the attribute key in the serialized output and OpenAPI shape (e.g., delegating 'user', 'email' with as: 'userEmail' outputs the value under userEmail)
        • default - Value to use when the target object or its attribute is null/undefined (not available when delegating to a 'type' STI discriminator column: substituting a discriminator string when the association is actually missing produces a response indistinguishable from "association present with that type," which is misleading)
        • openapi - OpenAPI schema definition; required for non-Dream targets and json/jsonb columns, optional for standard Dream columns (where types are inferred)
        • optional - Set to true to mark the value as nullable in the OpenAPI schema (wraps the type in anyOf: [schema, { type: 'null' }]). OpenAPI-only — the key is still rendered (as null). For Dream models, this is auto-inferred from optional BelongsTo associations. Use this when delegating through a HasOne or other nullable association.
        • precision - Round decimal values to the specified number of decimal places (0–9) during rendering; does not affect the OpenAPI shape (not available when delegating to a 'type' STI discriminator column, which is always a string enum)
        • required - Set to false to omit the key from the rendered output when the resolved value is undefined (including a missing delegated association with no default), and to mark the field as not required in the OpenAPI schema

      Returns DreamSerializerBuilder<
          DataTypeForOpenapi,
          MaybeNullDataType,
          PassthroughDataType,
          DataType,
      >

      The serializer builder for method chaining

      // Delegate to a Dream association's column (type inferred)
      .delegatedAttribute('currentLocalizedText', 'title', { openapi: 'string' })

      // With default value for null target or attribute
      .delegatedAttribute('user', 'displayName', {
      openapi: { type: 'string' },
      default: 'Unknown User'
      })

      // Rename the output key
      .delegatedAttribute('profile', 'avatarUrl', {
      openapi: 'string',
      as: 'avatar'
      })
    • Executes the serializer and returns the serialized output.

      Processes all defined attributes, custom attributes, delegated attributes, and associations to produce the final serialized object.

      Parameters

      • passthrough: any = {}

        Additional data to pass through to nested serializers (e.g., locale, current user context)

      • opts: SerializerRendererOpts = {}

        Rendering options for customizing the serialization process

      Returns Record<string, any> | null

      The serialized object, suitable for JSON responses

      const result = UserSerializer(user).render()
      // Returns: { id: 1, email: 'user@example.com', ... }

      // With passthrough data for nested serializers
      const result = UserSerializer(user).render({ currentUserId: 123 })