Skip to content

trailbase-db-collection: optional parse and serialize arguemtns crash module if not provided #1002

@MentalGear

Description

@MentalGear
  • I've validated the bug against the latest version of DB packages

Bug Report: trailbase-db-collection Documentation Issue

Summary

The @tanstack/trailbase-db-collection package documentation incorrectly marks parse and serialize properties as optional in the TrailBaseCollectionConfig interface, but the implementation requires these properties to be defined (even if empty) to avoid runtime errors.

Problem Description

Issue

When using trailBaseCollectionOptions without providing parse and serialize properties, the application crashes with:

Uncaught (in promise) TypeError: can't access property "some_collection", c is undefined

Root Cause

In the trailbase.ts file, the convert function (line 62) and convertPartial function (line 120) expect the conversions parameter to be defined, but when parse and serialize are not provided in the configuration, they become undefined, causing the error when trying to access properties on the undefined object.

Code Location

  • File: node_modules/@tanstack/trailbase-db-collection/src/trailbase.ts
  • Functions: convert() (line 62), convertPartial() (line 120)
  • Error path: convertparseinitialFetchtrailBaseCollectionOptions

Reproduction Steps

  1. Create a collection using trailBaseCollectionOptions without parse and serialize properties:
trailBaseCollectionOptions({
    id: 'some_collection',
    recordApi: trailbase.records('some_collection') as any,
    getKey: (item) => item.id,
    schema: someSchema,
    // Missing parse and serialize properties
})
  1. Attempt to fetch data from the collection
  2. Error occurs when the convert function is called with undefined conversions

Expected Behavior

According to the documentation/type definitions, parse and serialize should be optional. If not provided, the library should handle this gracefully by using identity conversions (returning the input as-is).

Current Behavior

The application crashes with a TypeError when trying to access properties on the undefined conversions object.

Suggested Fix

Option 1: Make parse/serialize truly optional

Modify the convert and convertPartial functions to handle undefined conversions:

function convert<InputType, OutputType>(
  conversions: Conversions<InputType, OutputType> | undefined,
  input: InputType
): OutputType {
  const c = conversions ?? {}
  // ... rest of the function
}

Option 2: Update documentation

If the properties are required, update the documentation/type definitions to reflect this requirement.

Option 3: Provide default empty objects

Modify the trailBaseCollectionOptions function to provide default empty objects:

export function trailBaseCollectionOptions<TItem, TRecord, TKey>(
  config: TrailBaseCollectionConfig<TItem, TRecord, TKey>
): CollectionConfig<TItem, TKey> & { utils: TrailBaseCollectionUtils } {
  const parseConfig = config.parse ?? {}
  const serializeConfig = config.serialize ?? {}

  const parse = (record: TRecord) =>
    convert<TRecord, TItem>(parseConfig, record)
  // ... rest of the function
}

Workaround

Users must provide empty objects for parse and serialize even when no conversions are needed:

trailBaseCollectionOptions({
    id: 'some_collection',
    recordApi: trailbase.records('some_collection') as any,
    getKey: (item) => item.id,
    schema: someSchema,
    parse: {},      // Required workaround
    serialize: {},  // Required workaround
})

Impact

This issue affects all users of the @tanstack/trailbase-db-collection package who follow the documented API and omit the parse and serialize properties when no field conversions are needed.

Additional Context

  • Package version: @tanstack/[email protected]
  • Related to: @tanstack/db integration with TrailBase
  • Error properties observed: "some_collection", (varies based on the record being processed)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions