Next.js + TRPC + Prisma
Client Setup
Make sure you've installed the library first!
Create a file to configure your data models - put this in
src/datamodels.tsx:
import { useCallback, useMemo } from 'react';
import { useRouter } from 'next/router';
import {
DataModels,
AdminContextProvider,
queryParameterStateCache,
RemoteDataModels,
} from '@/admin';
import {
useGenerateRemoteDataModelsTRPCClient,
} from "@/admin/remote-data-model-adapters/protocols/trpc";
import { api } from "@/utils/api";
export default function AllDataModels({ children }: { children: React.ReactNode}) {
// 1. Set up a "remote data model client" that communicates with the server over trpc.
const { fetchRemoteDataModels } = useGenerateRemoteDataModelsTRPCClient(
api,
// NOTE: for older versions of trpc, this should be `api.useContext()`
api.useUtils(),
// NOTE: This is a path to the place the corresponding trpc router is mounted.
// This default value matches with what will be configured later in this guide.
// `api.foo.bar.baz.(mounted router)` would be represented as `['foo', 'bar', 'baz']`
['admin'],
);
return (
<div style={{ padding: 8 }}>
<AdminContextProvider stateCache={queryParameterStateCache} nextRouter={useRouter()}>
{/* 2. Give the data models provider the way to get remote data models */}
<DataModels fetchRemoteDataModels={fetchRemoteDataModels}>
{/* 3. Include all remote data models in the admin interface */}
<RemoteDataModels />
{children}
</DataModels>
</AdminContextProvider>
</div>
);
}Create a new directory
src/pages/adminand put a file named[...path].tsxinside containing:
Create a new file
index.tsxinsidesrc/pages/admincontaining:
Wherever you add global css files, make sure to include
@/admin/styles.css:
Server Setup
Add the
adminrouter intoserver/api/root.ts:
Try it out!
Start your app, and go to /admin. You should see the admin interface.
Clicking on a model should go to the list view for it, showing a column per model field.
Clicking on a row should go to the detail page, where the model can be viewed, updated, or deleted.
Last updated