8

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 :)

JV Lobo
  • 5,526
  • 8
  • 34
  • 55

3 Answers3

5

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

Errorname
  • 2,228
  • 13
  • 23
  • Great response, thanks a lot for pointing to the relevant issues! – nburk Dec 05 '18 at 16:35
  • thank you for your help Errorname. it's a bit discouraging that every "simple" thing I try to do with Prisma it's not yet implemented I also tried with the raw access... but I wasn't able to get it working... anyways thank for the information. – JV Lobo Dec 06 '18 at 05:29
  • 1
    Prisma is a "young" technology, but it shows great promise. Which is why more and more developers are using Prisma. But that also mean there is more and more various feature requests, and more and more pressures on the team! They are hiring, but it's not an easy task to scale a team :) I guess we just have to be patient – Errorname Dec 06 '18 at 09:28
  • @Errorname do you know if this solution is up to date? In 2020 is there a case insensitive way to query? – Xalsar Apr 20 '20 at 11:19
2

Try mode

const users = await prisma.user.findMany({
  where: {
    email: {
      endsWith: "prisma.io",
      mode: "insensitive", // Default value: default
    },
  },
});
SHIKHIL S
  • 81
  • 1
  • 5
0

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
      }
    });
  }
);
Steve Mason
  • 205
  • 1
  • 2
  • 6
  • Just realized you were looking for case-sensitive search... sorry... I'd recommend making a separate database client to connect to the prisma database _not_ through prisma client, and just do a text search using the standard db client. For example, if you want to use mongodb for prisma, you can just use the mongo client in your resolver to perform a [text search](https://docs.mongodb.com/manual/text-search/) – Steve Mason Mar 10 '19 at 22:53