JV Lobo
JV Lobo

Reputation: 6316

Implementing search functionality in Prisma/GraphQL

I want to implement a simple searching functionality with Prisma I have seen some info about using the where clause but it’s case sensitive so it’s not a good solution.

I’ve also seen some tutorials using external services. I don’t want to use any external services. I’d like to do something as simple as possible.

Any way I can tweak the query to be case insensitive? or any other approach you guys recommend?

Thanks :)

Upvotes: 8

Views: 8542

Answers (3)

SHIKHIL S
SHIKHIL S

Reputation: 91

Try mode

const users = await prisma.user.findMany({
  where: {
    email: {
      endsWith: "prisma.io",
      mode: "insensitive", // Default value: default
    },
  },
});

Upvotes: 2

Errorname
Errorname

Reputation: 2459

This feature isn't implemented yet: https://github.com/prisma/prisma1/issues/1183

However, you can have a raw access to your database if it supports this feature: https://www.prisma.io/docs/prisma-graphql-api/reference/raw-database-access-qwe4/#overview

Upvotes: 5

Steve Mason
Steve Mason

Reputation: 215

You can do search-like queries using the prisma client. Here is an example of an auto-generated interface for the where query parameter of a User entity in one of my apps.

export interface UserWhereInput {
  name?: String;
  name_not?: String;
  name_in?: String[] | String;
  name_not_in?: String[] | String;
  name_lt?: String;
  name_lte?: String;
  name_gt?: String;
  name_gte?: String;
  name_contains?: String;
  name_not_contains?: String;
  name_starts_with?: String;
  name_not_starts_with?: String;
  name_ends_with?: String;
  name_not_ends_with?: String;
  ...
  AND?: UserWhereInput[] | UserWhereInput;
}

Note name_contains and name_starts_with. These are both valid to use for a simple search, here's an example resolver -

const userSearchResolver = async (_root, _args, _context) => {
    return await _context.prisma.users({
      where: {
        name_starts_with: _args.searchQuery
      }
    });
  }
);

Upvotes: 0

Related Questions