Mixing Remote and Local Data Models

As you further develop your admin interface, you may want some of the benefits of remote data models, but also want some of the customizability of local data models. Luckily, this isn't an either/or proposition, and there's a few steps of abstraction you can take to slowly move from one extreme to the other.

Also keep in mind that it is possible to customize the individual List and Detail pages, and that may prove to be a better solution depending on your aims.

Step 1: Remote Data Model

See here for more details.

// In `src/datamodels.tsx`:
const { fetchRemoteDataModels } = useGenerateRemoteDataModelsTRPCClient(/* ... */);

<AdminContextProvider>
  <DataModels fetchRemoteDataModels={fetchRemoteDataModels}>
    <RemoteDataModels />

    {children}
  </DataModels>
</AdminContextProvider>

Step 2: Remote Data Model, but with the ability to customize DataModel props

// In `src/datamodels.tsx`:
const { getPropsForRemoteDataModel } = useGenerateRemoteDataModelsTRPCClient(/* ... */);

<AdminContextProvider>
  <DataModels fetchRemoteDataModels={fetchRemoteDataModels}>
    {/* 1. Remove the "vehicle" remote data model from the auto generated list */}
    <RemoteDataModels exclude={["Vehicle"]} />

    {/* 2. Manually generate the data model */}
    <DataModel<Vehicle>
      name="Vehicle" // NOTE that this must be the same as the name the remote data model had!
      singularDisplayName="Vehicle"
      pluralDisplayName="Vehicles"

      {/* 3. Auto generated the props that do the data fetching - include create/update/delete */}
      {...getPropsForRemoteDataModel('Vehicle', ['createItem', 'updateItem', 'deleteItem'])}

      keyGenerator={vehicle => vehicle.id}
      detailLinkGenerator={vehicle => ({ type: 'href' as const, href: `/admin/vehicles/${vehicle.id}` })}
      listLink={{ type: 'href' as const, href: `/admin/vehicles` }}
      createLink={{ type: 'href', href: `/admin/vehicles/new` }}
    >
      {/* 4. Use all the existing remote field definitions */}
      <RemoteFields name="Vehicle" />
    </DataModel>

    {children}
  </DataModels>
</AdminContextProvider>

Step 3: Remote Data Model, but with custom DataModel supporting overridden custom fields

Step 4: Remote Data Model, but with fully custom DataModel fields:

The only thing that makes this data model "remote" is that it still relys on the server for data fetching.

Step 5: Local Data Model

Last updated