DEV Community

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

Posted on • Originally published at kristenkinnearohlmann.dev

3 1

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.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay