codeledge / ra-tools

React Admin Tools, namely the Prisma Data Adapter
66 stars 19 forks source link

Id doesn't support String data types #18

Closed fauh45 closed 2 years ago

fauh45 commented 2 years ago

A lot of the methods does this kind of operation to the body params id,

where: { id: +req.body.params.id }

This will result in NaN when the id being sent is type of string. Making it doesn't support string datatypes of id. And a simple change could be done like this,

diff --git a/node_modules/ra-data-simple-prisma/dist/index.js b/node_modules/ra-data-simple-prisma/dist/index.js
index b3dbbe5..0372eec 100644
--- a/node_modules/ra-data-simple-prisma/dist/index.js
+++ b/node_modules/ra-data-simple-prisma/dist/index.js
@@ -285,7 +285,7 @@ var getManyHandler = async (req, res, table) => {
 var getOneHandler = async (req, res, table, options) => {
   var _a, _b, _c;
   const row = await table.findUnique({
-    where: { id: +req.body.params.id },
+    where: { id: /^\d+$/.test(req.body.params.id) ? +req.body.params.id : req.body.params.id },
     select: (_a = options == null ? void 0 : options.select) != null ? _a : void 0,
     include: (_b = options == null ? void 0 : options.include) != null ? _b : void 0
   });
@@ -305,7 +305,7 @@ var updateHandler = async (req, res, table, options) => {
     return fields;
   }, {});
   const updated = await table.update({
-    where: { id: +req.body.params.id },
+    where: { id: /^\d+$/.test(req.body.params.id) ? +req.body.params.id : req.body.params.id },
     data
   });
   return res.json({ data: updated });
@@ -314,12 +314,12 @@ var updateHandler = async (req, res, table, options) => {
 // src/deleteHandler.ts
 var deleteHandler = async (req, res, table, options) => {
   const deleted = (options == null ? void 0 : options.softDeleteField) ? await table.update({
-    where: { id: +req.body.params.id },
+    where: { id: /^\d+$/.test(req.body.params.id) ? +req.body.params.id : req.body.params.id },
     data: {
       [options == null ? void 0 : options.softDeleteField]: new Date()
     }
   }) : await table.delete({
-    where: { id: +req.body.params.id }
+    where: { id: /^\d+$/.test(req.body.params.id) ? +req.body.params.id : req.body.params.id }
   });
   return res.json({ data: deleted });
 };

Edit : Add RegEx test if a string is a number

ogroppo commented 2 years ago

Yep, you are right, looking into it now! The problem is that react-admin was sending sometimes strings and sometimes numbers, will check if that's still the case, hopefully we could preserve whatever the original type is

ogroppo commented 2 years ago

Seems still the case, if the guys don't fix soonish, I will use your suggestion https://github.com/marmelab/react-admin/issues/7728

fauh45 commented 2 years ago

Seems still the case, if the guys don't fix soonish, I will use your suggestion marmelab/react-admin#7728

Huh, so it's just React Admin being weird now eh? Well I guess for now I'll just be running the regex version of mine, as it seemed like it works just fine. Thanks for the quick response!

ogroppo commented 2 years ago

ra-data-simple-prisma@0.3.3