diff --git a/.changeset/long-pillows-confess.md b/.changeset/long-pillows-confess.md new file mode 100644 index 00000000..f90f865d --- /dev/null +++ b/.changeset/long-pillows-confess.md @@ -0,0 +1,5 @@ +--- +"@premieroctet/next-admin": patch +--- + +BigInt support diff --git a/apps/example/options.tsx b/apps/example/options.tsx index 9ca0d28b..bc6704a9 100644 --- a/apps/example/options.tsx +++ b/apps/example/options.tsx @@ -100,6 +100,7 @@ export const options: NextAdminOptions = { "published", "author", "categories", + "rate" ], search: ["title", "content"], fields: { @@ -123,6 +124,7 @@ export const options: NextAdminOptions = { "published", "categories", "author", + "rate" ], }, }, diff --git a/apps/example/prisma/json-schema/json-schema.json b/apps/example/prisma/json-schema/json-schema.json index 407ae0f4..96b131ad 100644 --- a/apps/example/prisma/json-schema/json-schema.json +++ b/apps/example/prisma/json-schema/json-schema.json @@ -109,6 +109,12 @@ "items": { "$ref": "#/definitions/post_comment" } + }, + "rate": { + "type": [ + "number", + "null" + ] } }, "required": [ diff --git a/apps/example/prisma/migrations/20240129110259_add_rate/migration.sql b/apps/example/prisma/migrations/20240129110259_add_rate/migration.sql new file mode 100644 index 00000000..72b64112 --- /dev/null +++ b/apps/example/prisma/migrations/20240129110259_add_rate/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Post" ADD COLUMN "rate" DECIMAL(5,2); diff --git a/apps/example/prisma/schema.prisma b/apps/example/prisma/schema.prisma index 9a90470c..98f97641 100644 --- a/apps/example/prisma/schema.prisma +++ b/apps/example/prisma/schema.prisma @@ -45,6 +45,7 @@ model Post { authorId Int categories Category[] @relation("category") // implicit Many-to-many relation comments post_comment[] @relation("comments") // One-to-many relation + rate Decimal? @db.Decimal(5, 2) } model Profile { diff --git a/packages/next-admin/src/tests/prismaUtils.test.ts b/packages/next-admin/src/tests/prismaUtils.test.ts index 4c86c66a..34a5bf00 100644 --- a/packages/next-admin/src/tests/prismaUtils.test.ts +++ b/packages/next-admin/src/tests/prismaUtils.test.ts @@ -1,3 +1,4 @@ +import { Decimal } from "@prisma/client/runtime/library"; import { getMappedDataList } from "../utils/prisma"; import { options, prismaMock } from "./singleton"; @@ -11,6 +12,7 @@ describe("getMappedDataList", () => { published: true, author: 1, authorId: 1, + rate: new Decimal(5), }, { id: 2, @@ -19,6 +21,7 @@ describe("getMappedDataList", () => { published: true, author: 1, authorId: 1, + rate: new Decimal(5), }, ]; diff --git a/packages/next-admin/src/utils/server.ts b/packages/next-admin/src/utils/server.ts index 7d268592..123fefd8 100644 --- a/packages/next-admin/src/utils/server.ts +++ b/packages/next-admin/src/utils/server.ts @@ -275,6 +275,8 @@ export const transformData = ( acc[key] = data[key] ? JSON.stringify(data[key]) : null; } else if (fieldTypes === "Decimal") { acc[key] = data[key] ? Number(data[key]) : null; + } else if (fieldTypes === "BigInt") { + acc[key] = data[key] ? BigInt(data[key]).toString() : null; } else { acc[key] = data[key] ? data[key] : null; } @@ -333,19 +335,28 @@ export const findRelationInData = async ( } } - if (dmmfPropertyType === "DateTime") { + if (dmmfPropertyType === "DateTime" || + dmmfPropertyType === "Decimal" || + dmmfPropertyType === "BigInt") { data.map((item) => { if (item[dmmfProperty.name]) { - item[dmmfProperty.name] = { - type: "date", - value: item[dmmfProperty.name].toISOString(), - }; + if (dmmfPropertyType === "DateTime") { + item[dmmfProperty.name] = { + type: "date", + value: item[dmmfProperty.name].toISOString(), + }; + } else if (dmmfPropertyType === "Decimal") { + item[dmmfProperty.name] = Number(item[dmmfProperty.name]) + } else if (dmmfPropertyType === "BigInt") { + item[dmmfProperty.name] = BigInt(item[dmmfProperty.name]).toString() + } } else { return item; } }); } }); + return data; }; @@ -441,6 +452,7 @@ export const formattedFormData = async ( } } else { const dmmfPropertyName = dmmfProperty.name as keyof ScalarField; + console.log(dmmfPropertyName, dmmfPropertyType) if (dmmfPropertyType === "Int" || dmmfPropertyType === "Float" || dmmfPropertyType === "Decimal") { formattedData[dmmfPropertyName] = !isNaN( Number(formData[dmmfPropertyName]) @@ -461,6 +473,8 @@ export const formattedFormData = async ( } catch { // no-op } + } else if (dmmfPropertyType === "BigInt") { + formattedData[dmmfPropertyName] = formData[dmmfPropertyName] ? BigInt(formData[dmmfPropertyName]!) : null; } else if ( dmmfPropertyType === "String" && ["data-url", "file"].includes(