premieroctet / next-crud

Full-featured CRUD routes for Next.js

Home Page:https://next-crud.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prisma 5 support

AntonAndreevichMoroz opened this issue · comments

Greetings. Firstly, I would like to thank you for such a useful and convenient project.
And if you can add support for Prisma 5. I tried to set it up but got an error, next-crud can't get prisma schema. Rollback to version 4 solved the problem.

I think he abandoned project, better make you own version or try find another

I think he abandoned project, better make you own version or try find another

Hello, project is not abandoned, i try to look into it during my free time.

I tried to look at the prisma 5 issue and it is quite trivial as they changed many things related to dmmf. But i'd be glad if a PR can be submitted !

commented
import pluralize from 'pluralize'
import prisma from "../../../prisma/prisma";

const handler = async (req, res) => {

    const {model, queryString} = req.query
    let singularModel = pluralize.singular(model[0]);
    let id = undefined;
    if (model.length > 1) {
        id = model[1];
    }

    if (prisma[singularModel] === undefined){
        res.status(500).json({message: "No route exists"})
        return;
    }

    try {
        if (req.method === 'GET') {
            if (!id) {
                let result = await prisma[singularModel].findMany(queryString)
                res.status(200).json(result)
            }
            if (id) {
                let result = await prisma[singularModel].findFirst({
                    ...queryString,
                    where: {
                        ...queryString.where,
                        AND: [
                            ...queryString.where?.AND,
                            {id: id}
                        ]
                    }
                })
                res.status(200).json(result)
            }


        }
        if (req.method === 'POST') {
            let result = await prisma[singularModel].create({
                data: req.body
            })

            res.status(200).json(result)
        }

        if (req.method === 'PUT') {
            if (!id) {
                res.status(500).json({message: "No id provided"})
            }

            let result = await prisma[singularModel].update({
                data: req.body,
                where: {
                    ...queryString.where,
                    AND: [
                        ...queryString.where?.AND,
                        {id: id}
                    ]
                }
            })

            res.status(200).json(result)
        }

        if (req.method === 'DELETE') {
            if (!id) {
                res.status(500).json({message: "No id provided"})
            }

            let result = await prisma[singularModel].delete({
                ...queryString,
                where: {
                    id: id
                }
            })

            res.status(200).json(result)
        }
    } catch (e) {

        res.status(500).json({message: e.message})
    }

}
export default handler

Couldn't you simplify the whole library?

Hey everyone, Prisma 5 support is now available starting v3 of the lib. Sorry for the delay and thanks for the patience.

Don't hesitate another issue if you encounter any other issue.