Skip to content

Commit

Permalink
Merge pull request #134 from premieroctet/feature/decimal-input
Browse files Browse the repository at this point in the history
Fix table decimal, float and bigint
  • Loading branch information
cregourd authored Jan 29, 2024
2 parents 6918ea2 + 8434ec4 commit a59b622
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-pillows-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@premieroctet/next-admin": patch
---

BigInt support
2 changes: 2 additions & 0 deletions apps/example/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const options: NextAdminOptions = {
"published",
"author",
"categories",
"rate"
],
search: ["title", "content"],
fields: {
Expand All @@ -123,6 +124,7 @@ export const options: NextAdminOptions = {
"published",
"categories",
"author",
"rate"
],
},
},
Expand Down
6 changes: 6 additions & 0 deletions apps/example/prisma/json-schema/json-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@
"items": {
"$ref": "#/definitions/post_comment"
}
},
"rate": {
"type": [
"number",
"null"
]
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Post" ADD COLUMN "rate" DECIMAL(5,2);
1 change: 1 addition & 0 deletions apps/example/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions packages/next-admin/src/tests/prismaUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Decimal } from "@prisma/client/runtime/library";
import { getMappedDataList } from "../utils/prisma";
import { options, prismaMock } from "./singleton";

Expand All @@ -11,6 +12,7 @@ describe("getMappedDataList", () => {
published: true,
author: 1,
authorId: 1,
rate: new Decimal(5),
},
{
id: 2,
Expand All @@ -19,6 +21,7 @@ describe("getMappedDataList", () => {
published: true,
author: 1,
authorId: 1,
rate: new Decimal(5),
},
];

Expand Down
24 changes: 19 additions & 5 deletions packages/next-admin/src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ export const transformData = <M extends ModelName>(
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;
}
Expand Down Expand Up @@ -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;
};

Expand Down Expand Up @@ -441,6 +452,7 @@ export const formattedFormData = async <M extends ModelName>(
}
} else {
const dmmfPropertyName = dmmfProperty.name as keyof ScalarField<M>;
console.log(dmmfPropertyName, dmmfPropertyType)
if (dmmfPropertyType === "Int" || dmmfPropertyType === "Float" || dmmfPropertyType === "Decimal") {
formattedData[dmmfPropertyName] = !isNaN(
Number(formData[dmmfPropertyName])
Expand All @@ -461,6 +473,8 @@ export const formattedFormData = async <M extends ModelName>(
} catch {
// no-op
}
} else if (dmmfPropertyType === "BigInt") {
formattedData[dmmfPropertyName] = formData[dmmfPropertyName] ? BigInt(formData[dmmfPropertyName]!) : null;
} else if (
dmmfPropertyType === "String" &&
["data-url", "file"].includes(
Expand Down

0 comments on commit a59b622

Please sign in to comment.