From 04091e14670cb1c44667d04407903d147c8dc125 Mon Sep 17 00:00:00 2001 From: Colin Regourd Date: Mon, 16 Oct 2023 17:28:52 +0200 Subject: [PATCH] Fix ID field doesn't display --- .../next-admin/src/components/DataTable.tsx | 5 +- packages/next-admin/src/router.tsx | 46 +--------- packages/next-admin/src/types.ts | 4 +- packages/next-admin/src/utils/prisma.ts | 87 +++++++++++++++---- 4 files changed, 79 insertions(+), 63 deletions(-) diff --git a/packages/next-admin/src/components/DataTable.tsx b/packages/next-admin/src/components/DataTable.tsx index 106c9067..0e1a6cf7 100644 --- a/packages/next-admin/src/components/DataTable.tsx +++ b/packages/next-admin/src/components/DataTable.tsx @@ -32,6 +32,9 @@ export function DataTable({ columns, data, resource }: DataTableProps) { manualSorting: true, columns, getCoreRowModel: getCoreRowModel(), + initialState: { + columnVisibility: { _accessorKey: false }, + }, }); return ( @@ -64,7 +67,7 @@ export function DataTable({ columns, data, resource }: DataTableProps) { className="cursor-pointer hover:bg-indigo-50" onClick={() => { router?.push({ - pathname: `${basePath}/${resource}/${row.original.id}`, + pathname: `${basePath}/${resource}/${row.original._accessorKey}`, }); }} > diff --git a/packages/next-admin/src/router.tsx b/packages/next-admin/src/router.tsx index d4d9e230..d060ce57 100644 --- a/packages/next-admin/src/router.tsx +++ b/packages/next-admin/src/router.tsx @@ -7,7 +7,6 @@ import { createRouter } from "next-connect"; import { fillRelationInSchema, - findRelationInData, flatRelationInData, formatSearchFields, formattedFormData, @@ -26,7 +25,7 @@ import { Select, EditFieldsOptions, } from "./types"; -import { preparePrismaListRequest } from "./utils/prisma"; +import { getMappedDataList } from "./utils/prisma"; import { validate } from "./utils/validator"; // Router @@ -123,37 +122,7 @@ export const nextAdminRouter = async ( // List const searchParams = new URLSearchParams(req.url!.split("?")[1]); - const prismaListRequest = preparePrismaListRequest( - resource, - searchParams, - options - ); - let data: any[] = []; - let total: number; - let error = null; - - try { - // @ts-expect-error - data = await prisma[resource].findMany(prismaListRequest); - // @ts-expect-error - total = await prisma[resource].count({ - where: prismaListRequest.where, - }); - } catch (e: any) { - const { skip, take, orderBy } = prismaListRequest; - // @ts-expect-error - data = await prisma[resource].findMany({ - skip, - take, - orderBy, - }); - // @ts-expect-error - total = await prisma[resource].count(); - error = e.message ? e.message : e; - console.error(e); - } - data = await findRelationInData(data, dmmfSchema?.fields); - + const { data, total, error } = await getMappedDataList(prisma, resource, dmmfSchema, options, searchParams); return { props: { ...defaultProps, @@ -221,16 +190,7 @@ export const nextAdminRouter = async ( // Delete redirect, display the list (this is needed because next keeps the HTTP method on redirects) if (resourceId === undefined && formData.action === "delete") { const searchParams = new URLSearchParams(req.url!.split("?")[1]); - const prismaListRequest = preparePrismaListRequest( - resource, - searchParams, - options - ); - // @ts-expect-error - let data = await prisma[resource].findMany(prismaListRequest); - data = await findRelationInData(data, dmmfSchema?.fields); - // @ts-expect-error - const total = await prisma[resource].count(); + const { data, total } = await getMappedDataList(prisma, resource, dmmfSchema, options, searchParams); return { props: { diff --git a/packages/next-admin/src/types.ts b/packages/next-admin/src/types.ts index 5b6938b9..946373c6 100644 --- a/packages/next-admin/src/types.ts +++ b/packages/next-admin/src/types.ts @@ -12,13 +12,13 @@ export type ObjectField = Prisma.TypeMap["model"][T]["paylo export type Model = ScalarField & { [P in keyof ObjectField]: - ObjectField[P] extends { scalars: infer S } ? T extends never ? S : T + ObjectField[P] extends { scalars: infer S } ? T extends never ? S : T : ObjectField[P] extends { scalars: infer S } | null ? T extends never ? S | null : T | null : ObjectField[P] extends { scalars: infer S }[] ? T extends never ? S[] : T[] : never; } -export type ModelWithoutRelationships = Model; +export type ModelWithoutRelationships = Model; export type Field

= keyof Model

; diff --git a/packages/next-admin/src/utils/prisma.ts b/packages/next-admin/src/utils/prisma.ts index 7033cd2d..3d74e8de 100644 --- a/packages/next-admin/src/utils/prisma.ts +++ b/packages/next-admin/src/utils/prisma.ts @@ -1,5 +1,5 @@ -import { Prisma } from "@prisma/client"; -import { getPrismaModelForResource } from "./server"; +import { Prisma, PrismaClient } from "@prisma/client"; +import { findRelationInData, getPrismaModelForResource } from "./server"; import { ListFieldsOptions, ModelName, @@ -18,21 +18,21 @@ export const createWherePredicate = ( ) => { return search ? { - OR: fieldsFiltered - ?.filter((field) => field.kind === "scalar") - .map((field) => { - if (field.type === "String") { - return { - [field.name]: { contains: search, mode: "insensitive" }, - }; - } - if (field.type === "Int" && !isNaN(Number(search))) { - return { [field.name]: Number(search) }; - } - return null; - }) - .filter(Boolean), - } + OR: fieldsFiltered + ?.filter((field) => field.kind === "scalar") + .map((field) => { + if (field.type === "String") { + return { + [field.name]: { contains: search, mode: "insensitive" }, + }; + } + if (field.type === "Int" && !isNaN(Number(search))) { + return { [field.name]: Number(search) }; + } + return null; + }) + .filter(Boolean), + } : {}; }; @@ -66,6 +66,7 @@ export const preparePrismaListRequest = ( const listKeys = Object.keys(list) as Array>; select = listKeys.reduce((acc, column) => { const field = model?.fields.find(({ name }) => name === column); + if (!list[column]?.display) return acc; if (field?.kind === "object") { if (!acc._count) acc._count = { select: {} }; acc._count.select = { ...acc._count.select, [column]: true }; @@ -76,6 +77,8 @@ export const preparePrismaListRequest = ( return acc; }, {} as Select); + if (!select.id) select.id = true; + fieldsFiltered = model?.fields.filter( ({ name }) => list[name as keyof ListFieldsOptions]?.search @@ -91,3 +94,53 @@ export const preparePrismaListRequest = ( take: itemsPerPage, }; }; + +export const getMappedDataList = async (prisma: PrismaClient, resource: ModelName, dmmfSchema: Prisma.DMMF.Model | undefined, options: NextAdminOptions, searchParams: URLSearchParams) => { + const prismaListRequest = preparePrismaListRequest( + resource, + searchParams, + options + ); + let data: any[] = []; + let total: number; + let error = null; + try { + // @ts-expect-error + data = await prisma[resource].findMany(prismaListRequest); + // @ts-expect-error + total = await prisma[resource].count({ + where: prismaListRequest.where, + }); + } catch (e: any) { + const { skip, take, orderBy } = prismaListRequest; + // @ts-expect-error + data = await prisma[resource].findMany({ + skip, + take, + orderBy, + }); + // @ts-expect-error + total = await prisma[resource].count(); + error = e.message ? e.message : e; + console.error(e); + } + + data = await findRelationInData(data, dmmfSchema?.fields); + + + const isIdDisplayed = options?.model?.[resource]?.list?.fields.id?.display; + + data.forEach((item) => { + const id = item.id; + if (!isIdDisplayed) { + delete item.id; + } + item._accessorKey = id; + }); + + return { + data, + total, + error, + }; +}; \ No newline at end of file