DEV Community

Cover image for Basic Find Query with Prisma
Kristen Kinnear-Ohlmann
Kristen Kinnear-Ohlmann

Posted on • Originally published at kristenkinnearohlmann.dev

Basic Find Query with Prisma

For my Breeze Lakes Point project, I am using Prisma as the ORM to handle data in my PostgreSQL database. This is the ORM that we used with the sample app from the Frontend Masters workshop I attended, and I found it straightforward with great documentation.

I am working on a feature to find a specific user in the database and return the data for display in a registration form for editing. The basic findUnique syntax is quite compact:

const data = await prisma.<model>.findUnique({
    where: {
        <lookupField>: <lookupValue>,
    },
    select: {
        returnField1: true,
        returnField2: true
    },
});
Enter fullscreen mode Exit fullscreen mode

Since I am still expanding my user model, I implemented a simple return object:

const qry = <GUID value>

const data = await prisma.user.findUnique({
    where: {
        id: qry,
    },
    select: {
        id: true,
        username: true,
        email: true,
        firstName: true,
        lastName: true
    },
});
Enter fullscreen mode Exit fullscreen mode

For the final returned object, I spread the data object to include a message to confirm the data returned to my route, since this is currently the same data that the session contains:

return { ...data, msg: "Found" };
Enter fullscreen mode Exit fullscreen mode

With the basic framework in place to look up and return data via Prisma, I can work to expand both my user model and the models planned to hold related data.

Top comments (0)